时间序列的自相关是指一个给定时间点的时间序列中的值可能与另一个时间点的值具有相关性,也可以指序列数据中具有固定距离的任意两点之间是否存在相关性。
import wooldridge as woo
import pandas as pd
import numpy as np
import statsmodels.api as sm
import statsmodels.formula.api as smf
barium = woo.dataWoo('barium')
T = len(barium)
barium.index = pd.date_range(start='1978-02', periods=T, freq='M')
# print(barium.head())
reg = smf.ols(formula='np.log(chnimp) ~ np.log(chempi) + np.log(gas) +'
'np.log(rtwex) + befile6 + affile6 + afdec6',
data=barium)
results = reg.fit()
resid = results.resid#获取残差
1.图示法
由于残差 e t e_t et可以作为扰动项 μ t \mu_t μt的估计,因此,如果存在序列相关性,必然会由残差项 e t e_t et反映出来,因此可以用 e t e_t et的变化图形来判断随机干扰项的序列相关性。
1.1 滞后图
滞后图,就是将残差
e
t
e_t
et和残差滞后
n
n
n阶的散点图,需要用到pandas
的lag_plot
函数。
from pandas.plotting import lag_plot
import matplotlib.pyplot as plt
import seaborn as sns
sns.set(style='whitegrid')
plt.figure(dpi=160)
lag_plot(resid, lag=1)
<Axes: xlabel='y(t)', ylabel='y(t + 1)'>
# 残差与滞后1-4阶的图
fig, axes = plt.subplots(1, 4, figsize=(16,5), dpi=300, sharex=True, sharey=True)
for i in range(4):
lag_plot(resid, lag=i+1, ax=axes[i])
axes[i].set_title(f'Lag{i+1}')
1.2 自相关图
自相关图的绘制,可以使用pandas
的autocorrelation_plot
函数
from pandas.plotting import autocorrelation_plot
plt.figure(dpi=160)
autocorrelation_plot(resid)
plt.show
<function matplotlib.pyplot.show(close=None, block=None)>
1.3 自相关图和偏自相关图
自相关系数和偏自相关系数的区别
- 假设时间序列数据 y t y_t yt
- y t = α 0 + α 1 y t − 1 y_t=\alpha_0 + \alpha_1y_{t-1} yt=α0+α1yt−1, α 1 \alpha_1 α1就是 y t y_t yt和 y t − 1 y_{t-1} yt−1自相关系数;
- y t = α 0 + α 1 y t − 1 + α 2 y t − 2 + α 3 y t − 3 y_t=\alpha_0 + \alpha_1y_{t-1} + \alpha_2y_{t-2} + \alpha_3y_{t-3} yt=α0+α1yt−1+α2yt−2+α3yt−3, α 3 \alpha_3 α3就是 y t y_t yt和 y t − 3 y_{t-3} yt−3偏自相关系数。
from statsmodels.graphics.tsaplots import plot_acf, plot_pacf
def acf_pacf_plot(timeseries, lags):
fig, axes = plt.subplots(1, 2, figsize=(16,5), dpi=300)
plot_acf(timeseries, lags=lags, ax=axes[0])
axes[0].set_title('ACF')
plot_pacf(timeseries, lags=lags, ax=axes[1])
axes[1].set_title('PACF')
plt.show()
acf_pacf_plot(resid, 20) # ACF图1、2、3阶的自相关系数都在蓝色范围(95%置信区间)外,可以初步判断该序列存在短期自相关性
2. 假设检验
2.1 DW检验
DW检验是较早提出的自相关检验,由于它只能检验一阶自相关,且必须在解释变量满足严格外生性的情况下才成立,现在已经不常用。
from statsmodels.stats.stattools import durbin_watson
durbin_watson(results.resid)
1.4584144308481417
2.2 Breusch-Godfrey检验
BG检验克服了DW检验的缺陷,适合于高阶序列相关及模型中存在滞后被解释变量的情形。
考虑如下多元线性模型:
y
t
=
β
0
+
β
1
x
t
1
+
β
2
x
t
2
+
.
.
.
+
β
k
x
t
k
+
μ
y_t=\beta_0 + \beta_1x_{t1} + \beta_2x_{t2} + ... + \beta_kx_{tk} + \mu
yt=β0+β1xt1+β2xt2+...+βkxtk+μ
若怀疑随机干扰项存在p阶序列相关:
μ
t
=
ρ
1
μ
t
−
1
+
ρ
2
μ
t
−
2
+
.
.
.
+
ρ
p
μ
t
−
p
+
ε
t
\mu_t = \rho_1\mu_{t-1} + \rho_2\mu_{t-2} + ... + \rho_p\mu_{t-p} + \varepsilon_t
μt=ρ1μt−1+ρ2μt−2+...+ρpμt−p+εt
检验原假设:
H
0
:
ρ
1
=
ρ
2
=
.
.
.
=
ρ
p
=
0
H_0:\rho_1=\rho_2=...=\rho_p=0
H0:ρ1=ρ2=...=ρp=0
由于
μ
t
\mu_t
μt不可测,故用
e
t
e_t
et替代,并引入解释变量,进行如下辅助回归:
e
t
=
γ
1
x
t
1
+
γ
2
x
t
2
+
.
.
.
+
γ
k
x
t
k
+
δ
1
e
t
−
1
+
δ
2
e
t
−
2
+
.
.
.
+
δ
p
e
t
−
p
+
ε
t
e_t=\gamma_1x_{t1} + \gamma_2x_{t2} + ... + \gamma_kx_{tk} + \delta_1e_{t-1} + \delta_2e_{t-2} + ... + \delta_pe_{t-p} + \varepsilon_t
et=γ1xt1+γ2xt2+...+γkxtk+δ1et−1+δ2et−2+...+δpet−p+εt
无自相关的原假设相当于检验:
H
0
:
γ
1
=
γ
2
=
.
.
.
=
γ
p
=
0
H_0:\gamma_1=\gamma_2=...=\gamma_p=0
H0:γ1=γ2=...=γp=0
BG的检验步骤:
- 将 y t y_t yt对 x t 1 , x t 2 , . . . , x t k x_{t1},x_{t2},...,x_{tk} xt1,xt2,...,xtk做回归,求出OLS残差 e t e_t et
- 将 e t e_t et对 x t 1 , x t 2 , . . . , x t k , e t − 1 , e t − 2 , . . . , e t − p x_{t1},x_{t2},...,x_{tk},e_{t-1},e_{t-2},...,e_{t-p} xt1,xt2,...,xtk,et−1,et−2,...,et−p做回归
- 计算 e t − 1 , e t − 2 , . . . , e t − p e_{t-1},e_{t-2},...,e_{t-p} et−1,et−2,...,et−p联合显著的F检验
from statsmodels.stats.diagnostic import acorr_breusch_godfrey
bg_result = acorr_breusch_godfrey(results, nlags=3)
bg_lm_statistic = bg_result[0]
bg_lm_pval = bg_result[1]
bg_F_statistic = bg_result[2]
bg_F_pval = bg_result[3]
bg_test_output = pd.Series(bg_result[0:4], index=['bg_lm_statistic','bg_lm_pval','bg_F_statistic','bg_test_output'])
bg_test_output
bg_lm_statistic 14.768156
bg_lm_pval 0.002026
bg_F_statistic 5.124662
bg_test_output 0.002264
dtype: float64
2.3 Ljung-Box检验
LB检验:
- H 0 H_0 H0假设:序列的每个值是独立的,即纯随机
- H 1 H_1 H1假设:序列之间不是独立的,即存在相关性
from statsmodels.stats.diagnostic import acorr_ljungbox
acorr_ljungbox(results.resid, lags=[10]) # 对10阶做LB检验,存在相关性
lb_stat | lb_pvalue | |
---|---|---|
10 | 24.445298 | 0.006502 |
acorr_ljungbox(results.resid, lags=10) # 对1-10阶做LB检验
lb_stat | lb_pvalue | |
---|---|---|
1 | 9.821711 | 0.001725 |
2 | 16.072867 | 0.000323 |
3 | 21.332651 | 0.000090 |
4 | 21.532752 | 0.000248 |
5 | 21.571232 | 0.000632 |
6 | 21.619047 | 0.001419 |
7 | 22.365714 | 0.002197 |
8 | 22.973536 | 0.003398 |
9 | 24.381012 | 0.003738 |
10 | 24.445298 | 0.006502 |