Lecture 48 · Week 12· 19:14

Least squares

Normal equations, fitting a line to data.

Play: Lecture 48: Least squares
Lecture · 19:14 · Open on YouTube · Report a problem with this video
TranscriptThe full text of the lecture. Click any sentence to play the video from there.

Today: data that no line fits

Welcome back. Over the last three lectures we built the geometry of inner product spaces: lengths, right angles, orthogonal projections, and the QR factorisation. Today we put all of it to work on a very practical question. Recall problem fourteen of sheet two. A quantity y was measured three times: two at x equals one, three at x equals two, and six at x equals three. No straight line passes through all three points. From the first point to the second, the slope is one, and from the second to the third, it is three.

A line, y equals c zero plus c one x, would have to satisfy one equation for each point: c zero plus c one equals two, c zero plus two c one equals three, and c zero plus three c one equals six. In matrix form, this is A c equals y. The first column of A is all ones, and the second column holds the x values. The vector c holds the unknowns, c zero and c one, and the vector y holds the measurements. Elimination shows that there is no solution. Subtract the first row from the other two. That leaves c one equals one, and two c one equals four. Then subtract twice the second row from the third, which leaves zero equals two.

Real data almost never fits a model exactly, because measurements carry errors. So instead of a solution, we ask for the next best thing: the vector c that makes A c as close as possible to y. Here is the plan. First, what as close as possible means, and why the answer is an orthogonal projection. Then the normal equations, which compute it. Then, when the answer is unique. And finally, the best line through our three points.

The least-squares problem

Throughout today, A is a real m by n matrix, and b is a vector in R m. R m carries the dot product, with the usual length as its norm. A least-squares solution of A x equals b is a vector x hat in R n, such that the distance from b to A x hat is at most the distance from b to A x, for every x in R n. The vector b minus A x hat is its residual, r, and the length of r is the least-squares error.

Why least squares? The squared length of b minus A x is the sum of the squares of its entries. So a least-squares solution makes this sum of squares as small as possible. If the system has solutions, they are exactly its least-squares solutions, with error zero. A solution gives distance zero, and distance zero means that b equals A x hat. Now the picture. The vector A x is x one times the first column of A, plus up to x n times the last column. So as x runs over R n, A x runs over the column space of A.

For our data, A has two columns in R three: the column of ones, and the column one, two, three. Its column space is a plane. The right side b is the vector y, which is two, three, six, and it lies off the plane. Every choice of x gives a point A x of the plane. Its distance to b is the length of b minus A x, and we want the point where this length is smallest. Lecture forty six answered exactly this question. By the best approximation theorem, the orthogonal projection of a vector onto a subspace is closer to the vector than every other point of the subspace.

So here is the first theorem. x hat is a least-squares solution exactly when A x hat equals the projection of b onto the column space of A. Here is why. Call the projection b hat. If A x hat is b hat, then no point A x of the column space is closer to b, so x hat is a least-squares solution. If A x hat is any other point of the column space, then b hat is strictly closer, so x hat is not one. And b hat lies in the column space, so it is A x hat for some x hat. So least-squares solutions always exist, and all of them give the same b hat.

The residual, b minus b hat, is orthogonal to the column space: it makes a right angle with every column of A. That right angle is the key to computing x hat.

The normal equations

Right angles with the columns of A are dot products, and A transpose computes them all at once. Here is the main theorem. x hat is a least-squares solution of A x equals b exactly when A transpose A x hat equals A transpose b. These n equations in n unknowns are the normal equations, and they always have a solution. The proof is a chain of four equivalences, and it is worth taking slowly. Let U be the column space of A. First, by the theorem we just proved, x hat is a least-squares solution exactly when A x hat is the projection of b onto U.

Second, from lecture forty six: a vector p in U is the projection of b exactly when b minus p is orthogonal to U. A x hat lies in U, so the condition becomes: b minus A x hat is orthogonal to U. Third, U is spanned by the columns, a one up to a n. A vector is orthogonal to every vector of the span exactly when it is orthogonal to each column. So we need a j, dot b minus A x hat, to be zero, for every j. Fourth, the rows of A transpose are the columns of A. So by the row rule, these n dot products are exactly the entries of A transpose, times b minus A x hat. The condition is that this vector is zero.

Multiply out: A transpose b, minus A transpose A x hat, is the zero vector. That is exactly the normal equations. And since least-squares solutions always exist, so do solutions of the normal equations. Normal means perpendicular: the equations say that the residual is normal to the column space. In the language of lecture twenty seven, the residual lies in the null space of A transpose, the left null space, which is the orthogonal complement of the column space. The matrix A transpose A is n by n, and it is symmetric, because its transpose is A transpose A again. Its entries are the dot products of the columns of A with each other. Symmetric matrices get a theorem of their own in lecture forty nine.

So here is the recipe. Form A transpose A, and A transpose b. Solve the normal equations by elimination. Then b hat is A x hat, the residual is b minus b hat, and the error is the length of the residual. Two checks come for free. A transpose r is the zero vector. And by Pythagoras, the squared length of b is the squared length of b hat, plus the squared length of r.

When is the answer unique?

The projection b hat is unique, but several vectors x can have A x equal to b hat. If x hat is one least-squares solution, then all of them form x hat, plus the null space of A. So there is exactly one when the null space of A is zero, that is, when the columns of A are linearly independent. The normal equations see the same null space. Proposition: for every real matrix A, A transpose A has the same null space as A. One inclusion is clear: if A x is zero, then A transpose A x is zero too.

Conversely, let A transpose A x be zero, and take the dot product with x. Zero equals x dot A transpose A x, which is A x dot A x, by the transpose rule of lecture fifteen. That is the squared length of A x, so A x is zero. Notice where we used the real numbers: a sum of squares is zero only when every term is zero. Over Z two this fails. For the column one, one, A transpose A is one plus one, which is zero there. Now the theorem. For a real m by n matrix A, three statements are equivalent. The columns of A are linearly independent. A transpose A is invertible. And for every b, the system A x equals b has exactly one least-squares solution.

The first two are equivalent by the proposition. Independent columns mean that the null space of A is zero, so the null space of A transpose A is zero, and a square matrix with zero null space is invertible. And the least-squares solutions are the solutions of the normal equations, a square system with the matrix A transpose A. If that matrix is invertible, there is exactly one. If it is not, any nonzero vector of its null space can be added to a solution. In that case, x hat equals A transpose A, inverse, times A transpose b, and the projection b hat is A times x hat.

In practice we never compute this inverse: we solve the normal equations by elimination. And when a QR factorisation of A is at hand, there is a shorter way. Corollary: if A equals Q R, with orthonormal columns in Q, and R upper triangular and invertible, then the least-squares solution is the unique solution of R x hat equals Q transpose b, found by back substitution. Here is why. Q transpose Q is the identity, because the columns of Q are orthonormal. So A transpose A is R transpose R, and A transpose b is R transpose Q transpose b. R transpose is invertible, so it cancels from both sides.

Now an example with dependent columns. Let A be the three by two matrix whose rows are all (one, two), and let b be the vector (one, two, six). Dot products of the columns give A transpose A, with the rows (three, six), and (six, twelve), which is not invertible. Dot products with b give A transpose b, which is (nine, eighteen). Both normal equations reduce to x one plus two x two equals three. So the least-squares solutions form a line: the vector (three, zero), plus s times the vector (minus two, one).

But all of them give the same b hat, which is (three, three, three). Each entry of b is replaced by the mean of its entries, three. The residual is (minus two, minus one, three), which is orthogonal to the column of ones, and the error is the square root of fourteen.

Fitting a line to data

Now the application. We have m data points, x j, y j, with at least two different x values. We look for the line, y equals c zero plus c one x, that fits them best. At x j, the line predicts c zero plus c one x j, and misses the measurement by y j, minus that prediction. In the picture, the misses are the vertical distances from the points to the line. The least-squares line makes the sum of the squares of these misses as small as possible. Here each square is drawn on its miss. Watch the total as the line moves.

In matrix form. A line through every data point would satisfy, at each x j, c zero plus c one x j equals y j: one equation for each point. For our three points, c zero plus c one equals two, c zero plus two c one equals three, and c zero plus three c one equals six. Each unknown has a coefficient. In the line, c zero is the constant term, so in every equation it is multiplied by one. And c one is the coefficient of x, so in the equation of the point x j, it is multiplied by x j. The coefficients of c zero, all ones, form the first column of A. The coefficients of c one, the x values one, two and three, form the second column. The unknowns form c, and the right-hand sides, the y values, form the observation vector y.

So the line is the least-squares solution of A c equals y. The design matrix A has a row, one, x j, for each point, y holds the observations, and the sum of the squared misses is the squared length of y minus A c. The two columns of A, the ones and the x values, are independent, because the x values are not all equal. So by the theorem, there is exactly one least-squares line. The normal equations take a simple form. A transpose has the columns of A as its rows: a row of ones, and the row of x values.

Each entry of A transpose A is a row of A transpose times a column of A. The row of ones times the column of ones adds up m ones, which is m. The row of ones times the column of x values adds up the x j. The row of x values times the column of ones gives the same sum. And times itself, it gives the sum of the squares of the x j. In the same way, the entries of A transpose y are the row of ones times y, which is the sum of the y j, and the row of x values times y, which is the sum of the products x j y j. Back to our data, with m equal to three. The sum of the x values is one plus two plus three, which is six. The sum of their squares is one plus four plus nine, which is fourteen.

The sum of the y values is two plus three plus six, which is eleven. And the sum of the products is two, plus six, plus eighteen, which is twenty six. So the normal equations are three c zero plus six c one equals eleven, and six c zero plus fourteen c one equals twenty six. Subtract twice the first equation from the second: two c one equals four, so c one hat is two. Then three c zero equals eleven minus twelve, so c zero hat is minus one third. So the least-squares line is y equals minus one third, plus two x.

Now the residuals. At one, two and three, the line gives five thirds, eleven thirds and seventeen thirds. So the residuals are one third, minus two thirds, and one third. Two free checks. Their sum is zero, so the residual is orthogonal to the column of ones. And one third, minus four thirds, plus three thirds, is zero, so it is orthogonal to the column of x values too. The sum of the squared residuals is one ninth, plus four ninths, plus one ninth, which is two thirds. No other line has a smaller sum.

Here is the same fit in R three. The fitted values form the vector y hat, which is the projection of y onto the plane, and the residual is at right angles to the plane. The QR factorisation of lecture forty seven gives the same line. Gram Schmidt on the columns: a two, minus two a one, is the vector (minus one, zero, one). Normalising gives Q, and R is Q transpose A. Q transpose y has the entries eleven over root three, and four over root two. The second row of R c hat equals Q transpose y says root two times c one hat is four over root two, so c one hat is two.

The first row says root three times c zero hat, plus four root three, is eleven over root three. Multiply by root three: three c zero hat plus twelve is eleven, so c zero hat is minus one third, as before. One more fact, from the first normal equation. Divide it by m: the mean of the y values equals c zero hat, plus c one hat times the mean of the x values. So the least-squares line passes through the centre of the data, here the point (two, eleven thirds).

Other models, and a summary

Nothing here was special to lines. A model, y equals c one f one of x, plus up to c n f n of x, with fixed functions f one up to f n, is linear in its coefficients. Fitting it to data is the least-squares problem A c equals y, where the entry of A in row j and column k is f k of x j. To fit a parabola, y equals c zero plus c one x plus c two x squared, A gets a third column, holding the squares of the x values. With our three points, this A is square and invertible, so the parabola of sheet two passes through all three. With more points than coefficients, least squares finds the best compromise. For these four points, the book's example finds the best parabola, y equals three halves, plus three halves x, minus one half x squared.

Try it yourself

You can try this yourself, on this lecture's page at Open Semester dot com. The gold points are our three data points, the green line is their least-squares line, and the violet squares are the squared residuals. Press play, and a trial line glides towards the best one. The squares grow and shrink with it, and their total stops at its smallest value, two thirds. Now drag the middle point up. The best line follows at once, because it solves the normal equations for the new data.

Turn on your own line, and drag its two handles. Your sum of squares comes close, eleven sixteenths against two thirds, but it never goes below the green one. Turn your line off, and choose the parabola, with the four points of the last example. The best parabola is the one we found, and its sum of squares is five. Try your own data, and watch the squares.

To sum up

Let's sum up. A least-squares solution of A x equals b makes the distance from b to A x as small as possible. It is characterised by A x hat equals the projection of b onto the column space. Equivalently, x hat solves the normal equations, A transpose A x hat equals A transpose b, which always have a solution. The residual is orthogonal to every column of A. The solution is unique exactly when the columns of A are independent, and then a QR factorisation gives it by back substitution. Fitting a line is the case of a column of ones and a column of x values.

Before the next lecture, read section ten point four of the course book, try the problems on sheet twelve about least squares, and do the lecture check on this lecture's page at Open Semester dot com. Next time, we start the last week with the spectral theorem: every real symmetric matrix, such as A transpose A, can be diagonalised with an orthonormal basis of eigenvectors. See you there.

Try it

Fit data by least squares

Model
Data

Drag the gold data points, or tap an empty spot to add one. The green line makes the sum of the squared residuals (the violet squares) as small as possible. Press play to watch a line settle on it, or turn on Try your own line and drag its two handles: your sum can tie with the green one, never beat it.

Check yourself

3 questions, from warm-up to stretch

Work each one out on paper, type your answer and press Check. Stuck? Open a hint; the worked solution is there when you need it.

  1. Question 1 of 3 Warm-up

    Let and , so says and . Find the least-squares solution and the squared least-squares error , where .

    Hint 1 of 2

    By Theorem 10.4.3, is a least-squares solution exactly when it solves the normal equations . Here both sides are single numbers.

    Hint 2 of 2

    and , so the normal equation is .

    Worked solution
    1. and . The normal equation gives , the average of and .
    2. Then and . It is orthogonal to the column , as Theorem 10.4.2 says.
    3. , so the least-squares error is .
  2. Question 2 of 3 Core

    Find the least-squares line for the data points , , , and the sum of the squares of its residuals. Fractions such as 3/2 are fine.

    Hint 1 of 2

    Use the normal equations of a line (Proposition 10.4.9): . Compute the five sums first.

    Hint 2 of 2

    , , , , . So and .

    Worked solution
    1. With and , Proposition 10.4.9 gives .
    2. Four times the first equation minus three times the second: , so and . Then , so . The line is .
    3. The fitted values are , so the residuals are . They add up to , as they must.
    4. The sum of squares is .
  3. Question 3 of 3 Stretch

    Let and . Find the first entry of . Is the least-squares solution of unique? Find the least-squares solution with .

    Unique?
    Hint 1 of 2

    The second column is twice the first, so is the line spanned by . By Theorem 10.4.2, is a least-squares solution exactly when .

    Hint 2 of 2

    with and . And .

    Worked solution
    1. The second column of is twice the first, so is the line spanned by , and .
    2. By Theorem 10.4.2, is a least-squares solution exactly when , that is , or . The normal equations say the same: and , so both rows read after dividing by and by .
    3. The columns are dependent, so the solution is not unique (Theorem 10.4.5): the solutions are . With : , so . All of them have the same residual , orthogonal to .

Found a mistake, or something not working on this page? Report it and we'll fix it. See what we've corrected.