Turn nearest-point problems into orthogonal residuals and derive normal equations, projection matrices, and QR solves.
MathematicsLinear Algebra
On this page
An exact system Ax=b may be inconsistent. Least squares leaves the set of outputs generated by A unchanged and finds the point in the column space nearest to b.
Projection onto one direction
For nonzero a, the projection of b onto its span is
projab=⟨a,a⟩⟨b,a⟩a.
The coefficient follows from the orthogonality condition. The residual r=b−ca must satisfy ⟨r,a⟩=0. When a has unit norm, the coefficient is simply ⟨b,a⟩.
Projection onto a subspace finds the nearest point
Let W be finite dimensional. Suppose b∈W and r=b−b∈W⊥. Every w∈W then satisfies
∥b−w∥2=∥r∥2+∥b−w∥2.
Thus b is the unique nearest point. The argument decomposes error into orthogonal directions rather than beginning with differentiation of a distance formula.
If the columns of Q are an orthonormal basis of W, then
b=QQTb,PW=QQT.
The projection matrix satisfies PW2=PW and PWT=PW. One projection already lands in W, so a second one changes nothing.
Least squares and the normal equations
Minimizing ∥Ax−b∥2 requires Ax to be the projection of b onto col(A). The residual is orthogonal to every column, so
AT(b−Ax)=0.
These are the normal equations:
ATAx=ATb.
When the columns of A are independent, ATA is invertible and the coefficient vector is unique. With dependent columns, the best predicted output remains unique, but more than one coefficient vector may produce it.
ProofNecessity and uniqueness of coefficients
Suppose x minimizes the squared residual, and write r=b−Ax. Along any coefficient direction h the change in error is
∥r−tAh∥2−∥r∥2=−2trTAh+t2∥Ah∥2.
If the linear coefficient were nonzero, a sufficiently small t of the appropriate sign would lower the error. Thus rTAh=0 for all h, which is the normal equation. Existence follows from the orthogonal decomposition in the preceding chapter: its component in col(A) has at least one coefficient preimage.
Finally xTATAx=∥Ax∥2, so ker(ATA)=kerA. Full column rank makes this kernel zero, hence the square matrix ATA invertible. Dependent columns give a nonzero kernel direction and infinitely many minimizing coefficients.
Why the normal equations are sufficient
Let x satisfy them and set r=b−Ax. Every perturbation h satisfies
∥b−A(x+h)∥2=∥r∥2+∥Ah∥2.
The cross term vanishes because ATr=0. This proves a global minimum, not merely stationarity. Equality holds exactly for h∈kerA, so all minimizing coefficients form x+kerA. The predicted output is unique even when its coefficients are not.
From interpolation to fitting
Polynomial interpolation requires a curve to pass through every data point. Instead fit y=αx+β to (1,6),(2,7),(3,5):
A=123111,b=675.
The normal equations are
(14663)(αβ)=(3518),
which give α=−1/2 and β=7. The prediction is (6.5,6,5.5)T and the residual is (−1/2,1,−1/2)T. Direct multiplication verifies ATr=0.
QR avoids explicitly forming the normal equations
Gram–Schmidt applied to a full-column-rank matrix gives
A=QR,
where Q has orthonormal columns and R is upper triangular with nonzero diagonal. The least-squares problem reduces to
Rx=QTb.
This requires one orthogonalization and one triangular solve. The LU chapter factors a square matrix through elimination, while QR uses orthogonal bases for rectangular systems and least squares.
For the two-norm condition number of a full-column-rank matrix, define κ2(A)=σ1/σn. The SVD construction shows that ATA has positive eigenvalues σi2, so κ2(ATA)=κ2(A)2 exactly. Numerical stability of a particular QR implementation is a separate topic; Householder QR and modified Gram–Schmidt belong to a future numerical-linear-algebra treatment.
ProofWhy QR gives this triangular solve
The Gram–Schmidt prefix-span property expresses column j as ∑i≤jrijqi, where rij=qiTaj and rjj is the positive norm of its nonzero remainder. These column identities give A=QR and an invertible upper triangular R. Split b=QQTb+(I−QQT)b into orthogonal components. Then
∥Ax−b∥2=∥Rx−QTb∥2+∥(I−QQT)b∥2.
Here ∥Qz∥2=zTQTQz=∥z∥2. The second term is fixed and the first attains zero at the stated triangular solve.
Compute the fitting example completely with QR
Use the same three-point design matrix. Normalize its first column to obtain q1=(1,2,3)T/14. Removing its component from column two gives
111−146123=7141−2.
Thus q2=(4,1,−2)T/21 and
R=(1406/143/21),QTb=(35/1421/21).
Row two gives 3β=21; row one gives 14α+6β=35. Therefore β=7 and α=−1/2, verifying directly that QR and the normal equations produce the same best prediction.
Exercises
ExerciseProject onto a line
Project (3,1) onto the line spanned by (1,1) and find the residual.
Solution
The coefficient is 4/2=2, so the projection is (2,2) and the residual is (1,−1). Its inner product with (1,1) is zero.
ExerciseRecognize a projection matrix
For a matrix Q with orthonormal columns, prove that P=QQT satisfies P2=P.
Solution
P2=Q(QTQ)QT=QIQT=P.
ExerciseBest constant fit
Fit a single constant c to data y1,…,yn and show that the least-squares solution is the sample mean.
Solution
The design matrix is one column of ones. Its normal equation is nc=∑iyi, so c=n1∑iyi. The residuals sum to zero.
Comments