Examples
Use raw data to fit the full estimator, or supply cross-fitted nuisance predictions from your own models. Both R and Python return arm means, treatment contrasts, and confidence intervals.
Fit from raw data
The getting-started example generates data, fits the estimator, and shows the output in both languages. The package cross-fits outcome and treatment models, then calibrates the pooled predictions.
Supply nuisance predictions
Use fit_from_nuisances() in Python or calibrated_dml_from_nuisances() in R when you already have cross-fitted predictions. Both matrices have one row per observation and one column per treatment level.
| Input | Column for treatment level a |
|---|---|
| mu_mat | Predicted conditional mean E[Y | A = a, W]. |
| pi_mat | Predicted treatment probability P(A = a | W). |
fit = CalibratedDML(control_level=0).fit_from_nuisances(
A=A,
y=y,
mu_mat=mu_mat,
pi_mat=pi_mat,
treatment_levels=[0, 1],
)
fit.summary()fit <- calibrated_dml_from_nuisances(
A = A,
Y = Y,
mu_mat = mu_mat,
pi_mat = pi_mat,
treatment_levels = c(0, 1),
control_level = 0
)
summary(fit)Align the columns with treatment_levels. Keep observations in the same row order in the outcome, treatment, and nuisance arrays.
Compare more than two treatments
The standard estimator accepts categorical treatments. Set control_level to choose the comparison arm; the summary reports each arm's mean potential outcome and its contrast with that control. For supplied predictions, include a column for every treatment level.
Choose your learners
Python accepts scikit-learn regressors and classifiers. Treatment classifiers must implement predict_proba(). For example:
from sklearn.ensemble import RandomForestClassifier, RandomForestRegressor
fit = CalibratedDML(
outcome_model=RandomForestRegressor(n_estimators=200, random_state=123),
treatment_model=RandomForestClassifier(n_estimators=200, random_state=123),
random_state=123,
).fit(X, A, y)
R accepts built-in model names, custom model lists, and optional SuperLearner and sl3 learners. See the custom-learner vignette for examples.
Specify a linear functional
calibrated_linear_functional() accepts a linear map and cross-fitted regression and Riesz predictions. This example estimates the binary-treatment ATE. The two columns of mu_mat and pi_mat correspond to treatment levels 0 and 1.
import numpy as np
from calibrateddml import calibrated_linear_functional
alpha_mat = np.column_stack([-1 / pi_mat[:, 0], 1 / pi_mat[:, 1]])
rows = np.arange(len(A))
fit = calibrated_linear_functional(
y=y,
regression=mu_mat[rows, A],
riesz=alpha_mat[rows, A],
functional=lambda values: values[:, 1] - values[:, 0],
regression_evaluations=mu_mat,
riesz_evaluations=alpha_mat,
name="ATE",
)
fit.summary()library(calibratedDML)
alpha_mat <- cbind(-1 / pi_mat[, 1], 1 / pi_mat[, 2])
observed <- cbind(seq_along(A), A + 1)
fit <- calibrated_linear_functional(
Y = Y,
regression = mu_mat[observed],
riesz = alpha_mat[observed],
functional = function(values) values[, 2] - values[, 1],
regression_evaluations = mu_mat,
riesz_evaluations = alpha_mat,
name = "ATE"
)
summary(fit)The observed-input vectors are used to calibrate and debias. The evaluation matrices contain predictions at every input used by the functional. For each row, use models trained without that row's cross-fitting fold, including for counterfactual predictions. Here, the functional subtracts the control prediction from the treatment prediction.
PAVA calibrates the regression and the signed Riesz representer. The default 10-fold jackknife refits only calibration and final means, holding the initial predictions fixed.
Runnable Python example · Runnable R example · Function inputs
Read the results
See the example output for the result columns and the inference guide for confidence interval options.