Holidays and special dates
Guide to using calendar variables and special dates to improve forecast accuracy in time series.
This guide demonstrates how to use calendar variables and special dates to improve forecast accuracy for time series data — especially useful for window-based forecasting with models like TimeGPT-1.
What are calendar variables?
Calendar variables include indicators such as month, week, day, or hour. They help a model understand time-based patterns that might not be captured in a limited historical window.
Why holidays?
Special dates (like holidays) can cause significant changes in demand or activity. Including these events in your forecasts can lead to more accurate predictions.
Tutorial Overview
1. Import Packages
Import the required libraries and initialize the Nixtla client.
You can also specify the base_url to use an Azure AI endpoint in place of Nixtla’s default.
2. Load Data
We use a Google Trends dataset on “chocolate” with monthly frequency:
month | chocolate | |
---|---|---|
0 | 2004-01-31 | 35 |
1 | 2004-02-29 | 45 |
2 | 2004-03-31 | 28 |
3 | 2004-04-30 | 30 |
4 | 2004-05-31 | 29 |
3. Forecast with Holidays and Special Dates
Nixtla automatically creates common date features during forecasting. Here, we explicitly include holiday indicators (for the US) in the forecast.
Create Future Dataframe and US Holidays
Create Future Dataframe and US Holidays
This DataFrame now includes columns for each identified US holiday as binary indicators.
Apply Holiday Indicators to Historical Data
Apply Holiday Indicators to Historical Data
Now your historical DataFrame also contains holiday flags for each month.
Generate Forecast
Generate Forecast
This command uses the holiday features when generating forecasts.
Plot and Analyze Forecast
Plot and Analyze Forecast
Forecast plot including holiday effects.
You can also visualize the feature importance:
Holiday-related feature weights.
If you’re using an Azure AI endpoint, specify the model:
Available models:
-
timegpt-1 (default)
-
timegpt-1-long-horizon
For more information, see the Long Horizon Forecasting Tutorial.
Understanding the date_features
Parameter
date_features (bool, list, or callable)
date_features (bool, list, or callable)
True: automatically include common date features.
List: specify the features explicitly (e.g., ['weekday', 'month']
).
Callable: provide a custom function that returns date features.
date_features_to_one_hot (bool or list)
date_features_to_one_hot (bool or list)
True: convert all date features into one-hot encoded variables (default). List: select specific features to one-hot encode.
Adding these parameters often improves forecast accuracy by providing richer temporal context.
Congratulations! You have successfully integrated holiday and special date features into your time series forecasts. Use these steps as a starting point for further experimentation with advanced date features.