Math Core

Lesson 6.5 · Orthogonality and Least Squares

Least-squares problems

Real data rarely fits a model exactly. Measure four points that "should" lie on a line and they won't quite; the system Ax=bA\mathbf{x} = \mathbf{b} for the line's coefficients has no solution. Instead of giving up, you ask for the x\mathbf{x} that makes AxA\mathbf{x} as close to b\mathbf{b} as possible. Orthogonal projection answers that question exactly.

The least-squares problem

Definition

Least-squares solution

If AA is m×nm \times n and b\mathbf{b} is in Rm\mathbb{R}^m, a least-squares solution of Ax=bA\mathbf{x} = \mathbf{b} is a vector x^\hat{\mathbf{x}} in Rn\mathbb{R}^n such that

∥b−Ax^∥≤∥b−Ax∥for all x in Rn.\|\mathbf{b} - A\hat{\mathbf{x}}\| \le \|\mathbf{b} - A\mathbf{x}\| \quad \text{for all } \mathbf{x} \text{ in } \mathbb{R}^n.

The name comes from the fact that ∥b−Ax∥2\|\mathbf{b} - A\mathbf{x}\|^2 is a sum of squares of the entries of b−Ax\mathbf{b} - A\mathbf{x}. The number ∥b−Ax^∥\|\mathbf{b} - A\hat{\mathbf{x}}\| is the least-squares error.

The vectors AxA\mathbf{x} are exactly the vectors in Col⁡A\operatorname{Col} A. So you want the point of Col⁡A\operatorname{Col} A closest to b\mathbf{b}, and by the best approximation theorem that is

b^=proj⁡Col⁡Ab.\hat{\mathbf{b}} = \operatorname{proj}_{\operatorname{Col} A}\mathbf{b}.

Since b^\hat{\mathbf{b}} is in Col⁡A\operatorname{Col} A, the equation Ax=b^A\mathbf{x} = \hat{\mathbf{b}} is consistent, and its solutions are the least-squares solutions.

The normal equations

You usually don't have an orthogonal basis for Col⁡A\operatorname{Col} A, so computing b^\hat{\mathbf{b}} directly is awkward. Instead, use the key property of the projection: b−Ax^\mathbf{b} - A\hat{\mathbf{x}} is orthogonal to Col⁡A\operatorname{Col} A, so it is orthogonal to every column of AA. That means AT(b−Ax^)=0A^T(\mathbf{b} - A\hat{\mathbf{x}}) = \mathbf{0}, or:

The normal equations

The least-squares solutions of Ax=bA\mathbf{x} = \mathbf{b} are exactly the solutions of

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

This system is always consistent. If the columns of AA are linearly independent, then ATAA^TA is invertible and the least-squares solution is unique:

x^=(ATA)−1ATb.\hat{\mathbf{x}} = (A^TA)^{-1}A^T\mathbf{b}.

If the columns of AA are dependent, there are infinitely many least-squares solutions (they all give the same Ax^=b^A\hat{\mathbf{x}} = \hat{\mathbf{b}}).

Worked example: Solving with the normal equations

Find the least-squares solution of Ax=bA\mathbf{x} = \mathbf{b} and the least-squares error, where

A=[100111],b=[216].A = \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{bmatrix}, \qquad \mathbf{b} = \begin{bmatrix} 2 \\ 1 \\ 6 \end{bmatrix}.

The system is inconsistent: rows 1 and 2 force x1=2x_1 = 2, x2=1x_2 = 1, but then row 3 gives 3≠63 \neq 6. Compute

ATA=[2112],ATb=[2+61+6]=[87].A^TA = \begin{bmatrix} 2 & 1 \\ 1 & 2 \end{bmatrix}, \qquad A^T\mathbf{b} = \begin{bmatrix} 2 + 6 \\ 1 + 6 \end{bmatrix} = \begin{bmatrix} 8 \\ 7 \end{bmatrix}.

Solve 2x1+x2=82x_1 + x_2 = 8, x1+2x2=7x_1 + 2x_2 = 7: subtracting twice the second from the first gives −3x2=−6-3x_2 = -6, so x2=2x_2 = 2 and x1=3x_1 = 3. Thus x^=(3,2)\hat{\mathbf{x}} = (3, 2).

Then Ax^=(3,2,5)A\hat{\mathbf{x}} = (3, 2, 5) and b−Ax^=(−1,−1,1)\mathbf{b} - A\hat{\mathbf{x}} = (-1, -1, 1). Check it is orthogonal to both columns: (−1,−1,1)⋅(1,0,1)=0(-1, -1, 1) \cdot (1, 0, 1) = 0 and (−1,−1,1)⋅(0,1,1)=0(-1, -1, 1) \cdot (0, 1, 1) = 0. The least-squares error is 1+1+1=3\sqrt{1 + 1 + 1} = \sqrt{3}.

Common mistake

Don't "solve" Ax=bA\mathbf{x} = \mathbf{b} by cancelling ATA^T from ATAx=ATbA^TA\mathbf{x} = A^T\mathbf{b}, and don't write x^=A−1b\hat{\mathbf{x}} = A^{-1}\mathbf{b}: a non-square AA has no inverse. The whole point is that Ax=bA\mathbf{x} = \mathbf{b} has no solution, while the square system ATAx=ATbA^TA\mathbf{x} = A^T\mathbf{b} does. Also remember that x^\hat{\mathbf{x}} lives in Rn\mathbb{R}^n while b^=Ax^\hat{\mathbf{b}} = A\hat{\mathbf{x}} lives in Rm\mathbb{R}^m.

Fitting a line to data

Given data points (x1,y1),…,(xm,ym)(x_1, y_1), \ldots, (x_m, y_m), you want the line y=β0+β1xy = \beta_0 + \beta_1x that fits best. Asking every point to lie on the line gives mm equations β0+β1xi=yi\beta_0 + \beta_1x_i = y_i, that is, Xβ=yX\boldsymbol{\beta} = \mathbf{y} with

X=[1x11x2⋮⋮1xm],β=[β0β1],y=[y1y2⋮ym].X = \begin{bmatrix} 1 & x_1 \\ 1 & x_2 \\ \vdots & \vdots \\ 1 & x_m \end{bmatrix}, \qquad \boldsymbol{\beta} = \begin{bmatrix} \beta_0 \\ \beta_1 \end{bmatrix}, \qquad \mathbf{y} = \begin{bmatrix} y_1 \\ y_2 \\ \vdots \\ y_m \end{bmatrix}.

XX is the design matrix. The least-squares solution gives the least-squares line (the regression line), which minimizes the sum of the squared vertical distances from the points to the line. The normal equations are

XTX=[m∑xi∑xi∑xi2],XTy=[∑yi∑xiyi].X^TX = \begin{bmatrix} m & \sum x_i \\ \sum x_i & \sum x_i^2 \end{bmatrix}, \qquad X^T\mathbf{y} = \begin{bmatrix} \sum y_i \\ \sum x_iy_i \end{bmatrix}.

Worked example: The least-squares line

Find the least-squares line for the points (0,1)(0, 1), (1,1)(1, 1), (2,2)(2, 2), (3,4)(3, 4).

Here m=4m = 4, ∑xi=6\sum x_i = 6, ∑xi2=0+1+4+9=14\sum x_i^2 = 0 + 1 + 4 + 9 = 14, ∑yi=8\sum y_i = 8 and ∑xiyi=0+1+4+12=17\sum x_iy_i = 0 + 1 + 4 + 12 = 17. The normal equations are

4β0+6β1=86β0+14β1=17.\begin{aligned} 4\beta_0 + 6\beta_1 &= 8 \\ 6\beta_0 + 14\beta_1 &= 17. \end{aligned}

The determinant is 56−36=2056 - 36 = 20, so by Cramer's rule

β0=8(14)−6(17)20=1020=12,β1=4(17)−6(8)20=2020=1.\beta_0 = \frac{8(14) - 6(17)}{20} = \frac{10}{20} = \frac{1}{2}, \qquad \beta_1 = \frac{4(17) - 6(8)}{20} = \frac{20}{20} = 1.

The least-squares line is y=12+xy = \tfrac{1}{2} + x. Its predictions 0.5,1.5,2.5,3.50.5, 1.5, 2.5, 3.5 miss the data by 0.5,−0.5,−0.5,0.50.5, -0.5, -0.5, 0.5, and the sum of squared residuals is 4(0.25)=14(0.25) = 1. No other line does better.

The least-squares line y = 1/2 + x for the points (0, 1), (1, 1), (2, 2), (3, 4). Each point is off by 1/2 vertically.Open in grapher →

The same method fits any model that is linear in its coefficients, such as a parabola y=β0+β1x+β2x2y = \beta_0 + \beta_1x + \beta_2x^2: the design matrix just gets a column of xi2x_i^2.

Shortcuts: orthogonal columns and QR

If the columns a1,…,an\mathbf{a}_1, \ldots, \mathbf{a}_n of AA are orthogonal, ATAA^TA is diagonal, and each entry of x^\hat{\mathbf{x}} is a weight from the projection formula:

x^j=b⋅ajaj⋅aj.\hat{x}_j = \frac{\mathbf{b} \cdot \mathbf{a}_j}{\mathbf{a}_j \cdot \mathbf{a}_j}.

More generally, if A=QRA = QR, then ATA=RTRA^TA = R^TR and ATb=RTQTbA^T\mathbf{b} = R^TQ^T\mathbf{b}, and the normal equations reduce to the triangular system

Rx^=QTb,R\hat{\mathbf{x}} = Q^T\mathbf{b},

solved by back substitution. This is how software computes least-squares solutions: forming ATAA^TA explicitly can magnify rounding errors.

Worked example: Orthogonal columns

Find the least-squares solution of Ax=bA\mathbf{x} = \mathbf{b} for A=[1−11110]A = \begin{bmatrix} 1 & -1 \\ 1 & 1 \\ 1 & 0 \end{bmatrix} and b=(2,6,1)\mathbf{b} = (2, 6, 1).

The columns a1=(1,1,1)\mathbf{a}_1 = (1, 1, 1) and a2=(−1,1,0)\mathbf{a}_2 = (-1, 1, 0) are orthogonal: −1+1+0=0-1 + 1 + 0 = 0. So

x^1=2+6+13=3,x^2=−2+6+02=2,x^=(3,2).\hat{x}_1 = \frac{2 + 6 + 1}{3} = 3, \qquad \hat{x}_2 = \frac{-2 + 6 + 0}{2} = 2, \qquad \hat{\mathbf{x}} = (3, 2).

Check: b−Ax^=(2,6,1)−(1,5,3)=(1,1,−2)\mathbf{b} - A\hat{\mathbf{x}} = (2, 6, 1) - (1, 5, 3) = (1, 1, -2), which is orthogonal to both columns.

Tip

After solving, compute the residual b−Ax^\mathbf{b} - A\hat{\mathbf{x}} and dot it with each column of AA. Every result must be 00. For a fitted line with an intercept, this says the residuals add up to 00.

Practice

Practice 1

Find the least-squares solution of Ax=bA\mathbf{x} = \mathbf{b} for A=[100111]A = \begin{bmatrix} 1 & 0 \\ 0 & 1 \\ 1 & 1 \end{bmatrix} and b=(1,3,1)\mathbf{b} = (1, 3, 1).

Enter a point like (2, -3)

Practice 2

For the system in the previous problem, find the least-squares error ∥b−Ax^∥\|\mathbf{b} - A\hat{\mathbf{x}}\| as an exact value.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 3

Find the least-squares solution of Ax=bA\mathbf{x} = \mathbf{b} for A=[111213]A = \begin{bmatrix} 1 & 1 \\ 1 & 2 \\ 1 & 3 \end{bmatrix} and b=(1,2,2)\mathbf{b} = (1, 2, 2).

Enter a point like (2, -3)

Practice 4

Find the slope of the least-squares line y=β0+β1xy = \beta_0 + \beta_1x for the points (1,2)(1, 2), (2,3)(2, 3), (3,5)(3, 5).

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 5

Find the least-squares line for the points (−1,0)(-1, 0), (0,1)(0, 1), (1,3)(1, 3), and use it to predict yy when x=2x = 2.

Enter a number. Fractions like 3/4 and sqrt(2) are OK.

Practice 6

The columns of A=[111−1111−1]A = \begin{bmatrix} 1 & 1 \\ 1 & -1 \\ 1 & 1 \\ 1 & -1 \end{bmatrix} are orthogonal. Find the least-squares solution of Ax=bA\mathbf{x} = \mathbf{b} for b=(4,0,2,2)\mathbf{b} = (4, 0, 2, 2).

Enter a point like (2, -3)

Practice 7

Let x^\hat{\mathbf{x}} be a least-squares solution of Ax=bA\mathbf{x} = \mathbf{b}. Which statement is always true?