Skip to main content
Here we provide a collection of methods designed to provide hierarchically coherent probabilistic distributions, which means that they generate samples of multivariate time series with hierarchical linear constraints. We designed these methods to extend the core.HierarchicalForecast capabilities class. Check their usage example here.

1. Normality

Normality

Normality(S, P, y_hat, sigmah, W=None, seed=0, covariance_type='diagonal', residuals=None, shrinkage_ridge=_DEFAULT_SHRINKAGE_RIDGE)
Normality Probabilistic Reconciliation Class. The Normality method leverages the Gaussian Distribution linearity, to generate hierarchically coherent prediction distributions. This class is meant to be used as the sampler input as other HierarchicalForecast reconciliation classes. Given base forecasts under a normal distribution: y^h∼N(μ^,W^h)\hat{y}_{h} \sim \mathrm{N}(\hat{\boldsymbol{\mu}}, \hat{\mathbf{W}}_{h}) The reconciled forecasts are also normally distributed: y~h∼N(SPμ^,SPW^hP⊺S⊺)\tilde{y}_{h} \sim \mathrm{N}(\mathbf{S}\mathbf{P}\hat{\boldsymbol{\mu}}, \mathbf{S}\mathbf{P}\hat{\mathbf{W}}_{h} \mathbf{P}^{\intercal} \mathbf{S}^{\intercal}) Parameters:
NameTypeDescriptionDefault
SUnion[ndarray, spmatrix]Summing matrix of size (base, bottom).required
PUnion[ndarray, spmatrix]Reconciliation matrix of size (bottom, base).required
y_hatndarrayPoint forecasts values of size (base, horizon).required
sigmahndarrayForecast standard dev. of size (base, horizon).required
WUnion[ndarray, spmatrix]Hierarchical covariance matrix of size (base, base). Required when covariance_type='diagonal' (default). Ignored when covariance_type is 'full' or 'shrink' (covariance is computed from residuals instead). Default is None.None
seedintRandom seed for numpy generator’s replicability. Default is 0.0
covariance_typeUnion[str, CovarianceType]Type of covariance estimator. Can be a string or CovarianceType enum. Options are:
- 'diagonal' / CovarianceType.DIAGONAL: Uses the W matrix diagonal with correlation scaling (default, backward compatible). W is required. - 'full' / CovarianceType.FULL: Uses full empirical covariance from residuals. W is ignored. Warning: may be non-positive-definite if n_series > n_observations. - 'shrink' / CovarianceType.SHRINK: Uses SchΓ€fer-Strimmer shrinkage estimator. W is ignored. Recommended for numerical stability with many series.
Default is 'diagonal'.
β€˜diagonal’
residualsndarrayInsample residuals of size (base, obs). Required when covariance_type is 'full' or 'shrink'. Default is None.None
shrinkage_ridgefloatRidge parameter for shrinkage covariance estimator. Only used when covariance_type='shrink'. A warning is issued if provided with other covariance types. Default is 2e-8._DEFAULT_SHRINKAGE_RIDGE
Raises:
TypeDescription
ValueErrorIf covariance_type is invalid.
ValueErrorIf covariance_type='diagonal' and W is None.
ValueErrorIf covariance_type is 'full' or 'shrink' and residuals is None.
ValueErrorIf residuals shape doesn’t match expected (base, obs).
ValueErrorIf residuals has fewer than 2 observations.
ValueErrorIf residuals is empty.
ValueErrorIf any series in residuals has all NaN values.
Warns:
TypeDescription
UserWarningIf shrinkage_ridge is provided but covariance_type is not 'shrink'.
UserWarningIf W is provided but covariance_type is not 'diagonal' (W is ignored).
UserWarningIf any series has zero or near-zero variance (may affect correlation estimates).
UserWarningIf covariance_type='full' and n_series > n_observations (non-PSD risk).
Examples:
>>> # Using diagonal covariance (default, backward compatible)
>>> normality = Normality(S=S, P=P, y_hat=y_hat, sigmah=sigmah, W=W)
>>> samples = normality.get_samples(num_samples=100)
>>> # Using full empirical covariance from residuals
>>> normality = Normality(
...     S=S, P=P, y_hat=y_hat, sigmah=sigmah,
...     covariance_type="full", residuals=residuals
... )
>>> # Using shrinkage covariance (recommended for stability)
>>> normality = Normality(
...     S=S, P=P, y_hat=y_hat, sigmah=sigmah,
...     covariance_type=CovarianceType.SHRINK, residuals=residuals
... )

Normality.get_samples

get_samples(num_samples)
Normality Coherent Samples. Obtains coherent samples under the Normality assumptions. Parameters:
NameTypeDescriptionDefault
num_samplesintnumber of samples generated from coherent distribution.required
Returns:
NameTypeDescription
samplesndarrayCoherent samples of size (base, horizon, num_samples).

2. Bootstrap

Bootstrap

Bootstrap(S, P, y_hat, y_insample, y_hat_insample, num_samples=100, seed=0, W=None)
Bootstrap Probabilistic Reconciliation Class. This method goes beyond the normality assumption for the base forecasts, the technique simulates future sample paths and uses them to generate base sample paths that are latered reconciled. This clever idea and its simplicity allows to generate coherent bootstraped prediction intervals for any reconciliation strategy. This class is meant to be used as the sampler input as other HierarchicalForecast reconciliation classes. Given a boostraped set of simulated sample paths: y^tau[1],…,y^tau[B])\hat{\mathbf{y}}^{[1]}_{\\tau}, \dots ,\hat{\mathbf{y}}^{[B]}_{\\tau}) The reconciled sample paths allow for reconciled distributional forecasts: (SPy^tau[1],…,SPy^tau[B])(\mathbf{S}\mathbf{P}\hat{\mathbf{y}}^{[1]}_{\\tau}, \dots ,\mathbf{S}\mathbf{P}\hat{\mathbf{y}}^{[B]}_{\\tau}) Parameters:
NameTypeDescriptionDefault
Sndarray | spmatrixnp.array, summing matrix of size (base, bottom).required
Pndarray | spmatrixnp.array, reconciliation matrix of size (bottom, base).required
y_hatndarrayPoint forecasts values of size (base, horizon).required
y_insamplendarrayInsample values of size (base, insample_size).required
y_hat_insamplendarrayInsample point forecasts of size (base, insample_size).required
num_samplesintint, number of bootstraped samples generated.100
seedintint, random seed for numpy generator’s replicability.0

Bootstrap.get_samples

get_samples(num_samples)
Bootstrap Sample Reconciliation Method. Applies Bootstrap sample reconciliation method as defined by Gamakumara 2020. Generating independent sample paths and reconciling them with Bootstrap. Parameters:
NameTypeDescriptionDefault
num_samplesintint, number of samples generated from coherent distribution.required
Returns:
NameTypeDescription
samplesCoherent samples of size (base, horizon, num_samples).

3. PERMBU

PERMBU

PERMBU(S, tags, y_hat, y_insample, y_hat_insample, sigmah, num_samples=None, seed=0, P=None)
PERMBU Probabilistic Reconciliation Class. The PERMBU method leverages empirical bottom-level marginal distributions with empirical copula functions (describing bottom-level dependencies) to generate the distribution of aggregate-level distributions using BottomUp reconciliation. The sample reordering technique in the PERMBU method reinjects multivariate dependencies into independent bottom-level samples. residuals=Ο΅^i,tresiduals = \hat{\epsilon}_{i,t} Algorithm:
  1. For all series compute conditional marginals distributions.
  2. Compute residuals and obtain rank permutations.
  3. Obtain K-sample from the bottom-level series predictions.
  4. Apply recursively through the hierarchical structure:
    1. For a given aggregate series ii and its children series:
    2. Obtain children’s empirical joint using sample reordering copula.
    3. From the children’s joint obtain the aggregate series’s samples.
Parameters:
NameTypeDescriptionDefault
Sarraysumming matrix of size (base, bottom).required
tagsdict[str, ndarray]Each key is a level and each value its S indices.required
y_insamplearrayInsample values of size (base, insample_size).required
y_hat_insamplearrayInsample point forecasts of size (base, insample_size).required
sigmaharrayforecast standard dev. of size (base, horizon).required
num_samplesintnumber of normal prediction samples generated. Default is NoneNone
seedintrandom seed for numpy generator’s replicability. Default is 0.0

PERMBU.get_samples

get_samples(num_samples=None)
PERMBU Sample Reconciliation Method. Applies PERMBU reconciliation method as defined by Taieb et. al 2017. Generating independent base prediction samples, restoring its multivariate dependence using estimated copula with reordering and applying the BottomUp aggregation to the new samples. Parameters:
NameTypeDescriptionDefault
num_samplesintnumber of samples generated from coherent distribution.None
Returns:
NameTypeDescription
samplesndarrayCoherent samples of size (base, horizon, num_samples).

References