An exact system Ax=bA\mathbf x=\mathbf b may be inconsistent. Least squares leaves the set of outputs generated by AA unchanged and finds the point in the column space nearest to b\mathbf b.

Projection onto one direction

For nonzero a\mathbf a, the projection of b\mathbf b onto its span is

proj⁡ab=⟨b,a⟩⟨a,a⟩a.\operatorname{proj}_{\mathbf a}\mathbf b =\frac{\langle\mathbf b,\mathbf a\rangle} {\langle\mathbf a,\mathbf a\rangle}\mathbf a.

The coefficient follows from the orthogonality condition. The residual r=b−ca\mathbf r=\mathbf b-c\mathbf a must satisfy ⟨r,a⟩=0\langle\mathbf r,\mathbf a\rangle=0. When a\mathbf a has unit norm, the coefficient is simply ⟨b,a⟩\langle\mathbf b,\mathbf a\rangle.

Projection onto a subspace finds the nearest point

Let WW be finite dimensional. Suppose b^∈W\widehat{\mathbf b}\in W and r=b−b^∈W⊥\mathbf r=\mathbf b-\widehat{\mathbf b}\in W^\perp. Every w∈W\mathbf w\in W then satisfies

∥b−w∥2=∥r∥2+∥b^−w∥2.\|\mathbf b-\mathbf w\|^2 =\|\mathbf r\|^2+ \|\widehat{\mathbf b}-\mathbf w\|^2.

Thus b^\widehat{\mathbf 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 QQ are an orthonormal basis of WW, then

b^=QQTb,PW=QQT.\widehat{\mathbf b}=QQ^{\mathsf T}\mathbf b, \qquad P_W=QQ^{\mathsf T}.

The projection matrix satisfies PW2=PWP_W^2=P_W and PWT=PWP_W^{\mathsf T}=P_W. One projection already lands in WW, so a second one changes nothing.

Least squares and the normal equations

Minimizing ∥Ax−b∥2\|A\mathbf x-\mathbf b\|^2 requires Ax^A\widehat{\mathbf x} to be the projection of b\mathbf b onto col⁡(A)\operatorname{col}(A). The residual is orthogonal to every column, so

AT(b−Ax^)=0.A^{\mathsf T}(\mathbf b-A\widehat{\mathbf x})=\mathbf0.

These are the normal equations:

ATAx^=ATb.A^{\mathsf T}A\widehat{\mathbf x} =A^{\mathsf T}\mathbf b.

When the columns of AA are independent, ATAA^{\mathsf T}A 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^\widehat{\mathbf x} minimizes the squared residual, and write r=b−Ax^\mathbf r=\mathbf b-A\widehat{\mathbf x}. Along any coefficient direction h\mathbf h the change in error is

∥r−tAh∥2−∥r∥2=−2trTAh+t2∥Ah∥2.\|\mathbf r-tA\mathbf h\|^2-\|\mathbf r\|^2 =-2t\mathbf r^{\mathsf T}A\mathbf h+t^2\|A\mathbf h\|^2.

If the linear coefficient were nonzero, a sufficiently small tt of the appropriate sign would lower the error. Thus rTAh=0\mathbf r^{\mathsf T}A\mathbf h=0 for all h\mathbf h, which is the normal equation. Existence follows from the orthogonal decomposition in the preceding chapter: its component in col⁡(A)\operatorname{col}(A) has at least one coefficient preimage.

Finally xTATAx=∥Ax∥2\mathbf x^{\mathsf T}A^{\mathsf T}A\mathbf x=\|A\mathbf x\|^2, so ker⁡(ATA)=ker⁡A\ker(A^{\mathsf T}A)=\ker A. Full column rank makes this kernel zero, hence the square matrix ATAA^{\mathsf T}A invertible. Dependent columns give a nonzero kernel direction and infinitely many minimizing coefficients.

Why the normal equations are sufficient

Let x^\widehat{\mathbf x} satisfy them and set r=b−Ax^\mathbf r=\mathbf b-A\widehat{\mathbf x}. Every perturbation h\mathbf h satisfies

∥b−A(x^+h)∥2=∥r∥2+∥Ah∥2.\|\mathbf b-A(\widehat{\mathbf x}+\mathbf h)\|^2 =\|\mathbf r\|^2+\|A\mathbf h\|^2.

The cross term vanishes because ATr=0A^{\mathsf T}\mathbf r=0. This proves a global minimum, not merely stationarity. Equality holds exactly for h∈ker⁡A\mathbf h\in\ker A, so all minimizing coefficients form x^+ker⁡A\widehat{\mathbf x}+\ker A. 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+βy=\alpha x+\beta to (1,6),(2,7),(3,5)(1,6),(2,7),(3,5):

A=(112131),b=(675).A=\begin{pmatrix}1&1\\2&1\\3&1\end{pmatrix}, \qquad \mathbf b=\begin{pmatrix}6\\7\\5\end{pmatrix}.

The normal equations are

(14663)(αβ)=(3518),\begin{pmatrix}14&6\\6&3\end{pmatrix} \begin{pmatrix}\alpha\\\beta\end{pmatrix} =\begin{pmatrix}35\\18\end{pmatrix},

which give α=−1/2\alpha=-1/2 and β=7\beta=7. The prediction is (6.5,6,5.5)T(6.5,6,5.5)^{\mathsf T} and the residual is (−1/2,1,−1/2)T(-1/2,1,-1/2)^{\mathsf T}. Direct multiplication verifies ATr=0A^{\mathsf T}\mathbf r=\mathbf0.

QR avoids explicitly forming the normal equations

Gram–Schmidt applied to a full-column-rank matrix gives

A=QR,A=QR,

where QQ has orthonormal columns and RR is upper triangular with nonzero diagonal. The least-squares problem reduces to

Rx^=QTb.R\widehat{\mathbf x}=Q^{\mathsf T}\mathbf b.

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\kappa_2(A)=\sigma_1/\sigma_n. The SVD construction shows that ATAA^{\mathsf T}A has positive eigenvalues σi2\sigma_i^2, so κ2(ATA)=κ2(A)2\kappa_2(A^{\mathsf T}A)=\kappa_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 jj as ∑i≤jrijqi\sum_{i\le j}r_{ij}\mathbf q_i, where rij=qiTajr_{ij}=\mathbf q_i^{\mathsf T}\mathbf a_j and rjjr_{jj} is the positive norm of its nonzero remainder. These column identities give A=QRA=QR and an invertible upper triangular RR. Split b=QQTb+(I−QQT)b\mathbf b=QQ^{\mathsf T}\mathbf b+(I-QQ^{\mathsf T})\mathbf b into orthogonal components. Then

∥Ax−b∥2=∥Rx−QTb∥2+∥(I−QQT)b∥2.\|A\mathbf x-\mathbf b\|^2 =\|R\mathbf x-Q^{\mathsf T}\mathbf b\|^2 +\|(I-QQ^{\mathsf T})\mathbf b\|^2.

Here ∥Qz∥2=zTQTQz=∥z∥2\|Q\mathbf z\|^2=\mathbf z^{\mathsf T}Q^{\mathsf T}Q\mathbf z=\|\mathbf 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\mathbf q_1=(1,2,3)^{\mathsf T}/\sqrt{14}. Removing its component from column two gives

(111)−614(123)=17(41−2).\begin{pmatrix}1\\1\\1\end{pmatrix} -\frac{6}{14}\begin{pmatrix}1\\2\\3\end{pmatrix} =\frac17\begin{pmatrix}4\\1\\-2\end{pmatrix}.

Thus q2=(4,1,−2)T/21\mathbf q_2=(4,1,-2)^{\mathsf T}/\sqrt{21} and

R=(146/1403/21),QTb=(35/1421/21).R=\begin{pmatrix}\sqrt{14}&6/\sqrt{14}\\0&3/\sqrt{21}\end{pmatrix}, \qquad Q^{\mathsf T}\mathbf b= \begin{pmatrix}35/\sqrt{14}\\21/\sqrt{21}\end{pmatrix}.

Row two gives 3β=213\beta=21; row one gives 14α+6β=3514\alpha+6\beta=35. Therefore β=7\beta=7 and α=−1/2\alpha=-1/2, verifying directly that QR and the normal equations produce the same best prediction.

Exercises

ExerciseProject onto a line

Project (3,1)(3,1) onto the line spanned by (1,1)(1,1) and find the residual.

Solution

The coefficient is 4/2=24/2=2, so the projection is (2,2)(2,2) and the residual is (1,−1)(1,-1). Its inner product with (1,1)(1,1) is zero.

ExerciseRecognize a projection matrix

For a matrix QQ with orthonormal columns, prove that P=QQTP=QQ^{\mathsf T} satisfies P2=PP^2=P.

Solution

P2=Q(QTQ)QT=QIQT=PP^2=Q(Q^{\mathsf T}Q)Q^{\mathsf T}=QIQ^{\mathsf T}=P.

ExerciseBest constant fit

Fit a single constant cc to data y1,…,yny_1,\ldots,y_n 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=∑iyinc=\sum_i y_i, so c=1n∑iyic=\frac1n\sum_i y_i. The residuals sum to zero.