Multiple series forecasting

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.

Note that the forecasts are still univariate. This means that although TimeGPT is a global model, it wonโ€™t consider the inter-feature relationships within the target series. However, TimeGPT does support the use of exogenous variables such as categorical variables (e.g., category, brand), numerical variables (e.g., temperature, prices), or even special holidays.

Letโ€™s see this in action.

1. Import packages

First, we install and import the required packages and initialize the Nixtla client.

As always, we start off by intializing an instance of NixtlaClient.

import pandas as pd
from nixtla import NixtlaClient
nixtla_client = NixtlaClient(
    # defaults to os.environ.get("NIXTLA_API_KEY")
    api_key = 'my_api_key_provided_by_nixtla'
)

๐Ÿ‘

Use an Azure AI endpoint

To use an Azure AI endpoint, remember to set also the base_url argument:

nixtla_client = NixtlaClient(base_url="you azure ai endpoint", api_key="your api_key")

2. Load the data

The following dataset contains prices of different electricity markets in Europe.

Mutliple series are automatically detected in TimeGPT using the unique_id column. This column contains labels for each series. If there are multiple unique values in that column, then it knows it is handling a multi-series scneario.

In this particular case, the unique_id column contains the value BE, DE, FR, JPM, and NP.

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:

nixtla_client.plot(df)

3. Forecasting Multiple Series

To forecast all series at once, we simply pass the dataframe to the df argument. TimeGPt will automatically forecast all series.

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

๐Ÿ“˜

Available models in Azure AI

If you are using an Azure AI endpoint, please be sure to set model="azureai":

nixtla_client.forecast(..., model="azureai")

For the public API, we support two models: timegpt-1 and timegpt-1-long-horizon.

By default, timegpt-1 is used. Please see this tutorial on how and when to use timegpt-1-long-horizon.

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

From the figure above, we can see that the model effectively generated predictions for each unique series in the dataset.

Historical forecast

You can also compute prediction intervals for historical forecasts adding the add_history=True.

To specify the confidence interval, we use the level argument. Here, we pass the list [80, 90]. This will compute a 80% and 90% confidence interval.

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

๐Ÿ“˜

Available models in Azure AI

If you are using an Azure AI endpoint, please be sure to set model="azureai":

nixtla_client.forecast(..., model="azureai")

For the public API, we support two models: timegpt-1 and timegpt-1-long-horizon.

By default, timegpt-1 is used. Please see this tutorial on how and when to use timegpt-1-long-horizon.

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

In the figure above, we now see the historical predictions made by TimeGPT for each series, along with the 80% and 90% confidence intervals.