Multiple Series

TimeGPT provides a robust solution for multi-series forecasting, which involves analyzing multiple data series concurrently, rather than a single one. The tool can be fine-tuned using a broad collection of series, enabling you to tailor the model to suit your specific needs or tasks.


import pandas as pd
from nixtlats import TimeGPT

/home/ubuntu/miniconda/envs/nixtlats/lib/python3.11/site-packages/statsforecast/core.py:25: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from tqdm.autonotebook import tqdm

timegpt = TimeGPT(
    # defaults to os.environ.get("TIMEGPT_TOKEN")
    token = 'my_token_provided_by_nixtla'
)

The following dataset contains prices of different electricity markets. Let see how can we forecast them. The main argument of the forecast method is the input data frame with the historical values of the time series you want to forecast. This data frame can contain information from many time series. Use the unique_id column to identify the different time series of your dataset.


df = pd.read_csv('https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/electricity-short.csv')
df.head()

unique_iddsy
0BE2016-12-01 00:00:0072.00
1BE2016-12-01 01:00:0065.80
2BE2016-12-01 02:00:0059.99
3BE2016-12-01 03:00:0050.69
4BE2016-12-01 04:00:0052.58
Let’s plot this series using [`StatsForecast`](https://github.com/Nixtla/statsforecast):

timegpt.plot(df)


We just have to pass the dataframe to create forecasts for all the time series at once.


timegpt_fcst_multiseries_df = timegpt.forecast(df=df, h=24, level=[80, 90])
timegpt_fcst_multiseries_df.head()

INFO:nixtlats.timegpt:Validating inputs...
INFO:nixtlats.timegpt:Preprocessing dataframes...
INFO:nixtlats.timegpt:Inferred freq: H
INFO:nixtlats.timegpt:Restricting input...
INFO:nixtlats.timegpt:Calling Forecast Endpoint...
unique_iddsTimeGPTTimeGPT-lo-90TimeGPT-lo-80TimeGPT-hi-80TimeGPT-hi-90
0BE2016-12-31 00:00:0046.15117636.66047538.33701953.96533455.641878
1BE2016-12-31 01:00:0042.42659831.60222733.97671750.87647853.250968
2BE2016-12-31 02:00:0040.24288930.43996633.63498146.85079850.045813
3BE2016-12-31 03:00:0038.26533926.84148131.02209645.50858249.689197
4BE2016-12-31 04:00:0036.61880118.54138427.98134845.25625554.696218

timegpt.plot(df, timegpt_fcst_multiseries_df, max_insample_length=365, level=[80, 90])

Historical forecast

You can also compute prediction intervals for historical forecasts adding the add_history=True parameter as follows:


timegpt_fcst_multiseries_with_history_df = timegpt.forecast(df=df, h=24, level=[80, 90], add_history=True)
timegpt_fcst_multiseries_with_history_df.head()

INFO:nixtlats.timegpt:Validating inputs...
INFO:nixtlats.timegpt:Preprocessing dataframes...
INFO:nixtlats.timegpt:Inferred freq: H
INFO:nixtlats.timegpt:Calling Forecast Endpoint...
INFO:nixtlats.timegpt:Calling Historical Forecast Endpoint...
unique_iddsTimeGPTTimeGPT-lo-80TimeGPT-lo-90TimeGPT-hi-80TimeGPT-hi-90
0BE2016-12-06 00:00:0055.75632542.06646938.18558569.44618073.327064
1BE2016-12-06 01:00:0052.82019839.13034235.24945866.51005470.390938
2BE2016-12-06 02:00:0046.85107833.16122229.28033860.54093464.421818
3BE2016-12-06 03:00:0050.64088436.95102933.07014564.33074068.211624
4BE2016-12-06 04:00:0052.42039538.73053934.84965566.11025169.991134

timegpt.plot(
    df, 
    timegpt_fcst_multiseries_with_history_df.groupby('unique_id').tail(365 + 24), 
    max_insample_length=365, 
    level=[80, 90],
)