Skip to main content
Aggregating Exogenous Variables for Hierarchical Forecasting
When building forecasting models with exogenous (external) features, it is important that these features are also coherently aggregated across the hierarchy β€” just like the target variable. For example, if marketing_spend is available at the bottom level, the aggregated series at the state level should contain the sum of marketing spend across its child regions; while a rate-like variable such as cpi should be averaged. The aggregate function in HierarchicalForecast supports this through its exog_vars parameter: a dictionary mapping column names to their aggregation strategy (e.g., 'sum', 'mean', 'min', 'max'). This lets you aggregate exogenous variables alongside the target in a single call, and then use them as inputs to models that support exogenous regressors (e.g., AutoARIMA). In this notebook we demonstrate this workflow on the Australian Domestic Tourism dataset and compare forecasting performance with and without exogenous variables. You can run these experiments using CPU or GPU with Google Colab. Open In Colab

1. Load Data and Add Exogenous Variables

We start from the Tourism dataset and add two synthetic exogenous variables that illustrate different aggregation needs:
  • cpi β€” Consumer Price Index. This is a national-level economic indicator, identical across regions within each quarter. Because it is a rate (not a count), it should be averaged when aggregating.
  • marketing_spend β€” Regional marketing budget. This varies by state and grows over time. Because it represents a total amount, it should be summed when aggregating.

2. Aggregate with Exogenous Variables

The exog_vars parameter of aggregate takes a dictionary where: - keys are the column names of the exogenous variables. - values are the aggregation function(s) to apply β€” any valid Pandas aggregation function name such as 'sum', 'mean', 'min', 'max', 'std'. The aggregated columns are named {column}_{function} (e.g., cpi_mean, marketing_spend_sum).
The result is a Y_agg DataFrame that contains both the aggregated target (y) and the aggregated exogenous variables (cpi_mean, marketing_spend_sum) for every level of the hierarchy.
Notice that: - cpi_mean is the same at every level (since CPI is identical across bottom series within each quarter). - marketing_spend_sum grows as we move up the hierarchy, because it sums the spend of all child series.

Multiple aggregations per variable

You can also apply multiple aggregation functions to a single variable by passing a list of strings.

3. Train/Test Split

We use the final two years (8 quarters) as test set. We also aggregate a version without exogenous variables to compare later.

4. Base Forecasts

We use AutoARIMA from StatsForecast, which supports exogenous regressors. Any column in the training data besides unique_id, ds, and y is treated as an exogenous feature. Future values are passed via X_df. We produce base forecasts with and without the exogenous variables for comparison.

5. Reconcile Forecasts

Reconciliation works exactly the same way regardless of whether exogenous variables were used to produce the base forecasts. The reconciliation step adjusts the base forecasts to be coherent across the hierarchy.

6. Evaluation

We compare RMSE and MASE across hierarchy levels for the models with and without exogenous variables.

RMSE

MASE

References