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.
- ●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².
Theory
Projection onto a line
The closest point to on the line through is
and the residual is orthogonal to . The coefficient is a regression slope without intercept.
Projection onto a column space
For with full column rank, define
- is the point in the column space of closest to .
- is the residual vector.
- and are symmetric and idempotent (): projecting twice changes nothing.
- , , .
- and : the residuals live in an -dimensional space, the origin of "degrees of freedom".
The picture
With an intercept, apply the same to centred vectors: , and
the squared cosine of the angle between the centred and the centred .
Leverage
The diagonal entries of are leverages: how much observation pulls its own fitted value. They lie in and sum to . In simple regression, : points far from have high leverage.
Part 1 · Warm-up
Exercise 1 · Projecting onto an axis
Project onto and give the residual.
Show solution
; residual , orthogonal to .
Exercise 2 · Projecting onto a diagonal
Project onto , and check orthogonality.
Show solution
Step 1. Coefficient: .
Step 2. Projection: .
Step 3. Residual: .
Step 4. Orthogonality: . ✓
Exercise 3 · Projecting onto the constant
Write for () and compute for .
Show solution
, so . Regressing on a constant fits the mean.
Exercise 4 · The residual-maker
For the same data, compute .
Show solution
, the centring matrix of Day 17.
Exercise 5 · Pythagoras in data
Verify for Exercise 3.
Show solution
and . ✓
Part 2 · Proofs
Exercise 6 · P is a projection
Prove and .
Show solution
Step 1 (symmetry). Transpose the product in reverse order:
Step 2. is symmetric (Day 19, Exercise 7), so .
Step 3 (idempotence).
Step 4. For : and .
Exercise 7 · M annihilates X
Prove , and .
Show solution
, so . .
Exercise 8 · Residuals are orthogonal to the regressors
Show . With an intercept, deduce that residuals sum to zero and are uncorrelated with each regressor and with .
Show solution
Step 1. .
Step 2. The intercept row gives ; row gives , so the sample covariance is zero.
Step 3. Finally .
Exercise 9 · Degrees of freedom
Prove and .
Show solution
Step 1. By the cyclic trace (Day 17), , and .
Step 2. On Day 27 this gives .
Exercise 10 · Least squares really is least
Show that for any , , so minimises the sum of squared residuals.
Show solution
Step 1. .
Step 2. The cross term is by Exercise 8, so Pythagoras applies.
Step 3. The second term is , and zero only when , i.e. under full rank.
Part 3 · Applied
Use the four farms from Day 17: advisory visits and maize yields t/ha, with an intercept. OLS gives : 1.4 extra tonnes per hectare per visit.
Exercise 11 · Orthogonality in numbers
The residuals are . Check .
Show solution
Step 1. Intercept row of : .
Step 2. Visits row: .
Answer: : the yield residuals are uncorrelated with the number of visits.
Exercise 12 · R²
Compute TSS, RSS and .
Show solution
Step 1. Mean yield .
Step 2. TSS .
Step 3. RSS .
Step 4. .
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 .
Show solution
Step 1. Simple-regression leverage: , with and .
Step 2. Farms with 1 or 4 visits: .
Step 3. Farms with 2 or 3 visits: .
Step 4. Sum: . ✓
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 and two untrained workers earn (in $10/hour). What are the fitted values from regressing earnings on an intercept and the training dummy ?
Show solution
Step 1. The column space of contains exactly the vectors that are constant within each group.
Step 2. The closest such vector to replaces each value by its group mean: trained ; untrained .
Answer: ; the slope is , 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 rises slightly. Prove that adding any regressor cannot lower , and explain what this implies for choosing controls.
Show solution
Step 1. The column space of contains that of .
Step 2. OLS fitted values are the closest point to 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 can only rise or stay equal.
Answer: even the postcode digit raises a little by chance. 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 onto the column space of ; residuals are what is left, perpendicular to every regressor.
- and are symmetric and idempotent; their traces are the model and residual degrees of freedom.
- 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.
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).