Math for Causal Inference

Day 20. Projections, Orthogonality & the Geometry of Least Squares

Least squares is an orthogonal projection. Fitted values, residuals, R² and degrees of freedom all follow from that picture.

Video coming soon
Learning outcomes
  • ●Project a vector onto a line and onto the column space of X.
  • ●Prove that the projection matrices P and M are symmetric and idempotent, with MX = 0.
  • ●Show that OLS residuals are orthogonal to every regressor and derive R² from Pythagoras.
  • ●Compute leverage and explain why adding regressors never lowers R².
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

Projection onto a line

The closest point to yy on the line through uu is

proj⁡uy=u⊤yu⊤u u,\operatorname{proj}_u y = \frac{u^{\top}y}{u^{\top}u}\,u,

and the residual y−proj⁡uyy - \operatorname{proj}_u y is orthogonal to uu. The coefficient u⊤y/u⊤uu^{\top}y/u^{\top}u is a regression slope without intercept.

Projection onto a column space

For XX with full column rank, define

P=X(X⊤X)−1X⊤,M=I−P.P = X(X^{\top}X)^{-1}X^{\top}, \qquad M = I - P.
  • Py=Xβ^=y^Py = X\hat\beta = \hat y is the point in the column space of XX closest to yy.
  • My=y−y^=eMy = y - \hat y = e is the residual vector.
  • PP and MM are symmetric and idempotent (P2=PP^2 = P): projecting twice changes nothing.
  • PX=XPX = X, MX=0MX = 0, PM=0PM = 0.
  • tr⁡(P)=k\operatorname{tr}(P) = k and tr⁡(M)=n−k\operatorname{tr}(M) = n - k: the residuals live in an (n−k)(n - k)-dimensional space, the origin of "degrees of freedom".

The picture

y=Py⏟y^+My⏟e,y^⊤e=0,∥y∥2=∥y^∥2+∥e∥2.y = \underbrace{Py}_{\hat y} + \underbrace{My}_{e}, \qquad \hat y^{\top}e = 0, \qquad \|y\|^2 = \|\hat y\|^2 + \|e\|^2.

With an intercept, apply the same to centred vectors: TSS=ESS+RSS\text{TSS} = \text{ESS} + \text{RSS}, and

R2=1−RSSTSS=ESSTSS,R^2 = 1 - \frac{\text{RSS}}{\text{TSS}} = \frac{\text{ESS}}{\text{TSS}},

the squared cosine of the angle between the centred yy and the centred y^\hat y.

Leverage

The diagonal entries hiih_{ii} of PP are leverages: how much observation ii pulls its own fitted value. They lie in [0,1][0, 1] and sum to kk. In simple regression, hii=1n+(xi−xˉ)2∑j(xj−xˉ)2h_{ii} = \frac1n + \frac{(x_i - \bar x)^2}{\sum_j(x_j - \bar x)^2}: points far from xˉ\bar x have high leverage.

Part 1 · Warm-up

Exercise 1 · Projecting onto an axis

Project y=(3,4)y = (3, 4) onto u=(1,0)u = (1, 0) and give the residual.

Show solution

31(1,0)=(3,0)\frac{3}{1}(1, 0) = (3, 0); residual (0,4)(0, 4), orthogonal to uu.

Exercise 2 · Projecting onto a diagonal

Project y=(2,3)y = (2, 3) onto u=(1,1)u = (1, 1), and check orthogonality.

Show solution

Step 1. Coefficient: u⊤y/u⊤u=(2+3)/(1+1)=2.5u^{\top}y/u^{\top}u = (2 + 3)/(1 + 1) = 2.5.

Step 2. Projection: 2.5(1,1)=(2.5,2.5)2.5(1, 1) = (2.5, 2.5).

Step 3. Residual: (2,3)−(2.5,2.5)=(−0.5,0.5)(2, 3) - (2.5, 2.5) = (-0.5, 0.5).

Step 4. Orthogonality: (−0.5)(1)+(0.5)(1)=0(-0.5)(1) + (0.5)(1) = 0. ✓

Exercise 3 · Projecting onto the constant

Write PP for X=1X = \mathbf 1 (n=3n = 3) and compute PyPy for y=(1,2,6)y = (1, 2, 6).

Show solution

P=1(1⊤1)−11⊤=1311⊤P = \mathbf 1(\mathbf 1^{\top}\mathbf 1)^{-1}\mathbf 1^{\top} = \frac13\mathbf 1\mathbf 1^{\top}, so Py=yˉ1=(3,3,3)Py = \bar y\mathbf 1 = (3, 3, 3). Regressing on a constant fits the mean.

Exercise 4 · The residual-maker

For the same data, compute MyMy.

Show solution

My=y−yˉ1=(−2,−1,3)My = y - \bar y\mathbf 1 = (-2, -1, 3), the centring matrix of Day 17.

Exercise 5 · Pythagoras in data

Verify ∥y∥2=∥Py∥2+∥My∥2\|y\|^2 = \|Py\|^2 + \|My\|^2 for Exercise 3.

Show solution

1+4+36=411 + 4 + 36 = 41 and 27+14=4127 + 14 = 41. ✓

Part 2 · Proofs

Exercise 6 · P is a projection

Prove P⊤=PP^{\top} = P and P2=PP^2 = P.

Show solution

Step 1 (symmetry). Transpose the product in reverse order:

P⊤=(X(X⊤X)−1X⊤)⊤=X((X⊤X)−1)⊤X⊤.P^{\top} = \big(X(X^{\top}X)^{-1}X^{\top}\big)^{\top} = X\big((X^{\top}X)^{-1}\big)^{\top}X^{\top}.

Step 2. (X⊤X)−1(X^{\top}X)^{-1} is symmetric (Day 19, Exercise 7), so P⊤=PP^{\top} = P.

Step 3 (idempotence).

P2=X(X⊤X)−1X⊤X(X⊤X)−1⏟=IX⊤=X(X⊤X)−1X⊤=P.P^2 = X(X^{\top}X)^{-1}\underbrace{X^{\top}X(X^{\top}X)^{-1}}_{=I}X^{\top} = X(X^{\top}X)^{-1}X^{\top} = P.

Step 4. For M=I−PM = I - P: M⊤=MM^{\top} = M and M2=I−2P+P2=I−P=MM^2 = I - 2P + P^2 = I - P = M. ■\blacksquare

Exercise 7 · M annihilates X

Prove PX=XPX = X, MX=0MX = 0 and PM=0PM = 0.

Show solution

PX=X(X⊤X)−1X⊤X=XPX = X(X^{\top}X)^{-1}X^{\top}X = X, so MX=X−X=0MX = X - X = 0. PM=P−P2=0PM = P - P^2 = 0.

Exercise 8 · Residuals are orthogonal to the regressors

Show X⊤e=0X^{\top}e = 0. With an intercept, deduce that residuals sum to zero and are uncorrelated with each regressor and with y^\hat y.

Show solution

Step 1. X⊤e=X⊤My=(MX)⊤y=0X^{\top}e = X^{\top}My = (MX)^{\top}y = 0.

Step 2. The intercept row gives ∑ei=0\sum e_i = 0; row jj gives ∑xijei=0\sum x_{ij}e_i = 0, so the sample covariance is zero.

Step 3. Finally y^⊤e=β^⊤X⊤e=0\hat y^{\top}e = \hat\beta^{\top}X^{\top}e = 0.

Exercise 9 · Degrees of freedom

Prove tr⁡(P)=k\operatorname{tr}(P) = k and tr⁡(M)=n−k\operatorname{tr}(M) = n - k.

Show solution

Step 1. By the cyclic trace (Day 17), tr⁡(P)=tr⁡((X⊤X)−1X⊤X)=tr⁡(Ik)=k\operatorname{tr}(P) = \operatorname{tr}\big((X^{\top}X)^{-1}X^{\top}X\big) = \operatorname{tr}(I_k) = k, and tr⁡(M)=tr⁡(In)−k=n−k\operatorname{tr}(M) = \operatorname{tr}(I_n) - k = n - k.

Step 2. On Day 27 this gives E[e⊤e]=σ2(n−k)E[e^{\top}e] = \sigma^2(n - k).

Exercise 10 · Least squares really is least

Show that for any bb, ∥y−Xb∥2=∥y−Xβ^∥2+∥X(β^−b)∥2\|y - Xb\|^2 = \|y - X\hat\beta\|^2 + \|X(\hat\beta - b)\|^2, so β^\hat\beta minimises the sum of squared residuals.

Show solution

Step 1. y−Xb=e+X(β^−b)y - Xb = e + X(\hat\beta - b).

Step 2. The cross term is 2(β^−b)⊤X⊤e=02(\hat\beta - b)^{\top}X^{\top}e = 0 by Exercise 8, so Pythagoras applies.

Step 3. The second term is ≥0\ge 0, and zero only when X(β^−b)=0X(\hat\beta - b) = 0, i.e. b=β^b = \hat\beta under full rank.

Part 3 · Applied

Use the four farms from Day 17: advisory visits x=(1,2,3,4)x = (1, 2, 3, 4) and maize yields y=(2,3,5,6)y = (2, 3, 5, 6) t/ha, with an intercept. OLS gives β^=(0.5,1.4)\hat\beta = (0.5, 1.4): 1.4 extra tonnes per hectare per visit.

Exercise 11 · Orthogonality in numbers

The residuals are e=(0.1,−0.3,0.3,−0.1)e = (0.1, -0.3, 0.3, -0.1). Check X⊤e=0X^{\top}e = 0.

Show solution

Step 1. Intercept row of X⊤eX^{\top}e: ∑ei=0.1−0.3+0.3−0.1=0\sum e_i = 0.1 - 0.3 + 0.3 - 0.1 = 0.

Step 2. Visits row: ∑xiei=1(0.1)+2(−0.3)+3(0.3)+4(−0.1)=0.1−0.6+0.9−0.4=0\sum x_ie_i = 1(0.1) + 2(-0.3) + 3(0.3) + 4(-0.1) = 0.1 - 0.6 + 0.9 - 0.4 = 0.

Answer: X⊤e=0X^{\top}e = 0: the yield residuals are uncorrelated with the number of visits.

Exercise 12 · R²

Compute TSS, RSS and R2R^2.

Show solution

Step 1. Mean yield yˉ=16/4=4\bar y = 16/4 = 4.

Step 2. TSS =∑(yi−4)2=4+1+1+4=10= \sum(y_i - 4)^2 = 4 + 1 + 1 + 4 = 10.

Step 3. RSS =∑ei2=0.01+0.09+0.09+0.01=0.2= \sum e_i^2 = 0.01 + 0.09 + 0.09 + 0.01 = 0.2.

Step 4. R2=1−0.2/10=0.98R^2 = 1 - 0.2/10 = 0.98.

Answer: visits account for 98% of the variation in yields across these four farms.

Exercise 13 · Leverage

Compute the leverage of each observation and check that they sum to k=2k = 2.

Show solution

Step 1. Simple-regression leverage: hii=1n+(xi−xˉ)2∑j(xj−xˉ)2h_{ii} = \frac1n + \frac{(x_i - \bar x)^2}{\sum_j(x_j - \bar x)^2}, with xˉ=2.5\bar x = 2.5 and ∑(x−xˉ)2=2.25+0.25+0.25+2.25=5\sum(x - \bar x)^2 = 2.25 + 0.25 + 0.25 + 2.25 = 5.

Step 2. Farms with 1 or 4 visits: 0.25+2.25/5=0.700.25 + 2.25/5 = 0.70.

Step 3. Farms with 2 or 3 visits: 0.25+0.25/5=0.300.25 + 0.25/5 = 0.30.

Step 4. Sum: 0.7+0.3+0.3+0.7=2=k0.7 + 0.3 + 0.3 + 0.7 = 2 = k. ✓

Answer: the two extreme farms each pull their own fitted yield with leverage 0.70, more than twice the middle farms.

Exercise 14 · Regression on a training dummy fits group means

Two trained workers earn (5,7)(5, 7) and two untrained workers earn (1,3)(1, 3) (in $10/hour). What are the fitted values from regressing earnings on an intercept and the training dummy TT?

Show solution

Step 1. The column space of (1,T)(\mathbf 1, T) contains exactly the vectors that are constant within each group.

Step 2. The closest such vector to yy replaces each value by its group mean: trained (5+7)/2=6(5 + 7)/2 = 6; untrained (1+3)/2=2(1 + 3)/2 = 2.

Answer: y^=(6,6,2,2)\hat y = (6, 6, 2, 2); the slope is 6−2=46 - 2 = 4, the difference in mean earnings.

Exercise 15 · Adding a meaningless control never lowers R²

An analyst adds the last digit of each worker's postcode to a wage regression and R2R^2 rises slightly. Prove that adding any regressor cannot lower R2R^2, and explain what this implies for choosing controls.

Show solution

Step 1. The column space of [X,z][X, z] contains that of XX.

Step 2. OLS fitted values are the closest point to yy in the column space. Searching a larger set cannot give a point further away, so RSS can only fall or stay equal.

Step 3. TSS does not depend on the regressors, so R2=1−RSS/TSSR^2 = 1 - \text{RSS}/\text{TSS} can only rise or stay equal.

Answer: even the postcode digit raises R2R^2 a little by chance. R2R^2 is therefore a poor guide to whether a control belongs in a causal regression; that is decided by the causal structure (Phase 4).

Verify in Python

import numpy as np

x = np.array([1, 2, 3, 4.]); y = np.array([2, 3, 5, 6.])
X = np.column_stack([np.ones(4), x])
P = X @ np.linalg.inv(X.T @ X) @ X.T; M = np.eye(4) - P
print("symmetric:", np.allclose(P, P.T), " idempotent:", np.allclose(P @ P, P), " MX = 0:", np.allclose(M @ X, 0))
print("trace P, trace M:", round(np.trace(P), 6), round(np.trace(M), 6))
e = M @ y; yhat = P @ y
print("fitted:", yhat.round(2), " residuals:", e.round(2), " X'e:", (X.T @ e).round(10))
tss = ((y - y.mean())**2).sum(); rss = e @ e
print("R2 =", 1 - rss / tss, " leverages:", np.diag(P).round(2))

# Adding a pure-noise regressor never lowers R2
z = np.random.default_rng(20).normal(size=4)
X2 = np.column_stack([X, z]); e2 = y - X2 @ np.linalg.lstsq(X2, y, rcond=None)[0]
print("R2 with noise regressor:", round(1 - e2 @ e2 / tss, 4))

Key takeaways

  • OLS fitted values are the orthogonal projection of yy onto the column space of XX; residuals are what is left, perpendicular to every regressor.
  • PP and MM are symmetric and idempotent; their traces are the model and residual degrees of freedom.
  • R2R^2 comes from Pythagoras and can only rise when regressors are added.

Simulation Lab: Least Squares: Residuals & Orthogonality

Open full screen →

Move a candidate line; only the OLS line makes the residuals orthogonal to the regressors and the SSR minimal.

05100246810xy05010000.250.50.7511.251.5slope bmin SSR over a
candidate lineOLS linepositive residualnegative residual
SSR (candidate)
43.49
OLS minimum 24.20
R² (candidate)
0.629
Σ e
-12.53
0 at OLS
Σ x·e
-92.95
0 at OLS

Move the candidate line. Vertical segments are residuals. Only at the OLS line are the residuals orthogonal to the regressors: Σe = 0 and Σx·e = 0, and the SSR is minimal (right panel: SSR as a function of the slope, with the intercept re-optimised).

Day 19Day 21