Skip to main content

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

Project description

causal_toolkit_pmathew

Tests Publish to PyPI PyPI version

causal_toolkit_pmathew packages the course implementations from Weeks 02 through 05 into an installable causal inference toolkit.

Package contents

  • causal_toolkit_pmathew/rct.py: randomized controlled trial estimators from Week 02
  • causal_toolkit_pmathew/propensity.py: inverse propensity weighting and doubly robust estimators from Week 03
  • causal_toolkit_pmathew/meta_learners.py: S-, T-, X-learner, and double machine learning CATE estimators from Weeks 04 and 05
  • tests/: pytest coverage for the required modules

Installation

# Install from PyPI
pip install causal_toolkit_pmathew

# Or install from source
git clone https://github.com/prinupmathew/causal_toolkit_pmathew.git
cd causal_toolkit_pmathew
uv pip install -e .
python -m uv pip install -e .

Run tests

uv run pytest
python -m uv run pytest

Run pytest module with coverage

python -m pytest tests/ -v --cov=causal_toolkit_pmathew --cov-report=term-missing

Publish to PyPI

  • Create accounts:
  • Generate API tokens:
    • Go to Account Settings → API tokens → Add token
    • Scope: "Entire account"
    • Save token securely (starts with pypi-)
  • Publish to PyPI:
python -m pip install --upgrade twine build

# Set pypi credential locally in terminal (cmd)
set TWINE_USERNAME=__token__
set TWINE_PASSWORD=pypi-your-token-here

# Set pypi credential locally in terminal (bash)
export TWINE_USERNAME="__token__"
export TWINE_PASSWORD="pypi-your-token-here"

# Set pypi credential locally in terminal (powershell)
$env:TWINE_USERNAME="__token__"
$env:TWINE_PASSWORD="pypi-your-token-here"

# Verify tests pass
pytest tests/ -v

# Clean and rebuild
python -m build
python -m twine check dist/*
python -m twine upload dist/*

Automated publishing (GitHub Release -> PyPI)

  1. Add repository secret PYPI_API_TOKEN in GitHub Actions settings.
  2. Create and publish a GitHub Release (for example tag v0.1.1).
  3. GitHub Actions workflow publish.yml builds and uploads to PyPI automatically.

Usage

import pandas as pd

from causal_toolkit_pmathew import (
	calculate_ate_ci,
	doubly_robust,
	ipw,
	s_learner_discrete,
	t_learner_discrete,
)

trial_data = pd.DataFrame({
	"T": [1, 1, 0, 0],
	"Y": [11.2, 10.8, 8.9, 9.1],
})

ate, ci_lower, ci_upper = calculate_ate_ci(trial_data)

observational_data = pd.DataFrame({
	"x": [0.2, 0.7, -0.1, 1.3],
	"t": [1, 1, 0, 0],
	"y": [3.4, 2.8, 1.1, 1.6],
})

ipw_estimate = ipw(observational_data, "x", "t", "y")
dr_estimate = doubly_robust(observational_data, "x", "t", "y")

train = observational_data.rename(columns={"x": "x1"}).assign(x2=[1.0, 0.5, 0.1, -0.2])
test = train.copy()

s_cate = s_learner_discrete(train, test, ["x1", "x2"], "t", "y")
t_cate = t_learner_discrete(train, test, ["x1", "x2"], "t", "y")

API

  • calculate_ate_ci(data, alpha=0.05): estimate the average treatment effect and a two-sided confidence interval from an RCT dataset with T and Y columns
  • calculate_ate_pvalue(data): estimate the ATE, test statistic, and two-sided p-value for an RCT dataset with T and Y columns
  • ipw(df, ps_formula, T, Y): compute an inverse propensity weighted ATE
  • doubly_robust(df, formula, T, Y): compute a doubly robust ATE using propensity and outcome models
  • s_learner_discrete(train, test, X, T, y): estimate CATE with a single outcome model
  • t_learner_discrete(train, test, X, T, y): estimate CATE with separate treated and control models
  • x_learner_discrete(train, test, X, T, y): estimate CATE with the X-learner procedure
  • double_ml_cate(train, test, X, T, y): estimate CATE using residualized treatment and outcome models

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_pmathew-0.1.1.tar.gz (13.2 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_pmathew-0.1.1-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

Details for the file causal_toolkit_pmathew-0.1.1.tar.gz.

File metadata

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

File hashes

Hashes for causal_toolkit_pmathew-0.1.1.tar.gz
Algorithm Hash digest
SHA256 9903859728daf4e5a0f5f9acdfafa9f2b834d8fbf81977c42322533c90019d83
MD5 2c77dea1530381fc200e6b372952e4fa
BLAKE2b-256 3fb3e6118bf20aa3dd58e63b7d8f8e5dbcb73325a042d5d285abd6bf4caa2624

See more details on using hashes here.

File details

Details for the file causal_toolkit_pmathew-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for causal_toolkit_pmathew-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1d4a2dd9b4cbc2d871419dea32d38fbf470df31473fd52138929eb02be802c73
MD5 d7abcf2cdbae19905bc310cccdda0fb6
BLAKE2b-256 890aab6085d93319b00973da052a9e349a45750df134aad25f610fd93640dc36

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