自动时间序列预测

如何使用 AutoARIMAAutoETSAutoCESAutoTheta 进行自动预测。

Tip

对大量单变量时间序列的自动预测通常是必要的。常常需要对多个产品线或库存单位(sku)进行预测。在这种情况下,自动预测算法是一个必不可少的工具。自动预测算法必须确定合适的时间序列模型,估计参数并计算预测结果。它们必须对不寻常的时间序列模式具有鲁棒性,并且能够在没有用户干预的情况下适用于大量序列。

1. 安装 statsforecast 并加载数据

使用 pip 安装 statsforecast,并将航空乘客数据集加载作为示例。

# 取消注释以下行以安装该库
# %pip 安装 statsforecast
from statsforecast.utils import AirPassengersDF
Y_df = AirPassengersDF

2. 导入StatsForecast和模型

导入核心StatsForecast类和您想要使用的模型

from statsforecast import StatsForecast
from statsforecast.models import AutoARIMA, AutoETS, AutoTheta, AutoCES

3. 实例化类

使用适当的参数实例化StatsForecast类

season_length = 12 # 将季节长度定义为12个月用于月度数据
horizon = 1 # 预测时间范围设定为1个月

# 定义一组预测模型
models = [
    AutoARIMA(season_length=season_length), # 具有自动阶数选择和季节性成分的ARIMA模型
    AutoETS(season_length=season_length), # 具有自动误差、趋势和季节成分的ETS模型
    AutoTheta(season_length=season_length), # 具有自动季节性检测的Theta模型
    AutoCES(season_length=season_length), # 具有自动季节性检测功能的CES模型
]

# Instantiate StatsForecast class with models, data frequency ('M' for monthly),
# 并在所有 CPU 核心上进行并行计算(n_jobs=-1)
sf = StatsForecast(
    models=models, # 预测模型
    freq='M',  # 数据的频率
    n_jobs=1  # 并行运行的作业数量,-1表示使用所有处理器
)

4. a) 使用预测方法进行预测

.forecast 方法在分布式计算中更快,并且不会保存拟合的模型。

# 使用sf对象生成指定时间范围内的预测
Y_hat_df = sf.forecast(df=Y_df, h=horizon) # 预测数据
# 显示预测DataFrame的前几行
Y_hat_df.head() # 预测数据的预览
ds AutoARIMA AutoETS AutoTheta CES
unique_id
1.0 1961-01-31 444.300049 442.357178 442.940796 453.03418

4. b) 使用拟合和预测进行预测

.fit 方法保存拟合的模型

sf.fit(df=Y_df) # 使用StatsForecast对象的fit方法将模型拟合到数据上

sf.fitted_ # 从StatsForecast对象中访问拟合模型

Y_hat_df = sf.predict(h=horizon) # Predict or forecast 'horizon' steps ahead using the predict method

Y_hat_df.head() # 预览预测数据的前几行
ds AutoARIMA AutoETS AutoTheta CES
unique_id
1.0 1961-01-31 444.300049 442.357178 442.940796 453.03418

参考文献

Hyndman, RJ 和 Khandakar, Y (2008) “自动时间序列预测:R 的 forecast 包”, 统计软件杂志, 26(3).

Give us a ⭐ on Github