core24 min studyLesson 251 in Mathematics

Least squares as orthogonal projection

Derive fitted values from a projection and separate deterministic geometry from statistical assumptions.

On this page

What you’ll understand

  • Derive the normal equations from orthogonality.
  • Distinguish a unique fitted vector from unique coefficients.
  • Check a fitted line using its residual geometry.

Regression begins with a geometric question: which vector in an allowed model space lies closest to the observed response? A straight-line model permits only vectors obtained by combining its intercept column and its predictor column. The observed response usually lies outside that space. Least squares selects its perpendicular projection, leaving a residual that cannot be reduced by moving in any permitted direction. This construction exists before we assume anything about randomness, measurement errors, or a population relationship.

The model is a subspace of observation space

Let XX be a fixed real matrix with nn rows and pp columns, and let yy contain the nn observed responses. A coefficient vector bb proposes the fitted vector XbXb. Thus all possible fits form the column space of XX, a closed subspace of finite-dimensional Euclidean space. Linear here means linear in coefficients: a column containing squared predictor values is allowed, even though the resulting curve is not a straight line in the predictor.

Minimizing the squared Euclidean distance to that column space gives a unique fitted vector. To see why the error must be perpendicular, perturb a proposed fit along any column combination. If the residual has a component in that direction, a sufficiently small movement reduces its length. At the minimizing fit every such directional derivative vanishes. This argument also explains why a residual plot can show structure even though residuals are already orthogonal to the columns used in fitting.

β^∈arg min⁡b∈Rp∥y−Xb∥22,XT(y−Xβ^)=0\widehat\beta\in\operatorname*{arg\,min}_{b\in\mathbb R^p}\|y-Xb\|_2^2,\qquad X^{\mathsf T}(y-X\widehat\beta)=0
The normal equations express perpendicularity of the residual to every column of the fixed design matrix; they do not require a probability model.↔ Scroll wide equations horizontally.

Projection and coordinates answer different questions

If the columns are linearly independent, the Gram matrix is invertible and the coefficients are unique. Define the hat matrix HH to map responses to fitted values. It is symmetric and idempotent: applying the projection twice changes nothing. The complementary projection I−HI-H extracts residuals. Consequently the fitted vector and residual are perpendicular, and their squared lengths add to the squared length of the response. Centered sums of squares require an intercept column or a separate centering argument.

Dependent columns change the coordinate description, not the existence of the nearest point. Several coefficient vectors can represent the same fitted vector, and writing an ordinary inverse then becomes invalid. A pseudoinverse supplies one convention for coefficients, while an orthonormal basis for the column space supplies the projection directly. Numerically, QR or singular-value decompositions are usually preferable to explicitly forming the inverse of the Gram matrix, whose conditioning squares that of a full-rank design.

H=X(XTX)−1XT,HT=H,H2=H,∥y∥2=∥Hy∥2+∥(I−H)y∥2H=X(X^{\mathsf T}X)^{-1}X^{\mathsf T},\quad H^{\mathsf T}=H,\quad H^2=H,\quad \|y\|^2=\|Hy\|^2+\|(I-H)y\|^2
The displayed inverse formula assumes full column rank. The projection and perpendicular decomposition remain meaningful when redundant columns are removed or a pseudoinverse is used.↔ Scroll wide equations horizontally.

A fit is not yet an explanation of uncertainty

After finding the projection, a statistical model may posit Y=Xβ+εY=X\beta+\varepsilon with conditional mean-zero errors. That extra assumption makes full-rank least-squares coefficients unbiased. A covariance assumption is needed to calculate their variance, and Gaussian errors supply exact small-sample reference distributions. Neither a successful minimization nor a visually small residual establishes any of these assumptions. Predictor measurement error and omitted causes can change the scientific meaning of the fitted coefficients.

Orthogonality also has a useful invariance: replacing the columns by another basis of the same column space changes coefficient coordinates but leaves every fitted response and residual unchanged. By contrast, adding a genuinely new direction can improve the training fit without improving prediction. Keep three questions separate when interpreting software output: which space was permitted, which point was selected, and which probability assumptions justify uncertainty statements about it.

Project three responses onto an intercept and a slope

Fit y=(1,0,3)Ty=(1,0,3)^{\mathsf T} at predictor values x=(−1,0,1)Tx=(-1,0,1)^{\mathsf T} using an intercept and a slope. Verify the normal equations rather than accepting a plotted line.

  1. The columns are (1,1,1)T(1,1,1)^{\mathsf T} and (−1,0,1)T(-1,0,1)^{\mathsf T}, which are perpendicular. Their squared lengths are 33 and 22, and their dot products with yy are 44 and 22.

  2. Divide each dot product by its column's squared length to obtain intercept 4/34/3 and slope 11. The fitted vector is (1/3,4/3,7/3)T(1/3,4/3,7/3)^{\mathsf T}.

  3. Subtract to obtain residual (2/3,−4/3,2/3)T(2/3,-4/3,2/3)^{\mathsf T}. Its sum is zero and its dot product with xx is zero. Squaring and adding its components gives residual sum of squares 8/38/3.

The result

The line is y^=4/3+x\widehat y=4/3+x. Its residual cannot be shortened by changing either permitted coefficient because both residual-column dot products vanish.

Explore the model

Interactive lab

Interactive lab · vectors

Read a perpendicular decomposition

Pause and Reset; set Angle to 60 degrees and Magnitude to 2. Predict horizontal component 1 and vertical component about 1.732, then inspect the readouts. The components are perpendicular and their squared lengths sum to 4. This fixed-plane decomposition illustrates projection geometry; it does not fit a regression or change the design subspace.

Read a perpendicular decompositionPlayback traces a fixed vector and its projections. Angle and magnitude set the geometry; the moving marker does not change them. Current readings: x component: 1.393; y component: 0.975; Magnitude: 1.7. All parameters are adjustable with the labeled controls below.−7.46−3.7303.737.46−3.5−1.7501.753.5xyvvₓ = 1.39
Vectorx / y projectionsTrace marker
x component
1.393
y component
0.975
Magnitude
1.7
t = 0 s

Playback traces a fixed vector and its projections. Angle and magnitude set the geometry; the moving marker does not change them. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forProjection separates an allowed component from a perpendicular remainder; statistical assumptions enter afterward.

Check your understanding

What remains unique if two design columns are identical?

Try it yourself

Use the same design with y=(0,2,4)Ty=(0,2,4)^{\mathsf T}. Find the coefficients and residual, then explain whether a zero residual proves noiseless future observations.

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

The intercept is the response mean, and the centered predictor column has squared length 2.

Compare with the solution

The intercept is 22, while xTy=4x^{\mathsf T}y=4 gives slope 22. The proposed fit (0,2,4)T(0,2,4)^{\mathsf T} equals the observations, so the residual is zero. These three observations happen to lie in the model space. That deterministic fact neither specifies the process that generated them nor rules out future measurement noise.

Take it with you

  • Normal equations are residual orthogonality conditions.
  • The fitted vector is unique even when coefficient coordinates are redundant.
  • Inference requires probability assumptions beyond the least-squares calculation.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.