calibratedDML GitHub

Get started

This example generates data and estimates an average treatment effect. The defaults use lasso nuisance models, automatic calibration, and V-fold jackknife confidence intervals with V = 10.

Install

Install the current GitHub version
python -m pip install git+https://github.com/Larsvanderlaan/calibratedDML.git

Python requires version 3.10 or later.

Estimate a treatment effect

Generate 1,000 observations with two covariates. The first affects treatment assignment, and both affect the outcome. Treatment adds 2 to the outcome, so the true ATE is 2.

Generate data and estimate the ATE
import numpy as np
from calibrateddml import CalibratedDML

rng = np.random.default_rng(42)
X = rng.normal(size=(1000, 2))
propensity = 1 / (1 + np.exp(-0.7 * X[:, 0]))
A = rng.binomial(1, propensity)
y = 2 * A + X[:, 0] + X[:, 1] + rng.normal(size=1000)

fit = CalibratedDML(control_level=0, random_state=42).fit(X, A, y)
columns = ["estimand", "estimate", "std_error", "lower", "upper"]
print(fit.summary()[columns].round(3).to_string(index=False))
Output
estimand  estimate  std_error  lower  upper
          E[Y(0)]    -0.021      0.076 -0.192  0.151
          E[Y(1)]     1.751      0.065  1.604  1.898
E[Y(1)] - E[Y(0)]     1.772      0.101  1.543  2.001

The first two rows estimate the mean potential outcomes. The third estimates their difference, with its standard error and a 95% confidence interval in lower and upper.

Python script · R script

Use your data

In Python, pass your covariate matrix X, treatment vector A, and outcome vector y to fit(X, A, y). In R, replace df and the column names. Set control_level to your control arm.

Choose nuisance models

Set outcome_model and treatment_model to choose learners. See the Python and R guides for model names and optional dependencies.

Custom learners · Supplied predictions · Runnable tutorials