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 NixtlaClient


nixtla_client = NixtlaClient(
    # defaults to os.environ.get("NIXTLA_API_KEY")
    api_key = 'my_api_key_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 [`NixtlaClient`](https://nixtlaverse.nixtla.io/nixtla/index.html):

nixtla_client.plot(df)


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


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

INFO:nixtlats.nixtla_client:Validating inputs...
INFO:nixtlats.nixtla_client:Preprocessing dataframes...
INFO:nixtlats.nixtla_client:Inferred freq: H
INFO:nixtlats.nixtla_client:Restricting input...
INFO:nixtlats.nixtla_client:Calling Forecast Endpoint...
unique_iddsTimeGPTTimeGPT-lo-90TimeGPT-lo-80TimeGPT-hi-80TimeGPT-hi-90
0BE2016-12-31 00:00:0046.15117636.66047838.33701953.96533455.641875
1BE2016-12-31 01:00:0042.42659831.60223133.97672450.87647153.250964
2BE2016-12-31 02:00:0040.24288930.43997033.63498546.85079450.045809
3BE2016-12-31 03:00:0038.26533926.84148131.02209345.50858549.689197
4BE2016-12-31 04:00:0036.61880118.54138427.98134645.25625654.696218

nixtla_client.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 = nixtla_client.forecast(df=df, h=24, level=[80, 90], add_history=True)
timegpt_fcst_multiseries_with_history_df.head()

INFO:nixtlats.nixtla_client:Validating inputs...
INFO:nixtlats.nixtla_client:Preprocessing dataframes...
INFO:nixtlats.nixtla_client:Inferred freq: H
INFO:nixtlats.nixtla_client:Calling Forecast Endpoint...
INFO:nixtlats.nixtla_client:Calling Historical Forecast Endpoint...
unique_iddsTimeGPTTimeGPT-lo-80TimeGPT-lo-90TimeGPT-hi-80TimeGPT-hi-90
0BE2016-12-06 00:00:0055.75633242.06647638.18559369.44618873.327072
1BE2016-12-06 01:00:0052.82020639.13035035.24946666.51006270.390946
2BE2016-12-06 02:00:0046.85107033.16121429.28033160.54092664.421810
3BE2016-12-06 03:00:0050.64089236.95103633.07015264.33074868.211632
4BE2016-12-06 04:00:0052.42041038.73055434.84967066.11026669.991150

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