Skip to content

Uncertainty, structured prediction, and causal learning · Uncertainty, structured prediction, and causal learning · UNC014 · Algorithm

Double / debiased machine learning with cross-fitting

Estimate a low-dimensional target while using flexible predictive models for nuisance functions. Orthogonal scores reduce first-order sensitivity to nuisance estimation error, and cross-fitting separates fitting from…

Runs in CodeSplash Complete calculation CodeSplash's run and the reference's record

In CodeSplash

A CodeSplash method runs this entry in Python and R. Set it up from the method's page.

Do it in CodeSplash Run it on your own data, in Python or R.

Availability
Runs in CodeSplash
Code example
Complete calculation
Who ran it
CodeSplash's run and the reference's record
CodeSplash's run
Verified in Python and R through causal.dml. The reference's example ran here in one language.
The reference's record
Python: Ran in the reference's review. The report doesn't record which version of the code ran. R: Reviewed but not run: R wasn't available to the reference's authors.

Purpose

Estimate a low-dimensional target while using flexible predictive models for nuisance functions. Orthogonal scores reduce first-order sensitivity to nuisance estimation error, and cross-fitting separates fitting from score evaluation. It is an inference framework, not a specific deep architecture and not an automatic remedy for unmeasured confounding.

Question / hypotheses

What is the target treatment or structural parameter under the specified identifying model, and what uncertainty is justified after nuisance learning? Different scores target different quantities; partially linear slopes, average treatment effects and instrumental-variable effects are not interchangeable.

Architecture / computational structure

Split independent units into folds. For each fold, fit outcome and treatment nuisance regressions using other folds and predict only held-out observations. Aggregate an orthogonal estimating equation and estimate its influence-function variance. Hyperparameter tuning must remain inside each nuisance training fold or otherwise preserve the stated separation.

Assumptions

Identification requires the corresponding exogeneity, overlap, instrument or unconfoundedness assumptions. Root-n inference additionally needs regularity and sufficient nuisance convergence, often expressed through products of nuisance errors. Clusters or dependent time observations require appropriate sample splitting and variance estimators.

Formula / computational definition

For a partially linear model, let ℓ(x)=E[Y∣X=x]\ell(x)=\mathbb E[Y\mid X=x], m(x)=E[D∣X=x]m(x)=\mathbb E[D\mid X=x], Y~i=Yi−ℓ^−k(i)(Xi)\tilde Y_i=Y_i-\hat\ell_{-k(i)}(X_i) and D~i=Di−m^−k(i)(Xi)\tilde D_i=D_i-\hat m_{-k(i)}(X_i). Then θ^=∑iD~iY~i/∑iD~i2\hat\theta=\sum_i\tilde D_i\tilde Y_i/\sum_i\tilde D_i^2. With ψi=D~i(Y~i−θ^D~i)\psi_i=\tilde D_i(\tilde Y_i-\hat\theta\tilde D_i), an iid standard-error estimate is ψ2‾/[n(D~2‾)2]\sqrt{\overline{\psi^2}/[n(\overline{\tilde D^2})^2]}.

Parameters and notation

Estimated: target θ and nuisance functions ℓ/m, with possible additional nuisances for other scores. Hyperparameters/design choices: target estimand/score, folds and repetitions, learner complexity, covariate set, overlap policy and variance design. Nuisance predictive fit is useful but does not establish identification.

Fitting, fine-tuning, and adaptation

Fine-tune neural nuisance learners only on their permitted training folds. After data or learner changes, regenerate out-of-fold predictions and re-solve the score; reusing in-sample residuals defeats cross-fitting. If the scientific target changes, select a new score and analysis plan rather than merely warm-starting a network.

How to use and calibrate

Write the estimand and identification assumptions before fitting. Audit held-out nuisance errors, residual treatment variation, overlap and split sensitivity. Report confidence intervals using the appropriate score variance; compare across nuisance learners without choosing the estimate with the most favorable significance.

Example application

Estimate an average partially linear association/effect of an intervention intensity while adjusting flexibly for measured pre-treatment covariates. Plot overlap and nuisance residual diagnostics before interpreting the target coefficient.

Interpretation / pitfalls

Watch out

Orthogonality reduces sensitivity to certain nuisance errors; it does not remove bias from omitted confounders, post-treatment adjustment, weak overlap or a wrong structural target. The simple formula below is the PLR partialling-out version, not a universal DML estimator.

Python and R code examples

Scope: Small complete two-fold PLR partialling-out calculation with linear nuisance learners and an iid influence-score standard error. Synthetic data are generated independently within each language, so Python and R need not give identical random outputs; no causal claim is made about real data.

Dependencies / validation: use the shared NumPy/SciPy or base-R setup above. Python executed successfully with assertions; R reviewed but not executed.

Python and R

The reference's own programs, shown as written. A component demo shows one central piece of the method; it doesn't train a full model.

Python

Complete calculation — Small complete two-fold PLR partialling-out calculation with linear nuisance learners and an iid influence-score standard error. Synthetic data are generated independently within each language, so Python and R need not give identical random outputs; no causal claim is made about real data.

Shared setup for this chapter
import numpy as np
from scipy.special import logsumexp, gammaln
from scipy.linalg import expm
from itertools import product

def rbf(a, b, ell=1.0):
    a, b = np.atleast_2d(a), np.atleast_2d(b)
    return np.exp(-np.sum((a[:, None, :] - b[None, :, :])**2, axis=2)/(2*ell**2))
rng=np.random.default_rng(13); n=400
x=rng.normal(size=(n,2)); d=.6*x[:,0]-.3*x[:,1]+rng.normal(size=n)
y=1.5*d+x[:,0]+.5*x[:,1]+rng.normal(size=n)
B=np.column_stack([np.ones(n),x]); ry=np.empty(n); rd=np.empty(n)
fold=np.arange(n)%2
for k in range(2):
    tr=fold!=k; te=~tr
    bh_y=np.linalg.lstsq(B[tr],y[tr],rcond=None)[0]
    bh_d=np.linalg.lstsq(B[tr],d[tr],rcond=None)[0]
    ry[te]=y[te]-B[te]@bh_y; rd[te]=d[te]-B[te]@bh_d
J=np.mean(rd**2); theta=np.sum(rd*ry)/np.sum(rd**2)
psi=rd*(ry-theta*rd); se=np.sqrt(np.mean(psi**2)/(n*J**2))
assert se>0 and abs(theta-1.5)<.3 and abs(psi.mean())<1e-10
print({'estimate':float(theta),'standard_error':float(se),'true_simulation_effect':1.5})
R

Complete calculation — Small complete two-fold PLR partialling-out calculation with linear nuisance learners and an iid influence-score standard error. Synthetic data are generated independently within each language, so Python and R need not give identical random outputs; no causal claim is made about real data.

set.seed(13); n <- 400
x <- matrix(rnorm(n*2),n,2); d <- .6*x[,1]-.3*x[,2]+rnorm(n)
y <- 1.5*d+x[,1]+.5*x[,2]+rnorm(n)
B <- cbind(1,x); ry <- rd <- numeric(n); fold <- (seq_len(n)-1)%%2
for(k in 0:1) {
 tr <- fold!=k; te <- !tr
 bh_y <- qr.solve(B[tr,],y[tr]); bh_d <- qr.solve(B[tr,],d[tr])
 ry[te] <- y[te]-B[te,]%*%bh_y; rd[te] <- d[te]-B[te,]%*%bh_d
}
J <- mean(rd^2); theta <- sum(rd*ry)/sum(rd^2)
psi <- rd*(ry-theta*rd); se <- sqrt(mean(psi^2)/(n*J^2))
stopifnot(se>0,abs(theta-1.5)<.3,abs(mean(psi))<1e-10)
print(c(estimate=theta,standard_error=se,true_simulation_effect=1.5))

Sources

From the CodeSplash reference, entry UNC014.