Fifty measurements never lie exactly on one line, so the system that would fit a line to them has no solution, and the useful question becomes which line comes closest.
The previous lesson established that is solvable exactly when lies in the column space of . When there are more equations than unknowns the column space is a small subspace of a large space, and a built from measurements will miss it. This lesson replaces by the nearest point of the column space, and the entire method comes from one geometric fact: the shortest route from a point to a subspace meets it at a right angle.
Projection onto a line, again
The lesson on vectors projected one vector onto another by requiring the leftover to be perpendicular. Given and a direction , the projection is where the condition forces
That single equation is the seed of everything below, and it is worth naming what it accomplishes: it finds the point of the line closest to . Closest, because any other point on the line has by Pythagoras, the two pieces being perpendicular, and the second piece is positive unless .
Example. Project onto the line through .
and , so and the projection is . The error is , whose dot product with is , confirming the right angle.
Now you. Project onto the line through .
Answer
and , so and the projection is . The error dotted with gives .
Projection onto a subspace, and the normal equations
Now replace the line by the column space of a matrix . The problem is: find the vector in closest to . Since every element of the column space is for some , the unknown is , and the error vector is .
The condition is the same as before, generalised: the error must be perpendicular to the whole subspace, which means perpendicular to every column of . Perpendicularity to each column is a dot product being zero, and the dot products of all the columns with at once are the components of . So the condition is
which rearranges into the normal equations:
This is a square system, of size by whatever the shape of , and it is always consistent. When the columns of are independent, is invertible and the solution is unique. The projection is then , and the quantity being minimised is , the sum of the squared errors, which is where the name least squares comes from.
Note what has happened. An unsolvable problem became a solvable one, of smaller size, by a change of question: not "which satisfies the equations" but "which misses by the least". The method was published by Legendre in 1805 and used by Gauss to recover the orbit of Ceres from a handful of observations in 1801, after the asteroid was lost behind the sun.
Fitting a straight line
Here is the whole machine on real numbers. A ball rolls along a track and its position is measured at four times: at seconds the readings are metres. Fit .
Demanding exactness gives four equations in two unknowns: , , , . In matrix form with , and . The four points are not collinear, so no solution exists: is a vector in and the column space is only a plane.
Form the normal equations. has first row and second row , since . And , since . So
The determinant is , so a solution exists and is unique: and . The fitted line is , so the best estimate of the ball's speed is m s⁻¹.
The fit can be audited. The predicted positions are , , and , so the residuals are , , and . Their sum is zero, which is the first normal equation, that the error is perpendicular to the column of ones. Their dot product with is , which is the second. The sum of squares is , and no other line achieves a smaller value: that is what the perpendicularity guaranteed.
Example. Fit to the three points , and .
The sums are , , and . The normal equations are and . Their determinant is , so and . The fitted values are , and , with residuals , and , summing to zero as required, and a squared error of .
Now you. Fit a horizontal line to the four readings . What is , and what is it in ordinary language?
Answer
Now is the single column , so the normal equation is , giving . Fitting a constant by least squares gives the arithmetic mean, which is what the mean is for: the number whose squared deviations from the data are smallest.
The projection matrix
Substituting into gives the projection as a matrix acting on :
Two properties identify it, and both can be read off the geometry rather than computed. , because projecting a point that is already in the subspace leaves it alone. And : projection matrices are symmetric, which follows from the formula since is symmetric and the outer factors transpose into each other. Symmetry is not obvious geometrically, and the final lesson explains why projections belong to the best-behaved family of matrices.
Two special cases check the formula. If is square and invertible, its column space is everything, nothing needs projecting, and indeed . If is a single column , then is the number and , the line formula from the start of the lesson.
Orthonormal bases
Projection got complicated because the columns of were not perpendicular. When they are, everything simplifies at once.
A set of vectors is orthonormal when each has length one and any two distinct ones are perpendicular. If are orthonormal then the coordinates of any vector in their span are simply dot products: writing and dotting both sides with kills every term but one, leaving . No system needs solving.
Take and , which are unit vectors, since , and perpendicular, since . The coordinates of in this basis are and . Reconstructing: .
A square matrix whose columns are orthonormal is called an orthogonal matrix, and it satisfies , since the entry in row and column of is . So its inverse is its transpose, which costs nothing to compute. Such matrices preserve every length and every angle, because , and in the plane they are exactly the rotations, of determinant , and the reflections, of determinant . Their determinant is always , since .
Any independent set can be turned into an orthonormal one by Gram-Schmidt: keep the first vector, normalised; from the second, subtract its projection onto the first and normalise what remains; from the third, subtract its projections onto both and normalise; and so on. Starting from and : the first normalises to . The second has , so subtracting leaves , of length , giving . Their dot product is .
Example. Verify that is orthogonal, and find the coordinates of in its columns.
Each column has length one and the two are perpendicular, as computed above, so . The coordinates are dot products: and . Checking: . Note that , so the length is the same computed in either basis, which is what preserving length means.
Now you. What is the inverse of , and what transformation is it?
Answer
The inverse is the transpose, . Since , is a rotation, by the angle whose cosine is and sine is , about . Its inverse is the rotation by , which is what the transpose describes.
Honest limits
The normal equations are the right derivation and the wrong algorithm. Forming squares the condition number of , the ratio of its largest stretching factor to its smallest, and squaring a condition number of gives , which exceeds what double precision can absorb. Numerical libraries fit by QR factorisation, in which is written as an orthogonal matrix times a triangular one, or by the singular value decomposition, and they never form . The geometry above is exactly right; the arithmetic route to it is not.
Least squares is also a choice, not a law. Squaring the residuals means one badly wrong measurement counts as much as many slightly wrong ones, so a single outlier can drag the whole line. It is the correct choice when errors are independent and normally distributed with equal variance, which Gauss proved and which is sometimes even true. When it is not, other measures of misfit are used, at the price of losing the linear algebra: minimising the sum of absolute errors has no formula like the normal equations.
Finally, a fitted line is a description of the data, not a licence to extrapolate. The ball's fitted position at is metres while the measurement was , and nothing in the method says which is closer to the truth.
Coordinates in an orthonormal basis turned out to be free, which raises a question left open two lessons ago. If a basis can be chosen, what does a change of basis do to the matrix of a transformation, and how much can a good choice simplify it? That is the next lesson, and it leads directly to the last third of the course.