Causalmodels

0.4.0 · active · verified Fri Apr 17

Causalmodels is a Python library for defining, analyzing, and inferring causal relationships from data, drawing inspiration from Judea Pearl's do-calculus. It provides tools for building Bayesian causal models, performing matching, and conducting regression-based causal inference. The current version is 0.4.0, with an irregular release cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `Regression` module to estimate the Average Treatment Effect (ATE) of a treatment variable 'X' on an outcome 'Y', while controlling for a confounder 'Z'. It simulates data reflecting a causal relationship and then applies the regression model.

import pandas as pd
import numpy as np
from causalmodels.regression import Regression

# Simulate some data with a known causal effect
np.random.seed(42)
n_samples = 1000

# Confounder Z affects both Treatment X and Outcome Y
Z = np.random.normal(0, 1, n_samples)
# Treatment X is affected by Z
X = 0.5 * Z + np.random.normal(0, 1, n_samples)
# Outcome Y is affected by X and Z
Y = 2.0 * X + 1.0 * Z + np.random.normal(0, 1, n_samples)

data = pd.DataFrame({'Z': Z, 'X': X, 'Y': Y})

# Initialize the Regression model
# X: treatment variable, Y: outcome variable, control_variables: confounders
model = Regression(data, treatment='X', outcome='Y', control_variables=['Z'])

# Estimate the Average Treatment Effect (ATE)
ate_estimate = model.estimate_ate()

print(f"Observed data with N={n_samples} samples.")
print(f"Estimated Average Treatment Effect (ATE) of X on Y, controlling for Z: {ate_estimate:.4f}")

view raw JSON →