VARモデル

『Advanced Python 時系列解析』より
In [1]:
import statsmodels.api as sm
import pandas as pd

サンプルデータ用意

In [2]:
df_data = sm.datasets.macrodata.load_pandas().data
df_data.head()
Out[2]:
year quarter realgdp realcons realinv realgovt realdpi cpi m1 tbilrate unemp pop infl realint
0 1959.0 1.0 2710.349 1707.4 286.898 470.045 1886.9 28.98 139.7 2.82 5.8 177.146 0.00 0.00
1 1959.0 2.0 2778.801 1733.7 310.859 481.301 1919.7 29.15 141.7 3.08 5.1 177.830 2.34 0.74
2 1959.0 3.0 2775.488 1751.8 289.226 491.260 1916.4 29.35 140.5 3.82 5.3 178.657 2.74 1.09
3 1959.0 4.0 2785.204 1753.7 299.356 484.052 1931.3 29.37 140.0 4.33 5.6 179.386 0.27 4.06
4 1960.0 1.0 2847.699 1770.5 331.722 462.199 1955.5 29.54 139.6 3.50 5.2 180.007 2.31 1.19
  • realgdb:実質GPD
  • realcons:実質個人消費支出
  • realivn:実質設備投資

上記3変数を用いて、VARモデルを作成

In [5]:
# indexを年月日に変換
df_data.index = pd.date_range('1959', periods=51*4-1, freq='Q')

y = df_data[['realgdp', 'realcons', 'realinv']]
In [6]:
y
Out[6]:
realgdp realcons realinv
1959-03-31 2710.349 1707.4 286.898
1959-06-30 2778.801 1733.7 310.859
1959-09-30 2775.488 1751.8 289.226
1959-12-31 2785.204 1753.7 299.356
1960-03-31 2847.699 1770.5 331.722
... ... ... ...
2008-09-30 13324.600 9267.7 1990.693
2008-12-31 13141.920 9195.3 1857.661
2009-03-31 12925.410 9209.2 1558.494
2009-06-30 12901.504 9189.0 1456.678
2009-09-30 12990.341 9256.0 1486.398

203 rows × 3 columns

ADF検定により、各変数が単位根過程であるか確認

In [7]:
from statsmodels.tsa import stattools
In [8]:
# ADF検定
ctt_realgdp = stattools.adfuller(y.realgdp, regression='ctt')
ctt_realcons = stattools.adfuller(y.realcons, regression='ctt')
ctt_realinv = stattools.adfuller(y.realinv, regression='ctt')

print('ctt realgdp:')
print(ctt_realgdp)
print('ctt realcons:')
print(ctt_realcons)
print('ctt realinv:')
print(ctt_realinv)
ctt realgdp:
(-2.231625563440861, 0.7145024076165514, 12, 190, {'1%': -4.433161444379647, '5%': -3.8638358600379066, '10%': -3.572676939641347}, 2030.9096153927815)
ctt realcons:
(-3.0087027982374197, 0.2910505147103153, 3, 199, {'1%': -4.430309046694293, '5%': -3.8623972900169137, '10%': -3.5717916732395594}, 1787.2221616792606)
ctt realinv:
(-1.63192154661618, 0.9226177810403438, 3, 199, {'1%': -4.430309046694293, '5%': -3.8623972900169137, '10%': -3.5717916732395594}, 1945.7305980020012)
  • 2つ目がp値
  • 3つともp値が高く3変数すべてが単位根過程であることがわかる

定常過程にするために2次の階差をとり、再度、単位根検定を実施

In [9]:
# 2次の階差
y_diff = y.diff().diff().dropna()
In [10]:
# ADF検定
ctt_realgdp_diff = stattools.adfuller(y_diff.realgdp, regression='ctt')
ctt_realcons_diff = stattools.adfuller(y_diff.realcons, regression='ctt')
ctt_realinv_diff = stattools.adfuller(y_diff.realinv, regression='ctt')

print('ctt realgdp_diff:')
print(ctt_realgdp_diff)
print('ctt realcons_diff:')
print(ctt_realcons_diff)
print('ctt realinv_diff:')
print(ctt_realinv_diff)
ctt realgdp_diff:
(-5.7307111708532315, 4.5129005984889235e-05, 14, 186, {'1%': -4.434519413562635, '5%': -3.864520481428023, '10%': -3.5730981406825575}, 2025.6464949041335)
ctt realcons_diff:
(-16.930875191757355, 7.34261860797005e-25, 1, 199, {'1%': -4.430309046694293, '5%': -3.8623972900169137, '10%': -3.5717916732395594}, 1792.6484152650387)
ctt realinv_diff:
(-8.24609938470198, 8.39040862417975e-11, 7, 193, {'1%': -4.432180562792867, '5%': -3.8633412462204713, '10%': -3.5723725968816216}, 1936.5507934933223)
  • 3つすべてp値が閾値より小さいので、2次の階差をとることで単位根過程でなくなった

VARモデル作成

In [11]:
from statsmodels.tsa.api import VAR
In [12]:
# モデル作成
model_diff = VAR(y_diff)
In [13]:
# ラグ探索
model_diff.select_order(10).summary()
Out[13]:
VAR Order Selection (* highlights the minimums)
AIC BIC FPE HQIC
0 22.00 22.05 3.570e+09 22.02
1 21.20 21.40 1.607e+09 21.28
2 20.73 21.09* 1.007e+09 20.87
3 20.61 21.12 8.953e+08 20.82*
4 20.58 21.25 8.701e+08 20.85
5 20.55 21.37 8.406e+08 20.88
6 20.59 21.56 8.748e+08 20.98
7 20.61 21.74 8.983e+08 21.07
8 20.50 21.77 8.009e+08 21.01
9 20.49* 21.92 7.995e+08* 21.07
10 20.55 22.14 8.505e+08 21.19
  • AICでは、次数9のときに最適
  • しかし、statsmodelsのVARモデルには自動で最適なラグの次数を選択してくれる機能があるので、それを利用する
In [14]:
# AIC基準で最適なハイパーパラメータを選択したモデルのあてはめ
result_diff = model_diff.fit(maxlags=10, ic='aic')
In [15]:
# あてはめ結果
result_diff.summary()
Out[15]:
  Summary of Regression Results   
==================================
Model:                         VAR
Method:                        OLS
Date:           Mon, 08, Nov, 2021
Time:                     22:34:12
--------------------------------------------------------------------
No. of Equations:         3.00000    BIC:                    21.9079
Nobs:                     192.000    HQIC:                   21.0600
Log likelihood:          -2699.66    FPE:                7.91212e+08
AIC:                      20.4828    Det(Omega_mle):     5.25930e+08
--------------------------------------------------------------------
Results for equation realgdp
==============================================================================
                 coefficient       std. error           t-stat            prob
------------------------------------------------------------------------------
const               0.352231         3.570180            0.099           0.921
L1.realgdp         -1.247885         0.162953           -7.658           0.000
L1.realcons         1.084603         0.212161            5.112           0.000
L1.realinv          0.348446         0.200801            1.735           0.083
L2.realgdp         -1.113375         0.227777           -4.888           0.000
L2.realcons         1.458917         0.274373            5.317           0.000
L2.realinv          0.214894         0.256314            0.838           0.402
L3.realgdp         -1.201289         0.264611           -4.540           0.000
L3.realcons         1.643437         0.313106            5.249           0.000
L3.realinv          0.258869         0.297026            0.872           0.383
L4.realgdp         -1.158100         0.287695           -4.025           0.000
L4.realcons         1.540831         0.335685            4.590           0.000
L4.realinv          0.495671         0.319816            1.550           0.121
L5.realgdp         -1.114796         0.302466           -3.686           0.000
L5.realcons         1.198746         0.343534            3.489           0.000
L5.realinv          0.410151         0.331977            1.235           0.217
L6.realgdp         -1.140106         0.304264           -3.747           0.000
L6.realcons         1.430550         0.342515            4.177           0.000
L6.realinv          0.520990         0.331061            1.574           0.116
L7.realgdp         -1.169328         0.285298           -4.099           0.000
L7.realcons         1.446455         0.334971            4.318           0.000
L7.realinv          0.669876         0.312499            2.144           0.032
L8.realgdp         -0.935261         0.254398           -3.676           0.000
L8.realcons         0.921095         0.305941            3.011           0.003
L8.realinv          0.421465         0.277929            1.516           0.129
L9.realgdp         -0.440651         0.180958           -2.435           0.015
L9.realcons         0.524468         0.237984            2.204           0.028
L9.realinv          0.315725         0.205328            1.538           0.124
==============================================================================

Results for equation realcons
==============================================================================
                 coefficient       std. error           t-stat            prob
------------------------------------------------------------------------------
const               1.195015         2.018297            0.592           0.554
L1.realgdp         -0.203873         0.092121           -2.213           0.027
L1.realcons        -0.566795         0.119939           -4.726           0.000
L1.realinv          0.245954         0.113517            2.167           0.030
L2.realgdp         -0.361597         0.128767           -2.808           0.005
L2.realcons        -0.200141         0.155109           -1.290           0.197
L2.realinv          0.376111         0.144900            2.596           0.009
L3.realgdp         -0.547475         0.149590           -3.660           0.000
L3.realcons         0.210802         0.177005            1.191           0.234
L3.realinv          0.484738         0.167915            2.887           0.004
L4.realgdp         -0.757074         0.162640           -4.655           0.000
L4.realcons         0.357161         0.189770            1.882           0.060
L4.realinv          0.777500         0.180798            4.300           0.000
L5.realgdp         -0.738767         0.170990           -4.321           0.000
L5.realcons         0.335693         0.194207            1.729           0.084
L5.realinv          0.744021         0.187674            3.964           0.000
L6.realgdp         -0.662846         0.172007           -3.854           0.000
L6.realcons         0.393409         0.193631            2.032           0.042
L6.realinv          0.638098         0.187156            3.409           0.001
L7.realgdp         -0.751278         0.161285           -4.658           0.000
L7.realcons         0.579045         0.189366            3.058           0.002
L7.realinv          0.687395         0.176662            3.891           0.000
L8.realgdp         -0.543194         0.143816           -3.777           0.000
L8.realcons         0.335079         0.172955            1.937           0.053
L8.realinv          0.511003         0.157119            3.252           0.001
L9.realgdp         -0.173919         0.102299           -1.700           0.089
L9.realcons         0.098180         0.134538            0.730           0.466
L9.realinv          0.220091         0.116076            1.896           0.058
==============================================================================

Results for equation realinv
==============================================================================
                 coefficient       std. error           t-stat            prob
------------------------------------------------------------------------------
const              -1.278235         2.560032           -0.499           0.618
L1.realgdp         -0.063357         0.116847           -0.542           0.588
L1.realcons         0.745145         0.152132            4.898           0.000
L1.realinv         -0.746566         0.143986           -5.185           0.000
L2.realgdp          0.051753         0.163329            0.317           0.751
L2.realcons         0.828240         0.196742            4.210           0.000
L2.realinv         -0.827122         0.183792           -4.500           0.000
L3.realgdp         -0.055092         0.189742           -0.290           0.772
L3.realcons         0.731903         0.224516            3.260           0.001
L3.realinv         -0.673629         0.212985           -3.163           0.002
L4.realgdp         -0.046314         0.206294           -0.225           0.822
L4.realcons         0.745184         0.240706            3.096           0.002
L4.realinv         -0.481416         0.229327           -2.099           0.036
L5.realgdp         -0.119902         0.216886           -0.553           0.580
L5.realcons         0.460269         0.246334            1.868           0.062
L5.realinv         -0.407875         0.238048           -1.713           0.087
L6.realgdp         -0.406428         0.218175           -1.863           0.062
L6.realcons         0.751209         0.245604            3.059           0.002
L6.realinv         -0.018668         0.237391           -0.079           0.937
L7.realgdp         -0.386427         0.204576           -1.889           0.059
L7.realcons         0.613108         0.240195            2.553           0.011
L7.realinv          0.069317         0.224081            0.309           0.757
L8.realgdp         -0.322333         0.182419           -1.767           0.077
L8.realcons         0.336192         0.219378            1.532           0.125
L8.realinv         -0.084225         0.199292           -0.423           0.673
L9.realgdp         -0.215587         0.129757           -1.661           0.097
L9.realcons         0.200933         0.170649            1.177           0.239
L9.realinv          0.080880         0.147233            0.549           0.583
==============================================================================

Correlation matrix of residuals
             realgdp  realcons   realinv
realgdp     1.000000  0.516204  0.692511
realcons    0.516204  1.000000 -0.029880
realinv     0.692511 -0.029880  1.000000

  • summaryより、各変数に対する係数が9個づつあるので、ラグの次数9が選択されたことがわかる

予測誤差分散分解(FEVD)

  • FEVDを用いて、i次点先予測時の誤差の起因の程度を分解する
  • 誤差の起因の程度を分解することで、各変数に対して他のどの変数がどの程度の説明力を持っているかを知ることができる
In [16]:
fevd_diff = result_diff.fevd(4) # 3次点先までの誤差の起因
fevd_diff.summary()
FEVD for realgdp
      realgdp  realcons   realinv
0    1.000000  0.000000  0.000000
1    0.892571  0.096255  0.011175
2    0.887540  0.097189  0.015271
3    0.883109  0.095237  0.021655

FEVD for realcons
      realgdp  realcons   realinv
0    0.266466  0.733534  0.000000
1    0.283437  0.697520  0.019043
2    0.282337  0.698602  0.019061
3    0.281744  0.698428  0.019828

FEVD for realinv
      realgdp  realcons   realinv
0    0.479571  0.204551  0.315877
1    0.289142  0.461684  0.249174
2    0.292990  0.459687  0.247323
3    0.305582  0.449273  0.245145


  • 3次点先のFEVDの数値を見ると、realgdp(実質GDP)およびrealcons(実質個人消費支出)において、それぞれ0.883、0.698と自身のウェイトが高いが、realinv(実質設備投資)では、realgdpが0.305、relaconsが0.449となっており、realinvの予測誤差に対してrealgdpとrealconsの説明力が相対的に大きいことがわかる
In [17]:
# FEVDの可視化
result_diff.fevd(4).plot()
Out[17]:
In [ ]:
 
In [ ]: