Hierarchical Forecasting
Learn how to use TimeGPT for hierarchical forecasting across multiple levels.
What is hierarchical forecasting? Hierarchical forecasting involves generating forecasts for multiple time series that share a hierarchical structure (e.g., product demand by category, department, or region). The goal is to ensure that forecasts are coherent across each level of the hierarchy.
Hierarchical forecasting can be particularly important when you need to generate forecasts at different granularities (e.g., country, state, region) and ensure they align with each other and aggregate correctly at higher levels.
Using TimeGPT, you can create forecasts for multiple related time series and then apply hierarchical forecasting methods from HierarchicalForecast to reconcile those forecasts across your specified hierarchy.
Why use hierarchical forecasting?
-
Ensures consistency: Forecasts at lower levels add up to higher-level forecasts.
-
Improves accuracy: Reconciliation methods often yield more robust predictions.
-
Facilitates deeper insights: Understand how smaller segments contribute to overall trends.
TimeGPT
TimeGPT is Nixtla’s generative AI for time series forecasting. It supports forecasting across different levels of data granularity.
HierarchicalForecast
A library offering reconciliation algorithms like MinTrace
to keep forecasts coherent across hierarchy levels.
Step 1: Import and Initialize
Tip: Make sure you have the nixtla
package installed.
Use an Azure AI endpoint
To use an Azure AI endpoint, set the base_url
argument:
Step 2: Load and Prepare Data
This tutorial uses the Australian Tourism dataset from Forecasting: Principles and Practices. The dataset contains different levels of hierarchical data, from the entire country of Australia down to individual regions.
Examples of Australia's Tourism Hierarchy and Map
The dataset provides only the lowest-level series, so higher-level series need to be aggregated explicitly.
The dataset provides only the lowest-level series, so higher-level series need to be aggregated explicitly.
UserWarning: Could not infer format, falling back to parsing individually. Specify the format explicitly if you encounter inconsistencies.
Step 3: Hierarchical Forecasting Using TimeGPT
Now we’ll generate base forecasts across all series using TimeGPT and then apply hierarchical reconciliation to ensure the forecasts align across each level.
Generate Base Forecasts
Obtain forecasts with TimeGPT for all series in your training data:
Available models in Azure AI
Specify model="azureai"
when using a custom Azure AI endpoint. Public APIs support timegpt-1
and timegpt-1-long-horizon
.
Next, separate the generated forecasts into in-sample (historical) and out-of-sample (forecasted) periods:
Visualize TimeGPT Forecasts
Quickly visualize the forecasts for different hierarchy levels. Here, we look at the entire country, the state of Queensland, the Brisbane region, and holidays in Brisbane:
Apply Hierarchical Reconciliation
We use MinTrace
methods to reconcile forecasts across all levels of the hierarchy.
Now, let’s plot the reconciled forecasts to ensure they make sense across the full country → state → region → purpose hierarchy:
Evaluate Forecast Accuracy
Finally, evaluate your forecast performance using RMSE for different levels of the hierarchy, from total (country) to bottom-level (region/purpose).
The hierarchical reconciliation steps help maintain forecast coherence across all hierarchy layers and can also improve overall accuracy.