Skip to main content
Minimal Example of Hierarchical Reconciliation
Large collections of time series organized into structures at different aggregation levels often require their forecasts to follow their aggregation constraints, which poses the challenge of creating novel algorithms capable of coherent forecasts. The HierarchicalForecast package provides a wide collection of Python implementations of hierarchical forecasting algorithms that follow classic hierarchical reconciliation. In this notebook we will show how to use the StatsForecast library to produce base forecasts, and use HierarchicalForecast package to perform hierarchical reconciliation. You can run these experiments using CPU or GPU with Google Colab. Open In Colab

1. Libraries

2. Load Data

In this example we will use the TourismSmall dataset. The following cell gets the time series for the different levels in the hierarchy, the summing matrix S which recovers the full dataset from the bottom level hierarchy and the indices of each hierarchy denoted by tags.
We split the dataframe in train/test splits.

3. Base forecasts

The following cell computes the base forecast for each time series using the auto_arima and naive models. Observe that Y_hat_df contains the forecasts but they are not coherent.

4. Hierarchical reconciliation

The following cell makes the previous forecasts coherent using the HierarchicalReconciliation class. The used methods to make the forecasts coherent are:
  • BottomUp: The reconciliation of the method is a simple addition to the upper levels.
  • TopDown: The second method constrains the base-level predictions to the top-most aggregate-level serie and then distributes it to the disaggregate series through the use of proportions.
  • MiddleOut: Anchors the base predictions in a middle level.

4.1 Coherence Diagnostics

The reconcile method supports an optional diagnostics=True parameter that computes a detailed report showing how reconciliation changed the forecasts. This is useful for:
  • Verifying that base forecasts were incoherent and reconciliation fixed them
  • Understanding which hierarchy levels were adjusted the most
  • Detecting if reconciliation introduced negative values
  • Confirming numerical coherence within tolerance
The diagnostics are stored in hrec.diagnostics as a DataFrame with metrics per hierarchical level:
Key metrics explained:
  • coherence_residual_mae_before: Mean absolute incoherence in base forecasts (should be > 0 if base forecasts are incoherent)
  • coherence_residual_mae_after: Mean absolute incoherence after reconciliation (should be ~0)
  • adjustment_mae/rmse/max: How much forecasts were adjusted by reconciliation
  • negative_count_before/after: Count of negative forecast values
  • is_coherent: Whether the reconciled forecasts satisfy the hierarchical constraints (1.0 = yes)
Let’s filter to see just the coherence verification:
We can also see which levels required the largest adjustments:

5. Evaluation

The HierarchicalForecast package includes the evaluate function to evaluate the different hierarchies and we can use utilsforecast to compute the mean absolute error relative to a baseline model.

References