Projection Matrices and Least Squares
Fitting a line through data no line can pass through exactly is a shadow problem: the fitted values are literally the shadow of your data on the space of possible fits, cast straight down.
Prerequisites: Vector Spaces and Bases, Ordinary Least Squares (OLS), Inner Products and Orthogonality
You have 100 data points and want to fit a straight line. A line has 2 degrees of freedom; 100 points almost never lie exactly on one. There is no exact solution — no line passes through all of them. So what does "the best line" even mean, and how do you compute it without guessing and checking? The answer turns out to be geometric: you're not solving an equation, you're casting a shadow.
The analogy: a lamp shining straight down onto a table
Imagine a point floating in the air above a table, lit from directly overhead. Its shadow on the table is the closest point on the table to the floating point — no other point on the table is nearer. That's projection: given a point outside a flat surface, its projection onto that surface is the nearest point that actually lies on it.
Least squares is exactly this, with the "table" being every combination of your predictor variables can possibly produce, and the "floating point" being your actual observed outcomes, which almost certainly can't be produced by any combination of predictors. The best fit is the shadow of your data on the table of achievable fits — and "best" here means closest in the sum-of-squared-differences sense, which is precisely what a perpendicular shadow minimises.
Writing it down
Say you have observations and predictor variables, stacked into a matrix (n rows, p columns) and a target vector ( numbers). You want coefficients so that — the predictions — are as close to as possible. "As close as possible" in the squared sense is exactly the geometric picture: must be the point in the column space of (every vector reachable by combining 's columns) that is nearest to . Nearest, for a flat space, means the leftover error must be perpendicular to every column of :
In words: means "take the dot product of the error with each column of " — setting all of those to zero is the algebraic statement of "the error is perpendicular to the whole fitted space." Solving that system for gives the famous normal-equations formula.
The projection matrix packages this into one operator that goes straight from to the fitted values without stopping at :
In words: is a matrix that, applied to any vector, returns its shadow on the column space of . Two properties make it a projection and nothing else: (projecting an already-projected point does nothing — the shadow of a shadow is itself) and (it's symmetric, which is the algebraic fingerprint of "perpendicular drop," not some slanted cast). The leftover is also a projection — onto everything perpendicular to the column space — and it produces the residuals: .
This explorer shows a general linear map stretching a circle into an ellipse. A projection is the special, degenerate case: it flattens the circle onto a line or plane entirely — one direction survives untouched (anything already in the column space), every perpendicular direction collapses to zero. Try pushing the transform toward a flat ellipse to see the collapsing in action.
Worked example 1: fitting a line through three points, by hand
Fit through , , . Here , .
Step 1. — top-left is "count of points" (3), the rest come from summing and (, ).
Step 2. — sum of is ; sum of is .
Step 3. Invert the 2x2: , so .
Step 4. .
So the fitted line is . Check: at , (actual 1, residual ); at , (actual 2, residual ); at , (actual 4, residual ). Multiply residuals by the -column: (rounding); multiply by itself: . The perpendicularity condition holds exactly — that's the geometry confirming the algebra.
Worked example 2: the projection matrix directly
Same . Compute . Using from above, first form , a 3x2 matrix, then multiply by (2x3) to get a 3x3 matrix . Skipping the arithmetic (it's mechanical matrix multiplication), the diagonal of comes out to — these are the leverage values, and they sum to exactly 2, the number of parameters (), which is a general fact: always. Applying to reproduces the fitted values found above — same answer, computed via the operator instead of via .
What this means in practice
- Every OLS regression is a projection. The fitted values are literally ; the residuals are ; and those two pieces are geometrically perpendicular, which is why decomposes cleanly into "explained" and "unexplained" sums of squares.
- The trace of counts effective parameters. This generalises beyond plain regression — ridge regression and smoothing splines have a "projection-like" matrix whose trace gives an effective degrees of freedom, even when it isn't exactly .
- Numerically, nobody inverts directly. It's condition-number-sensitive (see the matrix condition number); production code uses QR or Cholesky factorisations of instead, which are algebraically equivalent but numerically stable.
Least squares is a projection, not merely an equation-solving trick: the fitted values are the closest point in the space of achievable predictions to your actual data, achieved by dropping a perpendicular. The projection matrix makes this literal — it satisfies and , the algebraic definition of "shadow-caster."
The classic confusion is thinking a small residual means a good model. Residual size only measures distance from the shadow to the point — it says nothing about whether the table itself (the column space of ) is the right table to be projecting onto. If you regress on the wrong variables entirely, will still faithfully produce the nearest achievable fit, with a residual that can look deceptively small if happens to be flexible (many columns) — that's overfitting wearing a low-residual costume, not evidence of a correct model.
Related concepts
Practice in interviews
Further reading
- Strang, Introduction to Linear Algebra (ch. 4)
- Hastie, Tibshirani & Friedman, The Elements of Statistical Learning (ch. 3)