Skip to main content

12  Linear Least Squares

Suppose the columns of \(\bA\) describe the responses available to a model. Every prediction has the form \(\bA\bx\) and therefore lies in the column space of \(\bA\). If an observation \(\bb\) lies outside that space, no choice of parameters can fit it exactly. Least squares asks for the point in the column space closest to the observation.

The Euclidean criterion adds the squares of the individual errors. Squaring prevents positive and negative errors from cancelling and gives greater weight to larger errors. Other norms express different choices about how errors should be measured.

12.1 The Geometric Problem

NoteDefinition: Linear Least-Squares Problem

For \(\bA\in\fR^{m\times n}\) and \(\bb\in\fR^m\), the linear least-squares problem is \[ \begin{align} \min_{\bx\in\fR^n}\|\bA\bx-\bb\|_2^2 = \min_{\bx\in\fR^n} \sum_{i=1}^m \left(\sum_{j=1}^n a_{ij}x_j-b_i\right)^2. \end{align} \] The residual associated with \(\bx\) is \(\br(\bx)=\bb-\bA\bx\).

NoteExample

Suppose three measurements of one constant are \(1\), \(2\), and \(6\). A fitted constant \(c\) predicts \((c,c,c)^T\), so the squared error is \[ \begin{align} (c-1)^2+(c-2)^2+(c-6)^2=3(c-3)^2+14. \end{align} \] It is smallest at \(c=3\). The residual \((-2,-1,3)^T\) is perpendicular to \((1,1,1)^T\), the direction in which the predictions can move. The residual itself is nonzero because the observations disagree.

WarningExercise

Replace the three observations by \(b_1,...,b_m\). Complete the square to show that their mean gives the best fitted constant. Show that the residual entries sum to zero. Why would minimizing the sum of the errors without squaring them fail to define the same problem?

For a general matrix, changing coefficient \(x_j\) moves the fitted vector along column \(\ba_j\). At a closest point, movement in either direction along that column cannot decrease the squared distance. This requires the residual to be perpendicular to every column. The word normal in the next theorem means perpendicular.

NoteTheorem: Orthogonal Characterization and Normal Equations

A vector \(\widehat{\bx}\) is a least-squares minimizer if and only if its residual is orthogonal to every column of \(\bA\): \[ \begin{align} \br=\bb-\bA\widehat{\bx} \perp\mathcal{R}(\bA). \end{align} \] Equivalently, \[ \begin{align} \bA^T\br=\bzero, \qquad \bA^T\bA\widehat{\bx}=\bA^T\bb. \end{align} \] These are the normal equations. A minimizer is unique exactly when the columns of \(\bA\) are linearly independent.

Decompose \(\bb-\bA\bx\) by inserting the candidate fitted vector \(\bA\widehat{\bx}\): \[ \begin{align} \bb-\bA\bx &= (\bb-\bA\widehat{\bx}) + \bA(\widehat{\bx}-\bx). \end{align} \] If \(\bb-\bA\widehat{\bx}\perp\mathcal{R}(\bA)\), then since \(\bA(\widehat{\bx}-\bx)\in\mathcal{R}(\bA)\), the two summands are orthogonal: \[ \begin{align} (\bb-\bA\widehat{\bx})^T\bA(\widehat{\bx}-\bx) = 0. \end{align} \] The Pythagorean identity therefore yields \[ \begin{align} \|\bb-\bA\bx\|_2^2 &= \|(\bb-\bA\widehat{\bx}) + \bA(\widehat{\bx}-\bx)\|_2^2 \\ &= \|\bb-\bA\widehat{\bx}\|_2^2 + \|\bA(\widehat{\bx}-\bx)\|_2^2 \ge \|\bb-\bA\widehat{\bx}\|_2^2. \end{align} \] Equality holds if and only if \(\|\bA(\widehat{\bx}-\bx)\|_2^2=0\), meaning \(\bA(\widehat{\bx}-\bx)=\bzero\). Thus \(\widehat{\bx}\) minimizes the residual norm.

This proves sufficiency: a candidate with perpendicular residual gives a residual no larger than that of any competitor. We now prove necessity without assuming beforehand that the candidate has this orthogonality.

Conversely, if \(\widehat{\bx}\) is a minimizer, define for any \(\bv\in\fR^n\) and \(t\in\fR\) the scalar function \[ \begin{align} \phi(t) &= \|\bb-\bA(\widehat{\bx}+t\bv)\|_2^2 \\ &= \|\br - t\bA\bv\|_2^2 \\ &= \|\br\|_2^2 - 2t\,\bv^T\bA^T\br + t^2\|\bA\bv\|_2^2, \end{align} \] where \(\br=\bb-\bA\widehat{\bx}\). Because \(\phi\) has a global minimum at \(t=0\), \[ \begin{align} \phi'(0) = -2\bv^T\bA^T\br = 0 \quad\text{for all }\bv\in\fR^n. \end{align} \] Choosing \(\bv = \be_j\) for \(j=1,...,n\) gives \(\bA^T\br=\bzero\), which means \(\br\perp\mathcal{R}(\bA)\). Expanding \(\bA^T(\bb-\bA\widehat{\bx})=\bzero\) yields the normal equations \[ \begin{align} \bA^T\bA\widehat{\bx}=\bA^T\bb. \end{align} \]

A minimizer exists because the column space is a finite-dimensional subspace, so the orthogonal projection of \(\bb\) onto it exists. By definition of that column space, some coefficient vector produces the projected point. The orthogonality argument just proved makes that vector a minimizer. Existence of a fitted point does not yet imply uniqueness of its coefficients.

For uniqueness, suppose \(\widehat{\bx}_1\) and \(\widehat{\bx}_2\) are both minimizers. Then \(\bA(\widehat{\bx}_1-\widehat{\bx}_2)=\bzero\), so \(\widehat{\bx}_1-\widehat{\bx}_2\in\mathcal{N}(\bA)\). When \(\bA\) has full column rank, \(\mathcal{N}(\bA)=\{\bzero\}\), forcing \(\widehat{\bx}_1=\widehat{\bx}_2\). If \(\bz\in\mathcal{N}(\bA)\setminus\{\bzero\}\), then \(\bA(\widehat{\bx}+\bz)=\bA\widehat{\bx}\), so \(\widehat{\bx}+\bz\) is also a minimizer.

The fitted vector is unique even when the coefficient vector is not. Indeed two minimizers have residuals perpendicular to the range, and their fitted-vector difference lies in that range and its orthogonal complement, so it is zero. When a nonzero null vector exists, adding any scalar multiple preserves this common fitted vector and produces distinct coefficients.

TipRemark

The same condition follows by differentiating one coordinate at a time. For \(f(\bx)=\tfrac12\|\bA\bx-\bb\|_2^2\), hold every coordinate except \(x_j\) fixed. Then \[ \begin{align} \frac{\partial f}{\partial x_j}=\ba_j^T(\bA\bx-\bb). \end{align} \] The vector of these partial derivatives is called the gradient. Its vanishing gives the normal equations. The proof above also establishes sufficiency, which does not follow merely from setting derivatives to zero for an arbitrary function.

TipRemark

(Conditioning of Normal Equations) When \(\bA\) has full column rank, \(\bA^T\bA\) is SPD because \[ \begin{align} \bx^T\bA^T\bA\bx=\|\bA\bx\|_2^2>0 \end{align} \] for nonzero \(\bx\). This proves that the normal equations have a unique solution.

The normal equations replace the rectangular problem by a square one, but forming \(\bA^T\bA\) hides the geometry and worsens sensitivity. QR instead chooses orthonormal coordinates adapted to the column space.

NoteTheorem: Least-Squares Solution by QR

Let \(\bA\in\fR^{m\times n}\) have full column rank and thin QR factorization \(\bA=\bQ_1\bR_1\). Then the unique least-squares solution is obtained from \[ \begin{align} \bR_1\widehat{\bx}=\bQ_1^T\bb. \end{align} \] The fitted vector and residual are \[ \begin{align} \widehat{\bb}=\bQ_1\bQ_1^T\bb, \qquad \br=(\bI-\bQ_1\bQ_1^T)\bb. \end{align} \]

Let \(\bQ = (\bQ_1 \bQ_2)\in\fR^{m\times m}\) be orthogonal, where \(\bQ_1\in\fR^{m\times n}\) and \(\bQ_2\in\fR^{m\times (m-n)}\). Since \(\bQ^T\bQ=\bI\), the Euclidean norm is orthogonally invariant: \[ \begin{align} \|\bA\bx-\bb\|_2^2 &= \|\bQ^T(\bA\bx-\bb)\|_2^2 \\ &= \left\| \begin{pmatrix}\bQ_1^T\\\bQ_2^T\end{pmatrix}(\bQ_1\bR_1\bx - \bb) \right\|_2^2 \\ &= \left\| \begin{pmatrix}\bR_1\bx - \bQ_1^T\bb\\ -\bQ_2^T\bb\end{pmatrix} \right\|_2^2 \\ &= \|\bR_1\bx-\bQ_1^T\bb\|_2^2 + \|\bQ_2^T\bb\|_2^2. \end{align} \] The columns \(\bQ_1\) span the fitted subspace. The columns \(\bQ_2\) span its perpendicular complement. In these coordinates, only the first block can change when \(\bx\) changes, because \(\bQ_2^T\bA=\bzero\).

The second term \(\|\bQ_2^T\bb\|_2^2\) is fixed and independent of \(\bx\). Because \(\bA\) has full column rank, \(\bR_1\) is non-singular, and setting \(\bR_1\widehat{\bx}=\bQ_1^T\bb\) reduces the first term to zero, attaining the minimum value \(\|\bQ_2^T\bb\|_2^2\). The fitted vector and residual follow: \[ \begin{align} \widehat{\bb} &= \bA\widehat{\bx} = \bQ_1\bR_1(\bR_1^{-1}\bQ_1^T\bb) = \bQ_1\bQ_1^T\bb, \\ \br &= \bb - \widehat{\bb} = (\bI - \bQ_1\bQ_1^T)\bb. \end{align} \]

The triangular matrix \(\bR_1\) is nonsingular because \(\bA\bx=\bzero\) would otherwise hold for a nonzero vector in its null space. Thus the triangular system has one solution and sets the adjustable block exactly to zero. This proves uniqueness without forming \(\bA^T\bA\). It is also the algebraic reason for the QR method’s use in least squares (Golub and Van Loan 2013).

WarningExercise

Let \(\bW\) be SPD. Weighted least squares minimizes \(\|\bA\bx-\bb\|_{\bW}^2\). Prove that the residual is \(\bW\)-orthogonal to \(\mathcal{R}(\bA)\) and derive \(\bA^T\bW\bA\bx=\bA^T\bW\bb\). Explain what statistical or physical information the weight matrix expresses.

TipRemark

(Geometry vs. Conditioning in Householder QR) For a matrix with full column rank, the referenced result proves \(\kappa_2(\bA^T\bA)=\kappa_2(\bA)^2\). Householder QR avoids forming \(\bA^T\bA\) and is the safer dense default. The normal equations remain useful when special structure, cost, or repeated updates make the trade worthwhile.

NoteExample

Fitting \(y=c_0+c_1t\) to \((0,1)\), \((1,2)\), and \((2,4)\) gives \[ \begin{align} \bA=\begin{pmatrix}1&0\\1&1\\1&2\end{pmatrix}, \qquad \bb=\begin{pmatrix}1\\2\\4\end{pmatrix}. \end{align} \] The solution is \(c_0=5/6\) and \(c_1=3/2\). The residual is orthogonal to both the constant column and the time column. Hence its entries sum to zero and have zero first moment with respect to the sample times.

12.2 Rank-Deficient Problems

NoteTheorem: All Minimizers and the Minimum-Norm Choice

Let \(\widehat{\bx}\) be any least-squares minimizer. Then every minimizer has the form \[ \begin{align} \widehat{\bx}+\bz, \qquad \bz\in\mathcal{N}(\bA). \end{align} \] Among them there is exactly one vector of smallest Euclidean norm. It is characterized by \[ \begin{align} \widehat{\bx}_{\min}\perp\mathcal{N}(\bA), \qquad\text{equivalently}\qquad \widehat{\bx}_{\min}\in\mathcal{R}(\bA^T). \end{align} \]

Let \(\widehat{\bx}\) be any minimizer. Any two minimizers have the same fitted vector by the preceding characterization. Their difference is therefore in the null space. This justifies describing every minimizer by adding a null-space vector to one chosen minimizer.

For any \(\bz\in\mathcal{N}(\bA)\), \(\bA(\widehat{\bx}+\bz)=\bA\widehat{\bx}\), so \(\widehat{\bx}+\bz\) is also a minimizer. Conversely, if \(\bx'\) is any minimizer, then \(\bA\bx'=\bA\widehat{\bx}\) by uniqueness of the orthogonal projection \(\widehat{\bb}\), which gives \(\bA(\bx'-\widehat{\bx})=\bzero\), so \(\bx'-\widehat{\bx}\in\mathcal{N}(\bA)\).

Every vector decomposes uniquely as \(\bx = \bx_{\perp} + \bx_0\) with \(\bx_0\in\mathcal{N}(\bA)\) and \(\bx_{\perp}\in\mathcal{N}(\bA)^\perp\). For any minimizer \(\widehat{\bx} + \bz\), we have \(\bz\in\mathcal{N}(\bA)\), so \[ \begin{align} \|\widehat{\bx}+\bz\|_2^2 &= \|(\widehat{\bx}_{\perp} + \widehat{\bx}_0) + \bz\|_2^2 \\ &= \|\widehat{\bx}_{\perp} + (\widehat{\bx}_0 + \bz)\|_2^2 \\ &= \|\widehat{\bx}_{\perp}\|_2^2 + \|\widehat{\bx}_0 + \bz\|_2^2 \ge \|\widehat{\bx}_{\perp}\|_2^2. \end{align} \] The minimum is attained uniquely when \(\bz = -\widehat{\bx}_0\), which yields \(\widehat{\bx}_{\min} = \widehat{\bx}_{\perp}\in\mathcal{N}(\bA)^\perp\). By the fundamental theorem of linear algebra, \(\mathcal{N}(\bA)^\perp = \mathcal{R}(\bA^T)\).

The decomposition into \(\mathcal N(\bA)\) and \(\mathcal N(\bA)^\perp\) is unique. The perpendicular component is therefore shared by all minimizers, and only the null-space component can vary. The displayed sum of squared norms shows directly why removing that component minimizes the norm, rather than merely asserting it from orthogonality.

WarningExercise

Suppose \(\bA\bx=\bb\) is consistent but underdetermined. Prove that the minimum-norm solution is orthogonal to the null space. Explain why adding any invisible null-space component increases the norm without changing the data fit.

If a column combination is nearly zero, a large change in its coefficients produces little change in the predictions. Noisy observations can then determine those coefficients poorly. One way to control this sensitivity is to penalize large coefficient vectors, while allowing a larger residual. The parameter in the following definition specifies the strength of that preference.

NoteDefinition: Tikhonov Regularization

For \(\lambda>0\), Tikhonov or ridge regularization solves \[ \begin{align} \min_{\bx} \left(\|\bA\bx-\bb\|_2^2+\lambda\|\bx\|_2^2\right). \end{align} \] It trades residual fit against parameter size. The problem is equivalently the augmented least-squares problem \[ \begin{align} \min_{\bx} \left\| \begin{pmatrix}\bA\\\sqrt{\lambda}\bI\end{pmatrix}\bx - \begin{pmatrix}\bb\\\bzero\end{pmatrix} \right\|_2^2, \end{align} \] and its normal equations are \[ \begin{align} (\bA^T\bA+\lambda\bI)\bx=\bA^T\bb. \end{align} \]

WarningExercise

For the scalar problem \(\min_x\{(ax-b)^2+\lambda x^2\}\), derive \(x=ab/(a^2+\lambda)\). Compare its sensitivity to \(b\) with that of \(x=b/a\) when \(a\) is small. For a general matrix, prove \(\bA^T\bA+\lambda\bI\) is SPD whenever \(\lambda>0\), even if \(\bA\) has dependent columns.

TipRemark

(Interpretation of Regularization) Regularization also adds a modeling assumption. Among models with similar fit, it prefers smaller parameter vectors. Whether that assumption is scientifically appropriate depends on the meaning and scaling of the parameters. the referenced result identifies the directions that regularization damps and the amount by which each is damped.

12.3 Further Exercises

WarningExercise

Starting only from the orthogonal decomposition theorem, rederive the normal equations and prove both directions of the referenced result. Do not use derivatives.

WarningExercise

Fit a parabola \(y=c_0+c_1t+c_2t^2\) to \((-1,1)\), \((0,0)\), \((1,1)\), and \((2,5)\) using thin QR. Verify orthogonality of the residual against each column to working precision.

WarningExercise

Construct two almost parallel columns in \(\fR^3\). Compare the condition and numerical orthogonality of solutions obtained from normal equations, modified Gram–Schmidt QR, and Householder QR as the angle between the columns decreases.

WarningExercise

Rescale one column of a data matrix by \(10^6\). The set of fitted vectors does not change if its coefficient is rescaled inversely, but the ridge penalty does. Explain why regularization cannot be interpreted independently of units.

NoteFurther reading

Meyer develops the geometry and algebra with unusual clarity (Meyer 2000). Golub and Van Loan give the algorithmic treatment, while Higham explains what finite precision changes (Golub and Van Loan 2013; Higham 2002). Trefethen and Bau provide a concise bridge between the geometry and the computation (Trefethen and Bau 1997).