Math for Causal Inference

Day 3. Random Variables, Expectation & Variance

Turning outcomes into numbers, and the two summaries every estimator is built from.

Video coming soon
Learning outcomes
  • ●Compute expectations and variances for discrete and continuous random variables.
  • ●Prove linearity of expectation and the variance rules for linear transformations.
  • ●Define the average treatment effect as an expectation of potential outcomes.
  • ●Use Jensen's inequality to reason about non-linear functions.
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

Random variables

A random variable XX is a function from the sample space to the real numbers, X:Ω→RX: \Omega \to \mathbb{R}. It turns outcomes ("the firm adopted clean tech") into numbers (emissions in tonnes). Its distribution is described by a probability mass function p(x)=P(X=x)p(x) = P(X = x) (discrete) or a density f(x)f(x) (continuous), with P(a≤X≤b)=∫abf(x) dxP(a \le X \le b) = \int_a^b f(x)\,dx.

Expectation

The expected value is the probability-weighted average:

E[X]=∑xx p(x)orE[X]=∫−∞∞xf(x) dx.E[X] = \sum_x x\,p(x) \qquad\text{or}\qquad E[X] = \int_{-\infty}^{\infty} x f(x)\,dx.

For any function gg, E[g(X)]=∑xg(x)p(x)E[g(X)] = \sum_x g(x)p(x) (the "law of the unconscious statistician").

Linearity of expectation holds for any random variables, dependent or not:

E[aX+bY+c]=aE[X]+bE[Y]+c.E[aX + bY + c] = aE[X] + bE[Y] + c.

Variance

Var⁡(X)=E[(X−E[X])2]=E[X2]−(E[X])2,Var⁡(aX+b)=a2Var⁡(X).\operatorname{Var}(X) = E\big[(X - E[X])^2\big] = E[X^2] - (E[X])^2, \qquad \operatorname{Var}(aX + b) = a^2 \operatorname{Var}(X).

The standard deviation sd⁡(X)=Var⁡(X)\operatorname{sd}(X) = \sqrt{\operatorname{Var}(X)} is in the units of XX. Shifting a variable moves its centre but not its spread; scaling by aa scales the spread by ∣a∣|a|.

Jensen's inequality

If gg is convex, E[g(X)]≥g(E[X])E[g(X)] \ge g(E[X]); if concave, the inequality flips. In general E[g(X)]≠g(E[X])E[g(X)] \ne g(E[X]), which is why the average of logs is not the log of the average (Day 12 builds on this).

Potential outcomes as random variables

Each unit has two potential outcomes: Y(1)Y(1) if treated and Y(0)Y(0) if not. The average treatment effect is an expectation:

τATE=E[Y(1)−Y(0)]=E[Y(1)]−E[Y(0)],\tau_{ATE} = E[Y(1) - Y(0)] = E[Y(1)] - E[Y(0)],

where the second equality is linearity. We will never observe Y(1)−Y(0)Y(1) - Y(0) for anyone, but linearity tells us we only need the two means separately.

Part 1 · Warm-up

Exercise 1 · Expected fine

A random inspection yields a fine of $0 with probability 0.6, $500 with probability 0.3 and $2,000 with probability 0.1. Find E[X]E[X].

Show solution E[X]=0(0.6)+500(0.3)+2000(0.1)=0+150+200=350.E[X] = 0(0.6) + 500(0.3) + 2000(0.1) = 0 + 150 + 200 = 350.

Exercise 2 · A second moment

For the same fine, compute E[X2]E[X^2].

Show solution E[X2]=02(0.6)+5002(0.3)+20002(0.1)=75,000+400,000=475,000.E[X^2] = 0^2(0.6) + 500^2(0.3) + 2000^2(0.1) = 75{,}000 + 400{,}000 = 475{,}000.

Exercise 3 · Variance by the shortcut

Compute Var⁡(X)\operatorname{Var}(X) and sd⁡(X)\operatorname{sd}(X).

Show solution Var⁡(X)=475,000−3502=475,000−122,500=352,500,\operatorname{Var}(X) = 475{,}000 - 350^2 = 475{,}000 - 122{,}500 = 352{,}500,

so sd⁡(X)=352,500≈593.7\operatorname{sd}(X) = \sqrt{352{,}500} \approx 593.7 dollars.

Exercise 4 · Linearity

Tax revenue is Y=15X−100Y = 15X - 100, where XX is emissions with E[X]=1,200E[X] = 1{,}200 tonnes. Find E[Y]E[Y].

Show solution

E[Y]=15(1200)−100=17,900E[Y] = 15(1200) - 100 = 17{,}900.

Exercise 5 · Variance of a linear transformation

If Var⁡(X)=400\operatorname{Var}(X) = 400, what are Var⁡(Y)\operatorname{Var}(Y) and sd⁡(Y)\operatorname{sd}(Y)?

Show solution

Step 1. Use Var⁡(aX+b)=a2Var⁡(X)\operatorname{Var}(aX + b) = a^2\operatorname{Var}(X) with a=15a = 15, b=−100b = -100:

Var⁡(Y)=152×400=225×400=90,000.\operatorname{Var}(Y) = 15^2 \times 400 = 225 \times 400 = 90{,}000.

Step 2. Standard deviation: sd⁡(Y)=∣15∣×400=15×20=300\operatorname{sd}(Y) = |15| \times \sqrt{400} = 15 \times 20 = 300.

Answer: variance 90,000 and sd 300. The constant −100-100 shifts revenue but does not change its spread.

Part 2 · Proofs

Exercise 6 · Linearity with constants

Prove E[aX+b]=aE[X]+bE[aX + b] = aE[X] + b for a discrete XX.

Show solution E[aX+b]=∑x(ax+b)p(x)=a∑xx p(x)+b∑xp(x)=aE[X]+b,E[aX + b] = \sum_x (ax + b)p(x) = a\sum_x x\,p(x) + b\sum_x p(x) = aE[X] + b,

using ∑xp(x)=1\sum_x p(x) = 1.

Exercise 7 · The variance shortcut

Prove Var⁡(X)=E[X2]−(E[X])2\operatorname{Var}(X) = E[X^2] - (E[X])^2.

Show solution

Let μ=E[X]\mu = E[X]. Expand the square and use linearity:

E[(X−μ)2]=E[X2]−2μE[X]+μ2=E[X2]−2μ2+μ2=E[X2]−μ2.E[(X - \mu)^2] = E[X^2] - 2\mu E[X] + \mu^2 = E[X^2] - 2\mu^2 + \mu^2 = E[X^2] - \mu^2.

Exercise 8 · Variance scaling

Prove Var⁡(aX+b)=a2Var⁡(X)\operatorname{Var}(aX + b) = a^2\operatorname{Var}(X).

Show solution

E[aX+b]=aμ+bE[aX + b] = a\mu + b, so

Var⁡(aX+b)=E[(aX+b−aμ−b)2]=E[a2(X−μ)2]=a2Var⁡(X).\operatorname{Var}(aX + b) = E\big[(aX + b - a\mu - b)^2\big] = E\big[a^2 (X - \mu)^2\big] = a^2\operatorname{Var}(X).

Exercise 9 · Indicator variables

Let 1A\mathbf{1}_A equal 1 if AA happens and 0 otherwise. Show that E[1A]=P(A)E[\mathbf{1}_A] = P(A) and Var⁡(1A)=P(A)(1−P(A))\operatorname{Var}(\mathbf{1}_A) = P(A)(1 - P(A)).

Show solution

Step 1. The indicator takes the value 1 with probability P(A)P(A) and 0 otherwise:

E[1A]=1⋅P(A)+0⋅P(Ac)=P(A).E[\mathbf{1}_A] = 1 \cdot P(A) + 0 \cdot P(A^c) = P(A).

Step 2. Since 12=11^2 = 1 and 02=00^2 = 0, 1A2=1A\mathbf{1}_A^2 = \mathbf{1}_A, so E[1A2]=P(A)E[\mathbf{1}_A^2] = P(A).

Step 3. Variance shortcut:

Var⁡(1A)=E[1A2]−(E[1A])2=P(A)−P(A)2=P(A)(1−P(A)).\operatorname{Var}(\mathbf{1}_A) = E[\mathbf{1}_A^2] - (E[\mathbf{1}_A])^2 = P(A) - P(A)^2 = P(A)(1 - P(A)).

This is why a share (e.g. "share treated") is simply the mean of a 0/1 variable.

Exercise 10 · The mean is the best constant prediction

Show that E[(X−c)2]=Var⁡(X)+(E[X]−c)2E[(X - c)^2] = \operatorname{Var}(X) + (E[X] - c)^2, and conclude that c=E[X]c = E[X] minimises the mean squared error.

Show solution

Step 1. Write X−c=(X−μ)+(μ−c)X - c = (X - \mu) + (\mu - c) and expand:

E[(X−c)2]=E[(X−μ)2]+2(μ−c)E[X−μ]⏟=0+(μ−c)2=Var⁡(X)+(μ−c)2.E[(X - c)^2] = E[(X - \mu)^2] + 2(\mu - c)\underbrace{E[X - \mu]}_{=0} + (\mu - c)^2 = \operatorname{Var}(X) + (\mu - c)^2.

Step 2. The second term is non-negative and zero only at c=μc = \mu.

Step 3. This is the seed of least squares: regression generalises "predict with the mean" to "predict with a function of XX" (Day 6).

Part 3 · Applied

Exercise 11 · An average treatment effect

A (hypothetical) table shows both potential outcomes for four workers:

WorkerY(1)Y(1)Y(0)Y(0)
1107
288
3129
464

Compute each individual effect, the ATE, and check that ATE=E[Y(1)]−E[Y(0)]\text{ATE} = E[Y(1)] - E[Y(0)].

Show solution

Step 1. Individual effects Y(1)−Y(0)Y(1) - Y(0): 10−7=310 - 7 = 3, 8−8=08 - 8 = 0, 12−9=312 - 9 = 3, 6−4=26 - 4 = 2.

Step 2. ATE = average of the individual effects: (3+0+3+2)/4=2(3 + 0 + 3 + 2)/4 = 2.

Step 3. Means of each potential outcome: E[Y(1)]=(10+8+12+6)/4=9E[Y(1)] = (10 + 8 + 12 + 6)/4 = 9 and E[Y(0)]=(7+8+9+4)/4=7E[Y(0)] = (7 + 8 + 9 + 4)/4 = 7.

Step 4. Difference of means: 9−7=29 - 7 = 2. ✓

Answer: ATE = 2 either way, as linearity of expectation promises.

Exercise 12 · How much to treat

A pilot cash-transfer programme assigns each eligible household to treatment with probability pp, so T∼Bernoulli(p)T \sim \text{Bernoulli}(p). Compute Var⁡(T)\operatorname{Var}(T) when p=0.3p = 0.3 and find the pp that maximises it. Why might the evaluation team care?

Show solution

Step 1. By Exercise 9, Var⁡(T)=p(1−p)\operatorname{Var}(T) = p(1 - p). At p=0.3p = 0.3: 0.3×0.7=0.210.3 \times 0.7 = 0.21.

Step 2. Maximise g(p)=p−p2g(p) = p - p^2: g′(p)=1−2p=0⇒p=0.5g'(p) = 1 - 2p = 0 \Rightarrow p = 0.5, and g′′(p)=−2<0g''(p) = -2 < 0, so it is a maximum.

Step 3. Maximum value: 0.5×0.5=0.250.5 \times 0.5 = 0.25.

Answer: 0.21 at p=0.3p = 0.3; maximised at p=0.5p = 0.5 (value 0.25). More variation in treatment means more information about its effect, so a 50/50 split gives the most precise estimate for a given number of households (Day 10).

Exercise 13 · A continuous voucher

A voucher amount is uniform on [0,10][0, 10] (thousand dollars), with density f(x)=1/10f(x) = 1/10. Compute E[X]E[X] and Var⁡(X)\operatorname{Var}(X) by integration.

Show solution

The density is f(x)=1/10f(x) = 1/10 on [0,10][0, 10].

Step 1. Mean:

E[X]=∫010x⋅110 dx=110[x22]010=110⋅50=5.E[X] = \int_0^{10} x\cdot\frac{1}{10}\,dx = \frac{1}{10}\Big[\frac{x^2}{2}\Big]_0^{10} = \frac{1}{10}\cdot 50 = 5.

Step 2. Second moment:

E[X2]=∫010x2⋅110 dx=110[x33]010=100030≈33.33.E[X^2] = \int_0^{10} x^2\cdot\frac{1}{10}\,dx = \frac{1}{10}\Big[\frac{x^3}{3}\Big]_0^{10} = \frac{1000}{30} \approx 33.33.

Step 3. Variance: Var⁡(X)=33.33−52=8.33\operatorname{Var}(X) = 33.33 - 5^2 = 8.33.

Answer: mean $5k and variance 8.33 (sd ≈ $2.9k), matching the formula (b−a)2/12=100/12(b - a)^2/12 = 100/12.

Exercise 14 · Risk aversion and Jensen

A household's income is $50k or $150k with equal probability, and its utility is u(y)=ln⁡yu(y) = \ln y. Compare E[ln⁡Y]E[\ln Y] with ln⁡E[Y]\ln E[Y] and find the certainty equivalent.

Show solution

Step 1. Expected utility:

E[ln⁡Y]=12ln⁡50+12ln⁡150≈12(3.9120+5.0106)=4.4613.E[\ln Y] = \tfrac12\ln 50 + \tfrac12\ln 150 \approx \tfrac12(3.9120 + 5.0106) = 4.4613.

Step 2. Utility of the expected income: E[Y]=100E[Y] = 100, so ln⁡E[Y]=ln⁡100≈4.6052\ln E[Y] = \ln 100 \approx 4.6052.

Step 3. Compare: 4.4613<4.60524.4613 < 4.6052, as Jensen's inequality predicts for the concave ln⁡\ln.

Step 4. The certainty equivalent cc gives the same utility as the gamble: ln⁡c=4.4613\ln c = 4.4613, so

c=e4.4613=50×150≈86.6.c = e^{4.4613} = \sqrt{50 \times 150} \approx 86.6.

Answer: the household values the gamble like a sure $86.6k, although it is worth $100k on average. The $13.4k gap is the risk premium.

Exercise 15 · Effect heterogeneity is not identified

For the four workers in Exercise 11, compute Var⁡(Y(1)−Y(0))\operatorname{Var}(Y(1) - Y(0)). Could you compute it from real data, where each worker shows only one potential outcome?

Show solution

Step 1. Individual effects from Exercise 11: 3, 0, 3, 2, with mean 2.

Step 2. Deviations from the mean: 1, −2, 1, 0; squared: 1, 4, 1, 0.

Step 3. Population variance: (1+4+1+0)/4=1.5(1 + 4 + 1 + 0)/4 = 1.5.

Step 4. Could real data give this? Expand the variance of a difference:

Var⁡(Y(1)−Y(0))=Var⁡(Y(1))+Var⁡(Y(0))−2Cov⁡(Y(1),Y(0)).\operatorname{Var}(Y(1) - Y(0)) = \operatorname{Var}(Y(1)) + \operatorname{Var}(Y(0)) - 2\operatorname{Cov}(Y(1), Y(0)).

The two variances can be estimated from the treated and control groups, but the covariance needs both outcomes for the same worker, which we never see.

Answer: 1.5 in this table, but in real data the variance of effects is not identified. Means are identified by linearity; the spread of effects is not.

Verify in Python

import numpy as np

x = np.array([0, 500, 2000]); p = np.array([0.6, 0.3, 0.1])
ex, ex2 = (x * p).sum(), (x**2 * p).sum()
print("E[X] =", ex, " E[X^2] =", ex2, " Var =", ex2 - ex**2)

y1 = np.array([10, 8, 12, 6]); y0 = np.array([7, 8, 9, 4])
print("ATE =", (y1 - y0).mean(), "  Var of effects =", (y1 - y0).var())

# Jensen: average of logs < log of average
inc = np.array([50, 150])
print("E[ln Y] =", np.log(inc).mean().round(4), " ln E[Y] =", np.log(inc.mean()).round(4),
      " certainty equivalent =", np.exp(np.log(inc).mean()).round(2))

# Linearity check by simulation: Y = 15X - 100 with Var(X) = 400
rng = np.random.default_rng(0)
X = rng.normal(1200, 20, 1_000_000); Y = 15 * X - 100
print("E[Y] ~", Y.mean().round(1), " Var(Y) ~", Y.var().round(0))

Key takeaways

  • Expectation is linear for any random variables; variance scales with the square of a multiplier and ignores shifts.
  • The ATE is a difference of two means, so it can be identified even though no individual effect is observed.
  • Non-linear functions do not commute with expectation (Jensen).

Simulation Lab: Linearity of Expectation & Variance Scaling

Open full screen →

Transform X into aX + b and watch the mean shift and the spread scale by |a|.

00.050.10.150.22.557.51012.51517.5valuedensity
X ~ N(10, 4)Y = aX + b
E[Y] = aE[X] + b
10.00
1.0 × 10 + 0.0
Var(Y) = a²Var(X)
4.00
1.00 × 4
sd(Y) = |a| sd(X)
2.00
E[X], Var(X)
10.00, 4.00

Shifting by b moves the centre but not the spread; multiplying by a stretches the spread by |a| and the variance by a².

Day 2Day 4