bayesian_vecm#
Bayesian Vector Error Correction Models (VECM) in Python, built on PyMC.
A VECM captures the long-run relationships between trending series and the short-run dynamics that pull them back when they drift apart. This package fits it in full Bayes: posterior uncertainty over every parameter, optional horseshoe shrinkage on the lags, and impulse responses with credible bands.
The main use is brand marketing. Brand metrics and sales move together over years, so spend that lifts awareness can lift sales for good. A media mix model whose effects fade within weeks misses that long-run tail.
Built on Peter Cain’s work. The method follows Cain (2022): Modelling short- and long-term marketing effects in the consumer purchase journey in the International Journal of Research in Marketing 39(1). Two ideas come from that paper. Brand metrics are tied to base sales by cointegration. Same-week links between them are estimated by instruments.
Cain (2025), Long-term advertising effects: The Adstock illusion, explains why a long adstock cannot stand in for brand building.
pip install bayesian_vecm
from bayesian_vecm import BayesianVECM, simulate_brand_data
data = simulate_brand_data()
model = BayesianVECM(k_ar_diff=1, coint_rank=2, deterministic="co")
model.fit(data.endog, exog=data.exog)
irf = model.irf(steps=52, method="girf")
Where to start#
The walkthrough: How to Measure Long-Term Brand Effects When Your MMM Can’t. The article that explains the method step by step, checked on simulated data.
The user guide: the practitioner notebook and pages on choosing the rank and lags and the Granger pretest.
The practitioner guide: the full workflow in a notebook, from data to return per channel.
API reference: every public function and class.
How it was built: one notebook per feature, in the order they were built.