Skip to main content

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, so every parameter, impulse response and return has an interval.

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 marketing mix model whose effects fade within weeks misses that tail. The method follows Peter Cain's work:

This is hard econometrics. Unit roots, the cointegration rank, base sales, carryover, saturation, same-week links and whether brand lifts last can each go quietly wrong. The agent skill is set up to help: it walks an analyst who has never used a VECM through every step, in plain words, and stops at the decisions only they can make.

Resources

📚 Documentation User guide, API reference and model details
📖 Walkthrough How to Measure Long-Term Brand Effects When Your MMM Can't. The method step by step, on simulated data
🔌 Response curves on your data Plug-in notebook: set your columns in one cell, get a response curve and return per £ for each channel
📓 Practitioner walkthrough Data → rank → fit → impulse responses → counterfactual ROI
⚖️ Comparison with statsmodels Same estimates as statsmodels, with interval coverage
📈 Carryover and saturation adstock= and saturation=
🔗 Same-week links contemporaneous= and excluded=
🧩 Putting it together Adstock, saturation and a same-week link in one model
🧮 Base sales from total sales Which base the VECM needs when you only have total sales
📊 Base sales from an MMM Why an MMM's trend fails as base sales. Needs pymc-marketing
⏳ Does brand last? fading=: lifts that fade, going dark, and what the data can settle
🤖 Agent skill Instructions for an AI agent to run the analysis with an analyst
🧭 Choosing the rank and the lags Which tests to use and how far to trust them
🔎 The Granger pretest What helps predict what, before fitting
🔧 How it was built One notebook per feature, each checked on data with a known answer

Installation

pip install bayesian_vecm

Requires Python 3.12+.

Quick start

endog holds the log brand metrics and sales, exog the drivers, both weekly.

from statsmodels.tsa.api import VAR
from bayesian_vecm import BayesianVECM, granger_pretest, select_coint_rank, simulate_brand_data

data = simulate_brand_data()
endog, exog = data.endog, data.exog

# 1. What helps predict what (drivers, delays, feedback)
print(granger_pretest(endog, exog))

# 2. Lags by HQ, rank by Johansen
orders = VAR(endog, exog=exog).select_order(maxlags=8, trend="c")
k_ar_diff = max(orders.selected_orders["hqic"] - 1, 1)
rank = select_coint_rank(endog, k_ar_diff=k_ar_diff, det_order=0).rank

# 3. Fit
model = BayesianVECM(k_ar_diff=k_ar_diff, coint_rank=rank, deterministic="co")
model.fit(endog, exog=exog, target_accept=0.90)

# 4. Results
model.summary(var_names=["alpha", "beta"])  # beta: long-run relations, alpha: speed back
irf = model.irf(steps=52, method="girf")     # shocks to the brand metrics
response = model.exog_irf(steps=260)         # a one-week rise in each driver

Options

Argument Use it when
exog_lags=1 A driver acts with a delay (monthly data usually needs it)
adstock=[...] A channel's effect carries over for weeks
saturation=[...] One more £ buys less as spend grows. Pass raw, non-negative spend
contemporaneous=, excluded= One brand metric moves another in the same week. A wrong exclusion biases the link
fading=[...] A brand lift may drain away once spend stops. Estimates how much lasts; without it every lift lasts by assumption
priors={"Gamma": {"dist": "Horseshoe"}} The lag order is unclear. Fit with target_accept=0.99

For a counterfactual, call sample_posterior_predictive(steps, exog_future=..., random_seed=0) on a baseline and an uplift path with the same seed, and take the difference. The guides show each option on simulated data with a known answer.

Preparing variables

Variable Where Treatment
Trending brand metrics and sales endog Log levels
Brand channels (spend, reach) exog Deviation from the average, in round units (raw spend with saturation)
Stationary drivers exog Deviation from the average
Trending outside drivers exog Weekly change
Launches and events exog 0/1 dummies
  • check_stationarity sorts trending series into endog and stationary ones into exog.
  • A driver must not respond to the brand metrics. If budgets follow sales, spend's effect absorbs that feedback.
  • With total sales only, see the base sales guide.

Development

uv sync --all-extras
uv run pytest -m "not slow" -n auto   # seconds: everything that does not sample
uv run pytest -n auto --dist loadscope   # the full suite, including PyMC fits
uv run ruff check . && uv run ruff format .
uv build

License

MIT. See LICENSE.

Metadata

Release files for bayesian-vecm 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bayesian-vecm 0.1.0
File Size Uploaded Provenance Actions
bayesian_vecm-0.1.0.tar.gz 149.5 kB With provenance Details for bayesian_vecm-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for bayesian-vecm 0.1.0
File Interpreter ABI Platform Actions
bayesian_vecm-0.1.0-py3-none-any.whl Python 3 none any Details for bayesian_vecm-0.1.0-py3-none-any.whl

Total release size: 251.3 kB

Release files / bayesian_vecm-0.1.0.tar.gz

Download URL bayesian_vecm-0.1.0.tar.gz
Size 149.5 kB
Tags Source
SHA-256 checksum
How to use checksums
2a23eb3820dd7147037a511ddf3719a81e67abaa06f66962727049f877e33e0a
BLAKE2b-256 checksum
How to use checksums
29c4e82da15ab1e15382a8b9955fa5a39e0484099aa5f1e96aa07a422a9c2b4f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Release files / bayesian_vecm-0.1.0-py3-none-any.whl

Download URL bayesian_vecm-0.1.0-py3-none-any.whl
Size 101.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
54ac68c3d63486c473cb5b47a36244a088f900f4200074132c30639307f96682
BLAKE2b-256 checksum
How to use checksums
60bb5a56a43c1d751144a2e2d8b418471b5c29bd6982bf49b3fa8b2a02d7adfa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

Security for bayesian-vecm 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for bayesian_vecm-0.1.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 23, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page