Why Fitting k Parameters Leaves n − k Directions
Ordinary least squares (OLS) fits a linear model by minimizing squared residuals. Geometrically, fitting k coefficients projects the observations onto a k-dimensional model space, leaving n − k perpendicular directions in which noise can remain. This is the same fact behind n − 1 in sample variance.
The Puzzle
In the previous post, we estimated the mean by choosing the value that minimized the squared deviations:
The underlying model was
This fit has one parameter, . After fitting it, the squared residuals have expectation , which is why the unbiased variance estimator divides their sum by .
Now suppose the mean is not constant. Each observation is a pair : is an observed predictor value and is the corresponding noisy response. Throughout this post, we treat as known, fixed numbers. The randomness comes from the noise in the responses.
The population mean of the response at predictor value is :
We do exactly what we did before, except that we now fit a candidate line instead of a candidate constant :
This procedure is ordinary least squares (OLS). The previous post was already its one-parameter version: a constant is just a line with no slope. Here we fit two parameters, the intercept and the slope. Does estimating one additional parameter change to ?
Provided and the predictor is not constant, the unbiased residual-variance estimator is no longer divided by , but by :
The optimization has barely changed. We replaced the candidate constant with the candidate line . Yet one fitted coefficient became two, and the divisor lost one more. In this post, we will see a geometric proof of why a full-rank linear fit with coefficients leaves residual degrees of freedom.
With two distinct points, a fitted line has zero residual sum of squares, but the variance estimate is not zero: it is undefined because its formula becomes . There are no residual degrees of freedom left with which to estimate the noise level.
The Model Is a Subspace
Write the observed responses as one vector . In a linear model, is the design matrix and is the unknown coefficient vector.
The column space is the set of all linear combinations of the columns of . As varies, runs through exactly this set, so the column space is the set of every response vector the model is capable of fitting. If the columns are linearly independent, it is a -dimensional flat subspace of .
For a line with an intercept and slope, the two columns are and . Choosing the intercept and slope chooses a linear combination of those two vectors:
Throughout the derivation we treat as fixed, assume full column rank with , and assume the mean function is correctly specified. Every observation has the same noise variance, and distinct noise terms are uncorrelated:
OLS Creates the Right Angle
OLS chooses the model vector closest to the observed response . The shortest path from a point to a flat subspace meets that subspace at a right angle. We can verify that claim directly.
Let be the fitted point and its residual:
The fit only has the freedom to choose a vector from . The residual is what remains of after making the best possible choice within that space: the part the model cannot cover using its available columns.
Now take any nonzero direction in the model space. Moving the fitted point by gives another allowed fitted point, . The vector from this new fitted point to the observed response is the new residual:
The squared length of the new residual is the new squared distance to . Expanding it gives
Suppose . Choose to remove the component of pointing along . Substituting that value into the expansion gives
The left-hand side is the squared distance from to the new fitted point ; the right-hand side is the squared distance from to the OLS fit . The inequality says the new point would be closer, contradicting the fact that OLS chose the closest point. Therefore for every direction in the model space: the residual is perpendicular to that space. The normal equations state the same conclusion in calculus notation:
Each entry of is the inner product of the residual with one column of . All are zero: the residual is perpendicular to every fitted direction.
Recall the variance question we started with: the residual sum of squares is , and we want to know how much noise remains in it after fitting the model. The right-angle argument identifies the part left over, but it does not yet explain why its expected squared length is exactly . To obtain that factor, we need an operator that isolates the residual part and lets us count its surviving noise directions.
The Hat Matrix Performs the Projection
The right-angle argument tells us what OLS does. Solving the normal equations gives the operator that actually does it:
is the hat matrix, named because it puts the hat on . It projects any response vector onto . Its companion extracts the perpendicular part:
The hat matrix did not create the geometry. Least squares did. What adds is a concrete projection operator that we can calculate with.
Why Only Noise Remains in the Residual
Recall that the true model decomposes the response as . The vector contains the noise-free population mean at each observed predictor value; this is the signal. The vector is the noise.
Substitute this decomposition into the residual operator:
The mean-response term disappears because it already lies in the fitted space, where reproduces it exactly. The residual is therefore not “the part of the data we threw away.” It is precisely the component of the noise that points outside the model space.
Before counting those directions, it is worth making this cancellation visible. With three observations, an intercept-and-slope model spans a literal plane in . Holding the noise fixed, changing the true intercept () or slope () moves both the generated response and its fitted component within that plane, leaving the residual unchanged. The slider adds a possible noise component perpendicular to the plane; because the model has no direction with which to cover it, the entire component passes into the residual.
This is the first half of the story: fitting removes the part of the response that lies in the model space, so the residual contains only the noise component perpendicular to that space. What remains is to count how much noise energy is expected to live there.
Counting the Noise Directions
This is the math-heavy section of the post, but the takeaway is simple: after the projection, the residual keeps exactly orthogonal noise coordinates, each contributing in expectation. Everything below is just making that sentence precise.
We could continue in the original observation coordinates and expand there. But each residual coordinate would mix several noise coordinates, and squaring them would produce many cross-terms. The geometry would be hidden inside the algebra.
To see the difficulty, write . Then
The cross-terms disappear under white noise, but the last double sum still does not visibly say . We could finish by uncovering the projector identities hidden inside ; the trace proof in Appendix A takes exactly that route. But there is a more revealing option: instead of simplifying the projector in awkward coordinates, choose coordinates in which the projector is already simple.
Choose an orthonormal basis aligned with the two perpendicular spaces: the first basis vectors span , and the remaining span its orthogonal complement. Because the basis is orthonormal, this changes only the coordinates used to describe the residual vector, not its squared length . In these coordinates, the projection becomes simple bookkeeping.
Because projects onto , it leaves each of the first basis vectors unchanged. Each of the remaining basis vectors is perpendicular to that space, so sends it to zero:
The notation means “the matrix of the transformation when vectors are described using coordinates in the basis .” The action above means that, in these coordinates, returns the first coordinates unchanged and replaces the remaining coordinates with zero. Its residual counterpart does the reverse:
Thus keeps the first coordinates and deletes the remaining , while does the reverse.
Now expand the noise in this basis:
where is the th entry of . Each is therefore a weighted sum of the original noise coordinates. White noise means that the are uncorrelated, each has mean zero, and each has variance .
is the amount of noise pointing along the unit direction . The next calculation shows that every unit direction contains expected noise energy , even though changing the direction changes the weighted combination . This is what will make the rest of the proof a dimension count: every residual direction that survives contributes one .
Now compute its expected squared magnitude:
Three facts work together in this calculation. First, the original noise coordinates are uncorrelated, so the cross-terms vanish. Second, each coordinate has variance , leaving . Third, is a unit vector, so . Since also has mean zero, its expected square equals its variance.
Finally, deletes the first coordinates and keeps the remaining :
That is the result:
Variance is noise energy per direction. OLS uses directions for the fit and leaves directions in the residuals.
The divisor counts residual directions, not discarded observations. Therefore, for , the unbiased residual-variance estimator is
Why n − 1 Is the Same Story
The previous post is now just the case . A mean-only model has , so its fitted space is the single direction containing all constant vectors. Its hat matrix is the averaging operator:
Fitting the mean uses one direction. The residual must lie in the perpendicular hyperplane whose coordinates sum to zero, leaving directions and therefore
For a line with an intercept and slope, the residuals instead satisfy two orthogonality constraints,
leaving directions. The two cases are the same projection at different dimensions.
| Case | Fitted space | Fitted dimension | Residual dimension | Divisor |
|---|---|---|---|---|
| Mean only | ||||
| Intercept + slope | ||||
| Full-rank OLS |
What the Design Changes, and What It Does Not
The result makes a stronger claim than “divide by .” The predictor values determine which -dimensional space OLS fits, so different designs can produce very different fitted lines and residual patterns. But under white noise, the orientation of that space does not affect the expected total residual energy. Every full-rank -dimensional fit leaves noise directions.
The animation below checks that design-invariance numerically. Each trial draws fresh white noise for two different designs. Their individual residual sums of squares fluctuate, but both running averages converge to the same target, .
This is why the divisor depends on the dimension , not on the particular values inside .
The Takeaway
If you retain one picture, let it be this: OLS can move only within the -dimensional space . It chooses the closest point there, leaving a perpendicular residual in the remaining directions. White noise supplies expected energy to each direction, so
The in and the in count fitted directions, not discarded observations.
Appendix B gives a less intuitive but direct algebraic derivation by extending the exact optimization identity from the previous post to dimensions.
Appendix A: The Same Count Using the Trace
The geometric proof is complete. This appendix records the same direction count algebraically.
is an orthogonal projector, not an orthogonal matrix. Its symmetry guarantees an orthonormal eigenbasis. Along the fitted directions, has eigenvalue ; along the residual directions, it has eigenvalue . The residual projector reverses those eigenvalues, so its trace is .
The defining properties of the projector and cyclicity of the trace give the fitted count directly:
Now write the residual sum of squares as a quadratic form:
For the quadratic-form identity, expand . Conditional on , the off-diagonal expectations vanish and every diagonal expectation is , leaving .
Appendix B: The Previous Proof in k Dimensions
The previous post used an exact scalar identity. The same identity exists for a -parameter least-squares fit because is quadratic and its gradient vanishes at . Here
the normalized least-squares loss. This has the same minimizer as the unnormalized squared distance above, but the factor matters for the Hessian scale. We also use the standard conditional unbiasedness fact under the assumptions stated at the start of the post. For OLS, the Hessian of the loss and the covariance of the estimator contain inverse factors that cancel when the expected quadratic gap is written as a trace:
This is the previous proof lifted from one dimension to . It computes the expected reduction in normalized squared error obtained by fitting the coefficients. Since
the result above immediately gives
or, after multiplying by , , exactly the result in Appendix A.
The two appendices therefore count complementary pieces of the same noise. Appendix B counts the expected absorbed by the fitted projector ; Appendix A counts the expected left by the residual projector . Their traces add to :
References
- Hoaglin, D. C., & Welsch, R. E. (1978). The hat matrix in regression and ANOVA. The American Statistician, 32(1), 17–22.
- Hastie, T., Tibshirani, R., & Friedman, J. (2009). The Elements of Statistical Learning (2nd ed.), §7.5–7.6. Springer.
- Efron, B., & Hastie, T. (2016). Computer Age Statistical Inference. Cambridge University Press.