Unit 7, lesson 33

Least Squares and Orthogonal Projection

What a least-squares solution is, how vertical residuals form one residual vector in observation space whose squared length is the sum of squared errors, and how projection gives the normal equations and a unique fitted vector.

  • Video 33
  • Least-Squares Fitting

Three measurements give the points (0,1)(0, 1), (1,2)(1, 2) and (2,2)(2, 2). The line through the first two points predicts 33 at input 22, and the horizontal line through the last two misses the first point, so no straight line passes through all three. The linear system for an intercept and a slope that match all three outputs has no solution, yet some lines clearly fit better than others. This lesson fixes what “best” means, the smallest sum of squared errors, and shows that orthogonal projection both finds the best fit and proves that nothing does better.

Key terms

Vertical residuals

Definition Suppose the data points (t1,y1),…,(tm,ym)(t_1, y_1), \dots, (t_m, y_m) are compared with a line y=a+bty = a + bt with intercept aa and slope bb. The residual of observation ii is

ri=yi−(a+bti),r_i = y_i - (a + bt_i),

the measured output minus the predicted output at the same input tit_i. In the plot it is the signed vertical distance from the line to the point, positive when the point lies above the line and negative when it lies below. The inputs tit_i are treated as exact and are never moved.

In plain words A residual says how far the line misses one measurement, measured straight up or down at that measurement’s input. Its size is never smaller than the shortest distance from the point to the line, and it is larger whenever the line is sloped and misses the point.

Example For the three points above, the line y=1+ty = 1 + t predicts 11, 22 and 33, so its residuals are 00, 00 and −1-1. The horizontal line y=2y = 2 has residuals −1-1, 00 and 00. At the point (2,2)(2, 2) the vertical residual of y=1+ty = 1 + t has size 11, while the perpendicular distance from that point to the line is only 1/21/\sqrt{2}. Least squares works with the vertical residual.

Sum of squared errors

Definition For a line with coefficients aa and bb, the sum of squared errors is

S(a,b)=∑i=1m(yi−a−bti)2.S(a, b) = \sum_{i=1}^{m} \bigl(y_i - a - bt_i\bigr)^2 .

Least squares uses this number as its error criterion, with every observation weighted equally. Squaring keeps positive and negative residuals from canceling, and it counts large misses more heavily than small ones. If the outputs carry a unit, the sum carries the square of that unit.

In plain words Square each miss and add. A smaller total means a closer fit in this particular sense. It is a chosen criterion, and a problem whose measurements deserve different trust may call for weights instead.

Example For the points (0,1)(0, 1), (1,2)(1, 2) and (2,2)(2, 2),

S(a,b)=(1−a)2+(2−a−b)2+(2−a−2b)2.\begin{aligned} S(a, b) &= (1 - a)^2 + (2 - a - b)^2 \\[0.4em] &\quad + (2 - a - 2b)^2 . \end{aligned}

The lines y=1+ty = 1 + t and y=2y = 2 both score S=1S = 1. Equal scores do not make either line best, and the next cards find a line that does better.

Residual vector in observation space

Definition Collect the outputs in the observation vector y=(y1,…,ym)\mathbf{y} = (y_1, \dots, y_m) in Rm\mathbb{R}^m and the coefficients in c=(a,b)\mathbf{c} = (a, b). On this page a tuple written for a vector stands for a column, while pairs such as (0,1)(0, 1) are points in the plot. The design matrix XX is the m×2m \times 2 matrix whose iith row is [1ti]\begin{bmatrix} 1 & t_i \end{bmatrix}, so XcX\mathbf{c} is the vector of predictions a+btia + bt_i. The residual vector is

r=y−Xc.\mathbf{r} = \mathbf{y} - X\mathbf{c}.

Its iith coordinate is the vertical residual of observation ii, and S(a,b)=∥y−Xc∥2S(a, b) = \lVert \mathbf{y} - X\mathbf{c} \rVert^2. The space Rm\mathbb{R}^m is called observation space because it has one coordinate axis for each observation. Every possible prediction vector lies in Col⁡X\operatorname{Col} X, and the system Xc=yX\mathbf{c} = \mathbf{y} is consistent exactly when y\mathbf{y} lies in Col⁡X\operatorname{Col} X.

In plain words The scatter plot has one point per observation. Observation space turns the whole data set into a single vector, and every choice of coefficients into a single prediction vector in a flat subspace. The residual vector is the arrow from the prediction to the data, and its squared length is the sum of squared errors.

Example For the three points,

X=[101112],y=[122],Xc=[aa+ba+2b].\begin{gathered} X = \begin{bmatrix} 1 & 0 \\ 1 & 1 \\ 1 & 2 \end{bmatrix}, \qquad \mathbf{y} = \begin{bmatrix} 1 \\ 2 \\ 2 \end{bmatrix}, \\[0.4em] X\mathbf{c} = \begin{bmatrix} a \\ a + b \\ a + 2b \end{bmatrix}. \end{gathered}

The column space of XX is a plane through the origin in R3\mathbb{R}^3. The vector y\mathbf{y} is not in it, since the first two coordinates force a=1a = 1 and b=1b = 1, and then the third prediction is 33 instead of 22.

Least-squares solution

Definition Let AA be a real m×nm \times n matrix and b\mathbf{b} a vector in Rm\mathbb{R}^m. A vector x^\hat{\mathbf{x}} in Rn\mathbb{R}^n is a least-squares solution of Ax=bA\mathbf{x} = \mathbf{b} if

∥b−Ax^∥2≤∥b−Ax∥2\lVert \mathbf{b} - A\hat{\mathbf{x}} \rVert^2 \le \lVert \mathbf{b} - A\mathbf{x} \rVert^2

for every x\mathbf{x} in Rn\mathbb{R}^n. Let p\mathbf{p} be the orthogonal projection of b\mathbf{b} onto Col⁡A\operatorname{Col} A. For every x\mathbf{x} the vector b−p\mathbf{b} - \mathbf{p} is orthogonal to Col⁡A\operatorname{Col} A and the vector p−Ax\mathbf{p} - A\mathbf{x} lies in it, so the Pythagorean theorem gives

∥b−Ax∥2=∥b−p∥2+∥p−Ax∥2.\begin{aligned} \lVert \mathbf{b} - A\mathbf{x} \rVert^2 &= \lVert \mathbf{b} - \mathbf{p} \rVert^2 \\[0.4em] &\quad + \lVert \mathbf{p} - A\mathbf{x} \rVert^2 . \end{aligned}

Hence x^\hat{\mathbf{x}} is a least-squares solution exactly when Ax^=pA\hat{\mathbf{x}} = \mathbf{p}. Least-squares solutions always exist, the smallest value of ∥b−Ax∥2\lVert \mathbf{b} - A\mathbf{x} \rVert^2 is ∥b−p∥2\lVert \mathbf{b} - \mathbf{p} \rVert^2, and when b\mathbf{b} lies in Col⁡A\operatorname{Col} A the least-squares solutions are exactly the ordinary solutions. In a fitting problem the matrix is the design matrix XX, the unknown is the coefficient vector c\mathbf{c} and the right side is the observation vector y\mathbf{y}.

In plain words When no coefficients reproduce the data, take the prediction vector closest to the data. The closest point of a subspace is the foot of the perpendicular, and any other prediction adds a second, perpendicular piece of error. This proves a global minimum without calculus and without trying values one by one. The right angle lives in observation space. In the scatter plot the fitted line need not meet its vertical residual segments at right angles.

Example For the three points, the coefficients c^=(7/6,1/2)\hat{\mathbf{c}} = (7/6, 1/2) give the predictions and residual

Xc^=[7/65/313/6],r=[−1/61/3−1/6].X\hat{\mathbf{c}} = \begin{bmatrix} 7/6 \\ 5/3 \\ 13/6 \end{bmatrix}, \qquad \mathbf{r} = \begin{bmatrix} -1/6 \\ 1/3 \\ -1/6 \end{bmatrix}.

The residual is orthogonal to both columns of XX, since −16+13−16=0-\tfrac16 + \tfrac13 - \tfrac16 = 0 and 0⋅(−16)+1⋅13+2⋅(−16)=00 \cdot (-\tfrac16) + 1 \cdot \tfrac13 + 2 \cdot (-\tfrac16) = 0. So the predictions are the projection p\mathbf{p}, and c^\hat{\mathbf{c}} is a least-squares solution. Its score is 136+19+136=16\tfrac{1}{36} + \tfrac19 + \tfrac{1}{36} = \tfrac16, less than the score 11 of either earlier line. The best line is y=76+12ty = \tfrac76 + \tfrac12 t.

Normal equations

Definition For a real m×nm \times n matrix XX and a vector y\mathbf{y} in Rm\mathbb{R}^m, the normal equations are

X⊤Xc=X⊤y.X^{\top}X\mathbf{c} = X^{\top}\mathbf{y}.

A vector c\mathbf{c} in Rn\mathbb{R}^n is a least-squares solution of Xc=yX\mathbf{c} = \mathbf{y} if and only if it satisfies the normal equations. The reason is that the entries of X⊤(y−Xc)X^{\top}(\mathbf{y} - X\mathbf{c}) are the dot products of the residual with the columns of XX. The equations say the residual is orthogonal to every column, hence to all of Col⁡X\operatorname{Col} X, and by the projection property that happens exactly when Xc=pX\mathbf{c} = \mathbf{p}. The normal equations always have at least one solution, even when Xc=yX\mathbf{c} = \mathbf{y} has none. The word normal means perpendicular.

In plain words The best residual has zero dot product with every column of the model. If it had a nonzero dot product with some vector w\mathbf{w} in Col⁡X\operatorname{Col} X, moving the prediction a little in the direction of w\mathbf{w} or of −w-\mathbf{w} would shrink the error. Writing the dot products for an unknown c\mathbf{c} gives a square system with one equation per coefficient.

Example For the three points,

X⊤X=[3335],X⊤y=[56],X^{\top}X = \begin{bmatrix} 3 & 3 \\ 3 & 5 \end{bmatrix}, \qquad X^{\top}\mathbf{y} = \begin{bmatrix} 5 \\ 6 \end{bmatrix},

so the normal equations are 3a+3b=53a + 3b = 5 and 3a+5b=63a + 5b = 6. The coefficients a=7/6a = 7/6 and b=1/2b = 1/2 satisfy both, since 72+32=5\tfrac72 + \tfrac32 = 5 and 72+52=6\tfrac72 + \tfrac52 = 6. The first equation comes from the all-ones column and says that the residuals sum to zero. A model without a constant column need not have residuals that sum to zero.

Fitted vector

Definition For a real m×nm \times n design matrix XX and observation vector y\mathbf{y}, the fitted vector is the orthogonal projection p\mathbf{p} of y\mathbf{y} onto Col⁡X\operatorname{Col} X. It equals Xc^X\hat{\mathbf{c}} for every least-squares solution c^\hat{\mathbf{c}}, and it is unique, because by the identity above any prediction with the smallest error is at distance 00 from p\mathbf{p}. If c^\hat{\mathbf{c}} is one least-squares solution, all of them are the vectors c^+h\hat{\mathbf{c}} + \mathbf{h} with h\mathbf{h} in Null⁡X\operatorname{Null} X. So the coefficients are unique if and only if the columns of XX are linearly independent.

In plain words The best prediction is always one definite vector. Whether exactly one set of coefficients produces it depends on whether the model has redundant columns.

Example For the three points the columns of XX are independent, so c^=(7/6,1/2)\hat{\mathbf{c}} = (7/6, 1/2) is the only least-squares solution, and the fitted vector is (7/6,5/3,13/6)(7/6, 5/3, 13/6). Now fit a constant aa to the two observations 22 and 66. The design column is (1,1)(1, 1), and orthogonality says the residuals 2−a2 - a and 6−a6 - a add to zero, so a=4a = 4. The fitted vector is (4,4)(4, 4), the residual is (−2,2)(-2, 2) and the smallest squared error is 88. If the design matrix contains the all-ones column twice, the prediction depends only on the sum of the two coefficients, and every pair with sum 44 gives the same fitted vector (4,4)(4, 4).

Check yourself

  1. For the points (0,1)(0, 1), (1,1)(1, 1) and (2,4)(2, 4), find the residual vector and the sum of squared errors of the line y=1+ty = 1 + t.
Show the answer

The line predicts 11, 22 and 33. Subtracting the predictions from the observations gives the residual vector (0,−1,1)(0, -1, 1), and the sum of squared errors is 0+1+1=20 + 1 + 1 = 2.

  1. For the same points, someone claims the least-squares line is y=12+32ty = \tfrac12 + \tfrac32 t. Check the claim without solving any equations, and find its sum of squared errors.
Show the answer

The predictions are 12\tfrac12, 22 and 72\tfrac72, so the residual vector is (12,−1,12)(\tfrac12, -1, \tfrac12). Its dot product with the all-ones column is 12−1+12=0\tfrac12 - 1 + \tfrac12 = 0, and with the input column (0,1,2)(0, 1, 2) it is 0−1+1=00 - 1 + 1 = 0. The residual is orthogonal to Col⁡X\operatorname{Col} X, so the predictions are the projection of the data and the claim is correct. The sum of squared errors is 14+1+14=32\tfrac14 + 1 + \tfrac14 = \tfrac32, smaller than the score 22 from the first question, as it must be.

  1. Fit the model y=bty = bt, a line through the origin, to the points (1,1)(1, 1) and (2,3)(2, 3). Do the residuals sum to zero?
Show the answer

The design matrix is the single column (1,2)(1, 2) and the observation vector is (1,3)(1, 3). The normal equation is 5b=1+6=75b = 1 + 6 = 7, so b=75b = \tfrac75. The fitted vector is (75,145)(\tfrac75, \tfrac{14}{5}) and the residual is (−25,15)(-\tfrac25, \tfrac15). It is orthogonal to the design column, since −25+2⋅15=0-\tfrac25 + 2 \cdot \tfrac15 = 0, but its entries sum to −15-\tfrac15, not zero. Residuals are guaranteed to sum to zero only when the all-ones vector lies in Col⁡X\operatorname{Col} X, for example when the model has an intercept, and this model has none.

How this lesson connects

It also uses the equation of a straight line with an intercept and a slope.