Unit 7, lesson 37

Projection and Least Squares Workshop

Mixed practice in choosing between an exact solve, a projection and a least-squares fit, with projection onto a plane, a line fit, proportional design columns, a sensitive measurement and residual checks.

  • Video 37
  • Projection and Least Squares Workshop

Which point of a plane lies closest to a vector that sticks out of it? Which line comes closest to three measurements that do not fall on one line? How far can a solved coordinate be trusted when a sensor reads it at a millionth of the strength of the other one? All three are linear algebra problems, yet each asks something different. This workshop practices choosing between an exact solve, a projection and a least-squares fit, carrying out that choice, and then checking what the result shows, including whether the coefficients are unique and whether the data determine them reliably.

Key terms

Error criterion

Definition The error criterion of a problem is the quantity its answer must make zero or as small as possible, and it is fixed before any calculation. Three questions are common. An exact solve asks for x\mathbf{x} with Ax=bA\mathbf{x} = \mathbf{b}, and a solution exists exactly when b\mathbf{b} lies in Col⁡A\operatorname{Col} A. A projection asks for the point p\mathbf{p} of a given subspace WW of Rm\mathbb{R}^m that minimizes the Euclidean distance ∥b−p∥\lVert \mathbf{b} - \mathbf{p} \rVert. A least-squares fit first chooses a model, given by a design matrix XX whose columns span the possible predictions, and then minimizes the sum of squared residuals ∥y−Xc∥2\lVert \mathbf{y} - X\mathbf{c} \rVert^2 over coefficient vectors c\mathbf{c}. At a minimizer c^\hat{\mathbf{c}} the fitted vector Xc^X\hat{\mathbf{c}} is the projection of y\mathbf{y} onto Col⁡X\operatorname{Col} X. A weighted criterion with positive weights w1,…,wmw_1, \dots, w_m minimizes

∑i=1mwi(yi−(Xc)i)2,\sum_{i=1}^{m} w_i \bigl(y_i - (X\mathbf{c})_i\bigr)^2,

which is ordinary least squares after row ii of XX and the entry yiy_i are multiplied by wi\sqrt{w_i}.

In plain words Decide what “best” means before computing anything. Different criteria ask different questions and can give different answers, and weights must come from a stated choice or a measurement model. Linear algebra solves the criterion you pick, but it cannot pick it for you.

Example Tuples such as (1,3)(1, 3) stand for column vectors on this page, except for data points written (ti,yi)(t_i, y_i). Fit a constant cc to two readings 11 and 33, so the data are y=(1,3)\mathbf{y} = (1, 3) and the model column is (1,1)(1, 1). Unweighted least squares projects y\mathbf{y} onto the line through (1,1)(1, 1) and gives the mean c=2c = 2. Now give the readings weights 11 and 33. Scaling each equation by the square root of its weight gives the column (1,3)(1, \sqrt{3}) and the data (1,33)(1, 3\sqrt{3}). The scaled residual (1−c,3(3−c))(1 - c, \sqrt{3}(3 - c)) must be perpendicular to (1,3)(1, \sqrt{3}), so

(1−c)+3(3−c)=10−4c=0,(1 - c) + 3(3 - c) = 10 - 4c = 0,

and c=5/2c = 5/2. The two answers differ because the criteria differ, not because either calculation is wrong.

Projection onto a plane through its normal

Definition Let n\mathbf{n} be a nonzero vector in R3\mathbb{R}^3, and let W={x:n⋅x=0}W = \{\mathbf{x} : \mathbf{n} \cdot \mathbf{x} = 0\} be the plane through the origin with normal n\mathbf{n}. For b\mathbf{b} in R3\mathbb{R}^3, the part of b\mathbf{b} along n\mathbf{n} is the residual, and the projection p\mathbf{p} onto WW is what remains,

r=b⋅nn⋅n n,p=b−r.\begin{aligned} \mathbf{r} &= \frac{\mathbf{b} \cdot \mathbf{n}}{\mathbf{n} \cdot \mathbf{n}}\,\mathbf{n}, \\[0.4em] \mathbf{p} &= \mathbf{b} - \mathbf{r}. \end{aligned}

The point p\mathbf{p} lies in WW, the residual r\mathbf{r} is perpendicular to WW, and for every q\mathbf{q} in WW

∥b−q∥2=∥r∥2+∥p−q∥2,\lVert \mathbf{b} - \mathbf{q} \rVert^2 = \lVert \mathbf{r} \rVert^2 + \lVert \mathbf{p} - \mathbf{q} \rVert^2,

so p\mathbf{p} is the unique closest point of WW to b\mathbf{b}, at distance ∥r∥=∣b⋅n∣/∥n∥\lVert \mathbf{r} \rVert = \lvert \mathbf{b} \cdot \mathbf{n} \rvert / \lVert \mathbf{n} \rVert. The same formulas hold for a hyperplane n⋅x=0\mathbf{n} \cdot \mathbf{x} = 0 in Rm\mathbb{R}^m. A plane n⋅x=d\mathbf{n} \cdot \mathbf{x} = d with d≠0d \ne 0 misses the origin and is not a subspace, and for it b⋅n\mathbf{b} \cdot \mathbf{n} is replaced by b⋅n−d\mathbf{b} \cdot \mathbf{n} - d.

In plain words The normal points straight out of the plane. Remove the part of b\mathbf{b} that points along the normal, and what is left lies in the plane and is as close to b\mathbf{b} as possible. One projection onto a line replaces a projection onto a plane.

Example Project b=(1,0,0)\mathbf{b} = (1, 0, 0) onto the plane x+y+z=0x + y + z = 0. Its equation reads n⋅x=0\mathbf{n} \cdot \mathbf{x} = 0 with n=(1,1,1)\mathbf{n} = (1, 1, 1). Since b⋅n=1\mathbf{b} \cdot \mathbf{n} = 1 and n⋅n=3\mathbf{n} \cdot \mathbf{n} = 3,

r=13[111],p=[2/3−1/3−1/3].\mathbf{r} = \frac{1}{3}\begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix}, \qquad \mathbf{p} = \begin{bmatrix} 2/3 \\ -1/3 \\ -1/3 \end{bmatrix}.

The coordinates of p\mathbf{p} add to 00, so p\mathbf{p} is in the plane, and r\mathbf{r} is a multiple of n\mathbf{n}, so it is perpendicular to the plane. The squared distance is ∥r∥2=3⋅19=13\lVert \mathbf{r} \rVert^2 = 3 \cdot \tfrac{1}{9} = \tfrac{1}{3}.

Line fit to observations

Definition Given data points (t1,y1),…,(tm,ym)(t_1, y_1), \dots, (t_m, y_m), a least-squares line y=a+bty = a + bt is one that minimizes the sum of squared vertical residuals

∑i=1m(yi−a−bti)2=∥y−Xc∥2,\sum_{i=1}^{m} (y_i - a - bt_i)^2 = \lVert \mathbf{y} - X\mathbf{c} \rVert^2,

where y=(y1,…,ym)\mathbf{y} = (y_1, \dots, y_m), c=(a,b)\mathbf{c} = (a, b), and the design matrix XX is the m×2m \times 2 matrix whose row ii is [1ti]\begin{bmatrix} 1 & t_i \end{bmatrix}. The minimizers are exactly the solutions of the normal equations

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

When at least two of the inputs tit_i differ, the columns of XX are independent, X⊤XX^{\top}X is invertible, and the line is unique.

In plain words Each data point gives one equation a+bti=yia + bt_i = y_i. With more points than unknowns the equations usually conflict, and the fit makes the vector of misses as short as possible.

Example Fit a line to the points (−1,1)(-1, 1), (0,0)(0, 0) and (1,2)(1, 2). Here

X=[1−11011],y=[102].X = \begin{bmatrix} 1 & -1 \\ 1 & 0 \\ 1 & 1 \end{bmatrix}, \qquad \mathbf{y} = \begin{bmatrix} 1 \\ 0 \\ 2 \end{bmatrix}.

The columns (1,1,1)(1, 1, 1) and (−1,0,1)(-1, 0, 1) are orthogonal, with squared lengths 33 and 22, and their dot products with y\mathbf{y} are 33 and 11, so

X⊤X=[3002],X⊤y=[31].X^{\top}X = \begin{bmatrix} 3 & 0 \\ 0 & 2 \end{bmatrix}, \qquad X^{\top}\mathbf{y} = \begin{bmatrix} 3 \\ 1 \end{bmatrix}.

The normal equations 3a=33a = 3 and 2b=12b = 1 give the line y=1+12ty = 1 + \tfrac{1}{2}t. Its predictions are (12,1,32)(\tfrac{1}{2}, 1, \tfrac{3}{2}), the residuals are (12,−1,12)(\tfrac{1}{2}, -1, \tfrac{1}{2}), and the sum of squared residuals is 14+1+14=32\tfrac{1}{4} + 1 + \tfrac{1}{4} = \tfrac{3}{2}.

Proportional design columns

Definition Suppose one column of a design matrix XX is a multiple of another, x2=kx1\mathbf{x}_2 = k\mathbf{x}_1 for some scalar kk. Then XX does not have full column rank, X⊤XX^{\top}X is singular, and the normal equations have infinitely many solutions. The fitted vector is still unique, because it is the projection of y\mathbf{y} onto Col⁡X\operatorname{Col} X, but the coefficients are not. Two least-squares coefficient vectors differ by a vector of Null⁡X\operatorname{Null} X, such as (k,−1)(k, -1) when XX has these two columns only. In general the coefficients are unique exactly when XX has full column rank.

In plain words Two names for one direction do not give two degrees of freedom. The model still finds the best prediction, but it cannot say how to split it between the two coefficients.

Example Replace the line model by the columns (1,1,1)(1, 1, 1) and (2,2,2)(2, 2, 2), with coefficients α\alpha and β\beta, and keep y=(1,0,2)\mathbf{y} = (1, 0, 2). Every prediction has all three entries equal to α+2β\alpha + 2\beta, so the model can only fit a constant. The projection of y\mathbf{y} onto the line through (1,1,1)(1, 1, 1) is the mean 11 in every entry, so the fitted vector is (1,1,1)(1, 1, 1), and every pair with α+2β=1\alpha + 2\beta = 1 is a least-squares solution, for example (1,0)(1, 0) and (−1,1)(-1, 1). The normal equations

[36612][αβ]=[36]\begin{bmatrix} 3 & 6 \\ 6 & 12 \end{bmatrix} \begin{bmatrix} \alpha \\ \beta \end{bmatrix} = \begin{bmatrix} 3 \\ 6 \end{bmatrix}

have a second row twice the first. The residual (0,−1,1)(0, -1, 1) has squared length 22. The model lost the slope direction, and its constant predictions are also lines, so its error cannot be smaller than the 32\tfrac{3}{2} of the line. It is larger here because the best line has slope 12\tfrac{1}{2}, not 00.

Sensitivity to a changed measurement

Definition Let AA be an invertible n×nn \times n matrix, viewed as a measurement map, with data b\mathbf{b} and solution x\mathbf{x} of Ax=bA\mathbf{x} = \mathbf{b}. A change δb\delta\mathbf{b} in the measurements changes the solution by δx=A−1δb\delta\mathbf{x} = A^{-1}\delta\mathbf{b}, so

∥δx∥≤∥A−1∥2 ∥δb∥,\lVert \delta\mathbf{x} \rVert \le \lVert A^{-1} \rVert_2 \, \lVert \delta\mathbf{b} \rVert,

and when b≠0\mathbf{b} \ne \mathbf{0} the relative change in x\mathbf{x} is at most the condition number κ2(A)=∥A∥2∥A−1∥2\kappa_2(A) = \lVert A \rVert_2 \lVert A^{-1} \rVert_2 times the relative change in b\mathbf{b}. The ratio ∥δx∥/∥δb∥\lVert \delta\mathbf{x} \rVert / \lVert \delta\mathbf{b} \rVert reaches its largest value ∥A−1∥2\lVert A^{-1} \rVert_2 when δb\delta\mathbf{b} is a nonzero multiple of AvA\mathbf{v}, where v\mathbf{v} is a unit input direction that AA stretches least. If the data are uncertain by about ∥Av∥\lVert A\mathbf{v} \rVert along the direction of AvA\mathbf{v}, the solution is uncertain by about one unit along v\mathbf{v}, however many digits are printed.

In plain words Change one reading by a realistic amount, solve again and compare. A large move in the answer means the data constrain that part of the solution only weakly.

Example Let AA be diagonal with entries 11 and 10−610^{-6}, and let b=(1,10−6)\mathbf{b} = (1, 10^{-6}), so x=(1,1)\mathbf{x} = (1, 1). Change the second measurement to 2×10−62 \times 10^{-6}. The new solution is (1,2)(1, 2). The measurements moved by 10−610^{-6} and the solution by 11, a factor of 106=∥A−1∥210^6 = \lVert A^{-1} \rVert_2. In relative terms the data changed by about 10−610^{-6} and the solution by 1/2≈0.7071/\sqrt{2} \approx 0.707, an amplification of about 7.1×1057.1 \times 10^5, below κ2(A)=106\kappa_2(A) = 10^6. Both solutions are exact, so the change comes from the data and not from the solver. The weakly measured direction is v=e2\mathbf{v} = \mathbf{e}_2 with ∥Ae2∥=10−6\lVert A\mathbf{e}_2 \rVert = 10^{-6}, so if the second reading is uncertain by about 10−610^{-6}, the second coordinate of the solution is not reliably known.

Checking the model and residual

Definition A projection or a fit is checked in the original problem. The answer must lie in the model space, so a projection p\mathbf{p} must lie in WW and a fitted vector must have the form XcX\mathbf{c}. The residual must be orthogonal to the model space in observation space, which for a fit means X⊤r=0X^{\top}\mathbf{r} = \mathbf{0}, one dot product for each design column. Membership and orthogonality are needed together. For the plane WW with normal n\mathbf{n}, every point of WW passes the first test, and every point b−tn\mathbf{b} - t\mathbf{n} on the normal line through b\mathbf{b} passes the second, but only p\mathbf{p} passes both. The units must agree, with residuals in the units of the observations and each coefficient in the units that make its term match them. Finally, the rank of XX decides whether the coefficients are unique, and the sensitivity of the problem decides whether the data constrain them reliably.

In plain words Put the answer back into the question that was asked. A residual that is small but not perpendicular, or perpendicular in the wrong space, points to a mistake.

Example For the line y=1+12ty = 1 + \tfrac{1}{2}t above, the residual r=(12,−1,12)\mathbf{r} = (\tfrac{1}{2}, -1, \tfrac{1}{2}) satisfies

(1,1,1)⋅r=12−1+12=0,(−1,0,1)⋅r=−12+0+12=0,\begin{aligned} (1, 1, 1) \cdot \mathbf{r} &= \tfrac{1}{2} - 1 + \tfrac{1}{2} = 0, \\[0.4em] (-1, 0, 1) \cdot \mathbf{r} &= -\tfrac{1}{2} + 0 + \tfrac{1}{2} = 0, \end{aligned}

so X⊤r=0X^{\top}\mathbf{r} = \mathbf{0}. This perpendicularity holds in the three-dimensional observation space, while in the scatter plot the residuals are vertical segments that need not meet the line at right angles. If tt is in seconds and yy in meters, then aa is in meters, bb in meters per second, each residual in meters and the sum of squared residuals is 32\tfrac{3}{2} square meters. The two columns are nonzero and orthogonal, hence independent, so the coefficients are unique.

Check yourself

  1. Find the point of the plane x+2y+2z=0x + 2y + 2z = 0 closest to (3,3,0)(3, 3, 0), and the distance between them.
Show the answer

This is a projection onto a plane through the origin with normal n=(1,2,2)\mathbf{n} = (1, 2, 2). With b=(3,3,0)\mathbf{b} = (3, 3, 0), we get b⋅n=9\mathbf{b} \cdot \mathbf{n} = 9 and n⋅n=9\mathbf{n} \cdot \mathbf{n} = 9, so the residual is r=n=(1,2,2)\mathbf{r} = \mathbf{n} = (1, 2, 2) and the closest point is

p=b−r=[21−2].\mathbf{p} = \mathbf{b} - \mathbf{r} = \begin{bmatrix} 2 \\ 1 \\ -2 \end{bmatrix}.

Check that 2+2⋅1+2⋅(−2)=02 + 2 \cdot 1 + 2 \cdot (-2) = 0, so p\mathbf{p} is in the plane, and that r\mathbf{r} is a multiple of the normal. The distance is ∥r∥=1+4+4=3\lVert \mathbf{r} \rVert = \sqrt{1 + 4 + 4} = 3.

  1. Three readings are all taken at the same time t=2t = 2, with values 11, 44 and 77. Someone fits y=a+bty = a + bt by least squares. Are aa and bb determined? Find the fitted values and every least-squares pair (a,b)(a, b).
Show the answer

The design columns are (1,1,1)(1, 1, 1) and (2,2,2)(2, 2, 2), which are proportional, so XX has rank 11 and the coefficients are not unique. Every prediction equals a+2ba + 2b, so the model fits a constant, and the best constant is the mean (1+4+7)/3=4(1 + 4 + 7)/3 = 4. The fitted vector is (4,4,4)(4, 4, 4), and the least-squares pairs are exactly those with a+2b=4a + 2b = 4, for example (4,0)(4, 0) and (2,1)(2, 1). Any two of them differ by a multiple of (2,−1)(2, -1), a vector of Null⁡X\operatorname{Null} X. The residual (−3,0,3)(-3, 0, 3) has squared length 1818. Readings at a single input cannot determine a slope, which is why the line model needs at least two distinct inputs.

  1. Fit y=a+bty = a + bt to the points (0,1)(0, 1), (1,2)(1, 2), (2,2)(2, 2) and (3,4)(3, 4), and check the residual. Then raise the last reading from 44 to 55. How do the coefficients change?
Show the answer

The design matrix has rows [1ti]\begin{bmatrix} 1 & t_i \end{bmatrix} for ti=0,1,2,3t_i = 0, 1, 2, 3, and y=(1,2,2,4)\mathbf{y} = (1, 2, 2, 4). The normal equations are

[46614][ab]=[918].\begin{bmatrix} 4 & 6 \\ 6 & 14 \end{bmatrix} \begin{bmatrix} a \\ b \end{bmatrix} = \begin{bmatrix} 9 \\ 18 \end{bmatrix}.

The determinant is 56−36=2056 - 36 = 20, and solving gives a=910a = \tfrac{9}{10} and b=910b = \tfrac{9}{10}. The predictions are (910,95,2710,185)(\tfrac{9}{10}, \tfrac{9}{5}, \tfrac{27}{10}, \tfrac{18}{5}) and the residual is r=(110,15,−710,25)\mathbf{r} = (\tfrac{1}{10}, \tfrac{1}{5}, -\tfrac{7}{10}, \tfrac{2}{5}). Its entries add to 00, and the input-weighted sum is 0+15−75+65=00 + \tfrac{1}{5} - \tfrac{7}{5} + \tfrac{6}{5} = 0, so X⊤r=0X^{\top}\mathbf{r} = \mathbf{0}. The sum of squared residuals is 710\tfrac{7}{10}.

With the last reading 55, the right-hand side becomes (10,21)(10, 21) and the solution is a=710a = \tfrac{7}{10}, b=65b = \tfrac{6}{5}. The change of 11 in one reading moved the intercept by −15-\tfrac{1}{5} and the slope by 310\tfrac{3}{10}. The coefficients depend linearly on the data, so the same change also comes from solving the normal equations with right-hand side X⊤(0,0,0,1)=(1,3)X^{\top}(0, 0, 0, 1) = (1, 3), which gives (−15,310)(-\tfrac{1}{5}, \tfrac{3}{10}).

How this lesson connects