%matplotlib inline
from __future__ import division
import numpy as np
import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
from pandas_datareader.data import DataReader
# Get the Hamilton dataset
from statsmodels.tsa.regime_switching.tests.test_markov_autoregression import rgnp
dta_hamilton = pd.Series(rgnp, index=pd.date_range('1951-04-01', '1984-10-01', freq='QS'), name='Hamilton')
# Get the updated dataset from FRED
gnp = DataReader('GNPC96', 'fred', start='1947-01-01', end='2016-10-01').resample('QS').mean()
dlngnp = np.log(gnp['GNPC96']).diff().iloc[1:] * 100
dta_updated = dlngnp.loc['1951-04-01':'1984-10-01']
# NBER recessions
usrec = DataReader('USREC', 'fred', start='1952-04-01', end='1984-10-01').resample('MS').last()['USREC']
# Plot the datasets
fig, ax = plt.subplots(figsize=(13, 4))
ax.plot(dta_hamilton)
ax.plot(dta_updated)
ax.fill_between(usrec.index, -3, 4, where=usrec.values, color='grey', alpha=0.4)
ax.legend();
First, estimate the model using Hamilton's (1989) dataset
mod_hamilton = sm.tsa.MarkovAutoregression(dta_hamilton, k_regimes=2, order=4, switching_ar=False)
res_hamilton = mod_hamilton.fit()
print(res_hamilton.summary())
Markov Switching Model Results ================================================================================ Dep. Variable: Hamilton No. Observations: 131 Model: MarkovAutoregression Log Likelihood -181.263 Date: Sun, 02 Apr 2017 AIC 380.527 Time: 19:52:31 BIC 406.404 Sample: 04-01-1951 HQIC 391.042 - 10-01-1984 Covariance Type: approx Regime 0 parameters ============================================================================== coef std err z P>|z| [0.025 0.975] ------------------------------------------------------------------------------ const -0.3588 0.265 -1.356 0.175 -0.877 0.160 Regime 1 parameters ============================================================================== coef std err z P>|z| [0.025 0.975] ------------------------------------------------------------------------------ const 1.1635 0.075 15.614 0.000 1.017 1.310 Non-switching parameters ============================================================================== coef std err z P>|z| [0.025 0.975] ------------------------------------------------------------------------------ sigma2 0.5914 0.103 5.761 0.000 0.390 0.793 ar.L1 0.0135 0.120 0.112 0.911 -0.222 0.249 ar.L2 -0.0575 0.138 -0.418 0.676 -0.327 0.212 ar.L3 -0.2470 0.107 -2.310 0.021 -0.457 -0.037 ar.L4 -0.2129 0.111 -1.926 0.054 -0.430 0.004 Regime transition parameters ============================================================================== coef std err z P>|z| [0.025 0.975] ------------------------------------------------------------------------------ p[0->0] 0.7547 0.097 7.819 0.000 0.565 0.944 p[1->0] 0.0959 0.038 2.542 0.011 0.022 0.170 ============================================================================== Warnings: [1] Covariance matrix calculated using numerical differentiation.
ax = res_hamilton.smoothed_marginal_probabilities[0].plot(figsize=(8, 4))
ax.fill_between(usrec.index, 0, 1, where=usrec.values, color='grey', alpha=0.4)
ax.set(title='Parameters and regime probabilities from Hamilton');
Now, we estimate the model using the updated dataset.
mod_updated = sm.tsa.MarkovAutoregression(endog, k_regimes=2, order=4, switching_ar=False)
res_updated = mod_updated.fit()
print(res_updated.summary())
Markov Switching Model Results ================================================================================ Dep. Variable: GNPC96 No. Observations: 131 Model: MarkovAutoregression Log Likelihood -188.002 Date: Sun, 02 Apr 2017 AIC 394.005 Time: 20:00:58 BIC 419.882 Sample: 04-01-1951 HQIC 404.520 - 10-01-1984 Covariance Type: approx Regime 0 parameters ============================================================================== coef std err z P>|z| [0.025 0.975] ------------------------------------------------------------------------------ const -1.2475 3.470 -0.359 0.719 -8.049 5.554 Regime 1 parameters ============================================================================== coef std err z P>|z| [0.025 0.975] ------------------------------------------------------------------------------ const 0.9364 0.453 2.066 0.039 0.048 1.825 Non-switching parameters ============================================================================== coef std err z P>|z| [0.025 0.975] ------------------------------------------------------------------------------ sigma2 0.8509 0.561 1.516 0.130 -0.249 1.951 ar.L1 0.3437 0.189 1.821 0.069 -0.026 0.714 ar.L2 0.0919 0.143 0.645 0.519 -0.187 0.371 ar.L3 -0.0846 0.251 -0.337 0.736 -0.577 0.408 ar.L4 -0.1727 0.258 -0.669 0.503 -0.678 0.333 Regime transition parameters ============================================================================== coef std err z P>|z| [0.025 0.975] ------------------------------------------------------------------------------ p[0->0] 0.0002 1.705 0.000 1.000 -3.341 3.341 p[1->0] 0.0397 0.186 0.213 0.831 -0.326 0.405 ============================================================================== Warnings: [1] Covariance matrix calculated using numerical differentiation.
ax = res_updated.smoothed_marginal_probabilities[0].plot(figsize=(8, 4))
ax.fill_between(usrec.index, 0, 1, where=usrec.values, color='grey', alpha=0.4)
ax.set(title='Parameters and regime probabilities from updated GNP');
Here we use the parameters that we found estimated using the Hamilton (1989) dataset, but apply them to the updated dataset. Two results:
res_updated2 = mod.smooth(res_hamilton.params)
print(res_updated2.summary())
Markov Switching Model Results ================================================================================ Dep. Variable: GNPC96 No. Observations: 131 Model: MarkovAutoregression Log Likelihood -191.807 Date: Sun, 02 Apr 2017 AIC 401.614 Time: 19:52:52 BIC 427.491 Sample: 04-01-1951 HQIC 412.129 - 10-01-1984 Covariance Type: opg Regime 0 parameters ============================================================================== coef std err z P>|z| [0.025 0.975] ------------------------------------------------------------------------------ const -0.3588 0.185 -1.939 0.053 -0.722 0.004 Regime 1 parameters ============================================================================== coef std err z P>|z| [0.025 0.975] ------------------------------------------------------------------------------ const 1.1635 0.083 13.967 0.000 1.000 1.327 Non-switching parameters ============================================================================== coef std err z P>|z| [0.025 0.975] ------------------------------------------------------------------------------ sigma2 0.5914 0.090 6.604 0.000 0.416 0.767 ar.L1 0.0135 0.100 0.134 0.893 -0.183 0.210 ar.L2 -0.0575 0.088 -0.651 0.515 -0.231 0.116 ar.L3 -0.2470 0.104 -2.384 0.017 -0.450 -0.044 ar.L4 -0.2129 0.084 -2.524 0.012 -0.378 -0.048 Regime transition parameters ============================================================================== coef std err z P>|z| [0.025 0.975] ------------------------------------------------------------------------------ p[0->0] 0.7547 0.100 7.563 0.000 0.559 0.950 p[1->0] 0.0959 0.051 1.872 0.061 -0.005 0.196 ============================================================================== Warnings: [1] Covariance matrix calculated using the outer product of gradients.
ax = res_updated2.smoothed_marginal_probabilities[0].plot(figsize=(8, 4))
ax.fill_between(usrec.index, 0, 1, where=usrec.values, color='grey', alpha=0.4)
ax.set(title='Parameters from Hamilton; regime probabilities from updated GNP');