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

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

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

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.0.tar.gz (12.3 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.0-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: causal_toolkit_pmathew-0.1.0.tar.gz
  • Upload date:
  • Size: 12.3 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.0.tar.gz
Algorithm Hash digest
SHA256 afbd8f68cfbe7fd4ad16f2064f9fda2b1b714eaf5c28407045d7b9dde649d4c2
MD5 575ed47f0cf99e7af181bd13b2a5e465
BLAKE2b-256 106bc0197b8483a3467fa5993d7992c50bd4b29b5384cad887927faa4c9a1b63

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for causal_toolkit_pmathew-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a89fe2e610ba8f2199a57dd8381dc25d225f3de150243b60e66807358e3376c3
MD5 a7c581a4e6052119b1d543d8f6d1d962
BLAKE2b-256 ffb39db75f4495f0e6bffb037e9fdfcfd0465144b95301f1a1d5f5645d521aea

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