Exogenous variables
Exogenous variables or external factors are crucial in time series forecasting as they provide additional information that might influence the prediction. These variables could include holiday markers, marketing spending, weather data, or any other external data that correlate with the time series data you are forecasting.
For example, if you’re forecasting ice cream sales, temperature data could serve as a useful exogenous variable. On hotter days, ice cream sales may increase.
To incorporate exogenous variables in TimeGPT, you’ll need to pair each point in your time series data with the corresponding external data.
1. Import packages
First, we import the required packages and initialize the Nixtla client.
👍 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 data
Let’s see an example on predicting day-ahead electricity prices. The
following dataset contains the hourly electricity price (y
column) for
five markets in Europe and US, identified by the unique_id
column. The
columns from Exogenous1
to day_6
are exogenous variables that
TimeGPT will use to predict the prices.
unique_id | ds | y | Exogenous1 | Exogenous2 | day_0 | day_1 | day_2 | day_3 | day_4 | day_5 | day_6 | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | BE | 2016-10-22 00:00:00 | 70.00 | 49593.0 | 57253.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
1 | BE | 2016-10-22 01:00:00 | 37.10 | 46073.0 | 51887.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
2 | BE | 2016-10-22 02:00:00 | 37.10 | 44927.0 | 51896.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
3 | BE | 2016-10-22 03:00:00 | 44.75 | 44483.0 | 48428.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
4 | BE | 2016-10-22 04:00:00 | 37.10 | 44338.0 | 46721.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
3a. Forecasting electricity prices using future exogenous variables
To produce forecasts with future exogenous variables we have to add the
future values of the exogenous variables. Let’s read this dataset. In
this case, we want to predict 24 steps ahead, therefore each unique_id
will have 24 observations.
unique_id | ds | Exogenous1 | Exogenous2 | day_0 | day_1 | day_2 | day_3 | day_4 | day_5 | day_6 | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | BE | 2016-12-31 00:00:00 | 64108.0 | 70318.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
1 | BE | 2016-12-31 01:00:00 | 62492.0 | 67898.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
2 | BE | 2016-12-31 02:00:00 | 61571.0 | 68379.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
3 | BE | 2016-12-31 03:00:00 | 60381.0 | 64972.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
4 | BE | 2016-12-31 04:00:00 | 60298.0 | 62900.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
Let’s call the forecast
method, adding this information:
unique_id | ds | TimeGPT | TimeGPT-hi-80 | TimeGPT-hi-90 | TimeGPT-lo-80 | TimeGPT-lo-90 | |
---|---|---|---|---|---|---|---|
0 | BE | 2016-12-31 00:00:00 | 74.540770 | 84.506860 | 89.003950 | 64.574684 | 60.077590 |
1 | BE | 2016-12-31 01:00:00 | 43.344290 | 52.200882 | 57.771790 | 34.487700 | 28.916796 |
2 | BE | 2016-12-31 02:00:00 | 44.429214 | 51.034620 | 57.623154 | 37.823810 | 31.235273 |
3 | BE | 2016-12-31 03:00:00 | 38.094400 | 48.108950 | 51.528008 | 28.079847 | 24.660790 |
4 | BE | 2016-12-31 04:00:00 | 37.389145 | 46.747680 | 52.186092 | 28.030607 | 22.592197 |
📘 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
andtimegpt-1-long-horizon
.By default,
timegpt-1
is used. Please see this tutorial on how and when to usetimegpt-1-long-horizon
.
We can also show the importance of the features.
This plot shows that Exogenous1
and Exogenous2
are the most
important for this forecasting task, as they have the largest weight.
3b. Forecasting electricity prices using historic exogenous variables
In the example above, we just loaded the future exogenous variables.
Often, these are not available because these variables are unknown. We
can also make forecasts using only historic exogenous variables. This
can be done by adding the hist_exog_list
argument with the list of
columns of df
to be considered as historical. In that case, we can
pass all extra columns available in df
as historic exogenous variables
using
hist_exog_list=['Exogenous1', 'Exogenous2', 'day_0', 'day_1', 'day_2', 'day_3', 'day_4', 'day_5', 'day_6']
.
Important
If you include historic exogenous variables in your model, you are implicitly making assumptions about the future of these exogenous variables in your forecast. It is recommended to make these assumptions explicit by making use of future exogenous variables.
Let’s call the forecast
method, adding hist_exog_list
:
unique_id | ds | TimeGPT | TimeGPT-hi-80 | TimeGPT-hi-90 | TimeGPT-lo-80 | TimeGPT-lo-90 | |
---|---|---|---|---|---|---|---|
0 | BE | 2016-12-31 00:00:00 | 45.769290 | 55.735280 | 60.224750 | 35.803303 | 31.313833 |
1 | BE | 2016-12-31 01:00:00 | 47.990700 | 56.851810 | 62.416267 | 39.129590 | 33.565132 |
2 | BE | 2016-12-31 02:00:00 | 49.495724 | 56.101387 | 62.687990 | 42.890060 | 36.303460 |
3 | BE | 2016-12-31 03:00:00 | 49.510230 | 59.525467 | 62.939160 | 39.494995 | 36.081303 |
4 | BE | 2016-12-31 04:00:00 | 48.509483 | 57.864616 | 63.305355 | 39.154350 | 33.713610 |
📘 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
andtimegpt-1-long-horizon
.By default,
timegpt-1
is used. Please see this tutorial on how and when to usetimegpt-1-long-horizon
.
3c. Forecasting electricity prices using future and historic exogenous variables
A third option is to use both historic and future exogenous variables.
For example, we might not have available the future information for
Exogenous1
and Exogenous2
. In this example, we drop these variables
from our future exogenous dataframe (because we assume we do not know
the future value of these variables), and add them to hist_exog_list
to be considered as historical exogenous variables.
📘 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
andtimegpt-1-long-horizon
.By default,
timegpt-1
is used. Please see this tutorial on how and when to usetimegpt-1-long-horizon
.
Note that TimeGPT informs you which variables are used as historic exogenous and which are used as future exogenous.
3d. Forecasting future exogenous variables
A fourth option in case the future exogenous variables are not available
is to forecast them. Below, we’ll show you how we can also forecast
Exogenous1
and Exogenous2
separately, so that you can generate the
future exogenous variables in case they are not available.
Next, we can use TimeGPT to forecast Exogenous1
and Exogenous2
. In
this case, we assume these quantities can be separately forecast.
📘 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
andtimegpt-1-long-horizon
.By default,
timegpt-1
is used. Please see this tutorial on how and when to usetimegpt-1-long-horizon
.
We can now start creating X_df
, which contains the future exogenous
variables.
Next, we also need to add the day_0
to day_6
future exogenous
variables. These are easy: this is just the weekday, which we can
extract from the ds
column.
We have now created X_df
, let’s investigate it:
unique_id | ds | Exogenous1 | Exogenous2 | day_0 | day_1 | day_2 | day_3 | day_4 | day_5 | day_6 | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | BE | 2016-12-31 00:00:00 | 66282.510 | 70861.390 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
1 | BE | 2016-12-31 01:00:00 | 64465.336 | 67851.720 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
2 | BE | 2016-12-31 02:00:00 | 63257.125 | 67246.550 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
3 | BE | 2016-12-31 03:00:00 | 62059.344 | 64027.210 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
4 | BE | 2016-12-31 04:00:00 | 61247.133 | 61523.867 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
5 | BE | 2016-12-31 05:00:00 | 62052.453 | 63053.930 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
6 | BE | 2016-12-31 06:00:00 | 63457.508 | 65199.170 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
7 | BE | 2016-12-31 07:00:00 | 65388.434 | 68285.375 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
8 | BE | 2016-12-31 08:00:00 | 67406.664 | 72037.670 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
9 | BE | 2016-12-31 09:00:00 | 68057.160 | 72820.470 | 0 | 0 | 0 | 0 | 0 | 1 | 0 |
Let’s compare it to our pre-loaded version:
unique_id | ds | Exogenous1 | Exogenous2 | day_0 | day_1 | day_2 | day_3 | day_4 | day_5 | day_6 | |
---|---|---|---|---|---|---|---|---|---|---|---|
0 | BE | 2016-12-31 00:00:00 | 64108.0 | 70318.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
1 | BE | 2016-12-31 01:00:00 | 62492.0 | 67898.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
2 | BE | 2016-12-31 02:00:00 | 61571.0 | 68379.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
3 | BE | 2016-12-31 03:00:00 | 60381.0 | 64972.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
4 | BE | 2016-12-31 04:00:00 | 60298.0 | 62900.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
5 | BE | 2016-12-31 05:00:00 | 60339.0 | 62364.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
6 | BE | 2016-12-31 06:00:00 | 62576.0 | 64242.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
7 | BE | 2016-12-31 07:00:00 | 63732.0 | 65884.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
8 | BE | 2016-12-31 08:00:00 | 66235.0 | 68217.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
9 | BE | 2016-12-31 09:00:00 | 66801.0 | 69921.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
As you can see, the values for Exogenous1
and Exogenous2
are
slightly different, which makes sense because we’ve made a forecast of
these values with TimeGPT.
Let’s create a new forecast of our electricity prices with TimeGPT using
our new X_df
:
unique_id | ds | TimeGPT | TimeGPT-hi-80 | TimeGPT-hi-90 | TimeGPT-lo-80 | TimeGPT-lo-90 | |
---|---|---|---|---|---|---|---|
0 | BE | 2016-12-31 00:00:00 | 46.578370 | 56.544460 | 61.041550 | 36.612278 | 32.115190 |
1 | BE | 2016-12-31 01:00:00 | 37.258366 | 46.114956 | 51.685863 | 28.401775 | 22.830870 |
2 | BE | 2016-12-31 02:00:00 | 41.779457 | 48.384860 | 54.973396 | 35.174053 | 28.585516 |
3 | BE | 2016-12-31 03:00:00 | 37.822342 | 47.836895 | 51.255950 | 27.807790 | 24.388733 |
4 | BE | 2016-12-31 04:00:00 | 37.389145 | 46.747680 | 52.186092 | 28.030607 | 22.592197 |
📘 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
andtimegpt-1-long-horizon
.By default,
timegpt-1
is used. Please see this tutorial on how and when to usetimegpt-1-long-horizon
.
Let’s create a combined dataframe with the two forecasts and plot the values to compare the forecasts.
As you can see, we obtain a slightly different forecast if we use our forecasted exogenous variables.