1 What Problem Does CausalPy Solve?
Most quasi-experimental causal inference tools— did, rdrobust, synthdid— produce point estimates with frequentist confidence intervals. While these are often appropriate, there are settings where a Bayesian approach offers meaningful advantages:
- Small samples: Prior information can regularise estimates when data are limited.
- Uncertainty propagation: The posterior predictive distribution provides a natural and interpretable representation of counterfactual uncertainty.
- Model comparison: Bayesian information criteria and posterior predictive checks allow formal comparison of model specifications.
- Complex model structures: Bayesian computation via MCMC handles hierarchical models, non-Gaussian likelihoods, and regression kinks naturally.
CausalPy [Martin et al., 2023] is an open-source Python package developed by PyMC Labs that implements Bayesian versions of the most common quasi-experimental designs: interrupted time series (ITS), difference-in-differences (DiD), regression discontinuity (RD), regression kink design (RKD), and synthetic control. It uses PyMC [Salvatier et al., 2016] for MCMC sampling and supports both MCMC and variational inference (ADVI) backends. As of version 0.8.0 (March 2026), CausalPy supports staggered adoption DiD and multiple treated units in synthetic control.
2 Installation and Setup
CausalPy requires Python 3.9+ and PyMC 5.x. MCMC sampling benefits from having a C compiler (JAX or C++) but will run on CPU without special hardware.
3 Interrupted Time Series: A Minimal Example
Interrupted time series is used when treatment is administered to a single unit at a known point in time, with pre- and post-treatment observations but no comparison unit. A government introduces a health policy in month 25 of a 48-month series; we want to estimate the change in level and trend.
The default model fits a linear trend before treatment and extrapolates the counterfactual post-treatment trajectory. The posterior predictive distribution is shown as a shaded band around the counterfactual, giving an immediate visual sense of uncertainty.
4 Difference-in-Differences
The Bayesian DiD model returns a full posterior for the interaction coefficient (the DiD ATT estimate), including credible intervals. Unlike frequentist point estimates with asymptotic confidence intervals, the credible interval has an exact probability interpretation: there is a 95% posterior probability that the true ATT lies in the reported interval, given the model and data.
5 Regression Discontinuity
The Bayesian RD model estimates a jump at the threshold as the difference in the posterior predictive means just above and below the cutoff. The posterior predictive is particularly useful here because it allows visualization of the full distribution of plausible treatment effects, not just a point estimate with standard errors. CausalPy also supports polynomial fits, kernel-weighted local regression, and bandwidth sensitivity analyses within the Bayesian framework.
6 Posterior Predictive Checks
A key advantage of the Bayesian framework is the ability to run posterior predictive checks (PPCs): simulating new datasets from the posterior and comparing them to the observed data. If the model is well-specified, simulated data should look like the observed data.
In frequentist quasi-experimental tools, model diagnostics typically focus on pre-trend tests and covariate balance. Bayesian PPCs add a complementary check: does the model's predicted distribution for the outcome match its observed distribution?
7 Synthetic Control
The Bayesian synthetic control uses a Dirichlet prior on donor weights, which automatically enforces non-negativity and the sum-to-one constraint. The posterior distribution over donor weights can be inspected to understand uncertainty about which donors are most informative.
8 Comparison with Frequentist Tools
9 Key Options and Pitfalls
- Tune MCMC carefully: Set
target_accept = 0.9for complex models; increase draws and tune for high posterior curvature.
- Check convergence: Use
az.plot_trace()andaz.summary()to verify R̂ < 1.01 and effective sample size > 400 for all parameters.
- Prior sensitivity: The default priors are weakly informative. If results change substantially under reasonable alternative priors, investigate why.
- ADVI for exploration: Use
cp.pymc_models.LinearRegressionWithADVI()for rapid exploration; switch to MCMC for final inference.
- Bandwidth in RD: CausalPy does not automatically select the optimal bandwidth (unlike
rdrobust). Use a grid search over bandwidths and check sensitivity.
References
- Abadie, A., Diamond, A., and Hainmueller, J. (2010). Synthetic control methods for comparative case studies. Journal of the American Statistical Association, 105(490), 493-505.
- Martin, O., Kumar, R., and Lao, J. (2023). CausalPy: A Python package for Bayesian causal inference in quasi-experiments. PyMC Labs. https://github.com/pymc-labs/CausalPy.
- Salvatier, J., Wiecki, T. V., and Fonnesbeck, C. (2016). Probabilistic programming in Python using PyMC3. PeerJ Computer Science, 2, e55.