Skip to main content

Causal inference for quasi-experiments in Python

Project description


Build Status Ruff PyPI version GitHub Repo stars Read the Docs PyPI - Downloads Interrogate codecov

CausalPy

A Python package focussing on causal inference in quasi-experimental settings. The package allows for sophisticated Bayesian model fitting methods to be used in addition to traditional OLS.

Installation

To get the latest release:

pip install CausalPy

If you run into installation issues with PyMC (e.g. BLAS or compilation), try the conda install: conda install -c conda-forge causalpy.

Alternatively, if you want the very latest version of the package you can install from GitHub:

pip install git+https://github.com/pymc-labs/CausalPy.git

Quickstart

import causalpy as cp
import matplotlib.pyplot as plt

# Import and process data
df = (
    cp.load_data("drinking")
    .rename(columns={"agecell": "age"})
    .assign(treated=lambda df_: df_.age > 21)
    )

# Run the analysis
result = cp.RegressionDiscontinuity(
    df,
    formula="all ~ 1 + age + treated",
    running_variable_name="age",
    model=cp.pymc_models.LinearRegression(),
    treatment_threshold=21,
    )

# Visualize outputs
fig, ax = result.plot();

# Get a results summary
result.summary()

plt.show()

Roadmap

Plans for the repository can be seen in the Issues.

Videos

Click on the thumbnail below to watch a video about CausalPy on YouTube. Youtube video thumbnail image

Features

CausalPy has a broad range of quasi-experimental methods for causal inference:

Method Description
Synthetic control Constructs a synthetic version of the treatment group from a weighted combination of control units. Used for causal inference in comparative case studies when a single unit is treated, and there are multiple control units.
Geographical lift Measures the impact of an intervention in a specific geographic area by comparing it to similar areas without the intervention. Commonly used in marketing to assess regional campaigns.
ANCOVA Analysis of Covariance combines ANOVA and regression to control for the effects of one or more quantitative covariates. Used when comparing group means while controlling for other variables.
Differences in Differences Compares the changes in outcomes over time between a treatment group and a control group. Used in observational studies to estimate causal effects by accounting for time trends.
Staggered Difference-in-Differences Estimates event-time treatment effects when different units adopt treatment at different times, using an imputation approach that models untreated outcomes and compares observed outcomes to counterfactual predictions.
Regression discontinuity Identifies causal effects by exploiting a cutoff or threshold in an assignment variable. Used when treatment is assigned based on a threshold value of an observed variable, allowing comparison just above and below the cutoff.
Regression kink designs Focuses on changes in the slope (kinks) of the relationship between variables rather than jumps at cutoff points. Used to identify causal effects when treatment intensity changes at a threshold.
Interrupted time series Analyzes the effect of an intervention by comparing time series data before and after the intervention. Used when data is collected over time and an intervention occurs at a known point, allowing assessment of changes in level or trend.
Instrumental variable regression Addresses endogeneity by using an instrument variable that is correlated with the endogenous explanatory variable but uncorrelated with the error term. Used when explanatory variables are correlated with the error term, providing consistent estimates of causal effects.
Inverse Propensity Score Weighting Weights observations by the inverse of the probability of receiving the treatment. Used in causal inference to create a synthetic sample where the treatment assignment is independent of measured covariates, helping to adjust for confounding variables in observational studies.

License

Apache License 2.0


Getting Help

Have questions about using CausalPy? We're here to help!

  • Questions & Help: Visit our GitHub Discussions Q&A to ask questions and get help from the community
  • Bug Reports & Feature Requests: Open an Issue for bugs or feature requests
  • Documentation: Check out our documentation for detailed guides and examples

Please use GitHub Discussions for general questions rather than opening issues, so we can keep the issue tracker focused on bugs and enhancements.

Support

This repository is supported by PyMC Labs.

If you are interested in seeing what PyMC Labs can do for you, then please email ben.vincent@pymc-labs.com. We work with companies at a variety of scales and with varying levels of existing modeling capacity. We also run corporate workshop training events and can provide sessions ranging from introduction to Bayes to more advanced topics.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

causalpy-0.8.1.tar.gz (1.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

causalpy-0.8.1-py3-none-any.whl (1.3 MB view details)

Uploaded Python 3

File details

Details for the file causalpy-0.8.1.tar.gz.

File metadata

  • Download URL: causalpy-0.8.1.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for causalpy-0.8.1.tar.gz
Algorithm Hash digest
SHA256 c3bed7cb736c33c970f3430900d2647af6e2962da31ba4e5cc31f21e0a0be578
MD5 42d4fe81cb67fdfbb596fd45ed080a42
BLAKE2b-256 e0ca5fd1549b0db6ea24b29f9c3a19b3c1833be95397470c3f0565f026d4f4c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for causalpy-0.8.1.tar.gz:

Publisher: release.yml on pymc-labs/CausalPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file causalpy-0.8.1-py3-none-any.whl.

File metadata

  • Download URL: causalpy-0.8.1-py3-none-any.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for causalpy-0.8.1-py3-none-any.whl
Algorithm Hash digest
SHA256 beac67d56139793c7d65aef1324a267f89a770a04d7b0b462bf62a58f13eaba0
MD5 7027718a9ffc6a8dfb1dc9ffd3dd9f44
BLAKE2b-256 6509f00942e9e98fb0fb043fe20ac76db5f41ba63a9b5a8782f6b29d5847878e

See more details on using hashes here.

Provenance

The following attestation bundles were made for causalpy-0.8.1-py3-none-any.whl:

Publisher: release.yml on pymc-labs/CausalPy

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page