






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







It often happens that Ax D b has no solution. The usual reason is: too many equations. The matrix has more rows than columns. There are more equations than unknowns (m is greater than n). The n columns span a small part of m-dimensional space. Unless all measurements are perfect, b is outside that column space. Elimination reaches an impossible equation and stops. But we can’t stop just because measurements include noise.
To repeat: We cannot always get the error e D b Ax down to zero. When e is zero, x is an exact solution to Ax D b. When the length of e is as small as possible, bx is a least squares solution. Our goal in this section is to compute bx and use it. These are real problems and they need an answer.
The previous section emphasized p (the projection). This section emphasizes bx (the least squares solution). They are connected by p D Abx. The fundamental equation is still ATAbx D ATb. Here is a short unofficial way to reach this equation:
When Ax D b has no solution, multiply by AT^ and solve ATAbx D ATb:
Example 1 A crucial application of least squares is fitting a straight line to m points. Start with three points: Find the closest line to the points .0; 6/; .1; 0/ , and .2; 0/.
No straight line b D C C Dt goes through those three points. We are asking for two numbers C and D that satisfy three equations. Here are the equations at t D 0; 1; 2 to match the given values b D 6; 0; 0:
t D 0 The first point is on the line b D C C Dt if C C D 0 D 6 t D 1 The second point is on the line b D C C Dt if C C D 1 D 0 t D 2 The third point is on the line b D C C Dt if C C D 2 D 0:
This 3 by 2 system has no solution : b D .6; 0; 0/ is not a combination of the columns .1; 1; 1/ and .0; 1; 2/. Read off A; x; and b from those equations:
(^5) x D
b D
(^5) Ax D b is not solvable.
The same numbers were in Example 3 in the last section. We computed bx D .5; 3/. Those numbers are the best C and D, so 5 3t will be the best line for the 3 points. We must connect projections to least squares, by explaining why ATAbx D ATb. In practical problems, there could easily be m D 100 points instead of m D 3. They don’t exactly match any straight line C C Dt. Our numbers 6; 0; 0 exaggerate the error so you can see e 1 ; e 2 , and e 3 in Figure 4.6.
How do we make the error e D b Ax as small as possible? This is an important question with a beautiful answer. The best x (called bx/ can be found by geometry or algebra or calculus: 90 ı^ angle or project using P or set the derivative of the error to zero.
By geometry Every Ax lies in the plane of the columns .1; 1; 1/ and .0; 1; 2/. In that plane, we look for the point closest to b. The nearest point is the projection p.
The best choice for Abx is p. The smallest possible error is e D b p. The three points at heights .p 1 ; p 2 ; p 3 / do lie on a line , because p is in the column space. In fitting a straight line, bx gives the best choice for .C; D/.
By algebra Every vector b splits into two parts. The part in the column space is p. The perpendicular part in the nullspace of AT^ is e. There is an equation we cannot solve .Ax D b/. There is an equation Abx D p we do solve (by removing e/:
Ax D b D p C e is impossible; Abx D p is solvable. (1)
The solution to Abx D p leaves the least possible error (which is e):
Squared length for any x kAx bk^2 D kAx pk^2 C kek^2 : (2)
This is the law c^2 D a^2 C b^2 for a right triangle. The vector Ax p in the column space is perpendicular to e in the left nullspace. We reduce Ax p to zero by choosing x to be bx. That leaves the smallest possible error e D .e 1 ; e 2 ; e 3 /. Notice what “smallest” means. The squared length of Ax b is minimized:
The least squares solution bx makes E D kAx bk^2 as small as possible.
Figure 4.6: Best line and projection: Two pictures, same problem. The line has heights p D .5; 2; 1/ with errors e D .1; 2; 1/. The equations ATAbx D ATb give bx D .5; 3/. The best line is b D 5 3t and the projection is p D 5 a 1 3 a 2.
Figure 4.6a shows the closest line. It misses by distances e 1 ; e 2 ; e 3 D 1; 2; 1. Those are vertical distances. The least squares line minimizes E D e^21 C e 22 C e 32.
Figure 4.7: The projection p D Abx is closest to b, so bx minimizes E D kb Axk^2.
In this section the situation is just the opposite. There are no solutions to Ax D b. Instead of splitting up x we are splitting up b. Figure 4.3 shows the big picture for least squares. Instead of Ax D b we solve Abx D p. The error e D b p is unavoidable. Notice how the nullspace N .A/ is very small—just one point. With independent columns, the only solution to Ax D 0 is x D 0. Then ATA is invertible. The equation ATAbx D ATb fully determines the best vector bx. The error has ATe D 0. Chapter 7 will have the complete picture—all four subspaces included. Every x splits into xr C xn, and every b splits into p C e. The best solution is bxr in the row space. We can’t help e and we don’t want xn—this leaves Abx D p.
Fitting a Straight Line
Fitting a line is the clearest application of least squares. It starts with m > 2 points, hopefully near a straight line. At times t 1 ; : : : ; tm those m points are at heights b 1 ; : : : ; bm. The best line C C Dt misses the points by vertical distances e 1 ; : : : ; em. No line is perfect, and the least squares line minimizes E D e^21 C C em^2. The first example in this section had three points in Figure 4.6. Now we allow m points (and m can be large). The two components of bx are still C and D. A line goes through the m points when we exactly solve Ax D b. Generally we can’t do it. Two unknowns C and D determine a line, so A has only n D 2 columns. To fit the m points, we are trying to solve m equations (and we only want two!):
Ax D b is
C C Dt 1 D b 1 C C Dt 2 D b 2 :: : C C Dtm D bm
with A D
1 t 1 1 t 2 :: :
1 tm
The column space is so thin that almost certainly b is outside of it. When b happens to lie in the column space, the points happen to lie on a line. In that case b D p. Then Ax D b is solvable and the errors are e D .0; : : : ; 0/.
The closest line C C Dt has heights p 1 ; : : : ; pm with errors e 1 ; : : : ; em. Solve A T Abx D A T b for bx D .C; D/. The errors are ei D bi C Dti. Fitting points by a straight line is so important that we give the two equations A TAbx D ATb, once and for all. The two columns of A are independent (unless all times ti are the same). So we turn to least squares and solve ATAbx D ATb.
Dot-product matrix ATA D
t 1 tm
1 t 1 :: :
1 tm
m
ti P ti
t (^) i^2
On the right side of the normal equation is the 2 by 1 vector ATb:
ATb D
t 1 tm
b 1 :: : bm
bi P ti bi
In a specific problem, these numbers are given. The best bx D .C; D/ is in equation (9).
The line C C Dt minimizes e 12 C C e^2 m D kAx bk^2 when ATAbx D ATb: " m
ti P ti
t (^) i^2
bi P ti bi
The vertical errors at the m points on the line are the components of e D b p. This error vector (the residual ) b Abx is perpendicular to the columns of A (geometry). The error is in the nullspace of AT^ (linear algebra). The best bx D .C; D/ minimizes the total error E, the sum of squares:
E.x/ D kAx bk^2 D .C C Dt 1 b 1 /^2 C C .C C Dtm bm/^2 :
When calculus sets the derivatives @E=@C and @E=@D to zero, it produces ATAbx D ATb. Other least squares problems have more than two unknowns. Fitting by the best parabola has n D 3 coefficients C; D; E (see below). In general we are fitting m data points by n parameters x 1 ; : : : ; xn. The matrix A has n columns and n < m. The derivatives of kAx bk^2 give the n equations ATAbx D ATb. The derivative of a square is linear. This is why the method of least squares is so popular.
Example 2 A has orthogonal columns when the measurement times ti add to zero.
May I ask you to convert this to a problem of projection? The column space of A has dimension. The projection of b is p D Abx, which combines the three columns using the coefficients C; D; E. The error at the first data point is e 1 D b 1 C Dt 1 Et 12. The total squared error is e^21 C. If you prefer to minimize by calculus, take the partial derivatives of E with respect to ; ;. These three derivatives will be zero when bx D .C; D; E/ solves the 3 by 3 system of equations. Section 8.5 has more least squares applications. The big one is Fourier series— approximating functions instead of vectors. The function to be minimized changes from a sum of squared errors e^21 C C em^2 to an integral of the squared error.
Example 3 For a parabola b D C CDt CEt 2 to go through the three heights b D 6; 0; 0 when t D 0; 1; 2, the equations are
C C D 0 C E 02 D 6 C C D 1 C E 12 D 0 C C D 2 C E 22 D^ 0:
This is Ax D b. We can solve it exactly. Three data points give three equations and a square matrix. The solution is x D .C; D; E/ D .6; 9; 3/. The parabola through the three points in Figure 4.8a is b D 6 9t C 3t 2. What does this mean for projection? The matrix has three columns, which span the whole space R^3. The projection matrix is the identity. The projection of b is b. The error is zero. We didn’t need ATAbx D ATb, because we solved Ax D b. Of course we could multiply by AT^ , but there is no reason to do it. Figure 4.8 also shows a fourth point b 4 at time t 4. If that falls on the parabola, the new Ax D b (four equations) is still solvable. When the fourth point is not on the parabola, we turn to ATAbx D ATb. Will the least squares parabola stay the same, with all the error at the fourth point? Not likely! The smallest error vector .e 1 ; e 2 ; e 3 ; e 4 / is perpendicular to .1; 1; 1; 1/, the first column of A. Least squares balances out the four errors, and they add to zero. ................. ................... .................. .................... .................... .................... ..................... ...................... ....................... ....................... ......................... ......................... ........................... ............................ ............................... ................................ ................................... ....................................... ............................................ ................................................... .................................................................. ...................................................................................................................... ..................................................................................................................................................................................................................................... .................................................................... ...................................
0 1 2
j t t 4
6
b 4 –^ ˝
b D 6 9t C 3t^2
2 (^66) 4
6 0 0 b 4
3 (^77) 5 is in^ R
4
2 6 6 4
0 1 4 t 42
3 7 7 5
2 (^66) 4
0 1 2 t 4
3 (^77) 2 5 6 6 4
1 1 1 1
3 7 7 5
Figure 4.8: From Example 3: An exact fit of the parabola at t D 0; 1; 2 means that p D b and e D 0. The point b 4 off the parabola makes m > n and we need least squares.
1. The least squares solution bx minimizes E D kAx bk^2. This is the sum of squares of the errors in the m equations .m > n/. 2. The best bx comes from the normal equations ATAbx D ATb. 3. To fit m points by a line b D C C Dt , the normal equations give C and D. 4. The heights of the best line are p D .p 1 ; : : : ; pm/. The vertical distances to the data points are the errors e D .e 1 ; : : : ; em/. 5. If we try to fit m points by a combination of n < m functions, the m equations Ax D b are generally unsolvable. The n equations ATAbx D ATb give the least squares solution—the combination with smallest MSE (mean square error).
4.3 A Start with nine measurements b 1 to b 9 , all zero , at times t D 1; : : : ; 9. The tenth measurement b 10 D 40 is an outlier. Find the best horizontal line y D C to fit the ten points .1; 0/; .2; 0/; : : : ; .9; 0/; .10; 40/ using three measures for the error E:
(1) Least squares E 2 D e^21 C C e^210 (then the normal equation for C is linear)
(2) Least maximum error E 1 D jemaxj (3) Least sum of errors E 1 D je 1 j C C je 10 j.
Solution (1) The least squares fit to 0; 0; : : : ; 0; 40 by a horizontal line is C D 4 :
A D column of 1 ’s ATA D 10 ATb D sum of bi D 40. So 10C D 40:
(2) The least maximum error requires C D 20 , halfway between 0 and 40.
(3) The least sum requires C D 0 (!!). The sum of errors 9 jC j C j 40 C j would increase if C moves up from zero.
The least sum comes from the median measurement (the median of 0; : : : ; 0; 40 is zero). Many statisticians feel that the least squares solution is too heavily influenced by outliers like b 10 D 40 , and they prefer least sum. But the equations become nonlinear. Now find the least squares straight line C C Dt through those ten points.
P ti ti
t (^) i^2
ATb D
P bi ti bi
Those come from equation ( 8 ). Then ATAbx D ATb gives C D 8 and D D 24=11.
What happens to C and D if you multiply the bi by 3 and then add 30 to get bnew D .30; 30; : : : ; 150/? Linearity allows us to rescale b D .0; 0; : : : ; 40/. Multiplying b by 3 will multiply C and D by 3. Adding 30 to all bi will add 30 to C.
6 Project b D .0; 8; 8; 20/ onto the line through a D .1; 1; 1; 1/. Find bx D aTb=aTa and the projection p D bxa. Check that e D b p is perpendicular to a, and find the shortest distance kek from b to the line through a.
7 Find the closest line b D Dt , through the origin , to the same four points. An exact fit would solve D 0 D 0; D 1 D 8; D 3 D 8; D 4 D 20. Find the 4 by 1 matrix and solve ATAbx D ATb. Redraw Figure 4.9a showing the best line b D Dt and the e’s.
8 Project b D .0; 8; 8; 20/ onto the line through a D .0; 1; 3; 4/. Find bx D D and p D bxa. The best C in Problems 5–6 and the best D in Problems 7–8 do not agree with the best .C; D/ in Problems 1–4. That is because .1; 1; 1; 1/ and .0; 1; 3; 4/ are perpendicular.
9 For the closest parabola b D C C Dt C Et 2 to the same four points, write down the unsolvable equations Ax D b in three unknowns x D .C; D; E/. Set up the three normal equations ATAbx D ATb (solution not required). In Figure 4.9a you are now fitting a parabola to 4 points—what is happening in Figure 4.9b?
10 For the closest cubic b D C C Dt C Et 2 C F t 3 to the same four points, write down the four equations Ax D b. Solve them by elimination. In Figure 4.9a this cubic now goes exactly through the points. What are p and e?
11 The average of the four times is bt D 14 .0 C 1 C 3 C 4/ D 2. The average of the
four b’s is bb D 14 .0 C 8 C 8 C 20/ D 9.
(a) Verify that the best line goes through the center point .bt ;bb/ D .2; 9/. (b) Explain why C C Dbt D bb comes from the first equation in ATAbx D ATb.
Figure 4.9: Problems 1–11 : The closest line C C Dt matches C a 1 C Da 2 in R^4.
Questions 12–16 introduce basic ideas of statistics—the foundation for least squares.
12 (Recommended) This problem projects b D .b 1 ; : : : ; bm/ onto the line through a D .1; : : : ; 1/. We solve m equations ax D b in 1 unknown (by least squares).
(a) Solve aTabx D aTb to show that bx is the mean (the average) of the b’s. (b) Find e D b abx and the variance kek^2 and the standard deviation kek. (c) The horizontal line bb D 3 is closest to b D .1; 2; 6/. Check that p D .3; 3; 3/ is perpendicular to e and find the 3 by 3 projection matrix P.
13 First assumption behind least squares: Ax D b (noise e with mean zero). Multiply the error vectors e D bAx by .ATA/^1 AT^ to get bxx on the right. The estimation errors bx x also average to zero. The estimate bx is unbiased.
14 Second assumption behind least squares: The m errors ei are independent with vari- ance 2 , so the average of .b Ax/.b Ax/T^ is 2 I. Multiply on the left by .ATA/^1 AT^ and on the right by A.ATA/^1 to show that the average matrix .bx x/.bx x/T^ is 2 .ATA/^1. This is the covariance matrix P in section 8.6.
15 A doctor takes 4 readings of your heart rate. The best solution to x D b 1 ; : : : ; x D b 4 is the average bx of b 1 ; : : : ; b 4. The matrix A is a column of 1’s. Problem 14 gives the expected error .bx x/^2 as 2 .ATA/^1 D. By averaging, the variance drops from 2 to 2 =4.
16 If you know the average bx 9 of 9 numbers b 1 ; : : : ; b 9 , how can you quickly find the average bx 10 with one more number b 10? The idea of recursive least squares is to avoid adding 10 numbers. What number multiplies bx 9 in computing bx 10?
bx 10 D 101 b 10 C bx 9 D 101 .b 1 C C b 10 / as in Worked Example 4:2 C.
Questions 17–24 give more practice with bx and p and e.
17 Write down three equations for the line b D C C Dt to go through b D 7 at t D 1 , b D 7 at t D 1 , and b D 21 at t D 2. Find the least squares solution bx D .C; D/ and draw the closest line.
18 Find the projection p D Abx in Problem 17. This gives the three heights of the closest line. Show that the error vector is e D .2; 6; 4/. Why is P e D 0?
19 Suppose the measurements at t D 1; 1; 2 are the errors 2; 6; 4 in Problem 18. Compute bx and the closest line to these new measurements. Explain the answer: b D .2; 6; 4/ is perpendicular to so the projection is p D 0.
20 Suppose the measurements at t D 1; 1; 2 are b D .5; 13; 17/. Compute bx and the closest line and e. The error is e D 0 because this b is.
21 Which of the four subspaces contains the error vector e? Which contains p? Which contains bx? What is the nullspace of A?