Skip to main content
Open In Colab In many cases, only the time series at the lowest level of the hierarchies (bottom time series) are available. HierarchicalForecast has tools to create time series for all hierarchies and also allows you to calculate prediction intervals for all hierarchies. In this notebook we will see how to do it.

Aggregate bottom time series

In this example we will use the Tourism dataset from the Forecasting: Principles and Practice book. The dataset only contains the time series at the lowest level, so we need to create the time series for all hierarchies.
The dataset can be grouped in the following non-strictly hierarchical structure.
Using the aggregate function from HierarchicalForecast we can generate: 1. Y_df: the hierarchical structured series y[a,b]τ\mathbf{y}_{[a,b]\tau} 2. S_df: the aggregation constraings dataframe with S[a,b]S_{[a,b]} 3. tags: a list with the ‘unique_ids’ conforming each aggregation level.
We can visualize the S matrix and the data using the HierarchicalPlot class as follows.

Split Train/Test sets

We use the final two years (8 quarters) as test set.

Computing base forecasts

The following cell computes the base forecasts for each time series in Y_df using the AutoARIMA and model. Observe that Y_hat_df contains the forecasts but they are not coherent. To reconcile the prediction intervals we need to calculate the uncoherent intervals using the level argument of StatsForecast.

Reconcile forecasts

The following cell makes the previous forecasts coherent using the HierarchicalReconciliation class. Since the hierarchy structure is not strict, we can’t use methods such as TopDown or MiddleOut. In this example we use BottomUp and MinTrace. If you want to calculate prediction intervals, you have to use the level argument as follows.
The dataframe Y_rec_df contains the reconciled forecasts.

Plot forecasts

Then we can plot the probabilistic forecasts using the following function.

Plot single time series

Plot hierarchichally linked time series

References