提示

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

1. 安装 statsforecast 并加载数据

使用 pip 安装 statsforecast 并加载 Air Passengers 数据集作为示例

# uncomment the following line to install the library
# %pip install statsforecast
from statsforecast.utils import AirPassengersDF
Y_df = AirPassengersDF

2. 导入 StatsForecast 和模型

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

import pandas as pd

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

3. 实例化类

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

season_length = 12 # Define season length as 12 months for monthly data
horizon = 1 # Forecast horizon is set to 1 month

# Define a list of models for forecasting
models = [
    AutoARIMA(season_length=season_length), # ARIMA model with automatic order selection and seasonal component
    AutoETS(season_length=season_length), # ETS model with automatic error, trend, and seasonal component
    AutoTheta(season_length=season_length), # Theta model with automatic seasonality detection
    AutoCES(season_length=season_length), # CES model with automatic seasonality detection
]

# Instantiate StatsForecast class with models, data frequency ('M' for monthly),
# and parallel computation on all CPU cores (n_jobs=-1)
sf = StatsForecast(
    models=models, # models for forecasting
    freq=pd.offsets.MonthEnd(),  # frequency of the timestamps
    n_jobs=1  # number of jobs to run in parallel, -1 means using all processors
)

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

.forecast 方法对于分布式计算更快,并且不保存拟合的模型

# Generate forecasts for the specified horizon using the sf object
Y_hat_df = sf.forecast(df=Y_df, h=horizon) # forecast data
# Display the first few rows of the forecast DataFrame
Y_hat_df.head() # preview of forecasted data
unique_iddsAutoARIMAAutoETSAutoThetaCES
01.01961-01-31444.309575442.357169442.940797453.03418

4. b) 使用 fit 和 predict 方法进行预测

.fit 方法保存拟合的模型

sf.fit(df=Y_df) # Fit the models to the data using the fit method of the StatsForecast object

sf.fitted_ # Access fitted models from the StatsForecast object

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

Y_hat_df.head() # Preview the first few rows of the forecasted data
unique_iddsAutoARIMAAutoETSAutoThetaCES
01.01961-01-31444.309575442.357169442.940797453.03418

参考资料

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