Forecast Using the Chronos Family¶
Import libraries¶
In [1]:
Copied!
import pandas as pd
from timecopilot import TimeCopilotForecaster
import pandas as pd
from timecopilot import TimeCopilotForecaster
Load the dataset¶
The DataFrame must include at least the following columns:
- unique_id: Unique identifier for each time series (string)
- ds: Date column (datetime format)
- y: Target variable for forecasting (float format)
The pandas frequency will be inferred from the ds column, if not provided. If the seasonality is not provided, it will be inferred based on the frequency. If the horizon is not set, it will default to 2 times the inferred seasonality.
In [2]:
Copied!
df = pd.read_csv(
"https://timecopilot.s3.amazonaws.com/public/data/events_pageviews.csv",
parse_dates=["ds"],
)
df.head()
df = pd.read_csv(
"https://timecopilot.s3.amazonaws.com/public/data/events_pageviews.csv",
parse_dates=["ds"],
)
df.head()
Out[2]:
| unique_id | ds | y | |
|---|---|---|---|
| 0 | Oktoberfest | 2020-01-31 | 25376 |
| 1 | Oktoberfest | 2020-02-29 | 28470 |
| 2 | Oktoberfest | 2020-03-31 | 23816 |
| 3 | Oktoberfest | 2020-04-30 | 46186 |
| 4 | Oktoberfest | 2020-05-31 | 31213 |
Plot the data¶
In [3]:
Copied!
TimeCopilotForecaster.plot(df)
TimeCopilotForecaster.plot(df)
Out[3]:
Import the Chronos class¶
In [ ]:
Copied!
from timecopilot.models.foundation.chronos import Chronos
from timecopilot.models.stats import SeasonalNaive
from timecopilot.models.foundation.chronos import Chronos
from timecopilot.models.stats import SeasonalNaive
Create a TimeCopilotForecaster¶
In [5]:
Copied!
chronos_av_models = [
"amazon/chronos-2",
"amazon/chronos-bolt-tiny",
"amazon/chronos-bolt-mini",
"amazon/chronos-bolt-small",
"amazon/chronos-bolt-base",
"amazon/chronos-t5-tiny",
"amazon/chronos-t5-mini",
"amazon/chronos-t5-small",
"amazon/chronos-t5-base",
"amazon/chronos-t5-large",
]
chronos_models = [Chronos(repo_id=model, alias=model) for model in chronos_av_models]
models = chronos_models + [SeasonalNaive()]
tcf = TimeCopilotForecaster(models=models)
chronos_av_models = [
"amazon/chronos-2",
"amazon/chronos-bolt-tiny",
"amazon/chronos-bolt-mini",
"amazon/chronos-bolt-small",
"amazon/chronos-bolt-base",
"amazon/chronos-t5-tiny",
"amazon/chronos-t5-mini",
"amazon/chronos-t5-small",
"amazon/chronos-t5-base",
"amazon/chronos-t5-large",
]
chronos_models = [Chronos(repo_id=model, alias=model) for model in chronos_av_models]
models = chronos_models + [SeasonalNaive()]
tcf = TimeCopilotForecaster(models=models)
Generate forecast¶
You can optionally specify the following parameters:
- freq: The frequency of your data (e.g., 'D' for daily, 'M' for monthly)
- h: The forecast horizon, which is the number of periods to predict
- seasonality: The seasonal period of your data, which can be inferred if not provided
In [ ]:
Copied!
level = [20, 40, 60, 80]
cv_df = tcf.cross_validation(df=df, h=12, level=level)
level = [20, 40, 60, 80]
cv_df = tcf.cross_validation(df=df, h=12, level=level)
In [7]:
Copied!
tcf.plot(df, cv_df.drop(columns=["cutoff", "y"]), level=[80])
tcf.plot(df, cv_df.drop(columns=["cutoff", "y"]), level=[80])
Out[7]:
In [8]:
Copied!
cv_df.head()
cv_df.head()
Out[8]:
| unique_id | ds | cutoff | y | amazon/chronos-2 | amazon/chronos-2-lo-20 | amazon/chronos-2-hi-20 | amazon/chronos-2-lo-40 | amazon/chronos-2-hi-40 | amazon/chronos-2-lo-60 | ... | amazon/chronos-t5-large-hi-80 | SeasonalNaive | SeasonalNaive-lo-20 | SeasonalNaive-lo-40 | SeasonalNaive-lo-60 | SeasonalNaive-lo-80 | SeasonalNaive-hi-20 | SeasonalNaive-hi-40 | SeasonalNaive-hi-60 | SeasonalNaive-hi-80 | |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 0 | Black Friday | 2024-09-30 | 2024-08-31 | 2607 | 2080.0 | 1992.0 | 2176.0 | 1896.0 | 2288.0 | 1784.0 | ... | 2832.483154 | 2160.0 | 518.105774 | -1238.539673 | -3294.386475 | -6145.491211 | 3801.894287 | 5558.539551 | 7614.386719 | 10465.491211 |
| 1 | Black Friday | 2024-10-31 | 2024-08-31 | 2470 | 2688.0 | 2320.0 | 3328.0 | 2048.0 | 4352.0 | 1856.0 | ... | 3999.947998 | 3746.0 | 2104.105713 | 347.460358 | -1708.386597 | -4559.491211 | 5387.894043 | 7144.539551 | 9200.386719 | 12051.491211 |
| 2 | Black Friday | 2024-11-30 | 2024-08-31 | 11058 | 18304.0 | 15936.0 | 20352.0 | 12352.0 | 22784.0 | 9152.0 | ... | 21284.275391 | 17053.0 | 15411.105469 | 13654.459961 | 11598.613281 | 8747.508789 | 18694.894531 | 20451.539062 | 22507.386719 | 25358.490234 |
| 3 | Black Friday | 2024-12-31 | 2024-08-31 | 3548 | 3264.0 | 2736.0 | 4160.0 | 2352.0 | 5376.0 | 1992.0 | ... | 3876.372070 | 5407.0 | 3765.105713 | 2008.460327 | -47.386593 | -2898.491211 | 7048.894043 | 8805.540039 | 10861.386719 | 13712.491211 |
| 4 | Black Friday | 2025-01-31 | 2024-08-31 | 1724 | 2008.0 | 1912.0 | 2080.0 | 1816.0 | 2192.0 | 1688.0 | ... | 2504.032227 | 2656.0 | 1014.105774 | -742.539673 | -2798.386475 | -5649.491211 | 4297.894043 | 6054.539551 | 8110.386719 | 10961.491211 |
5 rows × 103 columns
Evaluation¶
In [9]:
Copied!
from functools import partial
from utilsforecast.evaluation import evaluate
from utilsforecast.losses import mase, scaled_crps
from functools import partial
from utilsforecast.evaluation import evaluate
from utilsforecast.losses import mase, scaled_crps
In [10]:
Copied!
eval_df = evaluate(
cv_df.drop(columns=["cutoff"]),
train_df=df.query("ds <= '2024-08-31'"),
metrics=[partial(mase, seasonality=12), scaled_crps],
level=level,
)
eval_df.groupby("metric").mean(numeric_only=True).T.sort_values(by="scaled_crps").round(3)
eval_df = evaluate(
cv_df.drop(columns=["cutoff"]),
train_df=df.query("ds <= '2024-08-31'"),
metrics=[partial(mase, seasonality=12), scaled_crps],
level=level,
)
eval_df.groupby("metric").mean(numeric_only=True).T.sort_values(by="scaled_crps").round(3)
Out[10]:
| metric | mase | scaled_crps |
|---|---|---|
| amazon/chronos-t5-large | 1.106 | 0.302 |
| amazon/chronos-bolt-small | 0.996 | 0.306 |
| amazon/chronos-bolt-mini | 1.282 | 0.331 |
| amazon/chronos-bolt-tiny | 1.152 | 0.336 |
| amazon/chronos-bolt-base | 1.335 | 0.350 |
| amazon/chronos-t5-small | 1.377 | 0.358 |
| amazon/chronos-2 | 1.646 | 0.360 |
| amazon/chronos-t5-base | 1.419 | 0.379 |
| amazon/chronos-t5-mini | 1.426 | 0.428 |
| amazon/chronos-t5-tiny | 2.505 | 0.505 |
| SeasonalNaive | 1.576 | 0.521 |