For two discrete variables, the joint pmf is p(x,y)=P(X=x,Y=y). Summing over one variable gives the marginal, pX(x)=∑yp(x,y), and dividing gives the conditional, p(y∣x)=p(x,y)/pX(x). X and Y are independent when p(x,y)=pX(x)pY(y) for all x,y. Continuous variables work the same way with densities and integrals.
Covariance has units (dollars × years); correlation is unit-free. Useful rules:
Cov(X,X)=Var(X)
Cov(aX+b,cY+d)=acCov(X,Y)
Var(X±Y)=Var(X)+Var(Y)±2Cov(X,Y)
Independence ⇒ zero covariance, but not the other way round.
Covariance is the regression slope's numerator
The best linear predictor of Y given X has slope
β=Var(X)Cov(X,Y).
If Y=βX+γU+ε and you leave U out, the slope you estimate is β+γCov(X,U)/Var(X): the famous omitted-variable bias.
The bivariate normal
If (X,Y) is bivariate normal with correlation ρ, the conditional mean is linear and the conditional variance is constant:
E[Y∣X=x]=μY+ρσXσY(x−μX),Var(Y∣X)=σY2(1−ρ2).
Explore it in 3D in the Simulation Lab.
Part 1 · Warm-up
The joint distribution of job training X and employment Y is:
Y=0
Y=1
X=0
0.30
0.30
X=1
0.10
0.30
Exercise 1 · Marginals
Find P(X=1) and P(Y=1).
Show solution
P(X=1)=0.10+0.30=0.40 and P(Y=1)=0.30+0.30=0.60.
Exercise 2 · The product moment
Compute E[XY].
Show solution
XY=1 only when both are 1, so E[XY]=P(X=1,Y=1)=0.30.
Exercise 3 · Covariance
Compute Cov(X,Y).
Show solution
Cov(X,Y)=0.30−0.40×0.60=0.06.
Exercise 4 · Correlation
Compute ρXY.
Show solution
Var(X)=0.4×0.6=0.24 and Var(Y)=0.6×0.4=0.24, so ρ=0.06/0.24×0.24=0.25.
Exercise 5 · Conditionals and independence
Find P(Y=1∣X=1) and P(Y=1∣X=0). Are X and Y independent?
Show solution
P(Y=1∣X=1)=0.30/0.40=0.75 and P(Y=1∣X=0)=0.30/0.60=0.50. They differ, so X and Y are not independent (also, 0.30=0.40×0.60).
Part 2 · Proofs
Exercise 6 · Covariance shortcut
Prove Cov(X,Y)=E[XY]−E[X]E[Y].
Show solutionE[(X−μX)(Y−μY)]=E[XY]−μYE[X]−μXE[Y]+μXμY=E[XY]−μXμY.
Exercise 7 · Independence implies zero covariance
Prove that if X and Y are independent (discrete), Cov(X,Y)=0.
Show solutionE[XY]=x∑y∑xypX(x)pY(y)=(x∑xpX(x))(y∑ypY(y))=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).
Show solution
Let X~=X−μX and Y~=Y−μY. Then
Var(X+Y)=E[(X~+Y~)2]=E[X~2]+E[Y~2]+2E[X~Y~].
Exercise 9 · Correlation is bounded
Prove ∣ρXY∣≤1. Hint: consider Var(Y−tX)≥0 for every t.
Show solution
Step 1.h(t)=Var(Y−tX)=σY2−2tσXY+t2σX2≥0 for all t.
Step 2. A quadratic in t that never goes negative has a non-positive discriminant:
4σXY2−4σX2σY2≤0⇒σXY2≤σX2σY2⇒∣ρ∣≤1.
Step 3. Equality holds exactly when Y−tX is constant for some t, i.e. when Y is a linear function of X.
Exercise 10 · The slope on a binary regressor
Let X∈{0,1} with P(X=1)=p. Prove
Var(X)Cov(X,Y)=E[Y∣X=1]−E[Y∣X=0].Show solution
Step 1. Let m1=E[Y∣X=1] and m0=E[Y∣X=0].
Step 2. Then E[XY]=pm1, E[X]=p and E[Y]=pm1+(1−p)m0.
Step 3. So
Cov(X,Y)=pm1−p(pm1+(1−p)m0)=p(1−p)(m1−m0).
Step 4. Dividing by Var(X)=p(1−p) gives m1−m0.
Step 5. For the table above: 0.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, X, is −1 (cold snap), 0 or +1 (heat wave), each with probability 1/3. Excess hospital admissions are Y=X2 (both extremes raise admissions). Show that Cov(X,Y)=0 although Y is completely determined by X.
Show solution
Step 1.E[X]=(−1+0+1)/3=0.
Step 2.XY=X3, so E[XY]=((−1)3+0+13)/3=0.
Step 3.Cov(X,Y)=E[XY]−E[X]E[Y]=0−0⋅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. Find the standard deviation of total impact 0.5A+0.5B.
Show solution
Step 1. Covariance of the two programmes: Cov(A,B)=ρσAσB=−0.5×10×20=−100.
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 X (years, standardised so Var(X)=1) and ability U: Y=2X+3U+ε, with ε uncorrelated with X and U, and Cov(X,U)=0.5 (abler people stay in school longer). What slope do you get from regressing Y on X alone?
Show solution
Step 1. The short-regression slope is Cov(X,Y)/Var(X).
Step 4. Bias =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+ε, where X is hours of job training and ε collects every other cause of earnings (motivation, local labour demand, …). Show that the population regression slope Cov(X,Y)/Var(X) equals β1 if and only if Cov(X,ε)=0.
Show solution
Step 1. Take the covariance of both sides with X (constants have zero covariance):
Cov(X,Y)=β1Var(X)+Cov(X,ε).
Step 2. Divide by Var(X):
Var(X)Cov(X,Y)=β1+Var(X)Cov(X,ε).
Step 3. The second term vanishes exactly when Cov(X,ε)=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. This exogeneity condition is what every identification strategy in Phase 5 tries to secure.
Exercise 15 · Prediction with a bivariate normal
Years of education X have mean 12 and sd 2; log wages Y have mean 3 and sd 0.5; ρ=0.4, jointly normal. Find E[Y∣X=16] and sd(Y∣X).
Step 3. Conditional sd: 0.21≈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].
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 X biases the slope by (its effect) × (its slope on X).
Drag to rotate the joint density; change the correlation and see the contours and the regression line.
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).