module hierarchicalforecast.methods
class HReconciler
method fit
method fit_predict
method predict
S(np.ndarray): Summing matrix of size (base,bottom).y_hat(np.ndarray): Forecast values of size (base,horizon).level(Optional[list[int]], optional): float list 0-100, confidence levels for prediction intervals. Default is None.
dict: y_tilde: Reconciliated predictions.
method sample
intervals_method selected during the reconcilerβs instantiation. Currently available: normality, bootstrap, permbu.
Args:
num_samples(int): number of samples generated from coherent distribution.
np.ndarray: samples: Coherent samples of size (num_series,horizon,num_samples).
class BottomUp
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:
Args:
None
References:
method fit
S(np.ndarray): Summing matrix of size (base,bottom).y_hat(np.ndarray): Forecast values of size (base,horizon).idx_bottom(np.ndarray): Indices corresponding to the bottom level ofS, size (bottom).y_insample(Optional[np.ndarray], optional): In-sample values of size (base,horizon). Default is None.y_hat_insample(Optional[np.ndarray], optional): In-sample forecast values of size (base,horizon). Default is None.sigmah(Optional[np.ndarray], optional): Estimated standard deviation of the conditional marginal distribution. Default is None.intervals_method(Optional[str], optional): Sampler for prediction intervals, one ofnormality,bootstrap,permbu. Default is None.num_samples(Optional[int], optional): Number of samples for probabilistic coherent distribution. Default is None.seed(Optional[int], optional): Seed for reproducibility. Default is None.tags(Optional[dict[str, np.ndarray]], optional): Tags for hierarchical structure. Default is None.
BottomUp: object, fitted reconciler.
method fit_predict
S(np.ndarray): Summing matrix of size (base,bottom).y_hat(np.ndarray): Forecast values of size (base,horizon).idx_bottom(np.ndarray): Indices corresponding to the bottom level ofS, size (bottom).y_insample(Optional[np.ndarray], optional): In-sample values of size (base,insample_size). Default is None.y_hat_insample(Optional[np.ndarray], optional): In-sample forecast values of size (base,insample_size). Default is None.sigmah(Optional[np.ndarray], optional): Estimated standard deviation of the conditional marginal distribution. Default is None.level(Optional[list[int]], optional): float list 0-100, confidence levels for prediction intervals. Default is None.intervals_method(Optional[str], optional): Sampler for prediction intervals, one ofnormality,bootstrap,permbu. Default is None.num_samples(Optional[int], optional): Number of samples for probabilistic coherent distribution. Default is None.seed(Optional[int], optional): Seed for reproducibility. Default is None.tags(Optional[dict[str, np.ndarray]], optional): Tags for hierarchical structure. Default is None.
dict: y_tilde: Reconciliated y_hat using the Bottom Up approach.
method predict
S(np.ndarray): Summing matrix of size (base,bottom).y_hat(np.ndarray): Forecast values of size (base,horizon).level(Optional[list[int]], optional): float list 0-100, confidence levels for prediction intervals. Default is None.
dict: y_tilde: Reconciliated predictions.
method sample
intervals_method selected during the reconcilerβs instantiation. Currently available: normality, bootstrap, permbu.
Args:
num_samples(int): number of samples generated from coherent distribution.
np.ndarray: samples: Coherent samples of size (num_series,horizon,num_samples).
class BottomUpSparse
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.
method fit
S(np.ndarray): Summing matrix of size (base,bottom).y_hat(np.ndarray): Forecast values of size (base,horizon).idx_bottom(np.ndarray): Indices corresponding to the bottom level ofS, size (bottom).y_insample(Optional[np.ndarray], optional): In-sample values of size (base,horizon). Default is None.y_hat_insample(Optional[np.ndarray], optional): In-sample forecast values of size (base,horizon). Default is None.sigmah(Optional[np.ndarray], optional): Estimated standard deviation of the conditional marginal distribution. Default is None.intervals_method(Optional[str], optional): Sampler for prediction intervals, one ofnormality,bootstrap,permbu. Default is None.num_samples(Optional[int], optional): Number of samples for probabilistic coherent distribution. Default is None.seed(Optional[int], optional): Seed for reproducibility. Default is None.tags(Optional[dict[str, np.ndarray]], optional): Tags for hierarchical structure. Default is None.
BottomUp: object, fitted reconciler.
method fit_predict
S(np.ndarray): Summing matrix of size (base,bottom).y_hat(np.ndarray): Forecast values of size (base,horizon).idx_bottom(np.ndarray): Indices corresponding to the bottom level ofS, size (bottom).y_insample(Optional[np.ndarray], optional): In-sample values of size (base,insample_size). Default is None.y_hat_insample(Optional[np.ndarray], optional): In-sample forecast values of size (base,insample_size). Default is None.sigmah(Optional[np.ndarray], optional): Estimated standard deviation of the conditional marginal distribution. Default is None.level(Optional[list[int]], optional): float list 0-100, confidence levels for prediction intervals. Default is None.intervals_method(Optional[str], optional): Sampler for prediction intervals, one ofnormality,bootstrap,permbu. Default is None.num_samples(Optional[int], optional): Number of samples for probabilistic coherent distribution. Default is None.seed(Optional[int], optional): Seed for reproducibility. Default is None.tags(Optional[dict[str, np.ndarray]], optional): Tags for hierarchical structure. Default is None.
dict: y_tilde: Reconciliated y_hat using the Bottom Up approach.
method predict
S(np.ndarray): Summing matrix of size (base,bottom).y_hat(np.ndarray): Forecast values of size (base,horizon).level(Optional[list[int]], optional): float list 0-100, confidence levels for prediction intervals. Default is None.
dict: y_tilde: Reconciliated predictions.
method sample
intervals_method selected during the reconcilerβs instantiation. Currently available: normality, bootstrap, permbu.
Args:
num_samples(int): number of samples generated from coherent distribution.
np.ndarray: samples: Coherent samples of size (num_series,horizon,num_samples).
class TopDown
Top Down Reconciliation Class.
The Top Down hierarchical reconciliation method, distributes the total aggregate predictions and decomposes it down the hierarchy using proportions that can be actual historical values or estimated.
Args:
method: One offorecast_proportions,average_proportionsandproportion_averages.
- CW. Gross (1990). βDisaggregation methods to expedite product line forecastingβ. Journal of Forecasting, 9 , 233-254. doi:10.1002/for.3980090304.
- G. Fliedner (1999). β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.
method __init__
method fit
S: Summing matrix of size (base,bottom).y_hat: Forecast values of size (base,horizon).y_insample: Insample values of size (base,insample_size). Optional forforecast_proportionsmethod.y_hat_insample: Insample forecast values of size (base,insample_size). Optional forforecast_proportionsmethod.sigmah: Estimated standard deviation of the conditional marginal distribution.interval_method: Sampler for prediction intervals, one ofnormality,bootstrap,permbu.num_samples: Number of samples for probabilistic coherent distribution.seed: Seed for reproducibility.tags: Each key is a level and each value itsSindices.idx_bottom: Indices corresponding to the bottom level ofS, size (bottom).
self: object, fitted reconciler.
method fit_predict
S: Summing matrix of size (base,bottom).y_hat: Forecast values of size (base,horizon).tags: Each key is a level and each value itsSindices.idx_bottom: Indices corresponding to the bottom level ofS, size (bottom).y_insample: Insample values of size (base,insample_size). Optional forforecast_proportionsmethod.y_hat_insample: Insample forecast values of size (base,insample_size). Optional forforecast_proportionsmethod.sigmah: Estimated standard deviation of the conditional marginal distribution.level: float list 0-100, confidence levels for prediction intervals.intervals_method: Sampler for prediction intervals, one ofnormality,bootstrap,permbu.num_samples: Number of samples for probabilistic coherent distribution.seed: Seed for reproducibility.
y_tilde: Reconciliated y_hat using the Top Down approach.
method predict
S(np.ndarray): Summing matrix of size (base,bottom).y_hat(np.ndarray): Forecast values of size (base,horizon).level(Optional[list[int]], optional): float list 0-100, confidence levels for prediction intervals. Default is None.
dict: y_tilde: Reconciliated predictions.
method sample
intervals_method selected during the reconcilerβs instantiation. Currently available: normality, bootstrap, permbu.
Args:
num_samples(int): number of samples generated from coherent distribution.
np.ndarray: samples: Coherent samples of size (num_series,horizon,num_samples).
class TopDownSparse
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.
method __init__
method fit
S: Summing matrix of size (base,bottom).y_hat: Forecast values of size (base,horizon).y_insample: Insample values of size (base,insample_size). Optional forforecast_proportionsmethod.y_hat_insample: Insample forecast values of size (base,insample_size). Optional forforecast_proportionsmethod.sigmah: Estimated standard deviation of the conditional marginal distribution.interval_method: Sampler for prediction intervals, one ofnormality,bootstrap,permbu.num_samples: Number of samples for probabilistic coherent distribution.seed: Seed for reproducibility.tags: Each key is a level and each value itsSindices.idx_bottom: Indices corresponding to the bottom level ofS, size (bottom).
self: object, fitted reconciler.
method fit_predict
method predict
S(np.ndarray): Summing matrix of size (base,bottom).y_hat(np.ndarray): Forecast values of size (base,horizon).level(Optional[list[int]], optional): float list 0-100, confidence levels for prediction intervals. Default is None.
dict: y_tilde: Reconciliated predictions.
method sample
intervals_method selected during the reconcilerβs instantiation. Currently available: normality, bootstrap, permbu.
Args:
num_samples(int): number of samples generated from coherent distribution.
np.ndarray: samples: Coherent samples of size (num_series,horizon,num_samples).
class MiddleOut
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.
Args:
middle_level: Middle level.top_down_method: One offorecast_proportions,average_proportionsandproportion_averages.
method __init__
method fit
method fit_predict
S: Summing matrix of size (base,bottom).y_hat: Forecast values of size (base,horizon).tags: Each key is a level and each value itsSindices.y_insample: Insample values of size (base,insample_size). Only used forforecast_proportionsy_hat_insample: In-sample forecast values of size (base,insample_size).sigmah: Estimated standard deviation of the conditional marginal distribution.level: float list 0-100, confidence levels for prediction intervals.intervals_method: Sampler for prediction intervals, one ofnormality,bootstrap,permbu.num_samples: Number of samples for probabilistic coherent distribution.seed: Seed for reproducibility.
y_tilde: Reconciliated y_hat using the Middle Out approach.
method predict
method sample
intervals_method selected during the reconcilerβs instantiation. Currently available: normality, bootstrap, permbu.
Args:
num_samples(int): number of samples generated from coherent distribution.
np.ndarray: samples: Coherent samples of size (num_series,horizon,num_samples).
class MiddleOutSparse
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.
method __init__
method fit
method fit_predict
method predict
method sample
intervals_method selected during the reconcilerβs instantiation. Currently available: normality, bootstrap, permbu.
Args:
num_samples(int): number of samples generated from coherent distribution.
np.ndarray: samples: Coherent samples of size (num_series,horizon,num_samples).
class MinTrace
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 . The Min Trace algorithm minimizes the squared errors for the coherent forecasts under an unbiasedness assumption; the solution has a closed form.Args:
method: str, one ofols,wls_struct,wls_var,mint_shrink,mint_cov.nonnegative: bool, reconciled forecasts should be nonnegative?mint_shr_ridge: float=2e-8, ridge numeric protection to MinTrace-shr covariance estimator.num_threads: int=1, number of threads to use for solving the optimization problems (when nonnegative=True).
- Wickramasuriya, S. L., Athanasopoulos, G., & Hyndman, R. J. (2019). βOptimal forecast reconciliation for hierarchical and grouped time series through trace minimizationβ. Journal of the American Statistical Association, 114 , 804-819. doi:10.1080/01621459.2018.1448825..
- Wickramasuriya, S.L., Turlach, B.A. & Hyndman, R.J. (2020). βOptimal non-negative forecast reconciliationβ. Stat Comput 30, 1167-1182. https://doi.org/10.1007/s11222-020-09930-0.
method __init__
method fit
S: Summing matrix of size (base,bottom).y_hat: Forecast values of size (base,horizon).y_insample: Insample values of size (base,insample_size). Only used with βwls_varβ, βmint_covβ, βmint_shrinkβ.y_hat_insample: Insample forecast values of size (base,insample_size). Only used with βwls_varβ, βmint_covβ, βmint_shrinkβsigmah: Estimated standard deviation of the conditional marginal distribution.intervals_method: Sampler for prediction intervals, one ofnormality,bootstrap,permbu.num_samples: Number of samples for probabilistic coherent distribution.seed: Seed for reproducibility.tags: Each key is a level and each value itsSindices.idx_bottom: Indices corresponding to the bottom level ofS, size (bottom).
self: object, fitted reconciler.
method fit_predict
S: Summing matrix of size (base,bottom).y_hat: Forecast values of size (base,horizon).idx_bottom: Indices corresponding to the bottom level ofS, size (bottom).y_insample: Insample values of size (base,insample_size). Only used bywls_var,mint_cov,mint_shrinky_hat_insample: Insample fitted values of size (base,insample_size). Only used bywls_var,mint_cov,mint_shrinksigmah: Estimated standard deviation of the conditional marginal distribution.level: float list 0-100, confidence levels for prediction intervals.intervals_method: Sampler for prediction intervals, one ofnormality,bootstrap,permbu.num_samples: Number of samples for probabilistic coherent distribution.seed: Seed for reproducibility.tags: Each key is a level and each value itsSindices.
y_tilde: Reconciliated y_hat using the MinTrace approach.
method predict
S(np.ndarray): Summing matrix of size (base,bottom).y_hat(np.ndarray): Forecast values of size (base,horizon).level(Optional[list[int]], optional): float list 0-100, confidence levels for prediction intervals. Default is None.
dict: y_tilde: Reconciliated predictions.
method sample
intervals_method selected during the reconcilerβs instantiation. Currently available: normality, bootstrap, permbu.
Args:
num_samples(int): number of samples generated from coherent distribution.
np.ndarray: samples: Coherent samples of size (num_series,horizon,num_samples).
class MinTraceSparse
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.
Args:
method(str): One ofols,wls_struct, orwls_var.nonnegative(bool): Return non-negative reconciled forecasts.num_threads(int): Number of threads to execute non-negative quadratic programming calls.qp(bool): Implement non-negativity constraint with a quadratic programming approach. Setting this to True generally gives better results, but at the expense of higher cost to compute.
method __init__
method fit
S: Summing matrix of size (base,bottom).y_hat: Forecast values of size (base,horizon).y_insample: Insample values of size (base,insample_size). Only used with βwls_varβ.y_hat_insample: Insample forecast values of size (base,insample_size). Only used with βwls_varβsigmah: Estimated standard deviation of the conditional marginal distribution.intervals_method: Sampler for prediction intervals, one ofnormality,bootstrap,permbu.num_samples: Number of samples for probabilistic coherent distribution.seed: Seed for reproducibility.tags: Each key is a level and each value itsSindices.idx_bottom: Indices corresponding to the bottom level ofS, size (bottom).
self: object, fitted reconciler.
method fit_predict
S: Summing matrix of size (base,bottom).y_hat: Forecast values of size (base,horizon).idx_bottom: Indices corresponding to the bottom level ofS, size (bottom).y_insample: Insample values of size (base,insample_size). Only used bywls_var,mint_cov,mint_shrinky_hat_insample: Insample fitted values of size (base,insample_size). Only used bywls_var,mint_cov,mint_shrinksigmah: Estimated standard deviation of the conditional marginal distribution.level: float list 0-100, confidence levels for prediction intervals.intervals_method: Sampler for prediction intervals, one ofnormality,bootstrap,permbu.num_samples: Number of samples for probabilistic coherent distribution.seed: Seed for reproducibility.tags: Each key is a level and each value itsSindices.
y_tilde: Reconciliated y_hat using the MinTrace approach.
method predict
S(np.ndarray): Summing matrix of size (base,bottom).y_hat(np.ndarray): Forecast values of size (base,horizon).level(Optional[list[int]], optional): float list 0-100, confidence levels for prediction intervals. Default is None.
dict: y_tilde: Reconciliated predictions.
method sample
intervals_method selected during the reconcilerβs instantiation. Currently available: normality, bootstrap, permbu.
Args:
num_samples(int): number of samples generated from coherent distribution.
np.ndarray: samples: Coherent samples of size (num_series,horizon,num_samples).
class OptimalCombination
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 , the matrix of this method is defined by: where denotes the variance pseudo-inverse. The method was later proven equivalent to MinTrace variants.
Args:
method: str, allowed optimal combination methods: βolsβ, βwls_structβ.nonnegative: bool, reconciled forecasts should be nonnegative?
- Rob J. Hyndman, Roman A. Ahmed, George Athanasopoulos, Han Lin Shang (2010). βOptimal Combination Forecasts for Hierarchical Time Seriesβ..
- Shanika L. Wickramasuriya, George Athanasopoulos and Rob J. Hyndman (2010). βOptimal Combination Forecasts for Hierarchical Time Seriesβ..
- Wickramasuriya, S.L., Turlach, B.A. & Hyndman, R.J. (2020). βOptimal non-negative forecast reconciliationβ. Stat Comput 30, 1167-1182. https://doi.org/10.1007/s11222-020-09930-0.
method __init__
method fit
S: Summing matrix of size (base,bottom).y_hat: Forecast values of size (base,horizon).y_insample: Insample values of size (base,insample_size). Only used with βwls_varβ, βmint_covβ, βmint_shrinkβ.y_hat_insample: Insample forecast values of size (base,insample_size). Only used with βwls_varβ, βmint_covβ, βmint_shrinkβsigmah: Estimated standard deviation of the conditional marginal distribution.intervals_method: Sampler for prediction intervals, one ofnormality,bootstrap,permbu.num_samples: Number of samples for probabilistic coherent distribution.seed: Seed for reproducibility.tags: Each key is a level and each value itsSindices.idx_bottom: Indices corresponding to the bottom level ofS, size (bottom).
self: object, fitted reconciler.
method fit_predict
S: Summing matrix of size (base,bottom).y_hat: Forecast values of size (base,horizon).idx_bottom: Indices corresponding to the bottom level ofS, size (bottom).y_insample: Insample values of size (base,insample_size). Only used bywls_var,mint_cov,mint_shrinky_hat_insample: Insample fitted values of size (base,insample_size). Only used bywls_var,mint_cov,mint_shrinksigmah: Estimated standard deviation of the conditional marginal distribution.level: float list 0-100, confidence levels for prediction intervals.intervals_method: Sampler for prediction intervals, one ofnormality,bootstrap,permbu.num_samples: Number of samples for probabilistic coherent distribution.seed: Seed for reproducibility.tags: Each key is a level and each value itsSindices.
y_tilde: Reconciliated y_hat using the MinTrace approach.
method predict
S(np.ndarray): Summing matrix of size (base,bottom).y_hat(np.ndarray): Forecast values of size (base,horizon).level(Optional[list[int]], optional): float list 0-100, confidence levels for prediction intervals. Default is None.
dict: y_tilde: Reconciliated predictions.
method sample
intervals_method selected during the reconcilerβs instantiation. Currently available: normality, bootstrap, permbu.
Args:
num_samples(int): number of samples generated from coherent distribution.
np.ndarray: samples: Coherent samples of size (num_series,horizon,num_samples).
class ERM
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 (method='closed') follows the expression:
The alternative Lasso regularized solution (method='reg_bu') is useful when the observations of validation data is limited or the exact solution has low numerical stability.
Args:
method: str, one ofclosed,regandreg_bu.lambda_reg: float, l1 regularizer forregandreg_bu.
method __init__
method fit
S: Summing matrix of size (base,bottom).y_hat: Forecast values of size (base,horizon).y_insample: Train values of size (base,insample_size).y_hat_insample: Insample train predictions of size (base,insample_size).sigmah: Estimated standard deviation of the conditional marginal distribution.intervals_method: Sampler for prediction intervals, one ofnormality,bootstrap,permbu.num_samples: Number of samples for probabilistic coherent distribution.seed: Seed for reproducibility.tags: Each key is a level and each value itsSindices.idx_bottom: Indices corresponding to the bottom level ofS, size (bottom).
self: object, fitted reconciler.
method fit_predict
S: Summing matrix of size (base,bottom).y_hat: Forecast values of size (base,horizon).idx_bottom: Indices corresponding to the bottom level ofS, size (bottom).y_insample: Train values of size (base,insample_size).y_hat_insample: Insample train predictions of size (base,insample_size).sigmah: Estimated standard deviation of the conditional marginal distribution.level: float list 0-100, confidence levels for prediction intervals.intervals_method: Sampler for prediction intervals, one ofnormality,bootstrap,permbu.num_samples: Number of samples for probabilistic coherent distribution.seed: Seed for reproducibility.tags: Each key is a level and each value itsSindices.
y_tilde: Reconciliated y_hat using the ERM approach.
method predict
S(np.ndarray): Summing matrix of size (base,bottom).y_hat(np.ndarray): Forecast values of size (base,horizon).level(Optional[list[int]], optional): float list 0-100, confidence levels for prediction intervals. Default is None.
dict: y_tilde: Reconciliated predictions.
method sample
intervals_method selected during the reconcilerβs instantiation. Currently available: normality, bootstrap, permbu.
Args:
num_samples(int): number of samples generated from coherent distribution.
np.ndarray: samples: Coherent samples of size (num_series,horizon,num_samples).

