Bayesian Vector Error Correction (VECM)
Python · R · Part 6 of the VAR arc — cointegration
Overview
Part 6 of the VAR arc: cointegration. Every VAR so far treated its variables as either stationary (growth rates) or, in the large BVAR, as unit-root levels shrunk toward random walks. Cointegration is the economically loaded case in between: several series each wander like a random walk (I(1)), yet specific linear combinations of them are stationary — pinned by a long-run equilibrium the economy is always pulled back toward. Difference the level VAR into error-correction form and a single levels term carries all the cointegration; its rank classifies the system. The reduced-rank factorization splits it into the cointegrating vectors (the stationary combinations that don't wander) and the adjustment loadings (the speed each variable is pulled back off-equilibrium). Only the column space of is identified, so a linear normalization makes the model conditionally linear — a three-block conjugate Gibbs sampler in a new from-scratch module (bvecm.py, leaving bvar.py untouched), with the frequentist Johansen trace test alongside as the rank reference.
A cointegration trilogy — the rank dictates the forecast
The example is built as a trilogy that walks the full spectrum of what a cointegration analysis can find — and shows that the right forecasting model follows directly from the rank. Cointegration is only defined for series that are individually I(1) but share a stationary combination, so every notebook pre-tests the integration order first (Augmented Dickey–Fuller) before running the sampler — the discipline that separates a real long-run equilibrium from a category error. The same machinery meets three verdicts: the term structure is genuinely cointegrated; the Fisher effect fails the premise (one series is already stationary); and money demand is a relation that once held but has broken down.
Results
Getting the rank right is getting the forecasting model right. (1) Term structure (6a): the four Treasury yields are all I(1) but share only two common stochastic trends — Johansen and the Bayesian VECM agree at , the spreads mean-revert as the expectations hypothesis predicts, and the front of the curve does most of the error-correcting. The forecasting lesson is don't over-difference: the VAR-in-differences is clearly worst, while the VECM and levels-VAR track together and win. (2) Fisher effect (6b): the nominal rate tests I(1) but inflation is already I(0) — cointegration is impossible, and forcing a "Fisher coefficient" gives an interval that excludes −1. A cautionary tale: the analysis is only as good as its pre-tests. (3) Money demand (6c): real M2, income and the rate are all genuinely I(1), yet Johansen cannot reject — the classic equilibrium has broken down (the forced error-correction term wanders like a unit root rather than reverting), so the textbook prescription holds and the VAR-in-differences forecasts best. Across all three, the Bayesian machine supplies the full posterior of the adjustment speeds and cointegrating space where Johansen offers only a sequence of tests; each is cross-checked in R with urca.
Notebooks
Downloads
bvecm.py The from-scratch reduced-rank VECM Gibbs sampler (new self-contained module; the bvar.py engine is left untouched) bvar_yields_data.csv US Treasury constant-maturity yields (3m, 5y, 10y, 30y), monthly 1994–2024 (FRED) — the term-structure application bvecm_fisher_data.csv The 3-month Treasury rate and annualised CPI inflation, quarterly 1959–2019 (FRED) — the Fisher application bvecm_money_data.csv Log real M2, log real GDP, and the 3-month rate, quarterly 1959–2019 (FRED) — the money-demand application References
- Engle, R. F. & Granger, C. W. J. (1987). Co-integration and error correction: representation, estimation, and testing. Econometrica 55(2), 251–276. — the error-correction representation of cointegrated systems
- Johansen, S. (1991). Estimation and hypothesis testing of cointegration vectors in Gaussian vector autoregressive models. Econometrica 59(6), 1551–1580. — the trace test and the reduced-rank framework; the R
urcareference - Villani, M. (2005). Bayesian reference analysis of cointegration. Econometric Theory 21(2), 326–357. — the linear normalization and Bayesian inference on the cointegrating space
- Koop, G., León-González, R. & Strachan, R. W. (2010). Efficient posterior simulation for cointegrated models with priors on the cointegration space. Journal of Econometrics 154(1), 4–15. — the reduced-rank Gibbs sampler this example builds from scratch
- Campbell, J. Y. & Shiller, R. J. (1987). Cointegration and tests of present value models. Journal of Political Economy 95(5), 1062–1088. — the expectations hypothesis of the term structure as cointegration
- Stock, J. H. & Watson, M. W. (1993). A simple estimator of cointegrating vectors in higher order integrated systems. Econometrica 61(4), 783–820. — cointegration and the money-demand relation
- Dickey, D. A. & Fuller, W. A. (1979). Distribution of the estimators for autoregressive time series with a unit root. JASA 74(366), 427–431. — the ADF integration-order pre-test