Math for Causal Inference

Day 58. Mediation: Direct & Indirect Effects

How much of an effect runs through a particular mechanism? Controlled and natural direct effects, natural indirect effects, the mediation formula that identifies them, and the strong no-confounding assumptions they need, including the case where regression "controlling for the mediator" goes badly wrong.

Video coming soon
Learning outcomes
  • ●Define total, controlled direct, natural direct and natural indirect effects with potential outcomes.
  • ●Compute them with the mediation formula for binary mediators and with closed-form expressions in linear models with interaction.
  • ●State the identification assumptions and show why mediator–outcome confounding biases the "controlled regression" approach.
  • ●Choose the right mediation quantity for a policy question.
2-HOUR SESSION PLAN
Theory 25′Warm-up (1–5) 20′Proofs (6–10) 35′Applied (11–15) 25′Python & Lab 15′
Colab notebook (.ipynb) Jump to the simulator

Theory

Nested potential outcomes

Let XX be the treatment, MM a mediator and YY the outcome. Write M(x)M(x) for the mediator under treatment xx, and Y(x,m)Y(x, m) for the outcome when XX is set to xx and MM to mm. The observed outcome under treatment xx is Y(x)=Y(x,M(x))Y(x) = Y(x, M(x)).

Four effects (binary XX)

  • Total effect: TE=E[Y(1,M(1))−Y(0,M(0))]TE = E[Y(1, M(1)) - Y(0, M(0))].
  • Controlled direct effect at level mm: CDE(m)=E[Y(1,m)−Y(0,m)]CDE(m) = E[Y(1, m) - Y(0, m)]. Fix the mediator for everyone.
  • Natural direct effect: NDE=E[Y(1,M(0))−Y(0,M(0))]NDE = E[Y(1, M(0)) - Y(0, M(0))]. Change the treatment but keep each unit's mediator at the value it would have had without treatment.
  • Natural indirect effect: NIE=E[Y(1,M(1))−Y(1,M(0))]NIE = E[Y(1, M(1)) - Y(1, M(0))]. Keep the treatment on and shift only the mediator.

They add up: TE=NDE+NIETE = NDE + NIE. The proportion mediated is NIE/TENIE/TE.

Identification: the mediation formula

Assume, possibly within levels of pre-treatment covariates CC: no unmeasured confounding of (1) XX and YY, (2) XX and MM, (3) MM and YY, and (4) no confounder of MM and YY that is itself affected by XX. Then (Pearl's mediation formula)

NDE=∑m(E[Y∣X=1,m]−E[Y∣X=0,m]) P(m∣X=0),NDE = \sum_m \big(E[Y \mid X = 1, m] - E[Y \mid X = 0, m]\big)\,P(m \mid X = 0), NIE=∑mE[Y∣X=1,m] (P(m∣X=1)−P(m∣X=0)).NIE = \sum_m E[Y \mid X = 1, m]\,\big(P(m \mid X = 1) - P(m \mid X = 0)\big).

CDE(m)=E[Y∣X=1,m]−E[Y∣X=0,m]CDE(m) = E[Y \mid X = 1, m] - E[Y \mid X = 0, m] needs only assumptions (1) and (3) (and can be identified even when (4) fails, by the g-formula).

Linear models with interaction

If E[M∣x]=β0+β1xE[M \mid x] = \beta_0 + \beta_1 x and E[Y∣x,m]=θ0+θ1x+θ2m+θ3xmE[Y \mid x, m] = \theta_0 + \theta_1 x + \theta_2 m + \theta_3 xm:

CDE(m)=θ1+θ3m,NDE=θ1+θ3β0,NIE=(θ2+θ3)β1.CDE(m) = \theta_1 + \theta_3 m, \qquad NDE = \theta_1 + \theta_3\beta_0, \qquad NIE = (\theta_2 + \theta_3)\beta_1.

Without interaction (θ3=0\theta_3 = 0): NDE=CDE=θ1NDE = CDE = \theta_1 and NIE=θ2β1NIE = \theta_2\beta_1, the "product of coefficients" of Baron and Kenny.

The trap

"Regress YY on XX and MM and call the XX coefficient the direct effect" is right only under assumption (3). If something unobserved affects both MM and YY, MM is a collider on X→M←U→YX \to M \leftarrow U \to Y, and the coefficient is biased (Day 53, Exercise 9); it can even flip sign.

Part 1 · Warm-up

Exercise 1 · Read the potential outcomes

In words, what is Y(1,M(0))Y(1, M(0)) for a jobseeker in a training programme where MM is the number of job applications sent?

Show solution

Step 1. First argument: the jobseeker receives training.

Step 2. Second argument: applications are held at the number they would have sent without training.

Answer: earnings with training but without training's effect on job search, so only the skill (direct) channel operates. It is a cross-world quantity: never observed for anyone.

Exercise 2 · Linear, no interaction

M=3+0.5X+eMM = 3 + 0.5X + e_M and Y=2+1.0X+2.0M+eYY = 2 + 1.0X + 2.0M + e_Y, with no confounding. Find NDENDE, NIENIE and TETE.

Show solution

Step 1. NDE=θ1=1.0NDE = \theta_1 = 1.0.

Step 2. NIE=θ2β1=2.0×0.5=1.0NIE = \theta_2\beta_1 = 2.0 \times 0.5 = 1.0.

Step 3. TE=1.0+1.0=2.0TE = 1.0 + 1.0 = 2.0.

Exercise 3 · Linear with interaction

E[M∣x]=1+0.5xE[M \mid x] = 1 + 0.5x and E[Y∣x,m]=θ0+1.0x+2.0m+0.5xmE[Y \mid x, m] = \theta_0 + 1.0x + 2.0m + 0.5xm. Compute CDE(0)CDE(0), CDE(2)CDE(2), NDENDE, NIENIE and TETE.

Show solution

Step 1. CDE(0)=1.0CDE(0) = 1.0; CDE(2)=1.0+0.5(2)=2.0CDE(2) = 1.0 + 0.5(2) = 2.0.

Step 2. NDE=θ1+θ3β0=1.0+0.5(1)=1.5NDE = \theta_1 + \theta_3\beta_0 = 1.0 + 0.5(1) = 1.5.

Step 3. NIE=(θ2+θ3)β1=2.5×0.5=1.25NIE = (\theta_2 + \theta_3)\beta_1 = 2.5 \times 0.5 = 1.25.

Step 4. TE=1.5+1.25=2.75TE = 1.5 + 1.25 = 2.75. Check directly: E[Y∣do⁡(1)]−E[Y∣do⁡(0)]=[1.0+2.5(1.5)]−[2.0(1)]=4.75−2=2.75E[Y \mid \operatorname{do}(1)] - E[Y \mid \operatorname{do}(0)] = [1.0 + 2.5(1.5)] - [2.0(1)] = 4.75 - 2 = 2.75.

Exercise 4 · Proportion mediated

In Exercise 2, what share of the effect runs through MM? In Exercise 3?

Show solution

Step 1. Exercise 2: 1.0/2.0=50%1.0/2.0 = 50\%.

Step 2. Exercise 3: 1.25/2.75≈45%1.25/2.75 \approx 45\%.

Exercise 5 · Which effect answers which question?

Match: (a) "What would the effect of training be if we also guaranteed every trainee exactly 10 applications?" (b) "How much of training's effect is due to its effect on job search?" (c) "Does training work at all?"

Show solution

Step 1. (a) CDE(10)CDE(10): the mediator is fixed by policy.

Step 2. (b) NIENIE (or the proportion mediated): a mechanism question.

Step 3. (c) TETE.

Part 2 · Proofs

Exercise 6 · The decomposition

Prove TE=NDE+NIETE = NDE + NIE.

Show solution

Step 1. Add and subtract Y(1,M(0))Y(1, M(0)): Y(1,M(1))−Y(0,M(0))=[Y(1,M(1))−Y(1,M(0))]+[Y(1,M(0))−Y(0,M(0))]Y(1, M(1)) - Y(0, M(0)) = [Y(1, M(1)) - Y(1, M(0))] + [Y(1, M(0)) - Y(0, M(0))].

Step 2. Take expectations: the first bracket is NIENIE, the second NDENDE. ■\blacksquare

Exercise 7 · Deriving the mediation formula for the NDE

Assume (a) Y(x,m)⊥ ⁣ ⁣ ⁣⊥XY(x, m) \perp\!\!\!\perp X, (b) M(x)⊥ ⁣ ⁣ ⁣⊥XM(x) \perp\!\!\!\perp X, (c) Y(x,m)⊥ ⁣ ⁣ ⁣⊥M∣XY(x, m) \perp\!\!\!\perp M \mid X, and (d) Y(x,m)⊥ ⁣ ⁣ ⁣⊥M(x′)Y(x, m) \perp\!\!\!\perp M(x') (the cross-world assumption). Prove the NDE formula.

Show solution

Step 1. E[Y(1,M(0))]=∑mE[Y(1,m)∣M(0)=m] P(M(0)=m)E[Y(1, M(0))] = \sum_m E[Y(1, m) \mid M(0) = m]\,P(M(0) = m).

Step 2. By (d), E[Y(1,m)∣M(0)=m]=E[Y(1,m)]E[Y(1, m) \mid M(0) = m] = E[Y(1, m)].

Step 3. By (a) and (c), E[Y(1,m)]=E[Y(1,m)∣X=1]=E[Y(1,m)∣X=1,M=m]=E[Y∣X=1,M=m]E[Y(1, m)] = E[Y(1, m) \mid X = 1] = E[Y(1, m) \mid X = 1, M = m] = E[Y \mid X = 1, M = m] (consistency).

Step 4. By (b), P(M(0)=m)=P(M(0)=m∣X=0)=P(m∣X=0)P(M(0) = m) = P(M(0) = m \mid X = 0) = P(m \mid X = 0).

Step 5. So E[Y(1,M(0))]=∑mE[Y∣1,m]P(m∣0)E[Y(1, M(0))] = \sum_m E[Y \mid 1, m]P(m \mid 0); similarly E[Y(0,M(0))]=∑mE[Y∣0,m]P(m∣0)E[Y(0, M(0))] = \sum_m E[Y \mid 0, m]P(m \mid 0). Subtract. ■\blacksquare

Exercise 8 · Linear formulas

With E[M∣x]=β0+β1xE[M \mid x] = \beta_0 + \beta_1 x and E[Y∣x,m]=θ0+θ1x+θ2m+θ3xmE[Y \mid x, m] = \theta_0 + \theta_1 x + \theta_2 m + \theta_3 xm, derive NDENDE and NIENIE from the mediation formula.

Show solution

Step 1. E[Y∣1,m]−E[Y∣0,m]=θ1+θ3mE[Y \mid 1, m] - E[Y \mid 0, m] = \theta_1 + \theta_3 m.

Step 2. NDE=∑m(θ1+θ3m)P(m∣0)=θ1+θ3E[M∣X=0]=θ1+θ3β0NDE = \sum_m(\theta_1 + \theta_3 m)P(m \mid 0) = \theta_1 + \theta_3 E[M \mid X = 0] = \theta_1 + \theta_3\beta_0.

Step 3. NIE=∑m(θ0+θ1+(θ2+θ3)m)(P(m∣1)−P(m∣0))NIE = \sum_m(\theta_0 + \theta_1 + (\theta_2 + \theta_3)m)\big(P(m \mid 1) - P(m \mid 0)\big).

Step 4. Constants sum to zero against a difference of probabilities; the mm term gives (θ2+θ3)(E[M∣1]−E[M∣0])=(θ2+θ3)β1(\theta_2 + \theta_3)(E[M \mid 1] - E[M \mid 0]) = (\theta_2 + \theta_3)\beta_1. ■\blacksquare

Exercise 9 · No interaction, one direct effect

Show that if E[Y∣x,m]E[Y \mid x, m] has no x×mx \times m interaction, then CDE(m)=NDECDE(m) = NDE for every mm.

Show solution

Step 1. No interaction: E[Y∣1,m]−E[Y∣0,m]=cE[Y \mid 1, m] - E[Y \mid 0, m] = c, a constant.

Step 2. CDE(m)=cCDE(m) = c for all mm.

Step 3. NDE=∑mc P(m∣0)=cNDE = \sum_m c\,P(m \mid 0) = c. ■\blacksquare

Exercise 10 · A treatment-induced mediator–outcome confounder

Let X→L→M→YX \to L \to M \to Y, L→YL \to Y, X→MX \to M, X→YX \to Y (all observed). Explain why CDE(m)CDE(m) is identified but the NDENDE is not, without further assumptions.

Show solution

Step 1. CDE(m)=E[Y∣do⁡(x,m)]CDE(m) = E[Y \mid \operatorname{do}(x, m)] is a joint intervention; with XX unconfounded and {X,L}\{X, L\} blocking the back-door paths of MM, the g-formula gives E[Y∣do⁡(x,m)]=∑lE[Y∣x,l,m]P(l∣x)E[Y \mid \operatorname{do}(x, m)] = \sum_l E[Y \mid x, l, m]P(l \mid x) (Day 55, Exercise 13).

Step 2. The NDE needs E[Y(1,M(0))]E[Y(1, M(0))], which involves L(1)L(1) (through the direct path X→L→YX \to L \to Y) jointly with M(0)M(0), which depends on L(0)L(0).

Step 3. The joint distribution of L(1)L(1) and L(0)L(0) for the same unit is never observed and is not fixed by any observable distribution (a "recanting witness").

Answer: CDEs are identified; natural effects need extra assumptions or are reported with bounds. ■\blacksquare

Part 3 · Applied

Exercise 11 · Training, job finding and earnings

A training programme XX may raise monthly earnings YY ($100s) directly (skills) and by speeding up job finding MM (found a job within 3 months). With no confounding: P(M=1∣X=0)=0.4P(M = 1 \mid X = 0) = 0.4, P(M=1∣X=1)=0.6P(M = 1 \mid X = 1) = 0.6; E[Y∣X,M]E[Y \mid X, M]: (0,0)(0, 0): 10, (0,1)(0, 1): 20, (1,0)(1, 0): 12, (1,1)(1, 1): 24. Compute TETE, NDENDE, NIENIE, CDE(0)CDE(0), CDE(1)CDE(1) and the proportion mediated.

Show solution

Step 1. E[Y∣X=1]=0.4(12)+0.6(24)=19.2E[Y \mid X = 1] = 0.4(12) + 0.6(24) = 19.2; E[Y∣X=0]=0.6(10)+0.4(20)=14E[Y \mid X = 0] = 0.6(10) + 0.4(20) = 14. TE=5.2TE = 5.2.

Step 2. NDE=(12−10)(0.6)+(24−20)(0.4)=1.2+1.6=2.8NDE = (12 - 10)(0.6) + (24 - 20)(0.4) = 1.2 + 1.6 = 2.8.

Step 3. NIE=24(0.6−0.4)+12(0.4−0.6)=4.8−2.4=2.4NIE = 24(0.6 - 0.4) + 12(0.4 - 0.6) = 4.8 - 2.4 = 2.4.

Step 4. Check: 2.8+2.4=5.22.8 + 2.4 = 5.2. CDE(0)=2CDE(0) = 2, CDE(1)=4CDE(1) = 4.

Answer: about 46% of the effect (2.4/5.22.4/5.2) runs through faster job finding. The direct effect depends on the mediator level (2 or 4), so NDENDE is a weighted average of the CDEs.

Exercise 12 · Cash transfers, food spending and child growth

A cash transfer XX raises monthly food spending MM ($): E[M∣x]=50+20xE[M \mid x] = 50 + 20x. Child height-for-age z-scores follow E[Y∣x,m]=θ0+0.10x+0.004m+0.002xmE[Y \mid x, m] = \theta_0 + 0.10x + 0.004m + 0.002xm. Assume no unmeasured confounding. Compute the effects and the proportion mediated by food spending.

Show solution

Step 1. NDE=0.10+0.002×50=0.20NDE = 0.10 + 0.002 \times 50 = 0.20.

Step 2. NIE=(0.004+0.002)×20=0.12NIE = (0.004 + 0.002) \times 20 = 0.12.

Step 3. TE=0.32TE = 0.32; proportion mediated 0.12/0.32=37.5%0.12/0.32 = 37.5\%.

Answer: most of the effect runs through other channels (health care, schooling, reduced stress); the interaction means that food spending matters more for treated households.

Exercise 13 · Smoking, birth weight and infant mortality

Return to Day 48's paradox in mediation form. In a linear model, MM = birth weight (standardised), UU = birth defects (unobserved): M=−X−U+eMM = -X - U + e_M, Y=0.2X−0.5M+1.0U+eYY = 0.2X - 0.5M + 1.0U + e_Y, with Var⁡(U)=Var⁡(eM)=1\operatorname{Var}(U) = \operatorname{Var}(e_M) = 1. What is the true direct effect of smoking, and what does the regression of mortality on smoking and birth weight report?

Show solution

Step 1. True direct effect: θ1=0.2\theta_1 = 0.2 (harmful).

Step 2. Given XX and MM, −U+eM=M+X-U + e_M = M + X is known, so E[U∣X,M]=−k(M+X)E[U \mid X, M] = -k(M + X) with k=1/(1+1)=0.5k = 1/(1 + 1) = 0.5.

Step 3. E[Y∣X,M]=0.2X−0.5M+1.0×(−0.5)(M+X)=−0.3X−1.0ME[Y \mid X, M] = 0.2X - 0.5M + 1.0 \times (-0.5)(M + X) = -0.3X - 1.0M.

Answer: the regression reports −0.3-0.3: smoking looks protective holding birth weight fixed, because birth weight is a collider of smoking and defects. The simulation confirms −0.3.

Exercise 14 · Occupation and the gender pay gap

A study regresses wages on gender, "controlling for occupation", and calls the remaining gap "discrimination" (a direct effect). Draw the DAG with gender GG, occupation OO, wage WW and unobserved ability or preferences AA that affect both occupation and wages. What is wrong, and which quantity could be defended?

Show solution

Step 1. G→O→WG \to O \to W, G→WG \to W, A→OA \to O, A→WA \to W.

Step 2. Occupation is a mediator and a collider (G→O←AG \to O \leftarrow A). Conditioning on it opens G→O←A→WG \to O \leftarrow A \to W, so the "within-occupation gap" mixes discrimination with ability differences induced by selection into occupations.

Step 3. The total gap TETE is identified (gender is not caused by the other variables); the CDE given occupation needs AA measured.

Answer: report the total gap, and treat within-occupation gaps as descriptive unless the occupation–wage confounders are measured. Note also that occupational sorting can itself reflect discrimination, so the "indirect" part is not necessarily benign.

Exercise 15 · Obesity, hypertension and stroke

A health ministry can fund either a weight-loss programme or a hypertension-control programme. Researchers estimate the effect of obesity XX on stroke YY and the part mediated by hypertension MM. Which mediation quantity answers "how many strokes would remain among obese people if everyone's blood pressure were controlled?" What assumptions does it need?

Show solution

Step 1. Controlling blood pressure fixes the mediator at a normal level m0m_0 for everyone: CDE(m0)CDE(m_0) is the effect of obesity that remains.

Step 2. Strokes prevented by hypertension control among the obese: E[Y(1)]−E[Y(1,m0)]E[Y(1)] - E[Y(1, m_0)], which needs the same assumptions.

Step 3. Assumptions: no unmeasured confounding of obesity and stroke, and of hypertension and stroke given obesity and covariates (e.g. diet, smoking, kidney function); no cross-world assumption is needed.

Answer: the CDE answers the policy question; natural effects answer the mechanism question and need stronger assumptions.

Verify in Python

import numpy as np

# Exercise 11: mediation formula with a binary mediator
pM = {0: .4, 1: .6}; EY = {(0, 0): 10, (0, 1): 20, (1, 0): 12, (1, 1): 24}
P = lambda m, x: pM[x] if m else 1 - pM[x]
TE = sum(EY[1, m] * P(m, 1) for m in (0, 1)) - sum(EY[0, m] * P(m, 0) for m in (0, 1))
NDE = sum((EY[1, m] - EY[0, m]) * P(m, 0) for m in (0, 1))
NIE = sum(EY[1, m] * (P(m, 1) - P(m, 0)) for m in (0, 1))
print(f"TE {TE:.2f}  NDE {NDE:.2f}  NIE {NIE:.2f}  proportion mediated {NIE / TE:.2f}")

# Exercises 3 and 12: linear formulas, checked by simulating nested potential outcomes
def effects(b0, b1, t1, t2, t3, n=1_000_000, seed=58):
    rng = np.random.default_rng(seed)
    eM = rng.normal(size=n); eY = rng.normal(size=n)
    M = lambda x: b0 + b1 * x + eM
    Y = lambda x, m: t1 * x + t2 * m + t3 * x * m + eY
    return (Y(1, M(0)) - Y(0, M(0))).mean(), (Y(1, M(1)) - Y(1, M(0))).mean()
for args in [(1, .5, 1, 2, .5), (50, 20, .10, .004, .002)]:
    b0, b1, t1, t2, t3 = args
    nde, nie = effects(*args)
    print(f"simulated NDE {nde:.3f} (formula {t1 + t3 * b0:.3f}), NIE {nie:.3f} (formula {(t2 + t3) * b1:.3f})")

# Exercise 13: regression 'direct effect' with a confounded mediator
rng = np.random.default_rng(13); n = 1_000_000
X = rng.normal(size=n); U = rng.normal(size=n)
M = -X - U + rng.normal(size=n); Y = .2 * X - .5 * M + 1.0 * U + rng.normal(size=n)
coef = np.linalg.lstsq(np.column_stack([np.ones(n), X, M]), Y, rcond=None)[0]
print(f"coefficient on smoking given birth weight: {coef[1]:.3f} (true direct effect 0.2)")

Key takeaways

  • TE=NDE+NIETE = NDE + NIE; controlled direct effects fix the mediator by policy, natural effects describe mechanisms.
  • The mediation formula identifies natural effects under no confounding of XX–YY, XX–MM and MM–YY, plus a cross-world assumption that fails when treatment affects a mediator–outcome confounder.
  • "Controlling for the mediator" gives a direct effect only if the mediator is unconfounded with the outcome; otherwise it is collider bias.

Simulation Lab: Direct & Indirect Effects

Open full screen →

Controlled and natural direct effects, the indirect effect, and the bias from an unmeasured mediator–outcome confounder.

0123-2-101234mediator level meffectNDE 1.50TE 2.75E[M | X = 0]
CDE(m)NDE = CDE at E[M|X=0]total effect
NDE
1.50
estimated 1.50
NIE
1.25
estimated 1.25
TE
2.75
mediated 45%

M = β0 + β1·X + g·U + e_M and Y = θ1·X + θ2·M + θ3·X·M + c·U + e_Y. The controlled direct effect CDE(m) = θ1 + θ3·m varies with the mediator level (line); NDE and NIE come from the mediation formula. With an unmeasured mediator–outcome confounder U (g, c ≠ 0), estimates from the regression of Y on X, M and X·M (dashed) are biased.

Day 57Day 59