时间序列分析 - ARIMA/SARIMA 模型
模型
ARIMA
在自回归模型和滑动窗口的基础上,我们引入 ARIMA(p, d, q):
- AR:自回归
- I:差分
- MA:滑动平均
p
: 自回归项数d
: 平稳化序列所需的差分次数q
: 滑动平均项数
SARIMA
引入 Seasonal 参量
s
: 季节周期长度
此后,模型结构可记作 SARIMA(p, d, q)(P, D, Q, s)
,模型会分季节性和非季节性两部分进行计算,其中季节性部分均将 s 作为步长,这样有
- 季节性差分 D
- 季节性子回归 SAR, P
- 季节性移动平均 SMA, Q
statsmodels 将模型记作
\phi_p (L) \tilde \phi_P (L^s) \Delta^d \Delta_s^D y_t = A(t) + \theta_q (L) \tilde \theta_Q (L^s) \zeta_t
我还是更愿意看到
\begin{equation*}
\begin{align*}
& \underbrace{(1 - \phi_1 B - \cdots - \phi_p B^p)}_{\text{非季节性 AR}}
\underbrace{(1 - \Phi_1 B^s - \cdots - \Phi_P B^{Ps})}_{\text{季节性 AR}}
\underbrace{(1 - B)^d (1 - B^s)^D}_{\text{差分}} y_t\\
& = c +
\underbrace{(1 + \theta_1 B + \cdots + \theta_q B^q)}_{\text{非季节性 MA}}
\underbrace{(1 + \Theta_1 B^s + \cdots + \Theta_Q B^{Qs})}_{\text{季节性 MA}} \epsilon_t
\end{align*}
\end{equation*}
意味着实际上 SARIMA 是对非季节性和季节性分量做了乘性组合
SARIMAX
引入 X 外生变量 (eXogenous variables),引入对外部影响因素的考量,模型扩展为
y_t = \text{SARIMA} + \beta X_{eXog} + \varepsilon_t
实现
from statsmodels.tsa.arima.model import ARIMA
model = ARIMA(x)
res = model.fit()
print(res.summary())
statsmodels
的这一接口提供了下面所有的情况:
- AR
- MA
- ARMA
- ARIMA
- SARIMA
参数:
endog
时间序列数据exog
外生变量order=(p, d, q)
p, d, q 元组,其中 p 和 q 可以是整数列表seasonal_order=(P, D, Q, s)
其中 s 是整数trend='n'
允许的参数有{'n','c','t','ct'}
例子
再次使用 AirPassengers dataset
import pandas as pd
import numpy as np
from statsmodels.tsa.arima.model import ARIMA
from matplotlib import pyplot as plt
# 导入数据
data = pd.read_csv("./AirPassengers.csv")
data['Month'] = pd.to_datetime(data['Month'])
data = data.set_index('Month')
plt.plot(data)
plt.title('Original')
plt.show()
model = ARIMA(data, seasonal_order=(1, 1, 1, 12))
model_fit = model.fit()
print(model_fit.summary())
给出了以下结果
SARIMAX Results
================================================================================
Dep. Variable: #Passengers No. Observations: 144
Model: ARIMA(1, 1, [1], 12) Log Likelihood -585.491
Date: Sat, 03 May 2025 AIC 1176.982
Time: 20:10:29 BIC 1185.630
Sample: 01-01-1949 HQIC 1180.496
- 12-01-1960
Covariance Type: opg
==============================================================================
coef std err z P>|z| [0.025 0.975]
------------------------------------------------------------------------------
ar.S.L12 0.9937 0.013 75.136 0.000 0.968 1.020
ma.S.L12 -0.8045 0.157 -5.136 0.000 -1.111 -0.497
sigma2 341.5929 71.760 4.760 0.000 200.946 482.239
===================================================================================
Ljung-Box (L1) (Q): 65.92 Jarque-Bera (JB): 5.63
Prob(Q): 0.00 Prob(JB): 0.06
Heteroskedasticity (H): 2.31 Skew: -0.51
Prob(H) (two-sided): 0.01 Kurtosis: 2.99
===================================================================================
附带警告:
Covariance matrix calculated using the outer product of gradients (complex-step).
ValueWarning: No frequency information was provided, so inferred frequency MS will be used.
无视它们。
解释
- Dep.Variable: 因变量
- No. Observation: 观测值数据点的数量
- Log Likelihood: 拟合的似然函数值
- AIC/BIC/HQIC: 信息准则
- Covariance Type: 协方差类型 opg:梯度外积
- ar.S.L12: 季节性AR项
- ma.S.L12: 季节性MA项
- sigma2: 残差方差
- coef: 该项系数
- std err: 系数估计的标准差
- z: 系数和标准差的比值
- P>|z| Z 检验的双侧 p-value
- [0.025, 0.975] 95% 置信区间
- Ljung-Box (L1) (Q): Ljung-Box 检验残差的自相关性
- Prob(Q): 0.00 表示残差存在未建模的自相关
- Heteroskedasticity (H): 异方差性
- Prob(H) (two-sided): 0.01 表示残差确随时间变化
- Skew: 偏度
- Kurtosis: 峰度
时间序列分析 - ARIMA/SARIMA 模型
http://localhost:8090/archives/fAT962Cs