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]βββ=β1101000β1100100β1010010β1010001ββwhere 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 create 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 tags contains a dictionary of lists 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)