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 causal implementations 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.2.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.2-py3-none-any.whl (11.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: causal_toolkit_pmathew-0.1.2.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.2.tar.gz
Algorithm Hash digest
SHA256 d529fe08b08c5bbb581a437b4b0cc9b00ae876cf922a126c7882c2c2ea8ba9bc
MD5 98455febf08882bc716a51cc648f3686
BLAKE2b-256 0f207c73f0bc83840b88c38fb05ec4c8cd78cefecdaa9b7622d42e69323c89da

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for causal_toolkit_pmathew-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6e2d23fd6e40fe0c740ba0e9dbb151cd27fa4250c2aae1851f2fec8f77200ec8
MD5 8d916374d223e38acc7e9ef334a6819c
BLAKE2b-256 253d09ad3f6aaf82f49bd630ea94c0fa1732da5a6175afb5e200d3aacf7eb771

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