Simple Algorithm
Machine Learning · Convergence · 30 min

Causal Inference with Machine Learning

Treatment effects, double machine learning and causal forests.

Machine learning predicts; decisions need to know what would happen under an intervention. This lesson builds the bridge: the potential-outcome assumptions that identify causal effects from observational data, propensity scores and doubly robust estimation, double machine learning, which lets flexible predictors estimate nuisance functions without biasing the effect, heterogeneous effects with meta-learners and causal forests, and the sensitivity analyses that ask how wrong the assumptions would have to be to overturn a conclusion.

What you’ll learn

  • Define potential outcomes, the ATE and the CATE, and prove identification under consistency, unconfoundedness and overlap, together with its graphical form, the backdoor adjustment.
  • Derive inverse propensity weighting and the AIPW estimator, and prove double robustness with its product-of-errors bias.
  • Explain Neyman orthogonality and cross-fitting, and state when double machine learning is n\sqrt n-consistent and asymptotically normal.
  • Compare S-, T-, X- and R-learners and causal forests for heterogeneous effects, and quantify robustness to unmeasured confounding with E-values.

Potential outcomes and identification

Each unit has a binary treatment TT, covariates X\mathbf X and two potential outcomes: Y(1)Y(1) if treated and Y(0)Y(0) if not (Neyman, 1923; Rubin, 1974). Only one is ever observed, Y=Y(T)Y=Y(T): the fundamental problem of causal inference. The targets are the average treatment effect τ=E[Y(1)−Y(0)]\tau=\E[Y(1)-Y(0)] and the conditional one, τ(x)=E[Y(1)−Y(0)∣X=x]\tau(\mathbf x)=\E[Y(1)-Y(0)\mid\mathbf X=\mathbf x]. Write e(x)=P(T=1∣X=x)e(\mathbf x)=P(T=1\mid\mathbf X=\mathbf x) for the propensity score and μt(x)=E[Y∣T=t,X=x]\mu_t(\mathbf x)=\E[Y\mid T=t,\mathbf X=\mathbf x] for the outcome regressions.

DerivationProof
  1. E[Y(1)∣X]=E[Y(1)∣T=1,X]=E[Y∣T=1,X]=μ1(X)\E[Y(1)\mid\mathbf X]=\E[Y(1)\mid T=1,\mathbf X]=\E[Y\mid T=1,\mathbf X]=\mu_1(\mathbf X)

    Unconfoundedness lets you condition on the treatment, consistency replaces the potential outcome by the observed one, and overlap ensures the conditioning event has positive probability:

  2. E[TYe(X) ∣ X]=E[T∣X] E[Y(1)∣X]e(X)=E[Y(1)∣X]\E\Big[\frac{TY}{e(\mathbf X)}\ \Big|\ \mathbf X\Big]=\frac{\E[T\mid\mathbf X]\,\E[Y(1)\mid\mathbf X]}{e(\mathbf X)}=\E[Y(1)\mid\mathbf X]

    For the weighting form, TY=TY(1)TY=TY(1), and given X\mathbf X the treatment is independent of Y(1)Y(1):

  3. (Y(0),Y(1))⊥ ⁣ ⁣ ⁣⊥T∣X⟹(Y(0),Y(1))⊥ ⁣ ⁣ ⁣⊥T∣e(X)(Y(0),Y(1))\indep T\mid\mathbf X\quad\Longrightarrow\quad(Y(0),Y(1))\indep T\mid e(\mathbf X)

    The same steps give the control arm. Average over X\mathbf X. Rosenbaum & Rubin (1983) added that the propensity score is a balancing score, X⊥ ⁣ ⁣ ⁣⊥T∣e(X)\mathbf X\indep T\mid e(\mathbf X), so unconfoundedness given X\mathbf X implies unconfoundedness given the scalar e(X)e(\mathbf X): matching or stratifying on one number suffices.

In Pearl's graphical language (Pearl, 2009), a set ZZ satisfies the backdoor criterion for (T,Y)(T,Y) if no node in ZZ is a descendant of TT and ZZ blocks every path between TT and YY that starts with an arrow into TT. Then

P(y∣do(t))=∑zP(y∣t,z) P(z),P\big(y\mid\mathrm{do}(t)\big)=\sum_zP(y\mid t,z)\,P(z),

the graphical counterpart of unconfoundedness given ZZ. Descendants of the treatment must stay out: adjusting for a mediator removes part of the effect, and adjusting for a common effect of TT and YY (a collider) creates bias where there was none. In the sprinkler network, Cloudy is a confounder of Sprinkler and WetGrass, and {Cloudy}\{\text{Cloudy}\} satisfies the criterion.

Predict before you look

Use the backdoor formula with Z={Cloudy}Z=\{\text{Cloudy}\} and the numbers in the caption. What is P(WetGrass∣do(Sprinkler=on))P(\text{WetGrass}\mid\mathrm{do}(\text{Sprinkler}=\text{on})), in percent?

Show the answer

94.5 %

0.5⋅97.2%+0.5⋅91.8%=94.5%0.5\cdot97.2\%+0.5\cdot91.8\%=94.5\%, against the observational 92.7%. With the sprinkler forced off, 0.5⋅72%+0.5⋅18%=45%0.5\cdot72\%+0.5\cdot18\%=45\%, so the causal effect of the sprinkler is 49.5 points while the naive contrast is 92.7−52.7=4092.7-52.7=40 points. The naive contrast weights the strata by P(c∣Sprinkler)P(c\mid\text{Sprinkler}) instead of P(c)P(c).

Estimating the average effect: weighting and double robustness

Each identification formula suggests an estimator. Outcome regression fits μ^0,μ^1\hat\mu_0,\hat\mu_1 and averages μ^1(Xi)−μ^0(Xi)\hat\mu_1(\mathbf X_i)-\hat\mu_0(\mathbf X_i); any bias in the fits, such as shrinkage from regularization, passes straight into τ^\hat\tau. Inverse propensity weighting (IPW), in the spirit of Horvitz & Thompson (1952), averages TiYi/e^(Xi)−(1−Ti)Yi/(1−e^(Xi))T_iY_i/\hat e(\mathbf X_i)-(1-T_i)Y_i/(1-\hat e(\mathbf X_i)); it is unbiased with the true propensities, but units with ee near 0 or 1 receive huge weights, and its variance explodes when overlap is weak. In high dimensions overlap is a strong assumption: D'Amour, Ding, Feller, Lei & Sekhon (2021) showed that strict overlap sharply limits how different the treated and control covariate distributions can be, increasingly so as the dimension grows.

DerivationAugmented IPW and double robustness
  1. ψ(Z;μ,e)=μ1(X)−μ0(X)+T (Y−μ1(X))e(X)−(1−T)(Y−μ0(X))1−e(X),τ^=1n∑iψ(Zi;μ^,e^)\psi(Z;\mu,e)=\mu_1(\mathbf X)-\mu_0(\mathbf X)+\frac{T\,\big(Y-\mu_1(\mathbf X)\big)}{e(\mathbf X)}-\frac{(1-T)\big(Y-\mu_0(\mathbf X)\big)}{1-e(\mathbf X)},\qquad\hat\tau=\frac1n\sum_i\psi(Z_i;\hat\mu,\hat e)

    Combine the two (Robins, Rotnitzky & Zhao, 1994): start from the regression estimate and correct it with the weighted residuals:

  2. E[μ^1(X)+T(Y−μ^1(X))e^(X)]−E[Y(1)]=E[(μ1(X)−μ^1(X)) e(X)−e^(X)e^(X)]\E\Big[\hat\mu_1(\mathbf X)+\frac{T(Y-\hat\mu_1(\mathbf X))}{\hat e(\mathbf X)}\Big]-\E[Y(1)]=\E\Big[\big(\mu_1(\mathbf X)-\hat\mu_1(\mathbf X)\big)\,\frac{e(\mathbf X)-\hat e(\mathbf X)}{\hat e(\mathbf X)}\Big]

    Take expectations of the treated part with estimated nuisances. Given X\mathbf X, E[T(Y−μ^1)∣X]=e(X)(μ1(X)−μ^1(X))\E[T(Y-\hat\mu_1)\mid\mathbf X]=e(\mathbf X)\big(\mu_1(\mathbf X)-\hat\mu_1(\mathbf X)\big), so the error is a product:

  3. ∣bias∣ ≲ ∥μ^1−μ1∥L2 ∥e^−e∥L2\abs{\text{bias}}\ \lesssim\ \norm{\hat\mu_1-\mu_1}_{L_2}\,\norm{\hat e-e}_{L_2}

    Double robustness: the bias vanishes if either μ^1=μ1\hat\mu_1=\mu_1 or e^=e\hat e=e, and the control arm behaves the same way. More usefully for machine learning, the bias is second order, bounded by the product of the two errors by Cauchy–Schwarz, when e^\hat e is bounded away from 0:

The AIPW score ψ\psi, centered at τ\tau, is the efficient influence function of the ATE in the nonparametric model, so with good nuisance estimates the estimator attains the semiparametric efficiency bound; Bang & Robins (2005) discuss doubly robust estimation in missing-data and causal models. The product structure is the key that the next step generalizes: if each nuisance is estimated at rate oP(n−1/4)o_P(n^{-1/4}), their product is oP(n−1/2)o_P(n^{-1/2}), and the estimation error of the nuisances does not affect τ^\hat\tau to first order.

Double machine learning

Take the partially linear model of Robinson (1988), with a scalar treatment that may be continuous:

Y=θT+g(X)+U,E[U∣X,T]=0;T=m(X)+V,E[V∣X]=0.Y=\theta T+g(\mathbf X)+U,\quad\E[U\mid\mathbf X,T]=0;\qquad T=m(\mathbf X)+V,\quad\E[V\mid\mathbf X]=0.

Fitting gg with a lasso or a forest and regressing Y−g^(X)Y-\hat g(\mathbf X) on TT fails: the regularization bias of g^\hat g enters the estimating equation to first order, and because flexible learners converge more slowly than n−1/2n^{-1/2}, n(θ^−θ)\sqrt n(\hat\theta-\theta) does not even stay bounded. Chernozhukov, Chetverikov, Demirer, Duflo, Hansen, Newey & Robins (2018) fixed this with two ingredients, which together make up double machine learning.

DerivationWhy the orthogonal score is insensitive to nuisance errors
  1. ψ(W;θ,η)=(Y−ℓ(X)−θ(T−m(X)))(T−m(X)),E[ψ(W;θ0,η0)]=E[UV]=0\psi(W;\theta,\eta)=\Big(Y-\ell(\mathbf X)-\theta\big(T-m(\mathbf X)\big)\Big)\big(T-m(\mathbf X)\big),\qquad\E\big[\psi(W;\theta_0,\eta_0)\big]=\E[UV]=0

    Partial out X\mathbf X from both YY and TT. With ℓ(x)=E[Y∣X=x]=θm(x)+g(x)\ell(\mathbf x)=\E[Y\mid\mathbf X=\mathbf x]=\theta m(\mathbf x)+g(\mathbf x), the score uses the nuisances η=(ℓ,m)\eta=(\ell,m):

  2. E[ψ(W;θ0,η^)]=E[δℓ δm]−θ0 E[δm2]\E\big[\psi(W;\theta_0,\hat\eta)\big]=\E\big[\delta_\ell\,\delta_m\big]-\theta_0\,\E\big[\delta_m^2\big]

    Plug in estimates ℓ^,m^\hat\ell,\hat m fitted on other data. Writing δℓ=ℓ−ℓ^\delta_\ell=\ell-\hat\ell and δm=m−m^\delta_m=m-\hat m, the first factor becomes U+δℓ−θ0δmU+\delta_\ell-\theta_0\delta_m and the second V+δmV+\delta_m. All terms linear in the errors have mean zero because UU and VV have mean zero given X\mathbf X; only products of errors survive:

  3. ∂∂r E[ψ(W;θ0,η0+r(η−η0))]∣r=0=0\frac{\partial}{\partial r}\,\E\Big[\psi\big(W;\theta_0,\eta_0+r(\eta-\eta_0)\big)\Big]\Big|_{r=0}=0

    This is Neyman orthogonality: the derivative of the moment in the direction of the nuisances vanishes at the truth. By contrast, the naive score (Y−g^(X)−θT)T\big(Y-\hat g(\mathbf X)-\theta T\big)T has bias E[(g−g^)(X) T]\E[(g-\hat g)(\mathbf X)\,T], first order in the error. With the orthogonal score, nuisance errors of order o(n−1/4)o(n^{-1/4}) each give a moment bias of o(n−1/2)o(n^{-1/2}), negligible against sampling noise:

  4. θ^=∑i(Ti−m^(Xi))(Yi−ℓ^(Xi))∑i(Ti−m^(Xi))2\hat\theta=\frac{\sum_i\big(T_i-\hat m(\mathbf X_i)\big)\big(Y_i-\hat\ell(\mathbf X_i)\big)}{\sum_i\big(T_i-\hat m(\mathbf X_i)\big)^2}

    Cross-fitting supplies the other data. Split the sample into KK folds, estimate η^\hat\eta on all folds but kk, evaluate the score on fold kk, and solve the averaged equation. This removes the bias that arises when a flexible learner's own overfitting correlates with the noise of the points it is evaluated on, without assumptions limiting the complexity of the learner. In the partially linear model the solution is a residual-on-residual regression:

python
import numpy as np
from sklearn.ensemble import RandomForestRegressor
from sklearn.model_selection import cross_val_predict

# Cross-fitted nuisances: each prediction comes from a model that never saw that row
l_hat = cross_val_predict(RandomForestRegressor(min_samples_leaf=5), X, Y, cv=5)   # E[Y | X]
m_hat = cross_val_predict(RandomForestRegressor(min_samples_leaf=5), X, T, cv=5)   # E[T | X]
y_res, t_res = Y - l_hat, T - m_hat                                                 # partial out X

theta = np.sum(t_res * y_res) / np.sum(t_res ** 2)                                  # residual-on-residual slope
psi = (y_res - theta * t_res) * t_res
se = np.sqrt(np.mean(psi ** 2) / np.mean(t_res ** 2) ** 2 / len(Y))
print(f"theta = {theta:.3f} +/- {1.96 * se:.3f}")                                   # 95% interval

The forests may be heavily regularized and individually biased; the orthogonal score and cross-fitting keep that bias out of θ^\hat\theta to first order. Validity still rests on unconfoundedness: no estimator can remove confounding by variables that are not in X\mathbf X.

Heterogeneous effects

Targeting a treatment needs the conditional effect τ(x)\tau(\mathbf x), a regression on outcomes nobody observes. Meta-learners reduce it to ordinary supervised learning (Künzel, Sekhon, Bickel & Yu, 2019).

LearnerRecipeStrengthWeakness
S-learnerone model μ^(x,t)\hat\mu(\mathbf x,t) with TT as a feature; τ^(x)=μ^(x,1)−μ^(x,0)\hat\tau(\mathbf x)=\hat\mu(\mathbf x,1)-\hat\mu(\mathbf x,0)simple; shares information across armsregularization can shrink the treatment effect toward zero, or ignore TT entirely
T-learnerseparate μ^1,μ^0\hat\mu_1,\hat\mu_0; τ^=μ^1−μ^0\hat\tau=\hat\mu_1-\hat\mu_0flexiblethe difference of two noisy fits; poor when one arm is small
X-learnerimpute effects Di=Yi−μ^0(Xi)D_i=Y_i-\hat\mu_0(\mathbf X_i) for treated and μ^1(Xi)−Yi\hat\mu_1(\mathbf X_i)-Y_i for controls, regress each set on X\mathbf X, and combine the two fits with weights such as e^(x)\hat e(\mathbf x)efficient with unbalanced arms; exploits structure in τ\tau (under regularity conditions, a linear CATE can be learned at the parametric rate even when μ0,μ1\mu_0,\mu_1 are only Lipschitz)more steps; each a possible error source
R-learner (Nie & Wager, 2021)τ^=arg min⁡τ∑i[(Yi−ℓ^(Xi))−(Ti−e^(Xi))τ(Xi)]2+Λ(τ)\hat\tau=\argmin_\tau\sum_i\Big[\big(Y_i-\hat\ell(\mathbf X_i)\big)-\big(T_i-\hat e(\mathbf X_i)\big)\tau(\mathbf X_i)\Big]^2+\Lambda(\tau) with cross-fitted nuisancesorthogonalized; a quasi-oracle property (proved for penalized kernel regression): with moderately accurate nuisances it attains the error bound of an oracle that knows themunstable where e^\hat e is near 0 or 1
No meta-learner dominates; in Künzel et al.'s simulations the X-learner performed favorably but none was uniformly best. Kennedy (2023) analyzed a doubly robust variant (the DR-learner) that regresses cross-fitted AIPW pseudo-outcomes on X\mathbf X.

Evaluating CATE estimators is itself hard: individual effects are never observed, so held-out error must be estimated with pseudo-outcomes (built from AIPW scores or from randomized data) whose noise can swamp differences between models. Honest reporting therefore shows calibration-style checks, such as whether units predicted to benefit most actually do in held-out data, rather than a single error number. The forests of Random Forest Theory supply the underlying asymptotics.

When identification fails: sensitivity analysis

Overlap can be checked (look at the estimated propensities and trim or redefine the population if they pile up near 0 or 1). Unconfoundedness cannot: the data never show the potential outcome a unit did not receive. Sensitivity analysis asks how strong an unmeasured confounder would have to be to change the conclusion. Rosenbaum's model bounds by a factor Γ\Gamma how much two units with the same covariates may differ in their odds of treatment, and reports the largest Γ\Gamma at which the finding survives (Rosenbaum, Observational Studies, 2002). The E-value of VanderWeele & Ding (2017) gives a single number for an observed risk ratio RR>1RR>1:

E-value=RR+RR (RR−1)E\text{-value}=RR+\sqrt{RR\,(RR-1)}
The minimum strength of association, on the risk-ratio scale, that an unmeasured confounder would need with both the treatment and the outcome, beyond the measured covariates, to fully explain away the observed association. For RR<1RR<1, apply the formula to 1/RR1/RR.
Predict before you look

An observational study adjusted for measured confounders finds a risk ratio of 2.0 for disease among the exposed. What is its E-value?

Show the answer

3.41

2+2⋅1=2+1.414≈3.412+\sqrt{2\cdot1}=2+1.414\approx3.41. An unmeasured confounder associated with both exposure and disease by risk ratios of 3.41 each could explain the whole association; weaker confounding could not. Whether such a confounder is plausible is a scientific judgment the number informs but does not make.

Cinelli & Hazlett (2020) express the same question for regression estimates through partial R2R^2 values, which can be benchmarked against observed covariates ("a confounder as strong as income"). When unconfoundedness is implausible altogether, other designs identify effects under different assumptions: instrumental variables, regression discontinuity and difference-in-differences, each with its own untestable conditions.

Key takeaways

  • Potential outcomes define τ=E[Y(1)−Y(0)]\tau=\E[Y(1)-Y(0)] and τ(x)\tau(\mathbf x). Consistency, unconfoundedness and overlap identify them as μ1−μ0\mu_1-\mu_0 or by inverse propensity weighting; the backdoor criterion is the graphical version.
  • AIPW, μ1−μ0+T(Y−μ1)/e−(1−T)(Y−μ0)/(1−e)\mu_1-\mu_0+T(Y-\mu_1)/e-(1-T)(Y-\mu_0)/(1-e), is doubly robust, and its bias is a product of nuisance errors, so two o(n−1/4)o(n^{-1/4}) errors give an o(n−1/2)o(n^{-1/2}) bias.
  • Double machine learning combines a Neyman-orthogonal score with cross-fitting; with product-rate conditions on the nuisances, θ^\hat\theta is n\sqrt n-consistent and asymptotically normal, with the variance of the known-nuisance estimator.
  • Heterogeneous effects: S-, T-, X- and R-learners turn CATE estimation into supervised learning, and honest causal forests are asymptotically Gaussian with infinitesimal-jackknife confidence intervals.
  • Unconfoundedness is untestable: report sensitivity analyses such as Rosenbaum's Γ\Gamma or the E-value, RR+RR(RR−1)RR+\sqrt{RR(RR-1)}.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Which identification assumption cannot be checked from observational data on (X,T,Y)(\mathbf X,T,Y)?

  1. Overlap: 0<e(x)<10<e(\mathbf x)<1
  2. Unconfoundedness: (Y(0),Y(1))⊥ ⁣ ⁣ ⁣⊥T∣X(Y(0),Y(1))\indep T\mid\mathbf X
  3. That the outcome model is well specified
  4. That the treatment is binary
Show the answer

B. Unconfoundedness: (Y(0),Y(1))⊥ ⁣ ⁣ ⁣⊥T∣X(Y(0),Y(1))\indep T\mid\mathbf X

Unconfoundedness is a statement about potential outcomes, one of which is never observed for each unit, so no data on (X,T,Y)(\mathbf X,T,Y) can confirm or refute it. Overlap can be inspected through estimated propensities, and model fit through held-out error.

Question 2

An analyst's propensity model is badly misspecified, but the outcome regressions μ^0,μ^1\hat\mu_0,\hat\mu_1 are consistent. What happens to the AIPW estimate of the ATE?

  1. It is consistent: the bias is the product of the two errors, and one of them vanishes
  2. It is inconsistent, because the weights are wrong
  3. It is consistent only if the propensities are bounded by 0.5
  4. It equals the IPW estimate
Show the answer

A. It is consistent: the bias is the product of the two errors, and one of them vanishes

The bias of each arm is E[(μt−μ^t)(e−e^)/e^]\E\big[(\mu_t-\hat\mu_t)(e-\hat e)/\hat e\big] (with 1−e1-e for the control arm); with μ^t→μt\hat\mu_t\to\mu_t it vanishes even if e^\hat e stays wrong, provided e^\hat e is bounded away from 0 and 1. That is double robustness.

Question 3

In double machine learning, what does cross-fitting accomplish?

  1. It makes the nuisance estimators unbiased
  2. It replaces the orthogonal score
  3. It removes the bias from evaluating flexible nuisance fits on the same points they were trained on, without limiting the learners' complexity
  4. It guarantees unconfoundedness
Show the answer

C. It removes the bias from evaluating flexible nuisance fits on the same points they were trained on, without limiting the learners' complexity

If η^\hat\eta is fitted on the same observations at which the score is evaluated, its overfitting correlates with their noise and biases the estimate. Fitting on the other folds breaks that correlation; orthogonality then handles the remaining (independent) estimation error.

Question 4

A study reports a risk ratio of 3.0 after adjustment. What is the E-value, and what does it mean?

  1. 3.0: a confounder with risk ratio 3 with the outcome alone could explain the effect
  2. About 5.45: a confounder would need risk ratios of about 5.45 with both treatment and outcome to explain the association away
  3. About 1.73: the effect is fragile
  4. It cannot be computed without knowing the confounder
Show the answer

B. About 5.45: a confounder would need risk ratios of about 5.45 with both treatment and outcome to explain the association away

3+3⋅2=3+2.449≈5.453+\sqrt{3\cdot2}=3+2.449\approx5.45. Only confounding at least that strong in both associations, beyond the measured covariates, could fully explain the observed risk ratio.

Further reading

  • P. R. Rosenbaum & D. B. Rubin, "The central role of the propensity score in observational studies for causal effects," Biometrika 70(1), 41–55 (1983).
  • J. M. Robins, A. Rotnitzky & L. P. Zhao, "Estimation of regression coefficients when some regressors are not always observed," Journal of the American Statistical Association 89(427), 846–866 (1994).
  • V. Chernozhukov, D. Chetverikov, M. Demirer, E. Duflo, C. Hansen, W. Newey & J. Robins, "Double/debiased machine learning for treatment and structural parameters," The Econometrics Journal 21(1), C1–C68 (2018).
  • S. Wager & S. Athey, "Estimation and inference of heterogeneous treatment effects using random forests," Journal of the American Statistical Association 113(523), 1228–1242 (2018).
  • S. R. Künzel, J. S. Sekhon, P. J. Bickel & B. Yu, "Metalearners for estimating heterogeneous treatment effects using machine learning," Proceedings of the National Academy of Sciences 116(10), 4156–4165 (2019).
  • G. W. Imbens & D. B. Rubin, Causal Inference for Statistics, Social, and Biomedical Sciences: An Introduction (Cambridge University Press, 2015).