Exogenous variables (external factors) play a key role in time series forecasting. By providing additional context—like holidays, marketing spend, or weather conditions—they can significantly improve your model’s accuracy.

1

1. Import Packages

Use the following code to import the required libraries and initialize the Nixtla client.

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"
)

If you’re using an Azure AI endpoint, explicitly define the base_url parameter:

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

2. Load Dataset

In this tutorial, we’ll predict day-ahead electricity prices. The dataset contains hourly electricity prices (y) from various markets (identified by unique_id) along with exogenous variables (Exogenous1 to day_6).

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

3a. Forecasting with Future Exogenous Variables

To make a forecast using exogenous variables, you need to provide future exogenous values. Below is an example dataset containing future exogenous variables:

future_ex_vars_df = pd.read_csv("https://raw.githubusercontent.com/Nixtla/transfer-learning-time-series/main/datasets/electricity-short-future-ex-vars.csv")
future_ex_vars_df.head()

Ensure you maintain consistent data formatting and columns in both historical and future exogenous datasets (e.g., dates, unique_id, variable names).

Important Note on Historical Data Use:
Using historical exogenous variables to predict the future assumes the same patterns will hold. Whenever possible, explicitly forecast these external variables to increase accuracy.

Example Forecast Visualization

Once you have generated your forecasts, you can visualize the results to compare forecasts using provided exogenous variables vs. forecasted ones.

nixtla_client.plot(
    df[["unique_id", "ds", "y"]],
    forecasts,
    max_insample_length=365,
)

Forecast comparison using provided vs. forecasted exogenous variables

Key Takeaways

  • Exogenous variables enrich time series forecasting.
  • Ensure proper alignment of historical and future exogenous data.
  • Use separate models to forecast exogenous variables if necessary.

Next Steps

  • Explore feature engineering to create domain-specific exogenous data.
  • Experiment with different modeling approaches for external variables.
  • Validate forecast accuracy by comparing with real future data.

Congratulations! You have mastered the fundamentals of adding exogenous variables to your TimeGPT forecasts. Keep refining your approach by incorporating advanced feature engineering and robust data validation.

By following these steps and guidelines, you can seamlessly integrate exogenous variables into your forecasting workflows, unlocking more accurate predictions and deeper insights into the factors driving your time series data.