Day 3. Random Variables, Expectation & Variance
Turning outcomes into numbers, and the two summaries every estimator is built from.
- ●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.
Theory
Random variables
A random variable is a function from the sample space to the real numbers, . It turns outcomes ("the firm adopted clean tech") into numbers (emissions in tonnes). Its distribution is described by a probability mass function (discrete) or a density (continuous), with .
Expectation
The expected value is the probability-weighted average:
For any function , (the "law of the unconscious statistician").
Linearity of expectation holds for any random variables, dependent or not:
Variance
The standard deviation is in the units of . Shifting a variable moves its centre but not its spread; scaling by scales the spread by .
Jensen's inequality
If is convex, ; if concave, the inequality flips. In general , 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: if treated and if not. The average treatment effect is an expectation:
where the second equality is linearity. We will never observe 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 .
Show solution
Exercise 2 · A second moment
For the same fine, compute .
Show solution
Exercise 3 · Variance by the shortcut
Compute and .
Show solution
so dollars.
Exercise 4 · Linearity
Tax revenue is , where is emissions with tonnes. Find .
Show solution
.
Exercise 5 · Variance of a linear transformation
If , what are and ?
Show solution
Step 1. Use with , :
Step 2. Standard deviation: .
Answer: variance 90,000 and sd 300. The constant shifts revenue but does not change its spread.
Part 2 · Proofs
Exercise 6 · Linearity with constants
Prove for a discrete .
Show solution
using .
Exercise 7 · The variance shortcut
Prove .
Show solution
Let . Expand the square and use linearity:
Exercise 8 · Variance scaling
Prove .
Show solution
, so
Exercise 9 · Indicator variables
Let equal 1 if happens and 0 otherwise. Show that and .
Show solution
Step 1. The indicator takes the value 1 with probability and 0 otherwise:
Step 2. Since and , , so .
Step 3. Variance shortcut:
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 , and conclude that minimises the mean squared error.
Show solution
Step 1. Write and expand:
Step 2. The second term is non-negative and zero only at .
Step 3. This is the seed of least squares: regression generalises "predict with the mean" to "predict with a function of " (Day 6).
Part 3 · Applied
Exercise 11 · An average treatment effect
A (hypothetical) table shows both potential outcomes for four workers:
| Worker | ||
|---|---|---|
| 1 | 10 | 7 |
| 2 | 8 | 8 |
| 3 | 12 | 9 |
| 4 | 6 | 4 |
Compute each individual effect, the ATE, and check that .
Show solution
Step 1. Individual effects : , , , .
Step 2. ATE = average of the individual effects: .
Step 3. Means of each potential outcome: and .
Step 4. Difference of means: . ✓
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 , so . Compute when and find the that maximises it. Why might the evaluation team care?
Show solution
Step 1. By Exercise 9, . At : .
Step 2. Maximise : , and , so it is a maximum.
Step 3. Maximum value: .
Answer: 0.21 at ; maximised at (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 (thousand dollars), with density . Compute and by integration.
Show solution
The density is on .
Step 1. Mean:
Step 2. Second moment:
Step 3. Variance: .
Answer: mean $5k and variance 8.33 (sd ≈ $2.9k), matching the formula .
Exercise 14 · Risk aversion and Jensen
A household's income is $50k or $150k with equal probability, and its utility is . Compare with and find the certainty equivalent.
Show solution
Step 1. Expected utility:
Step 2. Utility of the expected income: , so .
Step 3. Compare: , as Jensen's inequality predicts for the concave .
Step 4. The certainty equivalent gives the same utility as the gamble: , so
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 . 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: .
Step 4. Could real data give this? Expand the variance of a difference:
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|.
Shifting by b moves the centre but not the spread; multiplying by a stretches the spread by |a| and the variance by a².