Math for Causal Inference

Day 5. Joint Distributions, Covariance & Correlation

How two variables move together, and why that is the raw material of regression and omitted-variable bias.

Video coming soon
Learning outcomes
  • ●Read marginal and conditional distributions off a joint table.
  • ●Compute and interpret covariance and correlation, and prove their key properties.
  • ●Show that a regression slope on a binary regressor equals a difference in means.
  • ●Derive the omitted-variable-bias formula in its simplest form.
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

Joint, marginal and conditional

For two discrete variables, the joint pmf is p(x,y)=P(X=x,Y=y)p(x, y) = P(X = x, Y = y). Summing over one variable gives the marginal, pX(x)=∑yp(x,y)p_X(x) = \sum_y p(x, y), and dividing gives the conditional, p(y∣x)=p(x,y)/pX(x)p(y \mid x) = p(x, y)/p_X(x). XX and YY are independent when p(x,y)=pX(x)pY(y)p(x, y) = p_X(x)p_Y(y) for all x,yx, y. Continuous variables work the same way with densities and integrals.

Covariance and correlation

Cov⁡(X,Y)=E[(X−μX)(Y−μY)]=E[XY]−E[X]E[Y],ρXY=Cov⁡(X,Y)σXσY∈[−1,1].\operatorname{Cov}(X, Y) = E\big[(X - \mu_X)(Y - \mu_Y)\big] = E[XY] - E[X]E[Y], \qquad \rho_{XY} = \frac{\operatorname{Cov}(X, Y)}{\sigma_X \sigma_Y} \in [-1, 1].

Covariance has units (dollars × years); correlation is unit-free. Useful rules:

  • Cov⁡(X,X)=Var⁡(X)\operatorname{Cov}(X, X) = \operatorname{Var}(X)
  • Cov⁡(aX+b,cY+d)=ac Cov⁡(X,Y)\operatorname{Cov}(aX + b, cY + d) = ac\,\operatorname{Cov}(X, Y)
  • Var⁡(X±Y)=Var⁡(X)+Var⁡(Y)±2Cov⁡(X,Y)\operatorname{Var}(X \pm Y) = \operatorname{Var}(X) + \operatorname{Var}(Y) \pm 2\operatorname{Cov}(X, Y)
  • Independence ⇒ zero covariance, but not the other way round.

Covariance is the regression slope's numerator

The best linear predictor of YY given XX has slope

β=Cov⁡(X,Y)Var⁡(X).\beta = \frac{\operatorname{Cov}(X, Y)}{\operatorname{Var}(X)}.

If Y=βX+γU+εY = \beta X + \gamma U + \varepsilon and you leave UU out, the slope you estimate is β+γ Cov⁡(X,U)/Var⁡(X)\beta + \gamma\,\operatorname{Cov}(X, U)/\operatorname{Var}(X): the famous omitted-variable bias.

The bivariate normal

If (X,Y)(X, Y) is bivariate normal with correlation ρ\rho, the conditional mean is linear and the conditional variance is constant:

E[Y∣X=x]=μY+ρσYσX(x−μX),Var⁡(Y∣X)=σY2(1−ρ2).E[Y \mid X = x] = \mu_Y + \rho\frac{\sigma_Y}{\sigma_X}(x - \mu_X), \qquad \operatorname{Var}(Y \mid X) = \sigma_Y^2(1 - \rho^2).

Explore it in 3D in the Simulation Lab.

Part 1 · Warm-up

The joint distribution of job training XX and employment YY is:

Y=0Y = 0Y=1Y = 1
X=0X = 00.300.30
X=1X = 10.100.30

Exercise 1 · Marginals

Find P(X=1)P(X = 1) and P(Y=1)P(Y = 1).

Show solution

P(X=1)=0.10+0.30=0.40P(X = 1) = 0.10 + 0.30 = 0.40 and P(Y=1)=0.30+0.30=0.60P(Y = 1) = 0.30 + 0.30 = 0.60.

Exercise 2 · The product moment

Compute E[XY]E[XY].

Show solution

XY=1XY = 1 only when both are 1, so E[XY]=P(X=1,Y=1)=0.30E[XY] = P(X = 1, Y = 1) = 0.30.

Exercise 3 · Covariance

Compute Cov⁡(X,Y)\operatorname{Cov}(X, Y).

Show solution

Cov⁡(X,Y)=0.30−0.40×0.60=0.06\operatorname{Cov}(X, Y) = 0.30 - 0.40 \times 0.60 = 0.06.

Exercise 4 · Correlation

Compute ρXY\rho_{XY}.

Show solution

Var⁡(X)=0.4×0.6=0.24\operatorname{Var}(X) = 0.4 \times 0.6 = 0.24 and Var⁡(Y)=0.6×0.4=0.24\operatorname{Var}(Y) = 0.6 \times 0.4 = 0.24, so ρ=0.06/0.24×0.24=0.25\rho = 0.06/\sqrt{0.24 \times 0.24} = 0.25.

Exercise 5 · Conditionals and independence

Find P(Y=1∣X=1)P(Y = 1 \mid X = 1) and P(Y=1∣X=0)P(Y = 1 \mid X = 0). Are XX and YY independent?

Show solution

P(Y=1∣X=1)=0.30/0.40=0.75P(Y = 1 \mid X = 1) = 0.30/0.40 = 0.75 and P(Y=1∣X=0)=0.30/0.60=0.50P(Y = 1 \mid X = 0) = 0.30/0.60 = 0.50. They differ, so XX and YY are not independent (also, 0.30≠0.40×0.600.30 \ne 0.40 \times 0.60).

Part 2 · Proofs

Exercise 6 · Covariance shortcut

Prove Cov⁡(X,Y)=E[XY]−E[X]E[Y]\operatorname{Cov}(X, Y) = E[XY] - E[X]E[Y].

Show solution E[(X−μX)(Y−μY)]=E[XY]−μYE[X]−μXE[Y]+μXμY=E[XY]−μXμY.E[(X - \mu_X)(Y - \mu_Y)] = E[XY] - \mu_Y E[X] - \mu_X E[Y] + \mu_X\mu_Y = E[XY] - \mu_X\mu_Y.

Exercise 7 · Independence implies zero covariance

Prove that if XX and YY are independent (discrete), Cov⁡(X,Y)=0\operatorname{Cov}(X, Y) = 0.

Show solution E[XY]=∑x∑yxy pX(x)pY(y)=(∑xx pX(x))(∑yy pY(y))=E[X]E[Y],E[XY] = \sum_x\sum_y xy\,p_X(x)p_Y(y) = \Big(\sum_x x\,p_X(x)\Big)\Big(\sum_y y\,p_Y(y)\Big) = E[X]E[Y],

so the covariance is zero.

Exercise 8 · Variance of a sum

Prove Var⁡(X+Y)=Var⁡(X)+Var⁡(Y)+2Cov⁡(X,Y)\operatorname{Var}(X + Y) = \operatorname{Var}(X) + \operatorname{Var}(Y) + 2\operatorname{Cov}(X, Y).

Show solution

Let X~=X−μX\tilde X = X - \mu_X and Y~=Y−μY\tilde Y = Y - \mu_Y. Then

Var⁡(X+Y)=E[(X~+Y~)2]=E[X~2]+E[Y~2]+2E[X~Y~].\operatorname{Var}(X + Y) = E[(\tilde X + \tilde Y)^2] = E[\tilde X^2] + E[\tilde Y^2] + 2E[\tilde X\tilde Y].

Exercise 9 · Correlation is bounded

Prove ∣ρXY∣≤1|\rho_{XY}| \le 1. Hint: consider Var⁡(Y−tX)≥0\operatorname{Var}(Y - tX) \ge 0 for every tt.

Show solution

Step 1. h(t)=Var⁡(Y−tX)=σY2−2t σXY+t2σX2≥0h(t) = \operatorname{Var}(Y - tX) = \sigma_Y^2 - 2t\,\sigma_{XY} + t^2\sigma_X^2 \ge 0 for all tt.

Step 2. A quadratic in tt that never goes negative has a non-positive discriminant:

4σXY2−4σX2σY2≤0  ⇒  σXY2≤σX2σY2  ⇒  ∣ρ∣≤1.4\sigma_{XY}^2 - 4\sigma_X^2\sigma_Y^2 \le 0 \;\Rightarrow\; \sigma_{XY}^2 \le \sigma_X^2\sigma_Y^2 \;\Rightarrow\; |\rho| \le 1.

Step 3. Equality holds exactly when Y−tXY - tX is constant for some tt, i.e. when YY is a linear function of XX.

Exercise 10 · The slope on a binary regressor

Let X∈{0,1}X \in \{0, 1\} with P(X=1)=pP(X = 1) = p. Prove

Cov⁡(X,Y)Var⁡(X)=E[Y∣X=1]−E[Y∣X=0].\frac{\operatorname{Cov}(X, Y)}{\operatorname{Var}(X)} = E[Y \mid X = 1] - E[Y \mid X = 0].
Show solution

Step 1. Let m1=E[Y∣X=1]m_1 = E[Y \mid X = 1] and m0=E[Y∣X=0]m_0 = E[Y \mid X = 0].

Step 2. Then E[XY]=p m1E[XY] = p\,m_1, E[X]=pE[X] = p and E[Y]=p m1+(1−p)m0E[Y] = p\,m_1 + (1 - p)m_0.

Step 3. So

Cov⁡(X,Y)=p m1−p(p m1+(1−p)m0)=p(1−p)(m1−m0).\operatorname{Cov}(X, Y) = p\,m_1 - p\big(p\,m_1 + (1 - p)m_0\big) = p(1 - p)(m_1 - m_0).

Step 4. Dividing by Var⁡(X)=p(1−p)\operatorname{Var}(X) = p(1 - p) gives m1−m0m_1 - m_0.

Step 5. For the table above: 0.06/0.24=0.25=0.75−0.500.06/0.24 = 0.25 = 0.75 - 0.50.

Step 6. Regressing an outcome on a treatment dummy is a difference in means.

Part 3 · Applied

Exercise 11 · A U-shaped health risk

Daily temperature deviation from the seasonal norm, XX, is −1-1 (cold snap), 0 or +1+1 (heat wave), each with probability 1/3. Excess hospital admissions are Y=X2Y = X^2 (both extremes raise admissions). Show that Cov⁡(X,Y)=0\operatorname{Cov}(X, Y) = 0 although YY is completely determined by XX.

Show solution

Step 1. E[X]=(−1+0+1)/3=0E[X] = (-1 + 0 + 1)/3 = 0.

Step 2. XY=X3XY = X^3, so E[XY]=((−1)3+0+13)/3=0E[XY] = ((-1)^3 + 0 + 1^3)/3 = 0.

Step 3. Cov⁡(X,Y)=E[XY]−E[X]E[Y]=0−0⋅E[Y]=0\operatorname{Cov}(X, Y) = E[XY] - E[X]E[Y] = 0 - 0 \cdot E[Y] = 0.

Answer: the covariance (and correlation) is zero, yet temperature determines admissions exactly. Covariance only measures linear association; a linear regression of admissions on temperature would wrongly suggest no relationship.

Exercise 12 · Diversifying programmes

A foundation splits its budget equally between programmes A and B with impact standard deviations 10 and 20 and correlation −0.5-0.5. Find the standard deviation of total impact 0.5A+0.5B0.5A + 0.5B.

Show solution

Step 1. Covariance of the two programmes: Cov⁡(A,B)=ρσAσB=−0.5×10×20=−100\operatorname{Cov}(A, B) = \rho\sigma_A\sigma_B = -0.5 \times 10 \times 20 = -100.

Step 2. Variance of the half-and-half portfolio:

Var⁡(0.5A+0.5B)=0.25(100)+0.25(400)+2(0.5)(0.5)(−100)=25+100−50=75.\operatorname{Var}(0.5A + 0.5B) = 0.25(100) + 0.25(400) + 2(0.5)(0.5)(-100) = 25 + 100 - 50 = 75.

Step 3. sd =75≈8.66= \sqrt{75} \approx 8.66.

Answer: 8.66, below the sd of either programme alone (10 and 20): negatively correlated programmes hedge each other.

Exercise 13 · Omitted ability in a wage regression

Log wages depend on schooling XX (years, standardised so Var⁡(X)=1\operatorname{Var}(X) = 1) and ability UU: Y=2X+3U+εY = 2X + 3U + \varepsilon, with ε\varepsilon uncorrelated with XX and UU, and Cov⁡(X,U)=0.5\operatorname{Cov}(X, U) = 0.5 (abler people stay in school longer). What slope do you get from regressing YY on XX alone?

Show solution

Step 1. The short-regression slope is Cov⁡(X,Y)/Var⁡(X)\operatorname{Cov}(X, Y)/\operatorname{Var}(X).

Step 2. Expand the covariance using the model:

Cov⁡(X,Y)=2Var⁡(X)+3Cov⁡(X,U)+Cov⁡(X,ε)=2(1)+3(0.5)+0=3.5.\operatorname{Cov}(X, Y) = 2\operatorname{Var}(X) + 3\operatorname{Cov}(X, U) + \operatorname{Cov}(X, \varepsilon) = 2(1) + 3(0.5) + 0 = 3.5.

Step 3. Divide by Var⁡(X)=1\operatorname{Var}(X) = 1: slope =3.5= 3.5.

Step 4. Bias =3.5−2=1.5= 3.5 - 2 = 1.5 = (effect of ability on wages, 3) × (slope of ability on schooling, 0.5).

Answer: 3.5. Leaving out ability overstates the return to schooling by 75%.

Exercise 14 · When is a training coefficient causal?

Earnings follow Y=β0+β1X+εY = \beta_0 + \beta_1X + \varepsilon, where XX is hours of job training and ε\varepsilon collects every other cause of earnings (motivation, local labour demand, …). Show that the population regression slope Cov⁡(X,Y)/Var⁡(X)\operatorname{Cov}(X, Y)/\operatorname{Var}(X) equals β1\beta_1 if and only if Cov⁡(X,ε)=0\operatorname{Cov}(X, \varepsilon) = 0.

Show solution

Step 1. Take the covariance of both sides with XX (constants have zero covariance):

Cov⁡(X,Y)=β1Var⁡(X)+Cov⁡(X,ε).\operatorname{Cov}(X, Y) = \beta_1\operatorname{Var}(X) + \operatorname{Cov}(X, \varepsilon).

Step 2. Divide by Var⁡(X)\operatorname{Var}(X):

Cov⁡(X,Y)Var⁡(X)=β1+Cov⁡(X,ε)Var⁡(X).\frac{\operatorname{Cov}(X, Y)}{\operatorname{Var}(X)} = \beta_1 + \frac{\operatorname{Cov}(X, \varepsilon)}{\operatorname{Var}(X)}.

Step 3. The second term vanishes exactly when Cov⁡(X,ε)=0\operatorname{Cov}(X, \varepsilon) = 0.

Answer: the slope is causal only if training hours are uncorrelated with all other causes of earnings. If motivated workers take more training, the slope overstates β1\beta_1. This exogeneity condition is what every identification strategy in Phase 5 tries to secure.

Exercise 15 · Prediction with a bivariate normal

Years of education XX have mean 12 and sd 2; log wages YY have mean 3 and sd 0.5; ρ=0.4\rho = 0.4, jointly normal. Find E[Y∣X=16]E[Y \mid X = 16] and sd⁡(Y∣X)\operatorname{sd}(Y \mid X).

Show solution

Step 1. Conditional mean of a bivariate normal:

E[Y∣X=16]=μY+ρσYσX(16−μX)=3+0.4×0.52×4=3+0.4=3.4.E[Y \mid X = 16] = \mu_Y + \rho\frac{\sigma_Y}{\sigma_X}(16 - \mu_X) = 3 + 0.4 \times \frac{0.5}{2} \times 4 = 3 + 0.4 = 3.4.

Step 2. Conditional variance: Var⁡(Y∣X)=σY2(1−ρ2)=0.25×0.84=0.21\operatorname{Var}(Y \mid X) = \sigma_Y^2(1 - \rho^2) = 0.25 \times 0.84 = 0.21.

Step 3. Conditional sd: 0.21≈0.458\sqrt{0.21} \approx 0.458, versus the unconditional 0.5.

Answer: predicted log wage 3.4 for 16 years of schooling, with sd 0.458. A correlation of 0.4 removes only 16% of the variance; and this slope is a predictive relationship, not a causal return to schooling.

Verify in Python

import numpy as np

joint = np.array([[0.30, 0.30],    # rows: X = 0, 1; columns: Y = 0, 1
                  [0.10, 0.30]])
x = np.array([0, 1]); y = np.array([0, 1])
px, py = joint.sum(1), joint.sum(0)
exy = sum(joint[i, j] * x[i] * y[j] for i in range(2) for j in range(2))
cov = exy - (px @ x) * (py @ y)
vx, vy = px @ x**2 - (px @ x)**2, py @ y**2 - (py @ y)**2
print(f"Cov = {cov:.2f}, corr = {cov / np.sqrt(vx * vy):.2f}, slope = {cov / vx:.2f}")

# Omitted-variable bias by simulation (Exercise 13)
rng = np.random.default_rng(7)
n = 500_000
X = rng.normal(0, 1, n)
U = 0.5 * X + rng.normal(0, 1, n)          # Cov(X, U) = 0.5
Y = 2 * X + 3 * U + rng.normal(0, 1, n)
print("short-regression slope:", round(np.cov(X, Y)[0, 1] / X.var(ddof=1), 3))

Key takeaways

  • Covariance measures linear co-movement; correlation rescales it to [−1,1][-1, 1].
  • A regression slope is a covariance divided by a variance; on a dummy it is a difference in means.
  • Leaving out a variable correlated with XX biases the slope by (its effect) × (its slope on XX).

Simulation Lab: Bivariate Normal in 3D

Open full screen →

Drag to rotate the joint density; change the correlation and see the contours and the regression line.

XY-202-3-2-10123X (standardised)Y (standardised)
simulated drawsdensity contoursE[Y | X] = ρx
ρ (true)
0.50
sample r
0.52
Var(Y | X)
0.75
1 − ρ²

Left: the joint density surface (drag to rotate). Right: its contour ellipses, 400 draws, and the conditional-mean line E[Y | X = x] = ρx (standardised variables).

Day 4Day 6