External packages: DoubleML, EconML, tlverse
This page sketches how to wire rieszreg’s cross-fit \(\hat\alpha\) into mature DML/TMLE libraries. The recipes use the synthetic ATE data frame df from the landing-page quickstart. Install the relevant external package locally and adapt the code to your data.
DoubleML (Python)
DoubleML separates the estimator class (DoubleMLPLR, DoubleMLIRM, …) from the nuisance learners. DoubleMLIRM expects a propensity classifier in its ml_m slot. For ATE the Riesz representer at \(A = 1\) is \(\alpha(1, x) = 1/\pi(x)\), so a Riesz learner can stand in for that classifier. The wrapper below is a regular sklearn classifier. DoubleML clones it and fits it once per fold, so \(\hat\alpha\) is cross-fit on the same folds as the outcome model.
import doubleml as dml
import numpy as np
from sklearn.base import BaseEstimator, ClassifierMixin, clone
from sklearn.ensemble import GradientBoostingRegressor
from rieszreg import ATE
from rieszboost import RieszBooster
class RieszPropensity(ClassifierMixin, BaseEstimator):
"""Propensity classifier backed by a Riesz learner: π̂(x) = 1 / α̂(1, x)."""
def __init__(self, learner=None):
self.learner = learner
def fit(self, X, d):
# Z = (treatment, covariates); column 0 is the treatment.
self.learner_ = clone(self.learner).fit(np.column_stack([d, X]))
self.classes_ = np.array([0, 1])
return self
def predict_proba(self, X):
alpha_1 = self.learner_.predict(np.column_stack([np.ones(len(X)), X]))
pi_hat = np.clip(1.0 / np.maximum(alpha_1, 1e-6), 0.01, 0.99)
return np.column_stack([1 - pi_hat, pi_hat])
def predict(self, X):
return (self.predict_proba(X)[:, 1] > 0.5).astype(int)
dml_data = dml.DoubleMLData(df, y_col="y", d_cols="a", x_cols=["x"])
irm = dml.DoubleMLIRM(
dml_data,
ml_g=GradientBoostingRegressor(n_estimators=200, max_depth=3, random_state=0),
ml_m=RieszPropensity(RieszBooster(estimand=ATE())),
n_folds=5,
)
irm.fit()
print(irm.summary)This recipe was run with DoubleML 0.11.4 on the quickstart data (true ATE 29.5). It gave an estimate of 29.52 with SE 0.08. irm.summary reports the point estimate, SE, and 95% CI.
DoubleMLIRM clips and inverts the propensity, so this route only covers ATE-type estimands where \(\hat\alpha\) is an inverse propensity. For continuous shifts, stochastic interventions, and custom estimands, use the custom-code DML/TMLE pattern.
DoubleML (R)
The R port has the same API, with nuisance learners from mlr3. The block below is an untested sketch. It fits \(\hat\alpha\) but does not finish the hand-off: you still need an mlr3 learner that returns \(\hat\pi\) from it.
library(DoubleML)
library(mlr3)
library(mlr3learners)
booster <- RieszBooster$new(
estimand = ATE(treatment = "a", covariates = "x"),
n_estimators = 400L, learning_rate = 0.05, max_depth = 3L
)
booster$fit(df)
alpha_hat <- booster$predict(df)
dml_data <- DoubleMLData$new(df, y_col = "y", d_cols = "a", x_cols = "x")
ml_g <- lrn("regr.ranger", num.trees = 500L)
# Wrap alpha_hat as a custom mlr3 PipeOp that returns it as predict_proba —
# see the DoubleML tutorial on injecting precomputed nuisances for the recipe.
pi_hat <- pmin(pmax(1 / pmax(alpha_hat[df$a == 1], 1e-6), 1e-6), 1 - 1e-6)EconML
EconML’s automatic_debiased_ml module ships its own Riesz learner (RieszNet, ForestRiesz). The forestriesz package in this family wraps EconML’s BaseGRF directly, so a forest-style fit is already EconML-native. For an estimand EconML doesn’t ship (e.g. a custom continuous-shift functional defined via the LinearForm tracer), use rieszreg end-to-end with the custom-code DML/TMLE pattern — the integration there is full transparency, not chaining.
tlverse (R)
tmle3 from tlverse is TMLE-native and accepts pre-fit nuisances through its Likelihood_factor API. You can pass cross-fit \(\hat\alpha\) as a fixed treatment-mechanism factor, and tmle3 performs the targeting step. The block below is an untested sketch of the first half of that recipe.
library(tmle3)
booster <- RieszBooster$new(
estimand = ATE(treatment = "a", covariates = "x"),
n_estimators = 400L, learning_rate = 0.05, max_depth = 3L
)
booster$fit(df)
alpha_hat <- booster$predict(df)
# Wrap alpha_hat in a tmle3 Likelihood_factor that returns the precomputed
# values when queried, and pass it to tmle3_Spec_TMLE_ATE() in place of the
# default sl3-trained propensity learner. See the tmle3 vignette on custom
# Likelihood_factor implementations for the full recipe.See also
- Estimation → custom code — no-dependency DML/TMLE walk-through.
DoubleMLuser guide.tmle3introduction.