Markov-Switching VAR
Python · R · Part 11 of the VAR arc — data-discovered regimes
Overview
Part 11 of the VAR arc: discrete regimes, discovered by the data. Part 5's time-varying-parameter VAR let the economy's dynamics drift smoothly. But some changes are not gradual — they are switches: the economy flips between a calm state and a turbulent one, between expansion and recession. The Markov-switching VAR (Hamilton 1989; Krolzig 1997) models exactly that — the VAR's parameters jump between a small number of regimes governed by a latent Markov chain the data infer. Where Part 10 imposed the Great-Moderation break to identify shocks, the MS-VAR is handed no dates at all: it discovers the regimes, and rediscovers 1984 on its own. Built from scratch with a Gibbs sampler whose centrepiece is the forward-filter–backward-sample of the hidden regime path.
An MSIH-VAR and its forward-filter–backward-sample Gibbs
An MSIH-VAR — switching intercept and covariance, constant autoregressive dynamics — where is a latent Markov chain with transition matrix whose diagonal governs how persistent the regimes are. The from-scratch Gibbs sampler cycles three blocks: (1) the regime path by forward-filter–backward-sample — run the Hamilton filter forward (predict through , update with each regime's VAR likelihood), then sample backwards then down to — the discrete-state analogue of the Carter–Kohn smoother; (2) the parameters — given , each regime's intercept and covariance are conjugate Normal / inverse-Wishart draws and the constant lag matrix a GLS draw across regimes; (3) the transition matrix — each row a Dirichlet draw from the counted transitions. Label-switching is fixed by ordering regimes by volatility (calm vs turbulent), on the same monetary system as Parts 4/10 (GDP growth, inflation, funds rate), , .
Results
The Great Moderation, rediscovered without a date. The turbulent (high-volatility) regime is active 40% of the time before 1984 but only 3% after — the model draws the Great-Moderation line on its own, precisely where Part 10 had to assume it, and the calm regime once entered essentially never leaves (bar 2008). Turbulent = volatile and recessionary: the turbulent regime carries 2–4× the shock volatility of the calm one (GDP-growth std 5.1 vs 2.4) and its smoothed probability spikes in the NBER recessions (mean 0.57 in recessions vs 0.13 in expansions) and across the high-inflation 1970s — the postwar economy's "stress" state. Persistence differs sharply: the transition matrix implies the calm regime lasts ~26 quarters on average and the turbulent one ~6 — long stable stretches punctuated by shorter bouts of turbulence. Switches, not drift: where Part 5 asked how much has the world drifted?, the MS-VAR answers which regime are we in right now?, delivering a probabilistic, dated chronology of postwar macro that a smooth-drift model cannot — and acting as an endogenous break detector that locates the Great Moderation with no prior information. Together, Parts 5, 10 and 11 span the ways a VAR can be non-constant: smooth drift, imposed regimes, and data-discovered regimes. Cross-checked in R (MSwM).
Notebooks
Downloads
msvar.py The from-scratch MS-VAR module — Hamilton filter, forward-filter–backward-sample of the regime path, and the conjugate parameter / Dirichlet transition draws (new self-contained module) bvar_sv_data.csv The Part 4 monetary system — US quarterly GDP growth, GDP-deflator inflation, and the Fed funds rate, 1959–2019 (FRED-QD)
References
- Hamilton, J. D. (1989). A new approach to the economic analysis of nonstationary time series and the business cycle. Econometrica 57(2), 357–384. — the Markov-switching model and the Hamilton filter
- Krolzig, H.-M. (1997). Markov-Switching Vector Autoregressions. Springer. — the MSIH-VAR specification reproduced here
- Chib, S. (1996). Calculating posterior distributions and modal estimates in Markov mixture models. Journal of Econometrics 75(1), 79–97. — the forward-filter–backward-sample of the regime path
- Carter, C. K. & Kohn, R. (1994). On Gibbs sampling for state space models. Biometrika 81(3), 541–553. — the continuous-state smoother the FFBS is the discrete analogue of
- Kim, C.-J. & Nelson, C. R. (1999). State-Space Models with Regime Switching. MIT Press. — Bayesian estimation of regime-switching state-space models
- Sims, C. A. & Zha, T. (2006). Were there regime switches in U.S. monetary policy? American Economic Review 96(1), 54–81. — Markov-switching VARs for postwar US macro
- Sanchez-Espigares, J. A. & Lopez-Moreno, A. (2021). MSwM: Fitting Markov Switching Models. R package. — the R cross-check implementation