# step3:ADF测试,检验单个变量是否平稳defadfuller_test(series,signif=0.05,name='',verbose=False): r =adfuller(series, autolag='AIC') output ={'test_statistic':round(r[0], 4),'pvalue':round(r[1], 4),'n_lags':round(r[2], 4),'n_obs':r[3]} p_value = output['pvalue']defadjust(val,length=6): returnstr(val).ljust(length)# Print Summaryprint(f' Augmented Dickey-Fuller Test on "{name}"', "\n ", '-'*47)print(f' Null Hypothesis: Data has unit root. Non-Stationary.')print(f' Significance Level = {signif}')print(f' Test Statistic = {output["test_statistic"]}')print(f' No. Lags Chosen = {output["n_lags"]}')for key,val in r[4].items():print(f' Critical value {adjust(key)} = {round(val, 3)}')if p_value <= signif:print(f" => P-Value = {p_value}. Rejecting Null Hypothesis.")print(f" => Series is Stationary.")else:print(f" => P-Value = {p_value}. Weak evidence to reject the Null Hypothesis.")print(f" => Series is Non-Stationary.")
for name, column in df.iteritems():adfuller_test(column, name=column.name)print('\n')
Augmented Dickey-Fuller Test on "rgnp"
-----------------------------------------------
Null Hypothesis: Data has unit root. Non-Stationary.
Significance Level = 0.05
Test Statistic = 0.6419
No. Lags Chosen = 2
Critical value 1% = -3.486
Critical value 5% = -2.886
Critical value 10% = -2.58
=> P-Value = 0.9886. Weak evidence to reject the Null Hypothesis.
=> Series is Non-Stationary.
Augmented Dickey-Fuller Test on "pgnp" ----------------------------------------------- Null Hypothesis: Data has unit root. Non-Stationary. Significance Level = 0.05 Test Statistic = 1.2743 No. Lags Chosen = 1 Critical value 1% = -3.486 Critical value 5% = -2.886 Critical value 10% = -2.58 => P-Value = 0.9965. Weak evidence to reject the Null Hypothesis. => Series is Non-Stationary.
Augmented Dickey-Fuller Test on "ulc" ----------------------------------------------- Null Hypothesis: Data has unit root. Non-Stationary. Significance Level = 0.05 Test Statistic = 1.3967 No. Lags Chosen = 2 Critical value 1% = -3.486 Critical value 5% = -2.886 Critical value 10% = -2.58 => P-Value = 0.9971. Weak evidence to reject the Null Hypothesis. => Series is Non-Stationary.
Augmented Dickey-Fuller Test on "gdfco" ----------------------------------------------- Null Hypothesis: Data has unit root. Non-Stationary. Significance Level = 0.05 Test Statistic = 0.5762 No. Lags Chosen = 5 Critical value 1% = -3.488 Critical value 5% = -2.887 Critical value 10% = -2.58 => P-Value = 0.987. Weak evidence to reject the Null Hypothesis. => Series is Non-Stationary.
Augmented Dickey-Fuller Test on "gdf" ----------------------------------------------- Null Hypothesis: Data has unit root. Non-Stationary. Significance Level = 0.05 Test Statistic = 1.1129 No. Lags Chosen = 7 Critical value 1% = -3.489 Critical value 5% = -2.887 Critical value 10% = -2.58 => P-Value = 0.9953. Weak evidence to reject the Null Hypothesis. => Series is Non-Stationary.
Augmented Dickey-Fuller Test on "gdfim" ----------------------------------------------- Null Hypothesis: Data has unit root. Non-Stationary. Significance Level = 0.05 Test Statistic = -0.1987 No. Lags Chosen = 1 Critical value 1% = -3.486 Critical value 5% = -2.886 Critical value 10% = -2.58 => P-Value = 0.9387. Weak evidence to reject the Null Hypothesis. => Series is Non-Stationary.
Augmented Dickey-Fuller Test on "gdfcf" ----------------------------------------------- Null Hypothesis: Data has unit root. Non-Stationary. Significance Level = 0.05 Test Statistic = 1.6693 No. Lags Chosen = 9 Critical value 1% = -3.49 Critical value 5% = -2.887 Critical value 10% = -2.581 => P-Value = 0.9981. Weak evidence to reject the Null Hypothesis. => Series is Non-Stationary.
Augmented Dickey-Fuller Test on "gdfce" ----------------------------------------------- Null Hypothesis: Data has unit root. Non-Stationary. Significance Level = 0.05 Test Statistic = -0.8159 No. Lags Chosen = 13 Critical value 1% = -3.492 Critical value 5% = -2.888 Critical value 10% = -2.581 => P-Value = 0.8144. Weak evidence to reject the Null Hypothesis. => Series is Non-Stationary.
for name, column in df_differenced.iteritems():adfuller_test(column, name=column.name)print('\n')
Augmented Dickey-Fuller Test on "rgnp"
-----------------------------------------------
Null Hypothesis: Data has unit root. Non-Stationary.
Significance Level = 0.05
Test Statistic = -9.0123
No. Lags Chosen = 2
Critical value 1% = -3.489
Critical value 5% = -2.887
Critical value 10% = -2.58
=> P-Value = 0.0. Rejecting Null Hypothesis.
=> Series is Stationary.
Augmented Dickey-Fuller Test on "pgnp" ----------------------------------------------- Null Hypothesis: Data has unit root. Non-Stationary. Significance Level = 0.05 Test Statistic = -10.9813 No. Lags Chosen = 0 Critical value 1% = -3.488 Critical value 5% = -2.887 Critical value 10% = -2.58 => P-Value = 0.0. Rejecting Null Hypothesis. => Series is Stationary.
Augmented Dickey-Fuller Test on "ulc" ----------------------------------------------- Null Hypothesis: Data has unit root. Non-Stationary. Significance Level = 0.05 Test Statistic = -8.769 No. Lags Chosen = 2 Critical value 1% = -3.489 Critical value 5% = -2.887 Critical value 10% = -2.58 => P-Value = 0.0. Rejecting Null Hypothesis. => Series is Stationary.
Augmented Dickey-Fuller Test on "gdfco" ----------------------------------------------- Null Hypothesis: Data has unit root. Non-Stationary. Significance Level = 0.05 Test Statistic = -7.9102 No. Lags Chosen = 3 Critical value 1% = -3.49 Critical value 5% = -2.887 Critical value 10% = -2.581 => P-Value = 0.0. Rejecting Null Hypothesis. => Series is Stationary.
Augmented Dickey-Fuller Test on "gdf" ----------------------------------------------- Null Hypothesis: Data has unit root. Non-Stationary. Significance Level = 0.05 Test Statistic = -10.0351 No. Lags Chosen = 1 Critical value 1% = -3.489 Critical value 5% = -2.887 Critical value 10% = -2.58 => P-Value = 0.0. Rejecting Null Hypothesis. => Series is Stationary.
Augmented Dickey-Fuller Test on "gdfim" ----------------------------------------------- Null Hypothesis: Data has unit root. Non-Stationary. Significance Level = 0.05 Test Statistic = -9.4059 No. Lags Chosen = 1 Critical value 1% = -3.489 Critical value 5% = -2.887 Critical value 10% = -2.58 => P-Value = 0.0. Rejecting Null Hypothesis. => Series is Stationary.
Augmented Dickey-Fuller Test on "gdfcf" ----------------------------------------------- Null Hypothesis: Data has unit root. Non-Stationary. Significance Level = 0.05 Test Statistic = -6.922 No. Lags Chosen = 5 Critical value 1% = -3.491 Critical value 5% = -2.888 Critical value 10% = -2.581 => P-Value = 0.0. Rejecting Null Hypothesis. => Series is Stationary.
Augmented Dickey-Fuller Test on "gdfce" ----------------------------------------------- Null Hypothesis: Data has unit root. Non-Stationary. Significance Level = 0.05 Test Statistic = -5.1732 No. Lags Chosen = 8 Critical value 1% = -3.492 Critical value 5% = -2.889 Critical value 10% = -2.581 => P-Value = 0.0. Rejecting Null Hypothesis. => Series is Stationary.
# step7:选择模型阶数并训练,根据AIC值,lag=4时达到局部最优model =VAR(df_differenced)for i in [1,2,3,4,5,6,7,8,9]: result = model.fit(i)print('Lag Order =', i)print('AIC : ', result.aic, '\n')
Lag Order = 1
AIC : -1.3679402315450668
Lag Order = 2
AIC : -1.621237394447824
Lag Order = 3
AIC : -1.76580083870128
Lag Order = 4
AIC : -2.000735164470319
Lag Order = 5
AIC : -1.9619535608363936
Lag Order = 6
AIC : -2.3303386524829053
Lag Order = 7
AIC : -2.592331352347122
Lag Order = 8
AIC : -3.3172619764582087
Lag Order = 9
AIC : -4.804763125958635