Scientia sine finibus

Linear Algebra / Unit 6, Orthogonality / Topic 6.4

Units and Topics

Unit 6. Orthogonality

6.4 Least Squares

This topic finds the best approximate solution of a system that has more equations than unknowns and no exact solution. The method is called least squares. Its main use is fitting a line or a curve to measured data.

You need Topics 2.3, 6.1, and 6.3 for this topic.

1. A System with No Solution

Measurements usually give more equations than unknowns. Suppose that a quantity y is expected to depend on the time t along a line, y = C + Dt, and three measurements give the points (1, 1), (2, 2), and (3, 2). Each point gives one equation for the two unknowns C and D.

C + D = 1 C + 2D = 2 C + 3D = 2

In matrix form, Ax = b with

A = 111213 x = CD b = 122

The three points are not on one line, so the system has no solution. From Topic 2.3, this is the usual case when there are more equations than unknowns. The vector b is not in the column space of A.

2. The Best Approximate Solution

When no x makes Ax equal to b, look for the x that makes Ax as close to b as possible. The difference e = b − Ax is called the error, or the residual. Its length squared is the sum of the squares of its components.

|e|² = e₁² + e₂² + … + em²

The least squares solution is the vector x̂ that makes this sum as small as possible.

Topic 6.1 already solved this problem. The vectors Ax are the vectors of the column space, and the vector of the column space that is closest to b is the projection p = Ax̂. The error is then perpendicular to the column space.

The least squares solution satisfies the normal equations AT Ax̂ = ATb.

To obtain the normal equations, multiply both sides of the system Ax = b by AT. The new system always has a solution, and the solution is unique when the columns of A are independent.

3. Fitting a Line

For the three points of Part 1,

AT A = 111123 111213 = 36614the left side of the normal equationsATb = 111123 122 = 511the right side of the normal equations 3C + 6D = 5the first normal equation6C + 14D = 11the second normal equation2D = 1subtract 2 times the first equation from the secondD = 1/2divide both sides by 2C = (5 − 3)/3 = 2/3the first equation, with 6D = 3y = 2/3 + 1/2tthe line

This is the line that is closest to the three points, in the sense that the sum of the squares of the vertical distances is as small as possible.

tMeasured yValue on the lineError
117/6−1/6
225/31/3
3213/6−1/6

The error vector is e = (−1/6, 1/3, −1/6). It is perpendicular to both columns of A.

−1/6 + 1/3 − 1/6 = 0the dot product with the column (1, 1, 1)−1/6 + 2/3 − 1/2 = 0the dot product with the column (1, 2, 3)1/36 + 4/36 + 1/36 = 1/6the sum of the squares of the errors

No other line gives a smaller sum.

Try it: your line and the least squares line

Drag the four points to set the data. Move the sliders to change the intercept C and the slope D of the blue line, and try to make the sum of the squares of the errors as small as you can. Then press the button to see the least squares line in green.

4. The Same Result from Calculus

The sum of the squares is a function of C and D.

S(C, D) = (C + D − 1)² + (C + 2D − 2)² + (C + 3D − 2)²

At its minimum, the derivative with respect to C is 0 and the derivative with respect to D is 0. Calculating these two derivatives and dividing by 2 gives 3C + 6D = 5 and 6C + 14D = 11. These are the normal equations again. The geometric argument with the projection and the calculus argument lead to the same system.

5. Other Models

The method works whenever the unknowns appear linearly, even if the curve is not a line. Each quantity that multiplies an unknown gives one column of A.

ModelUnknownsColumns of A
y = C + DtC, D1 and t
y = C + Dt + Et²C, D, E1, t, and t²
y = Dt, a line through the originDt
y = C cos t + D sin tC, Dcos t and sin t

The word linear in linear algebra refers to the unknowns. A parabola is not a line, but fitting a parabola is still a linear problem, because C, D, and E appear only to the first power.

A line through the origin

With one unknown, A has one column a, and the normal equations give the formula for the projection onto a line.

D = (a · b)/(a · a)

For example, Hooke’s law says that the force of a spring is F = kx. If the stretches 1, 2, and 3 cm give the measured forces 2.1, 3.9, and 6.2 N, then

k = ((1)(2.1) + (2)(3.9) + (3)(6.2))/(1 + 4 + 9) = 28.5/14 ≈ 2.04 N/cm

6. Least Squares with QR

If A = QR, as in Topic 6.3, then AT A = RT QT QR = RT R, and the normal equations simplify.

Rx̂ = QTb

The system is solved by back substitution. Computer programs use this form, because forming AT A makes rounding errors larger and the orthogonal matrix Q does not.

7. When the Columns Are Dependent

If the columns of A are dependent, then AT A has no inverse, and the normal equations have infinitely many solutions. All of them give the same projection p, but the coefficients are not determined. This happens, for example, when a line is fitted to data that were all measured at the same time t. Topic 7.3 shows how to choose one solution in this case.

Common Mistakes

  • Trying to solve Ax = b directly when it has no solution. Multiply both sides by AT first.
  • Forgetting the column of ones for the intercept C.
  • Measuring the error perpendicular to the line. Least squares for y = C + Dt uses the vertical distances.
  • Expecting the least squares line to pass through the data points. It usually passes through none of them.
  • Thinking that the method fits only straight lines. It fits any model in which the unknowns appear linearly.
  • Reporting Ax̂ as the solution. The solution is x̂, the vector of coefficients. Ax̂ is the vector of fitted values.

Summary

TermMeaning
Residuale = b − Ax
Least squares solutionThe x̂ that makes |e|² as small as possible
Normal equationsAT Ax̂ = ATb
Fitted valuesp = Ax̂, the projection of b onto the column space
With QRRx̂ = QTb
Line through the originD = (a · b) / (a · a)

Practice

Work each problem on paper first. Then open the answer to check your work.

1. Write the matrix A and the vector b for fitting y = C + Dt to the points (0, 1), (1, 3), (2, 4), and (3, 4).

A = 10111213 and b = (1, 3, 4, 4).

2. Find the least squares line for the points in Problem 1.

AT A = 46614ATb = (12, 23)4C + 6D = 12the first normal equation6C + 14D = 23the second normal equation5D = 5subtract 1.5 times the first equation from the secondD = 1C = (12 − 6)/4 = 1.5the first equation, with D = 1y = 1.5 + tthe line

3. Find the error vector for the line in Problem 2, and check that it is perpendicular to both columns of A.

The values on the line are 1.5, 2.5, 3.5, and 4.5, so e = (−0.5, 0.5, 0.5, −0.5).

−0.5 + 0.5 + 0.5 − 0.5 = 0the dot product with the column (1, 1, 1, 1)(0)(−0.5) + (1)(0.5) + (2)(0.5) + (3)(−0.5) = 0the dot product with the column (0, 1, 2, 3)

4. Find the best horizontal line y = C for the measurements 4, 5, and 9.

A is the single column (1, 1, 1).

C = (4 + 5 + 9)/3 = 6

The least squares fit of a constant is the average of the measurements.

5. A cart starts from rest. The distances 0.5, 2.1, and 4.4 m are measured at the times 1, 2, and 3 s. Fit the model d = kt², and find the acceleration.

The column is a = (1, 4, 9).

a · b = 0.5 + 8.4 + 39.6 = 48.5a · a = 1 + 16 + 81 = 98k = 48.5/98 ≈ 0.495 m/s²acceleration = 2k ≈ 0.99 m/s²because d = (1/2)at²

6. Write the matrix A for fitting the parabola y = C + Dt + Et² to data at t = −1, 0, 1, and 2.

The columns are 1, t, and t²: A = 1−11100111124.

Python Practice

Step 1. Build the matrix for a line

The matrix A for the model y = C + Dt has a column of ones and a column with the times. The instruction np.ones(3) makes a vector of three ones, and np.column_stack builds a matrix from its columns. The code below uses the three points of Part 1.

Python

The result has the rows 1, 1 and 1, 2 and 1, 3.

Step 2. Solve the normal equations

The expression A.T is the transpose of A, the @ sign multiplies matrices and vectors, and np.linalg.solve solves a system. The code below solves ATAx̂ = ATb.

Python

The result is [0.66666667 0.5], so C = 2/3 and D = 1/2, as in Part 3.

Now fit a line to the four points of Practice Problem 1: (0, 1), (1, 3), (2, 4), and (3, 4). Three lines have to change, because the column of ones needs four entries.

Python

Step 3. Calculate the errors

The code below calculates the error vector for the three points of Part 1, tests that it is perpendicular to the columns of A, and adds the squares of its components. Two stars mean a power, and np.sum adds the numbers in a list.

Python

The errors are −1/6, 1/3, and −1/6. The second result is two numbers such as 4.4e-16, which are rounding errors for 0. The sum of the squares is 1/6, which is about 0.167.

The next box tests a different line, y = 0.5 + 0.5t, on the same data. Its sum of squares is larger. Change the two numbers in the line that starts with x to any other values, and try to get a sum below 0.17.

Python

Step 4. Use the built-in fit

The instruction np.polyfit fits a polynomial by least squares. Its inputs are the times, the measured values, and the degree of the polynomial. A line has the degree 1. The result lists the coefficient of the highest power first, so for a line it gives the slope D and then the intercept C.

Python

The result is [0.5 0.66666667]: the slope 1/2 and the intercept 2/3.

Step 5. Fit a parabola

For the model y = C + Dt + Et², the matrix A gets a third column with the squares of the times. The data below lie close to a parabola.

Python

The matrix A is the one of Practice Problem 6. The fitted parabola is about y = 0.945 − 0.065t + 1.075t².

Step 6. Fit a line through the origin

With one unknown, the least squares solution is a ratio of two dot products. The code below finds the spring constant of Part 5. The instruction np.dot calculates a dot product.

Python

The result is about 2.04 N/cm.

Now solve Practice Problem 5. The model is d = kt², so the column holds the squares of the times 1, 2, and 3, and the measured distances are 0.5, 2.1, and 4.4. Change the first two lines.

Python

Quiz

This quiz has 5 questions on this topic. A question asks you either to choose one answer or to type numbers. The page grades your answers and explains each one. A number can be typed as a whole number, a decimal, a fraction such as 1/3, or a calculation such as sqrt(18)/2 or arccos(1/2). The page shows the value of a calculation next to the box. Angles in a calculation are in degrees. The Calculator button under each question opens a calculator. Your answers are saved in your browser, so they are still on the page when you come back to it.

Choose how your answers are checked

1. Which equations are the normal equations of the system Ax = b?

2. Three measurements of one quantity give 2, 3, and 7. Find the constant C that fits the three measurements best in the least squares sense.

3. Find the least squares line y = C + Dx for the points (0, 0), (1, 2), and (2, 2). You can type fractions.

4. Hooke’s law says that the force of a spring is F = kx. The stretches 1 cm and 2 cm give the measured forces 3 N and 5 N. Find the least squares value of k, in newtons per centimeter.

5. The least squares error is e = b − Ax̂. The error is perpendicular to which vectors?

Next: Topic 7.1, Symmetric Matrices and the Spectral Theorem