An inconsistent system Ax = b has no exact solution, but it has a best approximate one. A least-squares solution x̂ makes Ax̂ the point of the column space closest to b, which is the orthogonal projection of b onto Col A. The error b − Ax̂ is then orthogonal to every column of A, which is the statement Aᵀ(b − Ax̂) = 0, or the normal equations AᵀAx̂ = Aᵀb. Fitting a line y = β₀ + β₁x to data points is the most common case, and it minimizes the sum of the squared vertical errors.
What you'll learn
Explain a least-squares solution geometrically
Set up and solve the normal equations
Fit a line of best fit to data with a design matrix
Four measurements, one line: the points (1,1), (2,3), (3,4) and
(4,4) do not lie on any line y=β0+β1x. Demanding that they
did gives four equations in two unknowns,
11111234[β0β1]=1344
and the system is inconsistent. The useful question is not whether it has a
solution but which β comes closest.
A least-squares solution of Ax=b is a vector
x^ with ∥b−Ax^∥≤∥b−Ax∥
for every x.
The name comes from the quantity minimized: ∥b−Ax∥2 is
the sum of the squares of the individual errors.
As x varies, Ax runs over the column space of A. The
point of ColA closest to b is its
orthogonal projectionb^, and the error b−b^ is orthogonal
to ColA. So x^ is exactly a solution of
Ax=b^, and the error must be orthogonal to every
column aj:
aj⋅(b−Ax^)=0for every j
The transposeAT has the columns of A as its rows, so
ATv lists the dot products of the columns with
v. All the orthogonality conditions together read
AT(b−Ax^)=0, which rearranges
to the normal equations:
ATAx^=ATb
The best approximation leaves an error perpendicular to the column space, and
the normal equations are that perpendicularity written column by column. When
the columns of A are independent, ATA is invertible and the
solution is unique.
For the points (1,1), (2,2) and (3,2), write the design matrix and compute XTX and XTy.
Answer
XTX=[36614] and XTy=(5,11)
Full solution
X has rows (1,1), (1,2) and (1,3). The entries of XTX are 3 points, 1+2+3=6, and 1+4+9=14. And XTy=(1+2+2,1+4+6).
Solve the normal equations from exercise 2 for the best line.
Answer
y=32+21x
Full solution
3β0+6β1=5 and 6β0+14β1=11. Doubling the first and subtracting gives 2β1=1, so β1=21 and β0=35−3=32.
Find the least-squares line for (−1,0), (0,1) and (1,3).
Answer
y=34+1.5x
Full solution
XTX=[3002] because the x-values add to 0, and XTy=(4,3). So β0=34 and β1=23.
Find the residuals of the fit in exercise 4, and check that they add to zero.
Answer
61, −31 and 61
Full solution
The line gives −61, 34 and 617 at the three x-values. Subtracting from 0, 1, 3 gives the residuals, and 61−62+61=0.
Find the least-squares solution of [111−1]x=[31].
Answer
(2,1), the exact solution
Full solution
The system is consistent, so the smallest possible error, zero, is reached at its solution. The normal equations [2002]x^=[42] agree.
When does Ax=b have exactly one least-squares solution?
Answer
When the columns of A are linearly independent
Full solution
Then ATA is invertible and x^=(ATA)−1ATb. With dependent columns, many x give the same projection Ax^.
What is Ax^, geometrically?
Answer
The orthogonal projection of b onto ColA
Full solution
Ax^ is the point of the column space closest to b, and the closest point of a subspace is the orthogonal projection.
If b is orthogonal to every column of A, and the columns are independent, what is x^?
Answer
x^=0
Full solution
Then ATb=0, and the normal equations ATAx^=0 with invertible ATA force x^=0. The projection of b onto the column space is the origin.
A student fits a line to four points by picking two of them and drawing the line through those. What went wrong?
Hint
What happens to the other two points?
Answer
The line ignores half the data. Least squares uses all four points and minimizes the total squared error.
Full solution
The line through (2,3) and (4,4), for instance, is y=2+0.5x, with residuals −1.5, 0, 0.5 and 0 and squared error 2.5, worse than the 1 achieved by y=0.5+x. Only the normal equations balance every point at once.
Frequently asked questions
What is a least-squares solution?
A vector x̂ that makes ‖b − Ax̂‖ as small as possible. When Ax = b is consistent it is an ordinary solution; otherwise it is the best approximation.
What are the normal equations?
AᵀAx̂ = Aᵀb. They say the error b − Ax̂ is orthogonal to every column of A.
Why is it called least squares?
It minimizes ‖b − Ax‖², which is the sum of the squares of the individual errors.
When is the least-squares solution unique?
When the columns of A are linearly independent. Then AᵀA is invertible and x̂ = (AᵀA)⁻¹Aᵀb.
How do you fit a line to data with matrices?
Put a column of 1s and the column of x-values into a design matrix X, the y-values into a vector y, and solve XᵀXβ = Xᵀy for the intercept and slope.