In many applications, a set of time series is hierarchically organized.
Examples include the presence of geographic levels, products, or
categories that define different types of aggregations.In such scenarios, forecasters are often required to provide predictions
for all disaggregate and aggregate series. A natural desire is for those
predictions to be “coherent”, that is, for the bottom series to add
up precisely to the forecasts of the aggregated series.The above figure shows a simple hierarchical structure where we have
four bottom-level series, two middle-level series, and the top level
representing the total aggregation. Its hierarchical aggregations or
coherency constraints are:yTotal,τ=yβ1,τ+yβ2,τ+yβ3,τ+yβ4,τy[a],τ=[yTotal,τ,yβ1,τ+yβ2,τ,yβ3,τ+yβ4,τ]⊺y[b],τ=[yβ1,τ,yβ2,τ,yβ3,τ,yβ4,τ]⊺Luckily these constraints can be compactly expressed with the following
matrices:S[a,b][b]=A[a][b]I[b][b]=1101000110010010100101010001where A[a,b][b] aggregates the bottom series to the upper
levels, and I[b][b] is an identity matrix. The
representation of the hierarchical series is then:y[a,b],τ=S[a,b][b]y[b],τTo visualize an example, in Figure 2, one can think of the hierarchical
time series structure levels to represent different geographical
aggregations. For example, in Figure 2, the top level is the total
aggregation of series within a country, the middle level being its
states and the bottom level its regions.
To achieve “coherency”, most statistical solutions to the
hierarchical forecasting challenge implement a two-stage reconciliation
process.
First, we obtain a set of the base forecast
y^[a,b],τ
Later, we reconcile them into coherent forecasts
y~[a,b],τ.
Most hierarchical reconciliation methods can be expressed by the
following transformations:y~[a,b],τ=S[a,b][b]P[b][a,b]y^[a,b],τThe HierarchicalForecast library offers a Python collection of
reconciliation methods, datasets, evaluation and visualization tools for
the task. Among its available reconciliation methods we have
BottomUp,
TopDown,
MiddleOut,
MinTrace,
ERM.
Among its probabilistic coherent methods we have
Normality,
Bootstrap,
PERMBU.
We are going to creat a synthetic data set to illustrate a hierarchical
time series structure like the one in Figure 1.We will create a two level structure with four bottom series where
aggregations of the series are self evident.
The previously introduced hierarchical series y[a,b]τ
is captured within the Y_hier_df dataframe.The aggregation constraints matrix S[a][b] is captured
within the S_df dataframe.Finally the tags contains a list within Y_hier_df composing each
hierarchical level, for example the tags['top_level'] contains
Australia’s aggregated series index.
from hierarchicalforecast.methods import BottomUpfrom hierarchicalforecast.core import HierarchicalReconciliation
# You can select a reconciler from our collectionreconcilers = [BottomUp()] # MinTrace(method='mint_shrink')hrec = HierarchicalReconciliation(reconcilers=reconcilers)Y_rec_df = hrec.reconcile(Y_hat_df=Y_hat_df, Y_df=Y_fitted_df, S_df=S_df, tags=tags)Y_rec_df.groupby('unique_id').head(2)