pymc-labs/CausalPy
A Python package for causal inference in quasi-experimental settings
CausalPy – Bayesian‑first causal inference for quasi‑experimental designs
What it does – CausalPy is a Python library that lets analysts estimate causal effects when a true randomized experiment isn’t possible. It implements a suite of classic quasi‑experimental methods (difference‑in‑differences, synthetic control, regression discontinuity, etc.) and runs them as Bayesian models built on PyMC (with an optional OLS fallback). The output includes full posterior summaries, credible intervals, ROPE‑based practical‑significance checks and ready‑to‑publish plots.
Why it matters
- Uncertainty‑aware: instead of a single point estimate you get a distribution, HDI, tail probabilities, and ROPE analysis.
- Reproducible workflow: data loading, model specification, fitting and visualisation are all wrapped in high‑level classes (
RegressionDiscontinuity,SyntheticControl, …) that produce a singleresultobject withplot()andsummary()methods. - Built on PyMC: leverages modern probabilistic programming (PyMC 6, PyTensor 3, ArviZ) while still supporting classic OLS via scikit‑learn for quick checks.
- Consulting‑ready: the team offers paid consulting for high‑stakes lift‑testing or policy‑impact work.
Core features (from the README)
| Feature | Details |
|---|---|
| Methods | Synthetic control, Geographical lift, ANCOVA, Diff‑in‑Diff, Staggered DiD, Regression discontinuity/kink, Interrupted time series, Instrumental variables, Inverse propensity weighting |
| Bayesian first | Models are written with PyMC; posterior HDI, tail‑probabilities, ROPE. OLS fallback via scikit‑learn. |
| Decision‑ready outputs | effect_summary() returns tables and prose; plot() gives publication‑quality graphics. |
| Installation | pip install CausalPy or conda install -c conda‑forge causalpy. Works on Python 3.12+. |
| Runtime notes | Uses numba (via PyTensor) – first sample incurs compilation cost; native crashes possible; nutpie can silently change NUTS defaults. |
| AI‑agent skills | Small “skills” for AI coding assistants live under causalpy/skills/ and are published to Decision AI Hub. |
| Documentation & support | Read‑the‑Docs site, GitHub Discussions for Q&A, Issues for bugs, and a consulting Calendly link. |
| License | Apache 2.0. |
Typical usage (quick‑start example)
import causalpy as cp
import matplotlib.pyplot as plt
# Load a toy dataset bundled with the package
df = (
cp.load_data("drinking")
.rename(columns={"agecell": "age"})
.assign(treated=lambda d: d.age > 21)
)
# Fit a regression‑discontinuity model using a Bayesian linear regression
result = cp.RegressionDiscontinuity(
df,
formula="all ~ 1 + age + treated",
running_variable_name="age",
model=cp.pymc_models.LinearRegression(),
treatment_threshold=21,
)
# Visualise the jump at the cutoff
fig, ax = result.plot()
# Print a summary with posterior intervals
result.summary()
The result object encapsulates the fitted model, diagnostics, and helper methods for reporting.
Who should use it?
- Product/marketing analysts evaluating geo‑lift, policy changes, or staggered roll‑outs.
- Social scientists needing rigorous DiD or RD analyses with uncertainty quantification.
- Data scientists who prefer a Bayesian approach but still want the option of fast OLS.
It is not aimed at:
- Pure causal‑discovery from observational data.
- Fully automated “black‑box” causal answers.
- Large‑scale production pipelines (no built‑in orchestration).
Getting started & resources
- Installation:
pip install CausalPy(or conda). Requires Python 3.12+, PyMC 6. - Docs: https://causalpy.readthedocs.io – includes tutorials for each method.
- Community: GitHub Discussions for usage questions; Issues for bugs.
- Video demo: YouTube link in the README.
TL;DR
CausalPy is a research‑grade, Bayesian‑first Python toolbox for quasi‑experimental causal inference. It wraps classic designs in a PyMC‑powered workflow, delivering uncertainty‑aware estimates, diagnostics, and ready‑to‑share visualisations, while remaining easy to install and use for analysts familiar with Python’s data‑science stack.
Related
- Project
- Project
- Project
- Project
- Project