Skip to main content
In hierarchical forecasting, we aim to create forecasts for many time series concurrently, whilst adhering to pre-specified hierarchical relationships that exist between the time series. We can enforce this coherence by performing a post-processing reconciliation step on the forecasts. The HierarchicalForecast package provides the most comprehensive collection of Python implementations of hierarchical forecasting algorithms that follow classic hierarchical reconciliation. All the methods have a reconcile function capable of reconciling base forecasts using numpy arrays.

Cross-sectional hierarchies

Traditionally, hierarchical forecasting methods reconcile cross-sectional aggregations. For example, we may have forecasts for individual product demand, but also for the overall product group, department and store, and we are interested in making sure these forecasts are coherent with each other. This can be formalized as: Y~=SPY^β€…β€Š,\tilde{\textbf{Y}} = SP\hat{\textbf{Y}} \;, where Y^∈RmΓ—p\hat{\textbf{Y}} \in \mathbb{R}^{m \times p} denotes the matrix of forecasts for all mm time series for all pp time steps in the hierarchy, S∈{0,1}mΓ—nS \in \lbrace 0, 1 \rbrace^{m \times n} is a matrix that defines the hierarchical relationship between the nn bottom-level time series and the mβˆ—=mβˆ’nm^* = m - n aggregations, P∈RnΓ—mP \in \mathbb{R}^{n \times m} is a matrix that encapsulates the contribution of each forecast to the final estimate, and Y~∈RmΓ—p\tilde{\textbf{Y}} \in \mathbb{R}^{m \times p} is the matrix of reconciled forecasts. We can use the matrix PP to define various forecast contribution scenarios. Cross-sectional reconciliation methods aim to find the optimal PP matrix.

Temporal hierarchies

We can also perform temporal reconciliation. For example, we may have forecasts for daily demand, weekly, and monthly, and we are interested in making sure these forecasts are coherent with each other. We formalize the temporal hierarchical forecasting problem as: Y~=(StePteY^⊺)βŠΊβ€…β€Š,\tilde{\textbf{Y}} = \left( S_{te} P_{te} \hat{\textbf{Y}}^{\intercal} \right)^{\intercal} \;, where Ste∈{0,1}pΓ—kS_{te} \in \lbrace 0, 1 \rbrace^{p \times k} is a matrix that defines the hierarchical relationship between the kk bottom-level time steps and the pβˆ—=pβˆ’kp^* = p - k aggregations and Pte∈RkΓ—pP_{te} \in \mathbb{R}^{k \times p} is a matrix that encapsulates the contribution of each forecast to the final estimate. We can use the matrix PteP_{te} to define various forecast contribution scenarios. Temporal reconciliation methods aim to find the optimal PteP_{te} matrix.

Cross-temporal reconciliation

We can combine cross-sectional and temporal hierarchical forecasting by performing cross-sectional reconciliation and temporal reconciliation in a two-step procedure.

References

-Hyndman, Rob. Notation for forecast reconciliation.

1. Bottom-Up


BottomUp

Bases: HReconciler Bottom Up Reconciliation Class. The most basic hierarchical reconciliation is performed using an Bottom-Up strategy. It was proposed for the first time by Orcutt in 1968. The corresponding hierarchical β€œprojection” matrix is defined as: PtextBU=[0[b],[a]β€…β€Šβˆ£β€…β€ŠI[b][b]]\mathbf{P}_{\\text{BU}} = [\mathbf{0}_{\mathrm{[b],[a]}}\;|\;\mathbf{I}_{\mathrm{[b][b]}}] References:

BottomUp.fit

Bottom Up Fit Method. Parameters: Returns:

BottomUp.predict

Predict using reconciler. Predict using fitted mean and probabilistic reconcilers. Parameters: Returns:

BottomUp.fit_predict

BottomUp Reconciliation Method. Parameters: Returns:

BottomUp.sample

Sample probabilistic coherent distribution. Generates n samples from a probabilistic coherent distribution. The method uses fitted mean and probabilistic reconcilers, defined by the intervals_method selected during the reconciler’s instantiation. Currently available: normality, bootstrap, permbu. Parameters: Returns:

BottomUpSparse

Bases: BottomUp BottomUpSparse Reconciliation Class. This is the implementation of a Bottom Up reconciliation using the sparse matrix approach. It works much more efficient on datasets with many time series. [makoren: At least I hope so, I only checked up until ~20k time series, and there’s no real improvement, it would be great to check for smth like 1M time series, where the dense S matrix really stops fitting in memory] See the parent class for more details.

BottomUpSparse.fit

Bottom Up Fit Method. Parameters: Returns:

BottomUpSparse.predict

Predict using reconciler. Predict using fitted mean and probabilistic reconcilers. Parameters: Returns:

BottomUpSparse.fit_predict

BottomUp Reconciliation Method. Parameters: Returns:

BottomUpSparse.sample

Sample probabilistic coherent distribution. Generates n samples from a probabilistic coherent distribution. The method uses fitted mean and probabilistic reconcilers, defined by the intervals_method selected during the reconciler’s instantiation. Currently available: normality, bootstrap, permbu. Parameters: Returns:

2. Top-Down

TopDown

Bases: HReconciler Top Down Reconciliation Class. The Top Down hierarchical reconciliation method, distributes the total aggregate predictions and decomposes it down the hierarchy using proportions p[b]\mathbf{p}_{\mathrm{[b]}} that can be actual historical values or estimated. P=[p[b]β€…β€Šβˆ£β€…β€Š0[b][a,bβ€…β€Šβˆ’1]]\mathbf{P}=[\mathbf{p}_{\mathrm{[b]}}\;|\;\mathbf{0}_{\mathrm{[b][a,b\;-1]}}] Parameters: References:

TopDown.fit

TopDown Fit Method. Parameters: Returns:

TopDown.predict

Predict using reconciler. Predict using fitted mean and probabilistic reconcilers. Parameters: Returns:

TopDown.fit_predict

Top Down Reconciliation Method. Parameters: Returns:

TopDown.sample

Sample probabilistic coherent distribution. Generates n samples from a probabilistic coherent distribution. The method uses fitted mean and probabilistic reconcilers, defined by the intervals_method selected during the reconciler’s instantiation. Currently available: normality, bootstrap, permbu. Parameters: Returns:

TopDownSparse

Bases: TopDown TopDownSparse Reconciliation Class. This is an implementation of top-down reconciliation using the sparse matrix approach. It works much more efficiently on data sets with many time series. See the parent class for more details.

TopDownSparse.fit

TopDown Fit Method. Parameters: Returns:

TopDownSparse.predict

Predict using reconciler. Predict using fitted mean and probabilistic reconcilers. Parameters: Returns:

TopDownSparse.fit_predict

TopDownSparse.sample

Sample probabilistic coherent distribution. Generates n samples from a probabilistic coherent distribution. The method uses fitted mean and probabilistic reconcilers, defined by the intervals_method selected during the reconciler’s instantiation. Currently available: normality, bootstrap, permbu. Parameters: Returns:

3. Middle-Out

MiddleOut

Bases: HReconciler Middle Out Reconciliation Class. This method is only available for strictly hierarchical structures. It anchors the base predictions in a middle level. The levels above the base predictions use the Bottom-Up approach, while the levels below use a Top-Down. Parameters: References:

MiddleOut.fit

MiddleOut.predict

MiddleOut.fit_predict

Middle Out Reconciliation Method. Parameters: Returns:

MiddleOut.sample

Sample probabilistic coherent distribution. Generates n samples from a probabilistic coherent distribution. The method uses fitted mean and probabilistic reconcilers, defined by the intervals_method selected during the reconciler’s instantiation. Currently available: normality, bootstrap, permbu. Parameters: Returns:

MiddleOutSparse

Bases: MiddleOut MiddleOutSparse Reconciliation Class. This is an implementation of middle-out reconciliation using the sparse matrix approach. It works much more efficiently on data sets with many time series. See the parent class for more details.

MiddleOutSparse.fit

MiddleOutSparse.predict

MiddleOutSparse.fit_predict

MiddleOutSparse.sample

Sample probabilistic coherent distribution. Generates n samples from a probabilistic coherent distribution. The method uses fitted mean and probabilistic reconcilers, defined by the intervals_method selected during the reconciler’s instantiation. Currently available: normality, bootstrap, permbu. Parameters: Returns:

4. Min-Trace

MinTrace

Bases: HReconciler MinTrace Reconciliation Class. This reconciliation algorithm proposed by Wickramasuriya et al. depends on a generalized least squares estimator and an estimator of the covariance matrix of the coherency errors Wh\mathbf{W}_{h}. The Min Trace algorithm minimizes the squared errors for the coherent forecasts under an unbiasedness assumption; the solution has a closed form. PMinT=(S⊺WhS)βˆ’1S⊺Whβˆ’1\mathbf{P}_{\text{MinT}}=\left(\mathbf{S}^{\intercal}\mathbf{W}_{h}\mathbf{S}\right)^{-1}\mathbf{S}^{\intercal}\mathbf{W}^{-1}_{h} Parameters: References:

MinTrace.fit

MinTrace Fit Method. Parameters: Returns:

MinTrace.predict

Predict using reconciler. Predict using fitted mean and probabilistic reconcilers. Parameters: Returns:

MinTrace.fit_predict

MinTrace Reconciliation Method. Parameters: Returns:

MinTrace.sample

Sample probabilistic coherent distribution. Generates n samples from a probabilistic coherent distribution. The method uses fitted mean and probabilistic reconcilers, defined by the intervals_method selected during the reconciler’s instantiation. Currently available: normality, bootstrap, permbu. Parameters: Returns:

MinTraceSparse

Bases: MinTrace MinTraceSparse Reconciliation Class. This is the implementation of OLS and WLS estimators using sparse matrices. It is not guaranteed to give identical results to the non-sparse version, but works much more efficiently on data sets with many time series. See the parent class for more details. Parameters:

MinTraceSparse.fit

MinTraceSparse Fit Method. Parameters: Returns:

MinTraceSparse.predict

Predict using reconciler. Predict using fitted mean and probabilistic reconcilers. Parameters: Returns:

MinTraceSparse.fit_predict

MinTrace Reconciliation Method. Parameters: Returns:

MinTraceSparse.sample

Sample probabilistic coherent distribution. Generates n samples from a probabilistic coherent distribution. The method uses fitted mean and probabilistic reconcilers, defined by the intervals_method selected during the reconciler’s instantiation. Currently available: normality, bootstrap, permbu. Parameters: Returns:

5. Optimal Combination

OptimalCombination

Bases: MinTrace Optimal Combination Reconciliation Class. This reconciliation algorithm was proposed by Hyndman et al. 2011, the method uses generalized least squares estimator using the coherency errors covariance matrix. Consider the covariance of the base forecast textrmVar(Ο΅h)=Ξ£h\\textrm{Var}(\epsilon_{h}) = \Sigma_{h}, the P\mathbf{P} matrix of this method is defined by: P=left(S⊺Σh†Sright)βˆ’1S⊺Σh† \mathbf{P} = \\left(\mathbf{S}^{\intercal}\Sigma_{h}^{\dagger}\mathbf{S}\\right)^{-1}\mathbf{S}^{\intercal}\Sigma^{\dagger}_{h} where Ξ£h†\Sigma_{h}^{\dagger} denotes the variance pseudo-inverse. The method was later proven equivalent to MinTrace variants. Parameters:

OptimalCombination.fit

MinTrace Fit Method. Parameters: Returns:

OptimalCombination.predict

Predict using reconciler. Predict using fitted mean and probabilistic reconcilers. Parameters: Returns:

OptimalCombination.fit_predict

MinTrace Reconciliation Method. Parameters: Returns:

OptimalCombination.sample

Sample probabilistic coherent distribution. Generates n samples from a probabilistic coherent distribution. The method uses fitted mean and probabilistic reconcilers, defined by the intervals_method selected during the reconciler’s instantiation. Currently available: normality, bootstrap, permbu. Parameters: Returns:

6. Emp. Risk Minimization

ERM

Bases: HReconciler Empirical Risk Minimization Reconciliation Class. The Empirical Risk Minimization reconciliation strategy relaxes the unbiasedness assumptions from previous reconciliation methods like MinT and optimizes square errors between the reconciled predictions and the validation data to obtain an optimal reconciliation matrix P. The exact solution for P\mathbf{P} (method='closed') follows the expression: Pβˆ—=left(S⊺Sright)βˆ’1Y⊺Y^left(Y^Y^right)βˆ’1\mathbf{P}^{*} = \\left(\mathbf{S}^{\intercal}\mathbf{S}\\right)^{-1}\mathbf{Y}^{\intercal}\hat{\mathbf{Y}}\\left(\hat{\mathbf{Y}}\hat{\mathbf{Y}}\\right)^{-1} The alternative Lasso regularized P\mathbf{P} solution (method='reg_bu') is useful when the observations of validation data is limited or the exact solution has low numerical stability. Pβˆ—=textargminP∣∣Yβˆ’SPY^∣∣22+λ∣∣Pβˆ’PtextBU∣∣1\mathbf{P}^{*} = \\text{argmin}_{\mathbf{P}} ||\mathbf{Y}-\mathbf{S} \mathbf{P} \hat{Y} ||^{2}_{2} + \lambda ||\mathbf{P}-\mathbf{P}_{\\text{BU}}||_{1} Parameters:

ERM.fit

ERM Fit Method. Parameters: Returns:

ERM.predict

Predict using reconciler. Predict using fitted mean and probabilistic reconcilers. Parameters: Returns:

ERM.fit_predict

ERM Reconciliation Method. Parameters: Returns:

ERM.sample

Sample probabilistic coherent distribution. Generates n samples from a probabilistic coherent distribution. The method uses fitted mean and probabilistic reconcilers, defined by the intervals_method selected during the reconciler’s instantiation. Currently available: normality, bootstrap, permbu. Parameters: Returns:

References

General Reconciliation

Hierarchical Probabilistic Coherent Predictions