Quick Start (Polars)
Minimal Example of Hierarchical Reconciliation using Polars
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.
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
.
unique_id | nsw-hol-city | nsw-hol-noncity | vic-hol-city | vic-hol-noncity | qld-hol-city |
---|---|---|---|---|---|
str | f64 | f64 | f64 | f64 | f64 |
”total” | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 |
”hol” | 1.0 | 1.0 | 1.0 | 1.0 | 1.0 |
”vfr” | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
”bus” | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
”oth” | 0.0 | 0.0 | 0.0 | 0.0 | 0.0 |
”nsw-hol” | 1.0 | 1.0 | 0.0 | 0.0 | 0.0 |
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.
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.
level | metric | AutoARIMA | AutoARIMA/BottomUp | AutoARIMA/TopDown_method-forecast_proportions |
---|---|---|---|---|
str | str | f64 | f64 | f64 |
”Country" | "mse-scaled” | 0.317897 | 0.226999 | 0.317897 |
”Country/Purpose" | "mse-scaled” | 0.323207 | 0.199359 | 0.251368 |
”Country/Purpose/State" | "mse-scaled” | 0.266118 | 0.305711 | 0.308241 |
”Country/Purpose/State/CityNonC… | “mse-scaled” | 0.305173 | 0.305173 | 0.305913 |
”Overall" | "mse-scaled” | 0.311707 | 0.234934 | 0.289406 |
References
- Orcutt, G.H., Watts, H.W., & Edwards, J.B.(1968). Data aggregation and information loss. The American Economic Review, 58 , 773(787).
- Disaggregation methods to expedite product line forecasting.
Journal of Forecasting, 9 , 233–254.
doi:10.1002/for.3980090304.
- An investigation of aggregate variable time series forecast strategies with specific subaggregate time series statistical correlation. Computers and Operations Research, 26 , 1133–1149. doi:10.1016/S0305-0548(99)00017-9.
- Hyndman, R.J., & Athanasopoulos, G. (2021). “Forecasting: principles and practice, 3rd edition: Chapter 11: Forecasting hierarchical and grouped series.”. OTexts: Melbourne, Australia. OTexts.com/fpp3 Accessed on July 2022.