Skip to main content

A Python package for causal inference methods including ATE estimation, propensity score methods, and meta-learners

Project description

causal-toolkit-yamashita

Tests PyPI version

A Python package for causal inference methods, including:

  • Randomized experiment analysis — average treatment effect (ATE) with confidence intervals and p-values
  • Propensity score methods — inverse probability weighting (IPW) and doubly robust estimation
  • Meta-learners — S-learner, T-learner, X-learner, and double machine learning for conditional average treatment effects (CATE)

Installation

Install the released package from PyPI:

pip install causal-toolkit-yamashita

Or install from source in editable mode for development:

git clone https://github.com/yamashann/causal-toolkit-yamashita.git
cd causal-toolkit-yamashita
uv pip install -e .

Quick Start

After installing (see above), save the following as quickstart.py and run it with python quickstart.py:

import numpy as np
import pandas as pd

from causal_toolkit_yamashita import calculate_ate_ci, doubly_robust, t_learner_discrete

rng = np.random.default_rng(0)
n = 1000

# --- Randomized experiment: estimate the average treatment effect (ATE) ---
# The DataFrame must have a treatment column "T" (0/1) and an outcome column "Y".
T = rng.integers(0, 2, size=n)
Y = 3.0 * T + rng.normal(size=n)                 # true effect = 3.0
rct = pd.DataFrame({"T": T, "Y": Y})

ate, lo, hi = calculate_ate_ci(rct)
print(f"RCT ATE        = {ate:.2f}  (95% CI [{lo:.2f}, {hi:.2f}])")

# --- Observational data where covariates confound the treatment ---
x1, x2 = rng.normal(size=n), rng.normal(size=n)
treat = rng.binomial(1, 1 / (1 + np.exp(-(0.8 * x1 + 0.5 * x2))))
outcome = 2.0 * treat + 1.5 * x1 + x2 + rng.normal(size=n)   # true effect = 2.0
obs = pd.DataFrame({"x1": x1, "x2": x2, "treat": treat, "outcome": outcome})

# doubly_robust(df, formula, T, Y) — `formula` is a patsy formula of the covariates
print(f"Doubly-robust  = {doubly_robust(obs, 'x1 + x2', 'treat', 'outcome'):.2f}")

# --- Per-row heterogeneous effects (CATE) via a meta-learner ---
train, test = obs.iloc[:800], obs.iloc[800:]
cate = t_learner_discrete(train, test, X=["x1", "x2"], T="treat", y="outcome")
print(f"Mean CATE      = {cate['cate'].mean():.2f}  ({len(cate)} test rows)")

Expected output (estimates land near the true effects baked into the data):

RCT ATE        = 3.12  (95% CI [3.00, 3.25])
Doubly-robust  = 2.08
Mean CATE      = 2.16  (200 test rows)

All eight functions (calculate_ate_ci, calculate_ate_pvalue, ipw, doubly_robust, s_learner_discrete, t_learner_discrete, x_learner_discrete, double_ml_cate) are importable directly from causal_toolkit_yamashita; see the API table below for signatures.

API

rct — Randomized experiments

Function Description
calculate_ate_ci(data, alpha=0.05) Returns (ate, ci_lower, ci_upper)
calculate_ate_pvalue(data) Returns (ate, t_stat, p_value)

propensity — Propensity score methods

Function Description
ipw(df, ps_formula, T, Y) Inverse probability weighted ATE
doubly_robust(df, formula, T, Y) Doubly robust ATE estimate

meta_learners — CATE estimation

Function Description
s_learner_discrete(train, test, X, T, y) Single-model learner; returns DataFrame with cate column
t_learner_discrete(train, test, X, T, y) Two-model learner
x_learner_discrete(train, test, X, T, y) Cross-fitted X-learner
double_ml_cate(train, test, X, T, y) Double machine learning CATE

Running tests

uv run pytest

License

MIT

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

causal_toolkit_yamashita-0.1.2.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

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

causal_toolkit_yamashita-0.1.2-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file causal_toolkit_yamashita-0.1.2.tar.gz.

File metadata

  • Download URL: causal_toolkit_yamashita-0.1.2.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.15

File hashes

Hashes for causal_toolkit_yamashita-0.1.2.tar.gz
Algorithm Hash digest
SHA256 54fb047aa923c31f3fad782f9efd215b27692572760da84e3cd9bf33f0dfcddf
MD5 ba18de6e507c537276d852a8c5f3fcfb
BLAKE2b-256 14dfd828c98d75ee9565a214e699bd673a870f1a1018e62fd282dc99d09c49bc

See more details on using hashes here.

File details

Details for the file causal_toolkit_yamashita-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for causal_toolkit_yamashita-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7db401e2844237b850291b727e27614182c4dceebaa05c53a908fe1fcdcb2955
MD5 29bf32183d0d969fae52a8862bcc611e
BLAKE2b-256 c17042e10e86440cee557186ba9b5fc82c729edc0f37c6c474402310e87c8afd

See more details on using hashes here.

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