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
python -m pip install git+https://github.com/Larsvanderlaan/calibratedDML.gitremotes::install_github("Larsvanderlaan/calibratedDML")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.
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))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.001library(calibratedDML)
set.seed(42)
n <- 1000
df <- data.frame(W1 = rnorm(n), W2 = rnorm(n))
df$A <- rbinom(n, 1, plogis(0.7 * df$W1))
df$Y <- 2 * df$A + df$W1 + df$W2 + rnorm(n)
fit <- calibrated_dml(
data = df,
outcome = "Y",
treatment = "A",
covariates = c("W1", "W2"),
control_level = 0,
seed = 42
)
columns <- c("estimand", "estimate", "std_error", "lower", "upper")
print(summary(fit)[columns], row.names = FALSE, digits = 3)estimand estimate std_error lower upper
E[Y(0)] -0.0306 0.0620 -0.171 0.11
E[Y(1)] 1.8977 0.0669 1.746 2.05
E[Y(1)] - E[Y(0)] 1.9283 0.0681 1.774 2.08The 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.
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.