timecopilot.models.utils.forecaster
get_seasonality
get_seasonality(
freq: str,
custom_seasonalities: dict[str, int] | None = None,
) -> int
Get the seasonality of a frequency.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
freq
|
str
|
The frequency to get the seasonality of. |
required |
custom_seasonalities
|
dict[str, int] | None
|
Custom seasonalities to use. If None, the default seasonalities are used. |
None
|
Returns:
Name | Type | Description |
---|---|---|
int |
int
|
The seasonality of the frequency. |
Example
from timecopilot.models.utils.forecaster import get_seasonality
get_seasonality("D", custom_seasonalities={"D": 7})
# 7
get_seasonality("D") # default seasonalities are used
# 1
Source code in timecopilot/models/utils/forecaster.py
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
|
maybe_infer_freq
maybe_infer_freq(df: DataFrame, freq: str | None) -> str
Infer the frequency of the time series data.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
The time series data. |
required |
freq
|
str | None
|
The frequency of the time series data. If None, the frequency will be inferred from the data. |
required |
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
The inferred frequency of the time series data. |
Source code in timecopilot/models/utils/forecaster.py
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
|
Forecaster
forecast
forecast(
df: DataFrame,
h: int,
freq: str | None = None,
level: list[int | float] | None = None,
quantiles: list[float] | None = None,
) -> DataFrame
Generate forecasts for time series data using the model.
This method produces point forecasts and, optionally, prediction intervals or quantile forecasts. The input DataFrame can contain one or multiple time series in stacked (long) format.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame containing the time series to forecast. It must include as columns:
|
required |
h
|
int
|
Forecast horizon specifying how many future steps to predict. |
required |
freq
|
str
|
Frequency of the time series (e.g. "D" for daily, "M" for monthly). See Pandas frequency aliases for valid values. If not provided, the frequency will be inferred from the data. |
None
|
level
|
list[int | float]
|
Confidence levels for prediction intervals, expressed as percentages (e.g. [80, 95]). If provided, the returned DataFrame will include lower and upper interval columns for each specified level. |
None
|
quantiles
|
list[float]
|
List of quantiles to forecast, expressed as floats between 0
and 1. Should not be used simultaneously with |
None
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: DataFrame containing forecast results. Includes:
For multi-series data, the output retains the same unique identifiers as the input DataFrame. |
Source code in timecopilot/models/utils/forecaster.py
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
|
cross_validation
cross_validation(
df: DataFrame,
h: int,
freq: str | None = None,
n_windows: int = 1,
step_size: int | None = None,
level: list[int | float] | None = None,
quantiles: list[float] | None = None,
) -> DataFrame
Perform cross-validation on time series data.
This method splits the time series into multiple training and testing windows and generates forecasts for each window. It enables evaluating forecast accuracy over different historical periods. Supports point forecasts and, optionally, prediction intervals or quantile forecasts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame containing the time series to forecast. It must include as columns:
|
required |
h
|
int
|
Forecast horizon specifying how many future steps to predict in each window. |
required |
freq
|
str
|
Frequency of the time series (e.g. "D" for daily, "M" for monthly). See Pandas frequency aliases for valid values. If not provided, the frequency will be inferred from the data. |
None
|
n_windows
|
int
|
Number of cross-validation windows to generate. Defaults to 1. |
1
|
step_size
|
int
|
Step size between the start of consecutive windows. If None, it
defaults to |
None
|
level
|
list[int | float]
|
Confidence levels for prediction intervals, expressed as percentages (e.g. [80, 95]). When specified, the output DataFrame includes lower and upper interval columns for each level. |
None
|
quantiles
|
list[float]
|
Quantiles to forecast, expressed as floats between 0 and 1.
Should not be used simultaneously with |
None
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: DataFrame containing the forecasts for each cross-validation window. The output includes:
|
Source code in timecopilot/models/utils/forecaster.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
|
detect_anomalies
detect_anomalies(
df: DataFrame,
h: int | None = None,
freq: str | None = None,
n_windows: int | None = None,
level: int | float = 99,
) -> DataFrame
Detect anomalies in time-series using a cross-validated z-score test.
This method uses rolling-origin cross-validation to (1) produce
adjusted (out-of-sample) predictions and (2) estimate the
standard deviation of forecast errors. It then computes a per-point z-score,
flags values outside a two-sided prediction interval (with confidence level
),
and returns a DataFrame with results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame containing the time series to detect anomalies. |
required |
h
|
int
|
Forecast horizon specifying how many future steps to predict. In each cross validation window. If not provided, the seasonality of the data (inferred from the frequency) is used. |
None
|
freq
|
str
|
Frequency of the time series (e.g. "D" for daily, "M" for monthly). See Pandas frequency aliases for valid values. If not provided, the frequency will be inferred from the data. |
None
|
n_windows
|
int
|
Number of cross-validation windows to generate. If not provided, the maximum number of windows (computed by the shortest time series) is used. If provided, the number of windows is the minimum between the maximum number of windows (computed by the shortest time series) and the number of windows provided. |
None
|
level
|
int | float
|
Confidence levels for z-score, expressed as percentages (e.g. 80, 95). Default is 99. |
99
|
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: DataFrame containing the forecasts for each cross-validation window. The output includes:
|
Source code in timecopilot/models/utils/forecaster.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 |
|
plot
staticmethod
plot(
df: DataFrame | None = None,
forecasts_df: DataFrame | None = None,
ids: list[str] | None = None,
plot_random: bool = True,
max_ids: int | None = 8,
models: list[str] | None = None,
level: list[float] | None = None,
max_insample_length: int | None = None,
plot_anomalies: bool = False,
engine: str = "matplotlib",
palette: str | None = None,
seed: int | None = None,
resampler_kwargs: dict | None = None,
ax: Axes | ndarray | Figure | None = None,
)
Plot forecasts and insample values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
df
|
DataFrame
|
DataFrame with columns
[ |
None
|
forecasts_df
|
DataFrame
|
DataFrame with
columns [ |
None
|
ids
|
list[str]
|
Time Series to plot. If None, time series are selected randomly. Defaults to None. |
None
|
plot_random
|
bool
|
Select time series to plot randomly. Defaults to True. |
True
|
max_ids
|
int
|
Maximum number of ids to plot. Defaults to 8. |
8
|
models
|
list[str]
|
Models to plot. Defaults to None. |
None
|
level
|
list[float]
|
Prediction intervals to plot. Defaults to None. |
None
|
max_insample_length
|
int
|
Maximum number of train/insample observations to be plotted. Defaults to None. |
None
|
plot_anomalies
|
bool
|
Plot anomalies for each prediction interval. Defaults to False. |
False
|
engine
|
str
|
Library used to plot. 'plotly', 'plotly-resampler' or 'matplotlib'. Defaults to 'matplotlib'. |
'matplotlib'
|
palette
|
str
|
Name of the matplotlib colormap to use for the plots. If None, uses the current style. Defaults to None. |
None
|
seed
|
int
|
Seed used for the random number generator. Only used if plot_random is True. Defaults to 0. |
None
|
resampler_kwargs
|
dict
|
Keyword arguments to be passed to
plotly-resampler constructor. For further custumization ("show_dash")
call the method, store the plotting object and add the extra arguments
to its |
None
|
ax
|
matplotlib axes, array of matplotlib axes or plotly Figure
|
Object where plots will be added. Defaults to None. |
None
|
Source code in timecopilot/models/utils/forecaster.py
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 |
|