In [1]:
%matplotlib inline

import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt

from pandas_datareader.data import DataReader
C:\Users\Marcin\Anaconda3\lib\site-packages\statsmodels\compat\pandas.py:56: FutureWarning: The pandas.core.datetools module is deprecated and will be removed in a future version. Please use the pandas.tseries module instead.
  from pandas.core import datetools
In [2]:
from matplotlib import rcParams

# Restore old behavior of rounding default axis ranges
rcParams['axes.autolimit_mode'] = 'round_numbers'
rcParams['axes.xmargin'] = 0
rcParams['axes.ymargin'] = 0
In [3]:
dta_pl = pd.read_stata('data/data_PL.dta')
dta_pl.head()
Out[3]:
WIB3M HICP GDP t REER
0 22.15 17.233333 6.2 1 7.863447
1 22.50 15.066667 5.5 2 6.201926
2 25.01 14.666667 5.5 3 6.886372
3 24.99 13.166667 10.8 4 3.212281
4 24.90 13.900000 6.3 5 5.660251
In [4]:
yy = (2000+(dta_pl['t']-1)//4).astype(str)
qq = (1+(dta_pl['t']-1)%4).astype(str)
date = yy+'Q'+qq

from statsmodels.tsa.base.datetools import dates_from_str
date = dates_from_str(date)

dta_pl.index = date

dta_pl.head()
Out[4]:
WIB3M HICP GDP t REER
2000-03-31 22.15 17.233333 6.2 1 7.863447
2000-06-30 22.50 15.066667 5.5 2 6.201926
2000-09-30 25.01 14.666667 5.5 3 6.886372
2000-12-31 24.99 13.166667 10.8 4 3.212281
2001-03-31 24.90 13.900000 6.3 5 5.660251
In [5]:
model_pl = sm.tsa.VAR(dta_pl[['GDP','HICP','WIB3M']])
In [6]:
results_pl = model_pl.fit(4)
results_pl.summary()
C:\Users\Marcin\Anaconda3\lib\site-packages\statsmodels\tsa\vector_ar\var_model.py:461: FutureWarning: `rcond` parameter will change to the default of machine precision times ``max(M, N)`` where M and N are the input matrix dimensions.
To use the future default and silence this warning we advise to pass `rcond=None`, to keep using the old, explicitly pass `rcond=-1`.
  params = np.linalg.lstsq(z, y_sample)[0]
Out[6]:
  Summary of Regression Results   
==================================
Model:                         VAR
Method:                        OLS
Date:           Tue, 22, May, 2018
Time:                     12:59:35
--------------------------------------------------------------------
No. of Equations:         3.00000    BIC:                 -0.0443290
Nobs:                     66.0000    HQIC:                 -0.826940
Log likelihood:          -197.789    FPE:                   0.266451
AIC:                     -1.33822    Det(Omega_mle):        0.155370
--------------------------------------------------------------------
Results for equation GDP
===========================================================================
              coefficient       std. error           t-stat            prob
---------------------------------------------------------------------------
const            0.877186         0.284612            3.082           0.003
L1.GDP           0.623095         0.086485            7.205           0.000
L1.HICP          0.037303         0.150282            0.248           0.805
L1.WIB3M         0.186415         0.140886            1.323           0.191
L2.GDP           0.321833         0.109642            2.935           0.005
L2.HICP         -0.196321         0.211242           -0.929           0.357
L2.WIB3M        -0.249058         0.195896           -1.271           0.209
L3.GDP           0.228032         0.105036            2.171           0.034
L3.HICP         -0.102674         0.213589           -0.481           0.633
L3.WIB3M        -0.448083         0.192818           -2.324           0.024
L4.GDP          -0.337257         0.085608           -3.940           0.000
L4.HICP          0.237544         0.132955            1.787           0.080
L4.WIB3M         0.467334         0.116604            4.008           0.000
===========================================================================

Results for equation HICP
===========================================================================
              coefficient       std. error           t-stat            prob
---------------------------------------------------------------------------
const           -0.339084         0.288048           -1.177           0.244
L1.GDP           0.259376         0.087529            2.963           0.005
L1.HICP          1.082886         0.152096            7.120           0.000
L1.WIB3M         0.245151         0.142587            1.719           0.091
L2.GDP          -0.116663         0.110966           -1.051           0.298
L2.HICP         -0.349361         0.213792           -1.634           0.108
L2.WIB3M        -0.209264         0.198260           -1.055           0.296
L3.GDP           0.025530         0.106304            0.240           0.811
L3.HICP          0.088260         0.216168            0.408           0.685
L3.WIB3M        -0.042529         0.195146           -0.218           0.828
L4.GDP          -0.018845         0.086642           -0.218           0.829
L4.HICP          0.061003         0.134561            0.453           0.652
L4.WIB3M         0.021787         0.118012            0.185           0.854
===========================================================================

Results for equation WIB3M
===========================================================================
              coefficient       std. error           t-stat            prob
---------------------------------------------------------------------------
const            0.038174         0.310664            0.123           0.903
L1.GDP           0.117129         0.094401            1.241           0.220
L1.HICP          0.302660         0.164038            1.845           0.071
L1.WIB3M         1.248295         0.153782            8.117           0.000
L2.GDP          -0.089837         0.119678           -0.751           0.456
L2.HICP         -0.155077         0.230578           -0.673           0.504
L2.WIB3M        -0.189867         0.213827           -0.888           0.379
L3.GDP          -0.031610         0.114651           -0.276           0.784
L3.HICP         -0.294784         0.233140           -1.264           0.212
L3.WIB3M        -0.308431         0.210468           -1.465           0.149
L4.GDP           0.048748         0.093444            0.522           0.604
L4.HICP          0.230133         0.145126            1.586           0.119
L4.WIB3M         0.157305         0.127277            1.236           0.222
===========================================================================

Correlation matrix of residuals
              GDP      HICP     WIB3M
GDP      1.000000  0.118820  0.316870
HICP     0.118820  1.000000  0.448047
WIB3M    0.316870  0.448047  1.000000

In [7]:
irf_pl = results_pl.irf(20)
irf_pl.plot(orth=True, impulse='WIB3M', signif=0.1)
In [8]:
fred = DataReader(['GDPC1','GDPDEF','FEDFUNDS','M2SL'], 'fred', start='1945', end='2020')
In [9]:
temp = fred.resample('QS').mean()

iacoviel = 100*np.log(temp).diff(4)
iacoviel['FEDFUNDS'] = temp['FEDFUNDS']
iacoviel = iacoviel.dropna()

iacoviel.columns = ['Y','P','R','M']
iacoviel.head()
Out[9]:
Y P R M
DATE
1960-01-01 4.805450 1.319235 3.933333 3.705812
1960-04-01 2.022658 1.437794 3.696667 3.023212
1960-07-01 2.464116 1.426788 2.936667 3.441176
1960-10-01 0.853795 1.341532 2.296667 4.539756
1961-01-01 -0.671797 1.148748 2.003333 5.725141
In [10]:
fig, ((ax1, ax2), (ax3, ax4)) = plt.subplots(2, 2, figsize=(14, 9))

iacoviel['Y'].to_period('D').plot(ax=ax1)
iacoviel['P'].to_period('D').plot(ax=ax2)
iacoviel['M'].to_period('D').plot(ax=ax3)
iacoviel['R'].to_period('D').plot(ax=ax4)

plt.show()
In [11]:
model = sm.tsa.VAR(iacoviel[['Y','P','R','M']])
model.select_order(8)
                 VAR Order Selection                 
=====================================================
           aic          bic          fpe         hqic
-----------------------------------------------------
0        6.657        6.717        778.0        6.681
1       -2.642       -2.339      0.07120       -2.520
2       -3.362      -2.815*      0.03469       -3.141
3       -3.429       -2.640      0.03243       -3.111
4       -3.415       -2.383      0.03291       -2.998
5       -3.762       -2.487      0.02328       -3.248
6      -3.943*       -2.425     0.01946*      -3.330*
7       -3.923       -2.162      0.01989       -3.212
8       -3.915       -1.911      0.02010       -3.106
=====================================================
* Minimum

C:\Users\Marcin\Anaconda3\lib\site-packages\statsmodels\tsa\vector_ar\var_model.py:461: FutureWarning: `rcond` parameter will change to the default of machine precision times ``max(M, N)`` where M and N are the input matrix dimensions.
To use the future default and silence this warning we advise to pass `rcond=None`, to keep using the old, explicitly pass `rcond=-1`.
  params = np.linalg.lstsq(z, y_sample)[0]
Out[11]:
{'aic': 6, 'bic': 2, 'fpe': 6, 'hqic': 6}
In [12]:
results = model.fit(6)
C:\Users\Marcin\Anaconda3\lib\site-packages\statsmodels\tsa\vector_ar\var_model.py:461: FutureWarning: `rcond` parameter will change to the default of machine precision times ``max(M, N)`` where M and N are the input matrix dimensions.
To use the future default and silence this warning we advise to pass `rcond=None`, to keep using the old, explicitly pass `rcond=-1`.
  params = np.linalg.lstsq(z, y_sample)[0]
In [13]:
results.irf(20).plot()
plt.show()
In [14]:
results.irf(20).plot(orth=True, impulse='R', signif=0.05)
plt.savefig('Simple_VAR.pdf', transparent=True, bbox_inches='tight', pad_inches=0.05)
In [15]:
results.plot_forecast(4)