本节摘要:ARIMA / SARIMA 是经典时序模型族的代表,统计学上基于自相关 + 平稳性推导。本节给出 ARIMA(p,d,q) 三参数的工程含义、SARIMA 的季节项扩展,并通过一段合成 ARIMA(1,1,1) 数据演示完整拟合 + 残差诊断流程。
阅读完本节,你应当能够:
ARIMA(p, d, q) 由三个参数决定:
y(t) = c + φ₁ y(t-1) + ... + φₚ y(t-p) + ε。y(t) = c + ε(t) + θ₁ ε(t-1) + ... + θq ε(t-q)。直观上:
💡 关键直觉:p + q 应该不大。超过 5 阶基本都在过拟合。p、q 通常在 0–3 之间。
SARIMA(p,d,q)(P,D,Q,m) 在 ARIMA 基础上加了季节项:
季节部分的递推结构与 ARIMA 类似,只是"季节 lag"是 m 的倍数。
from statsmodels.tsa.arima.model import ARIMA # 合成 ARIMA(1,1,1) 序列 rng = np.random.default_rng(0) n = 200 y = np.cumsum(rng.normal(0, 1, n)) + rng.normal(0, 0.5, n) s = pd.Series(y, index=pd.date_range("2024-01-01", periods=n)) # 拟合 ARIMA(1,1,1) model = ARIMA(s, order=(1, 1, 1)).fit() print(model.summary())
model.summary() 输出关键信息:
对月度销量数据,季节周期 m=12:
model = ARIMA(s, order=(1, 1, 1), seasonal_order=(1, 1, 1, 12)).fit() print(model.summary())
季节项的阶数 P、Q 通常 0 或 1——过多阶数容易过拟合。
拟合完模型后,必须做残差诊断。一个"够好"的模型应当让残差:
from statsmodels.stats.diagnostic import acorr_ljungbox from statsmodels.stats.stattools import jarque_bera resid = model.resid # 1) 白噪声检验 lb = acorr_ljungbox(resid, lags=[10, 20], return_df=True) print("Ljung-Box p:\n", lb) # 2) 正态检验 jb_stat, jb_p, _, _ = jarque_bera(resid) print(f"Jarque-Bera p = {jb_p:.4f}") # 3) 残差图 import matplotlib.pyplot as plt fig, axes = plt.subplots(1, 2, figsize=(10, 3)) axes[0].plot(resid); axes[0].set_title("残差时序") from statsmodels.graphics.gofplots import qqplot qqplot(resid, line="45", fit=True, ax=axes[1]) plt.tight_layout()
如果 Ljung-Box 检验拒绝(p < 0.05)→ 模型没把信号剥干净,回 6.3 调阶。
# 未来 12 期预测 + 95% 置信区间 forecast = model.get_forecast(steps=12) mean = forecast.predicted_mean ci = forecast.conf_int(alpha=0.05) # 画图 fig, ax = plt.subplots(figsize=(10, 4)) ax.plot(s.index, s.values, label="历史") mean_index = pd.date_range(s.index[-1], periods=12, freq=s.index.freq) ax.plot(mean_index, mean.values, label="预测", color="red") ax.fill_between(mean_index, ci.iloc[:, 0], ci.iloc[:, 1], alpha=0.3, color="red", label="95% 区间") ax.legend()
预测区间宽度反映不确定性。长 horizon 的预测区间应当逐渐变宽——预测越远,不确定性越大。
| 局限 | 替代方案 |
|---|---|
| 不能建模多重季节性 | MSTL、Prophet、NeuralForecast |
| 假设残差正态 | GARCH 族(金融)、分位数回归 |
| 不支持外生变量 | ARIMAX、SARIMAX(statsmodels 支持) |
| 长周期难以处理 | Prophet changepoint、状态空间模型 |
| 100+ 阶滞后 | 神经网络方法(N-BEATS、TFT、Transformer-for-time-series) |
| 数据特征 | 推荐模型 |
|---|---|
| 平稳无季节 | ARMA(p, q) 或 ARIMA(p, 0, q) |
| 有趋势、无季节 | ARIMA(p, 1, q) |
| 强季节性 + 趋势 | SARIMA(p, 1, q)(P, 1, Q, m) |
| 多重季节性 | Prophet / MSTL / NeuralForecast |
| 有外生变量 | ARIMAX / SARIMAX |
| 长序列 + 复杂模式 | Prophet / 神经网络 |
