{ "cells": [ { "cell_type": "markdown", "id": "dc7dbdb5-f895-4737-a8e6-e336d3f08a90", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "# Forecasting with Chronos\n", "\n", "## Feng Li\n", "\n", "### Guanghua School of Management\n", "### Peking University\n", "\n", "### [feng.li@gsm.pku.edu.cn](feng.li@gsm.pku.edu.cn)\n", "### Course home page: [https://feng.li/forecasting-with-ai](https://feng.li/forecasting-with-ai)" ] }, { "cell_type": "markdown", "id": "cf4c3cba", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Chronos-2 Basics\n", "\n", "**Chronos-2** is a foundation model for time series forecasting that builds on [Chronos](https://arxiv.org/abs/2403.07815) and [Chronos-Bolt](https://aws.amazon.com/blogs/machine-learning/fast-and-accurate-zero-shot-forecasting-with-chronos-bolt-and-autogluon/). It offers significant improvements in capabilities and can handle diverse forecasting scenarios not supported by earlier models.\n", "\n", "| Capability | Chronos | Chronos-Bolt | Chronos-2 |\n", "|------------|---------|--------------|-----------|\n", "| Univariate Forecasting | ✅ | ✅ | ✅ |\n", "| Cross-learning across items | ❌ | ❌ | ✅ |\n", "| Multivariate Forecasting | ❌ | ❌ | ✅ |\n", "| Past-only (real/categorical) covariates | ❌ | ❌ | ✅ |\n", "| Known future (real/categorical) covariates | 🧩 | 🧩 | ✅ |\n", "| Fine-tuning support | ✅ | ✅ | ✅ |\n", "| Max. Context Length | 512 | 2048 | 8192 |\n", "\n", "🧩 Chronos/Chronos-Bolt do not natively support future covariates, but they can be combined with external covariate regressors (see [AutoGluon tutorial](https://auto.gluon.ai/stable/tutorials/timeseries/forecasting-chronos.html#incorporating-the-covariates)). This only models per-timestep effects, not effects across time. In contrast, Chronos-2 supports all covariate types natively.\n", "\n", "More details about Chronos-2 are available in the [technical report](https://www.arxiv.org/abs/2510.15821)." ] }, { "cell_type": "code", "execution_count": 1, "id": "e1d5f2e6", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "outputs": [], "source": [ "# pip install -U \"chronos-forecasting>=2.0\" \"pandas[pyarrow]\" \"matplotlib\" --break-system-packages" ] }, { "cell_type": "code", "execution_count": 2, "id": "fcc7e496", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "2025-11-14 11:24:09.328586: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:485] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n", "2025-11-14 11:24:09.345621: E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:8454] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n", "2025-11-14 11:24:09.350534: E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1452] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n", "2025-11-14 11:24:09.363797: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", "To enable the following instructions: AVX2 AVX512F FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", "2025-11-14 11:24:10.185523: W tensorflow/compiler/tf2tensorrt/utils/py_utils.cc:38] TF-TRT Warning: Could not find TensorRT\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Loaded Chronos-2 from local dir on cpu\n" ] } ], "source": [ "from chronos import BaseChronosPipeline, Chronos2Pipeline\n", "# https://huggingface.co/amazon/chronos-2\n", "LOCAL_MODEL_DIR = \"../data/chronos-2\" # Your offline pretrained time series foundation model\n", "pipeline: Chronos2Pipeline = BaseChronosPipeline.from_pretrained(\n", " LOCAL_MODEL_DIR, device_map='cpu'\n", ")\n", "print(\"Loaded Chronos-2 from local dir on\", 'cpu')" ] }, { "cell_type": "markdown", "id": "5ef707b6", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "source": [ "## Univariate Forecasting\n", "\n", "We start with a simple univariate forecasting example using the pandas API." ] }, { "cell_type": "code", "execution_count": 2, "id": "39de5d7e", "metadata": { "editable": true, "slideshow": { "slide_type": "slide" }, "tags": [] }, "outputs": [ { "data": { "text/html": [ "
| \n", " | item_id | \n", "timestamp | \n", "target | \n", "
|---|---|---|---|
| 0 | \n", "H1 | \n", "1750-01-01 00:00:00 | \n", "605.0 | \n", "
| 1 | \n", "H1 | \n", "1750-01-01 01:00:00 | \n", "586.0 | \n", "
| 2 | \n", "H1 | \n", "1750-01-01 02:00:00 | \n", "586.0 | \n", "
| 3 | \n", "H1 | \n", "1750-01-01 03:00:00 | \n", "559.0 | \n", "
| 4 | \n", "H1 | \n", "1750-01-01 04:00:00 | \n", "511.0 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "
| 353495 | \n", "H414 | \n", "1750-02-09 19:00:00 | \n", "48.0 | \n", "
| 353496 | \n", "H414 | \n", "1750-02-09 20:00:00 | \n", "41.0 | \n", "
| 353497 | \n", "H414 | \n", "1750-02-09 21:00:00 | \n", "35.0 | \n", "
| 353498 | \n", "H414 | \n", "1750-02-09 22:00:00 | \n", "26.0 | \n", "
| 353499 | \n", "H414 | \n", "1750-02-09 23:00:00 | \n", "17.0 | \n", "
353500 rows × 3 columns
\n", "| \n", " | item_id | \n", "timestamp | \n", "target_name | \n", "predictions | \n", "0.1 | \n", "0.9 | \n", "
|---|---|---|---|---|---|---|
| 0 | \n", "H1 | \n", "1750-01-30 04:00:00 | \n", "target | \n", "624.867920 | \n", "611.385071 | \n", "638.598694 | \n", "
| 1 | \n", "H1 | \n", "1750-01-30 05:00:00 | \n", "target | \n", "563.703125 | \n", "546.655029 | \n", "578.665649 | \n", "
| 2 | \n", "H1 | \n", "1750-01-30 06:00:00 | \n", "target | \n", "521.589905 | \n", "505.747437 | \n", "537.950806 | \n", "
| 3 | \n", "H1 | \n", "1750-01-30 07:00:00 | \n", "target | \n", "489.910706 | \n", "473.671814 | \n", "508.854126 | \n", "
| 4 | \n", "H1 | \n", "1750-01-30 08:00:00 | \n", "target | \n", "471.144501 | \n", "452.199371 | \n", "491.050354 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 9931 | \n", "H414 | \n", "1750-02-10 19:00:00 | \n", "target | \n", "61.697693 | \n", "49.787407 | \n", "75.447968 | \n", "
| 9932 | \n", "H414 | \n", "1750-02-10 20:00:00 | \n", "target | \n", "52.210609 | \n", "41.923550 | \n", "65.115601 | \n", "
| 9933 | \n", "H414 | \n", "1750-02-10 21:00:00 | \n", "target | \n", "46.259827 | \n", "36.681183 | \n", "55.795212 | \n", "
| 9934 | \n", "H414 | \n", "1750-02-10 22:00:00 | \n", "target | \n", "33.600002 | \n", "26.682114 | \n", "41.483673 | \n", "
| 9935 | \n", "H414 | \n", "1750-02-10 23:00:00 | \n", "target | \n", "22.696373 | \n", "17.629684 | \n", "26.969643 | \n", "
9936 rows × 6 columns
\n", "| \n", " | id | \n", "timestamp | \n", "Sales | \n", "Open | \n", "Promo | \n", "SchoolHoliday | \n", "StateHoliday | \n", "Customers | \n", "
|---|---|---|---|---|---|---|---|---|
| 0 | \n", "1 | \n", "2013-01-13 | \n", "32952.0 | \n", "0.857143 | \n", "0.714286 | \n", "5.0 | \n", "0.0 | \n", "3918.0 | \n", "
| 1 | \n", "1 | \n", "2013-01-20 | \n", "25978.0 | \n", "0.857143 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "3417.0 | \n", "
| 2 | \n", "1 | \n", "2013-01-27 | \n", "33071.0 | \n", "0.857143 | \n", "0.714286 | \n", "0.0 | \n", "0.0 | \n", "3862.0 | \n", "
| 3 | \n", "1 | \n", "2013-02-03 | \n", "28693.0 | \n", "0.857143 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "3561.0 | \n", "
| 4 | \n", "1 | \n", "2013-02-10 | \n", "35771.0 | \n", "0.857143 | \n", "0.714286 | \n", "0.0 | \n", "0.0 | \n", "4094.0 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 133795 | \n", "999 | \n", "2015-03-29 | \n", "43358.0 | \n", "0.857143 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "3252.0 | \n", "
| 133796 | \n", "999 | \n", "2015-04-05 | \n", "69663.0 | \n", "0.714286 | \n", "0.714286 | \n", "2.0 | \n", "1.0 | \n", "4424.0 | \n", "
| 133797 | \n", "999 | \n", "2015-04-12 | \n", "35267.0 | \n", "0.714286 | \n", "0.000000 | \n", "5.0 | \n", "1.0 | \n", "2771.0 | \n", "
| 133798 | \n", "999 | \n", "2015-04-19 | \n", "63849.0 | \n", "0.857143 | \n", "0.714286 | \n", "0.0 | \n", "0.0 | \n", "4230.0 | \n", "
| 133799 | \n", "999 | \n", "2015-04-26 | \n", "39247.0 | \n", "0.857143 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "3027.0 | \n", "
133800 rows × 8 columns
\n", "| \n", " | id | \n", "timestamp | \n", "Open | \n", "Promo | \n", "SchoolHoliday | \n", "StateHoliday | \n", "
|---|---|---|---|---|---|---|
| 0 | \n", "1 | \n", "2015-05-03 | \n", "0.714286 | \n", "0.714286 | \n", "0.0 | \n", "1.0 | \n", "
| 1 | \n", "1 | \n", "2015-05-10 | \n", "0.857143 | \n", "0.714286 | \n", "0.0 | \n", "0.0 | \n", "
| 2 | \n", "1 | \n", "2015-05-17 | \n", "0.714286 | \n", "0.000000 | \n", "0.0 | \n", "1.0 | \n", "
| 3 | \n", "1 | \n", "2015-05-24 | \n", "0.857143 | \n", "0.714286 | \n", "0.0 | \n", "0.0 | \n", "
| 4 | \n", "1 | \n", "2015-05-31 | \n", "0.714286 | \n", "0.000000 | \n", "0.0 | \n", "1.0 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 14490 | \n", "999 | \n", "2015-06-28 | \n", "0.857143 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "
| 14491 | \n", "999 | \n", "2015-07-05 | \n", "0.857143 | \n", "0.714286 | \n", "0.0 | \n", "0.0 | \n", "
| 14492 | \n", "999 | \n", "2015-07-12 | \n", "0.857143 | \n", "0.000000 | \n", "0.0 | \n", "0.0 | \n", "
| 14493 | \n", "999 | \n", "2015-07-19 | \n", "0.857143 | \n", "0.714286 | \n", "5.0 | \n", "0.0 | \n", "
| 14494 | \n", "999 | \n", "2015-07-26 | \n", "0.857143 | \n", "0.000000 | \n", "5.0 | \n", "0.0 | \n", "
14495 rows × 6 columns
\n", "| \n", " | id | \n", "timestamp | \n", "target_name | \n", "predictions | \n", "0.1 | \n", "0.9 | \n", "
|---|---|---|---|---|---|---|
| 0 | \n", "1 | \n", "2015-05-03 | \n", "Sales | \n", "28868.748047 | \n", "24831.210938 | \n", "32377.041016 | \n", "
| 1 | \n", "1 | \n", "2015-05-10 | \n", "Sales | \n", "23496.941406 | \n", "20528.724609 | \n", "28279.544922 | \n", "
| 2 | \n", "1 | \n", "2015-05-17 | \n", "Sales | \n", "28338.820312 | \n", "23889.726562 | \n", "32065.578125 | \n", "
| 3 | \n", "1 | \n", "2015-05-24 | \n", "Sales | \n", "24089.554688 | \n", "20643.382812 | \n", "29365.500000 | \n", "
| 4 | \n", "1 | \n", "2015-05-31 | \n", "Sales | \n", "27662.677734 | \n", "22583.529297 | \n", "31700.175781 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 14490 | \n", "999 | \n", "2015-06-28 | \n", "Sales | \n", "53684.523438 | \n", "38193.703125 | \n", "66396.875000 | \n", "
| 14491 | \n", "999 | \n", "2015-07-05 | \n", "Sales | \n", "56915.792969 | \n", "38879.417969 | \n", "66857.273438 | \n", "
| 14492 | \n", "999 | \n", "2015-07-12 | \n", "Sales | \n", "53435.722656 | \n", "37711.125000 | \n", "66004.101562 | \n", "
| 14493 | \n", "999 | \n", "2015-07-19 | \n", "Sales | \n", "56994.824219 | \n", "38699.746094 | \n", "66507.718750 | \n", "
| 14494 | \n", "999 | \n", "2015-07-26 | \n", "Sales | \n", "51786.867188 | \n", "37531.007812 | \n", "65674.789062 | \n", "
14495 rows × 6 columns
\n", "| \n", " | id | \n", "timestamp | \n", "target_name | \n", "predictions | \n", "0.1 | \n", "0.9 | \n", "
|---|---|---|---|---|---|---|
| 0 | \n", "1 | \n", "2015-05-03 | \n", "Sales | \n", "28939.392578 | \n", "25214.275391 | \n", "32411.097656 | \n", "
| 1 | \n", "1 | \n", "2015-05-10 | \n", "Sales | \n", "25541.919922 | \n", "21921.328125 | \n", "29191.929688 | \n", "
| 2 | \n", "1 | \n", "2015-05-17 | \n", "Sales | \n", "23640.240234 | \n", "20500.343750 | \n", "26884.666016 | \n", "
| 3 | \n", "1 | \n", "2015-05-24 | \n", "Sales | \n", "26778.261719 | \n", "23318.353516 | \n", "30162.820312 | \n", "
| 4 | \n", "1 | \n", "2015-05-31 | \n", "Sales | \n", "22679.357422 | \n", "19722.281250 | \n", "25990.042969 | \n", "
| ... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "... | \n", "
| 14490 | \n", "999 | \n", "2015-06-28 | \n", "Sales | \n", "40080.484375 | \n", "34807.414062 | \n", "47214.660156 | \n", "
| 14491 | \n", "999 | \n", "2015-07-05 | \n", "Sales | \n", "68556.195312 | \n", "61109.796875 | \n", "75537.335938 | \n", "
| 14492 | \n", "999 | \n", "2015-07-12 | \n", "Sales | \n", "40855.218750 | \n", "35225.363281 | \n", "48365.933594 | \n", "
| 14493 | \n", "999 | \n", "2015-07-19 | \n", "Sales | \n", "66134.984375 | \n", "59100.578125 | \n", "73635.484375 | \n", "
| 14494 | \n", "999 | \n", "2015-07-26 | \n", "Sales | \n", "39742.546875 | \n", "34394.359375 | \n", "46101.925781 | \n", "
14495 rows × 6 columns
\n", "