The Geometry of Least Squares
Projections, the hat matrix, leverage and numerically stable solvers.
Least squares is a projection, and a projection has a matrix. That matrix tells you how strongly each point steers its own fitted value, which points can move the whole fit on their own, how to strip one variable's influence out of another, and why the textbook formula is the wrong way to compute anything.
What you’ll learn
- Prove that the hat matrix is symmetric, idempotent and of trace , and derive the covariances of the fitted values and residuals.
- Compute leverages and Cook's distances, and derive the exact change in the coefficients when one observation is deleted.
- State and prove the Frisch–Waugh–Lovell theorem, and use it to explain variance inflation under collinearity.
- Quantify why QR and the SVD are numerically safer than the normal equations, in terms of the condition number.
The hat matrix
Throughout, has full column rank and with and . The fitted values are with the hat matrix , and the residuals are .
Symmetry, because is symmetric and so is its inverse; idempotence, because the middle factor cancels:
If with , then , so . A symmetric matrix is diagonalizable, so its rank is the number of unit eigenvalues, which is its trace. The cyclic property of the trace gives
Since , the residuals see only the noise. With and symmetric and idempotent,
Likewise . In particular and , and the fitted values have average variance .
With a thin QR factorization ( with orthonormal columns, upper triangular and invertible), the inverse disappears: . That is the computational form, and it shows that , the squared length of row of .
Leverage
The diagonal entry is the leverage of observation . Since , it is the derivative : how strongly a point pulls its own fitted value toward itself. Leverage depends on alone, never on . A high-leverage point is a potential problem; it becomes an actual one only if its response disagrees with the rest.
- Range. From and symmetry, , so . With an intercept column, .
- Average. , so the average leverage is . Hoaglin & Welsch (1978) suggest a second look at points with .
- The extreme. If then , so : the fit is forced through the point and its residual is zero however wrong is.
- Distance from the center. With an intercept, , where omits the constant and is the centered cross-product matrix of the features: a Mahalanobis distance from the centroid. For one feature, .
Simple regression (intercept and slope) on the four inputs . What is the leverage of the point at ?
Show the answer
0.7
and , so . The inner points get , and the four leverages sum to . Its residual has variance only : the line is pulled hard toward it.
Influence: what one point does to the fit
Leverage measures potential; influence measures effect. The cleanest way to measure the effect of observation is to delete it and refit. No refitting is needed: one rank-one update gives the answer in closed form.
Write and . Deleting row subtracts from and from , and the Sherman–Morrison formula inverts the rank-one update:
Multiply by , using , and :
The bracket collapses to the residual . The change in the coefficients is the residual, amplified by :
Multiplying by shows that deleting the point moves its own fitted value by , so its leave-one-out residual is : the identity behind one-fit leave-one-out cross-validation.
For the four points , , , , the least-squares residuals are , the leverages , and . Which point has the largest Cook's distance?
- , which has the largest residual
Show the answer
D.
with . For : . For : . For : about ; for : about . A moderate residual at high leverage beats a large residual at low leverage.
Partialling out: the Frisch–Waugh–Lovell theorem
Apply to the full fit. It annihilates , and it leaves alone because :
The residual is also orthogonal to the regressors of the reduced problem, since is symmetric and :
So and satisfy the normal equations of regressing on . Full column rank of makes of full column rank, so that solution is unique, and solving gives the formula. Since is idempotent, regressing the raw on gives the same coefficient, though not the same residuals.
What a coefficient means. Take to be a single column : its coefficient is the slope of on the part of that the other features cannot explain. Plotting against (an added-variable plot) shows that slope and the points that drive it. Centering is the special case , and including one indicator per group is the same as subtracting group means (the "within" estimator for panel data).
Why collinearity hurts. The theorem also gives the variance: . With an intercept in the model, , where and is the of regressing on the other features. So
and is the variance inflation factor: the price of feature being predictable from the others.
A feature can be predicted from the other features with . Compared with a design where it is uncorrelated with them (same and ), by what factor is the standard error of its coefficient multiplied?
Show the answer
3.16
The variance is multiplied by , so the standard error by . To get it back you would need ten times as much data. At the factor is 10 for the standard error and 100 for the variance.
Computing least squares stably
Let , the condition number of (the ratio of its largest to smallest singular value), and the unit roundoff of double precision. The least-squares problem has an intrinsic sensitivity: perturbing and by relative amounts of order moves by a relative amount of order
so the term matters only when the residual is large (Golub & Van Loan, 2013, §5.3). Backward-stable algorithms, Householder QR (introduced for least squares by Golub, 1965) and the SVD, achieve errors of this order. Forming instead squares the condition number whatever the residual: the normal equations behave like , losing about twice as many digits as necessary when the fit is good.
Think first Take with . has full rank. What happens to the normal equations in double precision?
, and rounds to exactly in double precision. The computed is the all-ones matrix, which is singular, so the normal equations break down, although is comfortably below , and a QR factorization of still delivers about eight correct digits ( when the residual is small). This is Läuchli's classic example.
| Method | Flops () | Error scale | Rank deficient? |
|---|---|---|---|
| Normal equations + Cholesky | breaks down | ||
| Householder QR, | with column pivoting | ||
| SVD, | a few times QR | yes: zero out tiny | |
| LSQR (Krylov iterations) | per iteration | depends on stopping | converges to the minimum-norm solution from |
lstsq); this truncated solution is the stable version of the pseudoinverse. The normal equations remain a reasonable choice when is small and speed matters.Key takeaways
- is the orthogonal projection onto : symmetric, idempotent, eigenvalues 0 and 1, trace . Fitted values have covariance and residuals .
- Leverage depends only on , lies in with an intercept and averages . Deleting point moves the coefficients by , and Cook's distance combines outlyingness with leverage.
- Frisch–Waugh–Lovell: a coefficient is the slope of on the part of its feature orthogonal to the others, so collinearity multiplies its variance by .
- The normal equations square the condition number. Householder QR and the SVD are backward stable, and the SVD also handles rank deficiency; randomized sketches speed up very tall problems.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A regression has observations and columns (intercept included). What is the average leverage?
- It depends on
Show the answer
B.
, so the average is , and the Hoaglin–Welsch screen flags points above .
Why can raw residuals hide a bad observation that has high leverage?
- Because high-leverage points always have large residuals
- Because leverage changes the noise variance
- Because residuals are not defined at high-leverage points
- Because its residual has variance : the fit is pulled toward the point, which shrinks its residual
Show the answer
D. Because its residual has variance : the fit is pulled toward the point, which shrinks its residual
, so a point with near 1 has a residual near zero even if is far off. Studentizing, , puts residuals on a common scale, and Cook's distance combines them with leverage.
and the fit is good (small residual). Working in double precision (about 16 significant digits), roughly how many digits of can the normal equations lose?
- About 12, since
- About 6, the same as QR
- About 24, so nothing survives
- None: is an exact formula
Show the answer
A. About 12, since
Forming squares the condition number, so errors of order leave about 4 correct digits. With a small residual, QR or the SVD lose only about digits.
You regress wages on education and experience. By the Frisch–Waugh–Lovell theorem, the coefficient on education equals the slope from which regression?
- Wages on education alone
- Education on wages
- The residuals of wages-on-experience on the residuals of education-on-experience
- The average of the two single-variable slopes
Show the answer
C. The residuals of wages-on-experience on the residuals of education-on-experience
Partial experience (and the intercept) out of both sides, then regress. The coefficient uses only the variation in education that experience does not explain, which is what "holding experience fixed" means for a linear fit.
Further reading
- D. C. Hoaglin & R. E. Welsch, "The hat matrix in regression and ANOVA," The American Statistician 32(1), 17–22 (1978).
- R. D. Cook, "Detection of influential observation in linear regression," Technometrics 19(1), 15–18 (1977).
- M. C. Lovell, "Seasonal adjustment of economic time series and multiple regression analysis," Journal of the American Statistical Association 58(304), 993–1010 (1963).
- G. H. Golub & C. F. Van Loan, Matrix Computations, 4th ed. (Johns Hopkins University Press, 2013), chapter 5.
- P. Drineas, M. W. Mahoney, S. Muthukrishnan & T. Sarlós, "Faster least squares approximation," Numerische Mathematik 117, 219–249 (2011).