module hierarchicalforecast.core


class HierarchicalReconciliation

Hierarchical Reconciliation Class. The core.HierarchicalReconciliation class allows you to efficiently fit multiple HierarchicaForecast methods for a collection of time series and base predictions stored in pandas DataFrames. The Y_df dataframe identifies series and datestamps with the unique_id and ds columns while the y column denotes the target time series variable. The Y_h dataframe stores the base predictions, example (AutoARIMA, ETS, etc.). Args: References:
  • [Rob J. Hyndman and George Athanasopoulos (2018). "Forecasting principles and practice, Hierarchical and Grouped Series".](https: //otexts.com/fpp3/hierarchical.html)

method __init__

__init__(reconcilers: list[HReconciler])

method bootstrap_reconcile

bootstrap_reconcile(
    Y_hat_df: Union[ForwardRef('DataFrame[Any]'), ForwardRef('LazyFrame[Any]')],
    S_df: Union[ForwardRef('DataFrame[Any]'), ForwardRef('LazyFrame[Any]')],
    tags: dict[str, ndarray],
    Y_df: Optional[ForwardRef('DataFrame[Any]'), ForwardRef('LazyFrame[Any]')] = None,
    level: Optional[list[int]] = None,
    intervals_method: str = 'normality',
    num_samples: int = -1,
    num_seeds: int = 1,
    id_col: str = 'unique_id',
    time_col: str = 'ds',
    target_col: str = 'y'
) → ~FrameT
Bootstraped Hierarchical Reconciliation Method. Applies N times, based on different random seeds, the reconcile method for the different reconciliation techniques instantiated in the reconcilers list. Args:
  • Y_hat_df (Frame): DataFrame, base forecasts with columns [‘unique_id’, ‘ds’] and models to reconcile.
  • S_df (Frame): DataFrame with summing matrix of size (base, bottom), see aggregate method.
  • tags (dict[str, np.ndarray]): Each key is a level and its value contains tags associated to that level.
  • Y_df (Optional[Frame], optional): DataFrame, training set of base time series with columns ['unique_id', 'ds', 'y']. If a class of self.reconciles receives y_hat_insample, Y_df must include them as columns. Default is None.
  • level (Optional[list[int]], optional): positive float list [0,100), confidence levels for prediction intervals. Default is None.
  • intervals_method (str, optional): method used to calculate prediction intervals, one of normality, bootstrap, permbu. Default is “normality”.
  • num_samples (int, optional): if positive return that many probabilistic coherent samples. Default is -1.
  • num_seeds (int, optional): random seed for numpy generator’s replicability. Default is 1.
  • id_col (str, optional): column that identifies each serie. Default is “unique_id”.
  • time_col (str, optional): column that identifies each timestep, its values can be timestamps or integers. Default is “ds”.
  • target_col (str, optional): column that contains the target. Default is “y”.
Returns:
  • FrameT: DataFrame, with bootstraped reconciled predictions.

method reconcile

reconcile(
    Y_hat_df: Union[ForwardRef('DataFrame[Any]'), ForwardRef('LazyFrame[Any]')],
    tags: dict[str, ndarray],
    S_df: Union[ForwardRef('DataFrame[Any]'), ForwardRef('LazyFrame[Any]')] = None,
    Y_df: Optional[ForwardRef('DataFrame[Any]'), ForwardRef('LazyFrame[Any]')] = None,
    level: Optional[list[int]] = None,
    intervals_method: str = 'normality',
    num_samples: int = -1,
    seed: int = 0,
    is_balanced: bool = False,
    id_col: str = 'unique_id',
    time_col: str = 'ds',
    target_col: str = 'y',
    id_time_col: str = 'temporal_id',
    temporal: bool = False,
    S: Union[ForwardRef('DataFrame[Any]'), ForwardRef('LazyFrame[Any]')] = None
) → ~FrameT
Hierarchical Reconciliation Method. The reconcile method is analogous to SKLearn fit_predict method, it applies different reconciliation techniques instantiated in the reconcilers list. Most reconciliation methods can be described by the following convenient linear algebra notation: y~[a,b],τ=S[a,b][b]P[b][a,b]y^[a,b],τ\tilde{\mathbf{y}}_{[a,b],\tau} = \mathbf{S}_{[a,b][b]} \mathbf{P}_{[b][a,b]} \hat{\mathbf{y}}_{[a,b],\tau} where a,ba, b represent the aggregate and bottom levels, S[a,b][b]\mathbf{S}_{[a,b][b]} contains the hierarchical aggregation constraints, and P[b][a,b]\mathbf{P}_{[b][a,b]} varies across reconciliation methods. The reconciled predictions are y~[a,b],τ\tilde{\mathbf{y}}_{[a,b],\tau}, and the base predictions y^[a,b],τ\hat{\mathbf{y}}_{[a,b],\tau}. Args:
  • Y_hat_df (Frame): DataFrame, base forecasts with columns [‘unique_id’, ‘ds’] and models to reconcile.
  • tags (dict[str, np.ndarray]): Each key is a level and its value contains tags associated to that level.
  • S_df (Frame, optional): DataFrame with summing matrix of size (base, bottom), see aggregate method. Default is None.
  • Y_df (Optional[Frame], optional): DataFrame, training set of base time series with columns ['unique_id', 'ds', 'y']. If a class of self.reconciles receives y_hat_insample, Y_df must include them as columns. Default is None.
  • level (Optional[list[int]], optional): positive float list [0,100), confidence levels for prediction intervals. Default is None.
  • intervals_method (str, optional): method used to calculate prediction intervals, one of normality, bootstrap, permbu. Default is “normality”.
  • num_samples (int, optional): if positive return that many probabilistic coherent samples. Default is -1.
  • seed (int, optional): random seed for numpy generator’s replicability. Default is 0.
  • is_balanced (bool, optional): wether Y_df is balanced, set it to True to speed things up if Y_df is balanced. Default is False.
  • id_col (str, optional): column that identifies each serie. Default is “unique_id”.
  • time_col (str, optional): column that identifies each timestep, its values can be timestamps or integers. Default is “ds”.
  • target_col (str, optional): column that contains the target. Default is “y”.
Returns:
  • FrameT: DataFrame, with reconciled predictions.