大量时间序列按照不同聚合级别组织成结构,其预测结果通常需要遵循聚合约束,这带来了创建能够进行一致性预测的新算法的挑战。

HierarchicalForecast 提供了一系列截面和时间协调方法,包括 BottomUpTopDownMiddleOutMinTraceERM,以及概率一致性预测,包括 NormalityBootstrapPERMBU

🎊 功能特点

  • 经典协调方法
    • BottomUp:简单地向上级汇总。
    • TopDown:将上层预测分配到整个层次结构中。
  • 其他协调方法
    • MiddleOut:将基础预测锚定在中间层。基础预测之上的层使用自下而上的方法,而基础预测之下的层使用自上而下。
    • MinTrace:通过最小迹协调(Minimum Trace reconciliation),最小化一致预测空间的总预测方差。
    • ERM:通过最小化 L1 正则化目标来优化协调矩阵。
  • 概率一致性方法
    • Normality:在正态性假设下,使用最小迹协方差矩阵的封闭形式。
    • Bootstrap:使用 Gamakumara 的自举法生成分层协调预测的分布。
    • PERMBU:通过使用估计的秩置换 Copulas 重新注入多变量依赖性,并执行自下而上的聚合,来协调独立样本预测。
  • 时间协调方法
    • 所有协调方法(除样本内方法外)也可用于时间层次结构。

遗漏了什么?请在此处提出问题或在 Slack 与我们联系。

📖 为什么?

简短: 我们希望通过为行业和学术界的分层预测任务提供可靠的基线和基准,从而为机器学习领域做出贡献。完整的 论文 在此。

详细: HierarchicalForecast 集成了公开可用的处理数据集、评估指标和一组精选的统计基线。在此库中,我们提供了使用示例和大量实验的参考文献,展示了基线的应用并评估了其预测的准确性。通过这项工作,我们希望通过弥合机器学习预测与统计和计量经济学建模之间的差距,并为基于对这些成熟模型的透彻比较开发新颖的分层预测算法提供工具,从而为机器学习预测做出贡献。我们打算继续维护和增加存储库,促进整个预测社区的合作。

💻 安装

PyPI

您可以从 Python 包索引 安装 `HierarchicalForecast` 的发布版本,命令如下:

pip install hierarchicalforecast

(建议在 python 虚拟环境或 conda 环境中安装。)

Conda

您也可以从 conda 安装 `HierarchicalForecast` 的发布版本,命令如下:

conda install -c conda-forge hierarchicalforecast

(建议在 python 虚拟环境或 conda 环境中安装。)

开发模式

如果您想对代码进行一些修改并实时查看效果(无需重新安装),请按照以下步骤操作:

git clone https://github.com/Nixtla/hierarchicalforecast.git
cd hierarchicalforecast
pip install -e .

🧬 如何使用

以下示例需要额外安装 `statsforecast` 和 `datasetsforecast` 包。如果未安装,请通过您首选的方法进行安装,例如 `pip install statsforecast datasetsforecast`。`datasetsforecast` 库允许我们下载分层数据集,我们将使用 `statsforecast` 计算需要协调的基础预测。

您可以在 Colab 中打开此示例

import pandas as pd

#obtain hierarchical dataset
from datasetsforecast.hierarchical import HierarchicalData

# compute base forecast no coherent
from statsforecast.core import StatsForecast
from statsforecast.models import AutoARIMA, Naive

#obtain hierarchical reconciliation methods and evaluation
from hierarchicalforecast.core import HierarchicalReconciliation
from hierarchicalforecast.evaluation import evaluate
from hierarchicalforecast.methods import BottomUp, TopDown, MiddleOut
from utilsforecast.losses import mse

# Load TourismSmall dataset
Y_df, S, tags = HierarchicalData.load('./data', 'TourismSmall')
Y_df['ds'] = pd.to_datetime(Y_df['ds'])
S = S.reset_index(names="unique_id")

#split train/test sets
Y_test_df  = Y_df.groupby('unique_id').tail(4)
Y_train_df = Y_df.drop(Y_test_df.index)

# Compute base auto-ARIMA predictions
fcst = StatsForecast(models=[AutoARIMA(season_length=4), Naive()],
                     freq='QE', n_jobs=-1)
Y_hat_df = fcst.forecast(df=Y_train_df, h=4)

# Reconcile the base predictions
reconcilers = [
    BottomUp(),
    TopDown(method='forecast_proportions'),
    MiddleOut(middle_level='Country/Purpose/State',
              top_down_method='forecast_proportions')
]
hrec = HierarchicalReconciliation(reconcilers=reconcilers)
Y_rec_df = hrec.reconcile(Y_hat_df=Y_hat_df, Y_df=Y_train_df,
                          S=S, tags=tags)

评估

df = Y_rec_df.merge(Y_test_df, on=['unique_id', 'ds'], how='left')

evaluate(df=df, metrics=[mse],
                   tags=tags, benchmark='Naive')

如何引用

完整的 论文 在此。

@article{olivares2022hierarchicalforecast,
    author    = {Kin G. Olivares and
                 Federico Garza and 
                 David Luo and 
                 Cristian Challú and
                 Max Mergenthaler and
                 Souhaib Ben Taieb and
                 Shanika L. Wickramasuriya and
                 Artur Dubrawski},
    title     = {{HierarchicalForecast}: A Reference Framework for Hierarchical Forecasting in Python},
    journal   = {Work in progress paper, submitted to Journal of Machine Learning Research.},
    volume    = {abs/2207.03517},
    year      = {2022},
    url       = {https://arxiv.org/abs/2207.03517},
    archivePrefix = {arXiv}
}