Exogenous Variables Tutorial
Learn how to incorporate external variables to improve your forecasting accuracy.
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. Import Packages
Use the following code to import the required libraries and initialize the Nixtla client.
If you’re using an Azure AI endpoint, explicitly define the base_url parameter:
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
).
unique_id | ds | y | Exogenous1 | Exogenous2 | day_0 | day_1 | day_2 | day_3 | day_4 | day_5 | day_6 |
---|---|---|---|---|---|---|---|---|---|---|---|
BE | 2016-10-22 00:00:00 | 70.00 | 57253.0 | 49593.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
BE | 2016-10-22 01:00:00 | 37.10 | 51887.0 | 46073.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
BE | 2016-10-22 02:00:00 | 37.10 | 51896.0 | 44927.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
BE | 2016-10-22 03:00:00 | 44.75 | 48428.0 | 44483.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
BE | 2016-10-22 04:00:00 | 37.10 | 46721.0 | 44338.0 | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 | 1.0 | 0.0 |
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:
Ensure you maintain consistent data formatting and columns in both historical and future exogenous datasets (e.g., dates, unique_id, variable names).
3b. Additional Steps for Preprocessing
3b. Additional Steps for Preprocessing
Often, you might need to clean or impute missing values in your exogenous dataset. Ensure data quality before feeding it into the forecasting model.
3c. Feature Engineering Tips
3c. Feature Engineering Tips
Depending on your domain, you can expand on the exogenous variables by creating new features. For instance, combine weather data into weekly averages or encode holidays as binary features.
3d. Advanced Forecasting Scenarios
3d. Advanced Forecasting Scenarios
In some cases, you may forecast future exogenous variables separately using their own time series models. Provide those forecasts as inputs to TimeGPT for improved accuracy.
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.
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.