Forecasting Hello World!¶
Feng Li¶
Guanghua School of Management¶
Peking University¶
feng.li@gsm.pku.edu.cn¶
Course home page: https://feng.li/forecasting-with-ai¶
Let's forecast¶
- We will utilize an online AI forecasting platform Nixtla
- https://dashboard.nixtla.io
In [3]:
! pip install nixtla --break-system-packages
Defaulting to user installation because normal site-packages is not writeable WARNING: Skipping /usr/lib/python3.12/dist-packages/charset_normalizer-3.3.2.dist-info due to invalid metadata entry 'name' Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting nixtla Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a8/4d/ba5d2e326e80f543c7a037d9c06e2525475d9b4928aaf1b8e15c68c44d2f/nixtla-0.7.0-py3-none-any.whl (125 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 126.0/126.0 kB 2.6 MB/s eta 0:00:00 MB/s eta 0:00:01 Requirement already satisfied: annotated-types in /usr/local/lib/python3.12/dist-packages (from nixtla) (0.7.0) Requirement already satisfied: httpx[zstd] in /usr/local/lib/python3.12/dist-packages (from nixtla) (0.27.2) Collecting orjson (from nixtla) Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a0/26/5f028c7d81ad2ebbf84414ba6d6c9cac03f22f5cd0d01eb40fb2d6a06b07/orjson-3.11.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (132 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 132.9/132.9 kB 11.6 MB/s eta 0:00:00 Requirement already satisfied: pandas in /usr/lib/python3/dist-packages (from nixtla) (2.1.4+dfsg) Requirement already satisfied: pydantic>=1.10 in /usr/local/lib/python3.12/dist-packages (from nixtla) (2.9.2) Collecting tenacity (from nixtla) Downloading https://pypi.tuna.tsinghua.edu.cn/packages/e5/30/643397144bfbfec6f6ef821f36f33e57d35946c44a2352d3c9f0ae847619/tenacity-9.1.2-py3-none-any.whl (28 kB) Requirement already satisfied: tqdm in /usr/lib/python3/dist-packages (from nixtla) (0.0.0) Collecting utilsforecast>=0.2.8 (from nixtla) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/75/9b/f6336ce71f4e6ed32877309314f549192cd6b982ad6d96fd8b1b5a230870/utilsforecast-0.2.12-py3-none-any.whl (42 kB) Requirement already satisfied: pydantic-core==2.23.4 in /usr/local/lib/python3.12/dist-packages (from pydantic>=1.10->nixtla) (2.23.4) Requirement already satisfied: typing-extensions>=4.6.1 in /usr/lib/python3/dist-packages (from pydantic>=1.10->nixtla) (4.10.0) Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (from utilsforecast>=0.2.8->nixtla) (1.26.4) Requirement already satisfied: packaging in /usr/lib/python3/dist-packages (from utilsforecast>=0.2.8->nixtla) (24.0) Requirement already satisfied: anyio in /usr/local/lib/python3.12/dist-packages (from httpx[zstd]->nixtla) (4.5.0) Requirement already satisfied: certifi in /usr/lib/python3/dist-packages (from httpx[zstd]->nixtla) (2023.11.17) Requirement already satisfied: httpcore==1.* in /usr/local/lib/python3.12/dist-packages (from httpx[zstd]->nixtla) (1.0.5) Requirement already satisfied: idna in /usr/lib/python3/dist-packages (from httpx[zstd]->nixtla) (3.6) Requirement already satisfied: sniffio in /usr/local/lib/python3.12/dist-packages (from httpx[zstd]->nixtla) (1.3.1) Collecting zstandard>=0.18.0 (from httpx[zstd]->nixtla) Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9a/c7/31674cb2168b741bbbe71ce37dd397c9c671e73349d88ad3bca9e9fae25b/zstandard-0.24.0-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (5.5 MB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 5.5/5.5 MB 7.3 MB/s eta 0:00:00m eta 0:00:010:01:01 Requirement already satisfied: h11<0.15,>=0.13 in /usr/local/lib/python3.12/dist-packages (from httpcore==1.*->httpx[zstd]->nixtla) (0.14.0) WARNING: Skipping /usr/lib/python3.12/dist-packages/charset_normalizer-3.3.2.dist-info due to invalid metadata entry 'name' Installing collected packages: zstandard, utilsforecast, tenacity, orjson, nixtla Successfully installed nixtla-0.7.0 orjson-3.11.3 tenacity-9.1.2 utilsforecast-0.2.12 zstandard-0.24.0
Load the Nixtla API¶
In [3]:
from nixtla import NixtlaClient
nixtla_client = NixtlaClient(
api_key='ASK-TA-FOR-API'
)
In [4]:
nixtla_client.validate_api_key()
INFO:nixtla.nixtla_client:Happy Forecasting! :)
Out[4]:
True
In [5]:
import pandas as pd
df = pd.read_csv('../data/air_passengers_with_id.csv')
df.head()
Out[5]:
unique_id | ds | y | |
---|---|---|---|
0 | D1 | 1/1/1949 | 112 |
1 | D1 | 2/1/1949 | 118 |
2 | D1 | 3/1/1949 | 132 |
3 | D1 | 4/1/1949 | 129 |
4 | D1 | 5/1/1949 | 121 |
In [6]:
nixtla_client.plot(df, time_col='ds', target_col='y')
Out[6]:
In [7]:
timegpt_fcst_df = nixtla_client.forecast(
df=df,
h=12,
freq='MS',
time_col='ds',
target_col='y'
)
timegpt_fcst_df.head()
INFO:nixtla.nixtla_client:Validating inputs... INFO:nixtla.nixtla_client:Preprocessing dataframes... INFO:nixtla.nixtla_client:Querying model metadata... INFO:nixtla.nixtla_client:Restricting input... INFO:nixtla.nixtla_client:Calling Forecast Endpoint...
Out[7]:
unique_id | ds | TimeGPT | |
---|---|---|---|
0 | D1 | 1961-01-01 | 437.83792 |
1 | D1 | 1961-02-01 | 426.06270 |
2 | D1 | 1961-03-01 | 463.11655 |
3 | D1 | 1961-04-01 | 478.24450 |
4 | D1 | 1961-05-01 | 505.64648 |
In [8]:
nixtla_client.plot(df, timegpt_fcst_df, time_col='ds', target_col='y')
Out[8]:
In [9]:
timegpt_fcst_df = nixtla_client.forecast(
df=df,
h=36,
freq='MS',
time_col='ds',
target_col='y',
model='timegpt-1-long-horizon'
)
timegpt_fcst_df.head()
INFO:nixtla.nixtla_client:Validating inputs... INFO:nixtla.nixtla_client:Preprocessing dataframes... INFO:nixtla.nixtla_client:Querying model metadata... WARNING:nixtla.nixtla_client:The specified horizon "h" exceeds the model horizon, this may lead to less accurate forecasts. Please consider using a smaller horizon. INFO:nixtla.nixtla_client:Restricting input... INFO:nixtla.nixtla_client:Calling Forecast Endpoint...
Out[9]:
unique_id | ds | TimeGPT | |
---|---|---|---|
0 | D1 | 1961-01-01 | 436.84340 |
1 | D1 | 1961-02-01 | 419.35153 |
2 | D1 | 1961-03-01 | 458.94315 |
3 | D1 | 1961-04-01 | 477.87607 |
4 | D1 | 1961-05-01 | 505.65692 |
In [10]:
nixtla_client.plot(df, timegpt_fcst_df, time_col='ds', target_col='y')
Out[10]:
In [11]:
timegpt_fcst_df = nixtla_client.forecast(
df=df,
h=6,
freq='MS',
time_col='ds',
target_col='y'
)
nixtla_client.plot(df, timegpt_fcst_df, time_col='ds', target_col='y')
INFO:nixtla.nixtla_client:Validating inputs... INFO:nixtla.nixtla_client:Preprocessing dataframes... INFO:nixtla.nixtla_client:Restricting input... INFO:nixtla.nixtla_client:Calling Forecast Endpoint...
Out[11]:
Calculates various features from time series data¶
- This part does not require an API.
- It is calculated via the
tsfeatures
package.
In [17]:
! pip install tsfeatures --break-system-packages
Defaulting to user installation because normal site-packages is not writeable WARNING: Skipping /usr/lib/python3.12/dist-packages/charset_normalizer-3.3.2.dist-info due to invalid metadata entry 'name' Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple Collecting tsfeatures Downloading https://pypi.tuna.tsinghua.edu.cn/packages/79/61/ba5bcaae1212907bb22508cc87fd59011037374fb0d825fb0a67ea0fe62c/tsfeatures-0.4.5-py3-none-any.whl (28 kB) Collecting antropy>=0.1.4 (from tsfeatures) Downloading https://pypi.tuna.tsinghua.edu.cn/packages/9c/d0/45190403c56c3cd0c2784d5e4bc0def9e8301787b59113d15afd1337e654/antropy-0.1.9-py3-none-any.whl (18 kB) Collecting arch>=4.11 (from tsfeatures) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/09/05/14d9b2773fea75d87e9e55a85f531ed615538eddf18a4b3cad4e34e2f12d/arch-7.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (978 kB) Requirement already satisfied: pandas>=1.0.5 in /usr/lib/python3/dist-packages (from tsfeatures) (2.1.4+dfsg) Collecting scikit-learn>=0.23.1 (from tsfeatures) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/f3/1b/998312db6d361ded1dd56b457ada371a8d8d77ca2195a7d18fd8a1736f21/scikit_learn-1.7.1-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (9.5 MB) Requirement already satisfied: statsmodels>=0.13.2 in /usr/lib/python3/dist-packages (from tsfeatures) (0.14.1+dfsg) Collecting supersmoother>=0.4 (from tsfeatures) Downloading https://pypi.tuna.tsinghua.edu.cn/packages/73/d7/44f69557f2685e80ef7d558b605b080cbb9ae9500c154e4105dbdddb017e/supersmoother-0.4.tar.gz (233 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 233.8/233.8 kB 5.7 MB/s eta 0:00:00[31m6.6 MB/s eta 0:00:01 Preparing metadata (setup.py) ... done Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (from antropy>=0.1.4->tsfeatures) (1.26.4) Requirement already satisfied: scipy in /usr/lib/python3/dist-packages (from antropy>=0.1.4->tsfeatures) (1.11.4) Collecting numba>=0.57 (from antropy>=0.1.4->tsfeatures) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/9a/2d/e518df036feab381c23a624dac47f8445ac55686ec7f11083655eb707da3/numba-0.61.2-cp312-cp312-manylinux2014_x86_64.manylinux_2_17_x86_64.whl (3.9 MB) Collecting joblib>=1.2.0 (from scikit-learn>=0.23.1->tsfeatures) Downloading https://pypi.tuna.tsinghua.edu.cn/packages/1e/e8/685f47e0d754320684db4425a0967f7d3fa70126bffd76110b7009a0090f/joblib-1.5.2-py3-none-any.whl (308 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 308.4/308.4 kB 9.7 MB/s eta 0:00:00 Collecting threadpoolctl>=3.1.0 (from scikit-learn>=0.23.1->tsfeatures) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/32/d5/f9a850d79b0851d1d4ef6456097579a9005b31fea68726a4ae5f2d82ddd9/threadpoolctl-3.6.0-py3-none-any.whl (18 kB) Collecting llvmlite<0.45,>=0.44.0dev0 (from numba>=0.57->antropy>=0.1.4->tsfeatures) Using cached https://pypi.tuna.tsinghua.edu.cn/packages/cb/da/8341fd3056419441286c8e26bf436923021005ece0bff5f41906476ae514/llvmlite-0.44.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (42.4 MB) Building wheels for collected packages: supersmoother Building wheel for supersmoother (setup.py) ... done Created wheel for supersmoother: filename=supersmoother-0.4-py3-none-any.whl size=16145 sha256=0d10164bd39bea6f9fcc7307fdba6cd4fedfda7f9b7d56a0450dfd4e5b0a26f6 Stored in directory: /home/fli/.cache/pip/wheels/33/d2/06/af28090f0f6bcaefe8b36c909a83568e359b889f78839ba70e Successfully built supersmoother WARNING: Skipping /usr/lib/python3.12/dist-packages/charset_normalizer-3.3.2.dist-info due to invalid metadata entry 'name' Installing collected packages: supersmoother, threadpoolctl, llvmlite, joblib, scikit-learn, numba, arch, antropy, tsfeatures Successfully installed antropy-0.1.9 arch-7.2.0 joblib-1.5.2 llvmlite-0.44.0 numba-0.61.2 scikit-learn-1.7.1 supersmoother-0.4 threadpoolctl-3.6.0 tsfeatures-0.4.5
In [13]:
from tsfeatures import tsfeatures
In [14]:
feat = tsfeatures(df, freq=1)
feat
Out[14]:
unique_id | hurst | series_length | unitroot_pp | unitroot_kpss | hw_alpha | hw_beta | hw_gamma | stability | nperiods | ... | flat_spots | entropy | crossing_points | arch_lm | x_acf1 | x_acf10 | diff1_acf1 | diff1_acf10 | diff2_acf1 | diff2_acf10 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | D1 | 1.039864 | 144 | -6.565597 | 2.739474 | NaN | NaN | NaN | 0.882625 | 0 | ... | 18 | 0.428737 | 7 | 0.917195 | 0.948047 | 5.670087 | 0.302855 | 0.408838 | -0.191006 | 0.25078 |
1 rows × 38 columns
In [15]:
feat.T
Out[15]:
0 | |
---|---|
unique_id | D1 |
hurst | 1.039864 |
series_length | 144 |
unitroot_pp | -6.565597 |
unitroot_kpss | 2.739474 |
hw_alpha | NaN |
hw_beta | NaN |
hw_gamma | NaN |
stability | 0.882625 |
nperiods | 0 |
seasonal_period | 1 |
trend | 0.911575 |
spike | 0.000001 |
linearity | 10.741923 |
curvature | 0.760306 |
e_acf1 | 0.619439 |
e_acf10 | 1.133758 |
x_pacf5 | 0.967097 |
diff1x_pacf5 | 0.212245 |
diff2x_pacf5 | 0.247661 |
nonlinearity | 0.423897 |
lumpiness | 0.026838 |
alpha | 1.0 |
beta | 0.0 |
arch_acf | NaN |
garch_acf | NaN |
arch_r2 | NaN |
garch_r2 | NaN |
flat_spots | 18 |
entropy | 0.428737 |
crossing_points | 7 |
arch_lm | 0.917195 |
x_acf1 | 0.948047 |
x_acf10 | 5.670087 |
diff1_acf1 | 0.302855 |
diff1_acf10 | 0.408838 |
diff2_acf1 | -0.191006 |
diff2_acf10 | 0.25078 |