Linear Algebra, Live
Strang §4.3 · Least squares approximations

Least squares: the best answer when Ax = b has no solution

Slide 3 found the closest point inside a column space. Now we use it when Ax = b has no solution, which happens all the time with real measurements. Three friends time the same runner and get three slightly different times. No single time fits all three, so everyone agrees on the best compromise: the average. Least squares does the same for any system with too many equations. It picks the x̂ that makes the total squared error ‖b − Ax‖² as small as possible. The answer is the shadow from slide 3: Ax̂ is the projection of b onto the column space, and x̂ solves the normal equations AᵀAx̂ = Aᵀb.

Lesson 5 · slide 4Least squares
AᵀA x̂ = Aᵀb    e = b − Ax̂    Aᵀe = 0
Key ideas
  1. Ax = b is solvable only when b lies in the column space C(A). Noisy data almost never does.
  2. Least squares minimizes E = ‖b − Ax‖², the sum of the squared errors.
  3. The best Ax̂ is the projection of b onto C(A). The error is perpendicular to every column: Aᵀe = 0.
  4. That condition is the normal equations AᵀAx̂ = Aᵀb.
  5. AᵀA is square and invertible when the columns of A are independent.

Three stopwatches, one runner

Three friends time one sprint: 10.2, 10.5 and 10.0 seconds. So x = 10.2, x = 10.5, x = 10.0. No x fits all three, but everyone would take the average, about 10.23.

That is least squares. A has one column, a = 111, and b = 10.210.510.0. Project b onto a, as on slide 2:

x̂ = aᵀb / aᵀa = (10.2 + 10.5 + 10.0) / 3 = 30.7 / 3 ≈ 10.23

The average is the projection onto 111. With only two stopwatches, reading 1 and 3, we can draw it in the plane:

a = 11,   b = 13:   x̂ = (1 + 3) / 2 = 2,   p = 22,   e = −11
The readings b = 13 drop onto the line of equal readings xx. They land at p = 22: the average, 2, in both places.

The general recipe

Now A has n columns and m rows, with more rows than columns (m > n). Every Ax lies in the column space, a flat subspace inside ℝᵐ (all vectors with m numbers). The data b sticks out of it. So we go to the closest point we can reach: the projection p = Ax̂.

The error e = b − Ax̂ is perpendicular to every column. Stack those dot products and you get Aᵀe = 0:

Aᵀ(b − Ax̂) = 0,   so   AᵀA x̂ = Aᵀb

We can't solve Ax = b, but when the columns of A are independent, AᵀAx̂ = Aᵀb has exactly one solution (shown below). It is only n by n, however many measurements there are.

Three equations in two unknowns: x₁ = 3, x₂ = 3, x₁ + x₂ = 0. The columns 101 and 011 span the sheet: everything Ax can reach. b = 330 sits off it, so we settle for its shadow Ax̂ = 112, with x̂ = 11.

A worked example

Fit a line to three readings: at t = 0, 1, 2 we read b = 1, 1, 4. Each reading gives one equation C + Dt = b, where C is the height of the line at t = 0 and D is its slope:

A = 101112,   x = CD,   b = 114
AᵀA = 3335,   Aᵀb = 1 + 1 + 40 + 1 + 8 = 69
Solve 3C + 3D = 6 and 3C + 5D = 9: subtracting gives 2D = 3, so D = 1.5 and C = 0.5.   The best line is 0.5 + 1.5t
Ax̂ = 0.523.5,   e = 0.5−10.5,   Aᵀe = 0.5 − 1 + 0.50 − 1 + 1 = 00 ✓
The line starts flat and swings into the best line 0.5 + 1.5t. The dashed gaps are the errors; the readout shows the sum of their squares fall to 1.5.

Why AᵀA has an inverse

Suppose AᵀAx = 0. Multiply by xᵀ: xᵀAᵀAx = (Ax)ᵀ(Ax) = ‖Ax‖² = 0, so Ax = 0. (Transposing a product reverses it: (Ax)ᵀ = xᵀAᵀ.) Independent columns force x = 0. So AᵀA has an inverse.

With dependent columns it fails. Take all three readings at the same time t = 1. Then both columns are 111:

AᵀA = 3333,   ad − bc = 9 − 9 = 0: no inverse

Three readings at one moment say nothing about a slope. For a line fit, use at least two different times.

Keep reading with the full course

Finish lesson 5 and you will be able to: fit the best line to noisy sensor data.

Still locked here:

  • The rest of this explanation
  • 4 more examples with step-by-step solutions
  • Five extra members-only problems with worked solutions

$20 once, lifetime access. Lessons 1 to 3 stay free.

Examples

Example 1 · The average is least squares

One unknown, three readings: x = 3, x = 5, x = 10.

Show solutionHide solution
a = 111,   b = 3510
x̂ = aᵀb / aᵀa = 18 / 3 = 6
e = −3−14,   sum of errors = 0 ✓ (perpendicular to 111)
Exercise Solve the normal equations so that the error e is perpendicular to the columns: Aᵀe = 0. Edit the code and press Run.
slide_4.py Loading Python…
1
Output · Ctrl+Enter runs
Press Run to execute this slide's code. Ctrl+Enter works too.

Questions and comments

Stuck on something, or found a mistake? Ask here and I'll answer. You sign in with GitHub.

The full course

From Ax = b to PCA, by running it

Lessons 1 to 3 are free. Finish all eight and you will be able to:

You get: all 8 lessons with Python labs · every worked example and solution · 5 extra problems per lesson · your progress tracked · answers to your questions · every future update.

$20 once · lifetime access
Continue to checkout →

Already a member? Log in to the course