时序分析 35
时序预测 从ARIMA到SARIMAX(四) SARIMA
接上
Step 5. SARIMA
现在我们来增加季节性因素。我们需要确定季节性因素的参数 P,Q,D.S。
从前面的ACF图中我们得到了提示,季节周期为7,也就是 S=7。
季节差分
我们已经知道为了使时序数据平稳,可以使用差分。对于ARIMA模型而言就是确定阶数 𝑑 。对于季节性时序数据,我们很可能需要应用季节性差分。但是,在季节性差分中,我们并不是用相邻的两个时序点数据相减,而是跨周期相减。
可以从上面的分析和图中观察到强季节性,也就是说 𝐷=1 。一般的规则是 𝑑+𝐷≤2 。接下来,我们需要寻找季节性的其他参数 P,Q,类似于非季节性因素参数,我们需要在季节差分数据上画出ACF和PACF。
# Create the figure
fig, (ax1, ax2) = plt.subplots(2,1,figsize=(8,6))
# Plot the ACF on ax1
plot_acf(df_store_2_item_28_time, lags=14, zero=False, ax=ax1)
# Plot the PACF on ax2
plot_pacf(df_store_2_item_28_time, lags=14, zero=False, ax=ax2)
plt.show()
# Take the first and seasonal differences (S=7) and drop NaNs
df_store_2_item_28_time_diff = df_store_2_item_28_time.diff().diff(7).dropna()
# Create the figure
fig, (ax1, ax2) = plt.subplots(2,1,figsize=(8,6))
# Plot the ACF on ax1
plot_acf(df_store_2_item_28_time_diff, lags=14, zero=False, ax=ax1)
# Plot the PACF on ax2
plot_pacf(df_store_2_item_28_time_diff, lags=14, zero=False, ax=ax2)
plt.show()
从上面时序的ACF和PACF中观察,非季节性的MA模型参数
对于季节性部分,我们画出一系列特定滞后点的ACF和PACF。
# Make list of lags
lags = [7, 14, 21, 28, 35]
# Create the figure
fig, (ax1, ax2) = plt.subplots(2,1,figsize=(8,6))
# Plot the ACF on ax1
plot_acf(df_store_2_item_28_time_diff, lags=lags, zero=False, ax=ax1)
# Plot the PACF on ax2
plot_pacf(df_store_2_item_28_time_diff, lags=lags, zero=False, ax=ax2)
plt.show()
看上去,季节性因素的MA模型的Q=1,至此我们得到SARIMA模型的参数为。
sarima_01_model = SARIMAX(df_store_2_item_28_time, order=(0,1,6), seasonal_order=(0,1,1,7))
sarima_01_results = sarima_01_model.fit()
# Calculate the mean absolute error from residuals
mae = np.mean(np.abs(sarima_01_results.resid))
# Print mean absolute error
print('MAE: %.3f' % mae)
MAE: 4.555
sarima_01_results.summary()
模型诊断报告中得到如下信息:
- Prob(Q)=0.97 > 0.97:不能拒绝原假设,也就是说残差没有相关性。
- Prob(JB)=0.13 > 0.05:不能拒绝原假设,说明残差呈正态分布。
sarima_01_results.plot_diagnostics()
plt.show()
- Standardized residual:没有发现明显模式,模型OK。
- Histogram plus kde estimate:与正态分布更加接近。
- Correlogram or ACF plot:无显著性滞后相关性。
- Normal Q-Q:更接近直线,说明残差为正态分布。
这四点都说明模型效果良好。
模型自动选择
前面也提到,依赖ACF和PACF来决定模型不是非常严谨,还是应该依据BIC和AIC等模型判断准则。下面我们采用pmdarima包进行模型自动选择。
import pmdarima as pm
# Create auto_arima model
model1 = pm.auto_arima(df_store_2_item_28_time, #time series
seasonal=True, # is the time series seasonal
m=7, # the seasonal period - one week?
d=1, # non-seasonal difference order
D=1, # seasonal difference order
max_p=6, # max value of p to test
max_q=6, # max value of p to test
max_P=6, # max value of P to test
max_Q=6, # max value of Q to test
information_criterion='aic', # used to select best mode
trace=True, # prints the information_criterion for each model it fits
error_action='ignore', # ignore orders that don't work
stepwise=True, # apply an intelligent order search
suppress_warnings=True)
# Print model summary
print(model1.summary())
模型自动选择基于AIC所得到的最好模型为
sarima_02_model = SARIMAX(df_store_2_item_28_time, order=(6,1,1), seasonal_order=(6,1,0,7))
sarima_02_results = sarima_02_model.fit()
# Calculate the mean absolute error from residuals
mae = np.mean(np.abs(sarima_02_results.resid))
# Print mean absolute error
print('MAE: %.3f' % mae)
MAE: 4.788
所得到的MAE比前面稍微高一些。
sarima_02_results.summary()
- Prob(Q)=1.00 > 0.05:残差不相关。
- Prob(JB)=0.98 > 0.05:残差为正态分布。
# Create the 4 diagostics plots
sarima_02_results.plot_diagnostics()
plt.show()
- Standardized residual:无明显模式,OK.
- Histogram plus KDE:与正态分布有一定差异。
- Correlogram or ACF:无显著性滞后相关,OK。
- Normal Q-Q:近正态分布,OK。
比较看来,前面的模型要优于这个模型。
未完待续…