Skip to main content

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

Metadata

Release files for causal-toolkit-yamashita 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for causal-toolkit-yamashita 0.1.2
File Size Uploaded Provenance Actions
causal_toolkit_yamashita-0.1.2.tar.gz 6.5 kB Without provenance Details for causal_toolkit_yamashita-0.1.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for causal-toolkit-yamashita 0.1.2
File Interpreter ABI Platform Actions
causal_toolkit_yamashita-0.1.2-py3-none-any.whl Python 3 none any Details for causal_toolkit_yamashita-0.1.2-py3-none-any.whl

Total release size: 13.1 kB

Release files / causal_toolkit_yamashita-0.1.2.tar.gz

Download URL causal_toolkit_yamashita-0.1.2.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
54fb047aa923c31f3fad782f9efd215b27692572760da84e3cd9bf33f0dfcddf
BLAKE2b-256 checksum
How to use checksums
14dfd828c98d75ee9565a214e699bd673a870f1a1018e62fd282dc99d09c49bc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release files / causal_toolkit_yamashita-0.1.2-py3-none-any.whl

Download URL causal_toolkit_yamashita-0.1.2-py3-none-any.whl
Size 6.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7db401e2844237b850291b727e27614182c4dceebaa05c53a908fe1fcdcb2955
BLAKE2b-256 checksum
How to use checksums
c17042e10e86440cee557186ba9b5fc82c729edc0f37c6c474402310e87c8afd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Security for causal-toolkit-yamashita 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page