School/✦ Research Engineer/Week 5
Least squares and the normal equations
Seven measurements, two unknowns, no exact solution. What is the most honest 'best' answer?
About 50 min · 3 goals · Checked by automated tests
1.Why should I care?
Every calibration curve, every fitted model parameter and every estimator in the next term starts here. Least squares is the bridge from linear algebra to data.
- ◇ Write a line fit as an over-determined system A·p ≈ y.
- ◇ Derive the normal equations AᵀA·p = Aᵀy by minimising the sum of squared residuals.
- ◇ Interpret residuals and recognise the sensitivity of least squares to outliers.
2.Discover
On 'Spring calibration', drag your slope and intercept to make Σr² as small as you can. Then reveal the least-squares line. Switch to the outlier set and see how far one bad reading drags the line.
Loading the interactive…
Can't use the interactive? Read a description instead
The least-squares diagram plots data points, your adjustable line and vertical residual segments. Sliders set slope and intercept; text gives your sum of squared residuals, and a button reveals the optimal line and its sum.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1 · a fresh version every time
Fit y = m·x + c by least squares to the points (0, 1.3), (1, 1.8), (2, 1.8), (3, 2.6), (4, 3.2). What is the slope m?
4.Predict
At the least-squares line, what is the sum of the residuals (not squared)?
Guess first. Being wrong here is how the surprise works.
5.Understand
Over-determined systems
More equations than unknowns, with noise, means no exact solution. Least squares chooses p to minimise ‖y − Ap‖²: the total squared vertical miss. The answer always exists, and it is unique when A's columns are independent (for a line: at least two distinct x values).
Reading residuals
Plot residuals against x. Random scatter around zero suggests the model is adequate. A curve suggests the wrong model (try a quadratic or a transformation). A funnel suggests noise growing with x. One huge residual suggests an outlier, and squaring gives it a lot of influence.
Linear in the parameters
'Linear' least squares means linear in p, not in x. y = a + bx + cx² is still linear least squares (three columns 1, x, x²). Exponential decays can be fitted by taking logs: ln(peak) = ln A₀ − ζω t, a straight line in t. You will use this in week 8.
Explain it another way
Imagine springs pulling the line vertically towards each point, with energy proportional to stretch squared. The line settles where the total spring energy is least.
p̂ = (AᵀA)⁻¹Aᵀy, and the fitted values Ap̂ = Py, where P = A(AᵀA)⁻¹Aᵀ is the projection onto col(A).
Under independent, equal-variance, zero-mean noise, the least-squares estimator is unbiased and has the smallest variance among linear unbiased estimators (Gauss–Markov).
Calibrating a load cell: hang known masses, record raw counts, fit counts = m·force + c. The slope is the sensitivity and the intercept is the zero offset.
Derivation: Normal equations for a straight line
Model yᵢ ≈ m·xᵢ + c. Stack all points: A·p ≈ y with A = [[x₁, 1], …, [xₙ, 1]] and p = (m, c). With n > 2 points there is usually no exact solution.
Residual vector r = y − A·p. Minimise S(p) = ‖r‖² = Σ (yᵢ − m xᵢ − c)².
∂S/∂m = −2 Σ xᵢ(yᵢ − m xᵢ − c) = 0 and ∂S/∂c = −2 Σ (yᵢ − m xᵢ − c) = 0.
So m Σxᵢ² + c Σxᵢ = Σxᵢyᵢ and m Σxᵢ + c·n = Σyᵢ. In matrix form: AᵀA·p = Aᵀy.
Solve: m = (nΣxy − ΣxΣy)/(nΣx² − (Σx)²), c = (Σy − mΣx)/n. Geometrically, Aᵀr = 0: the residual is perpendicular to every column of A.
Caution: κ(AᵀA) = κ(A)², so for ill-conditioned fits, libraries solve via QR instead of forming AᵀA.
Worked example 1
Fit a line to (0, 1), (1, 3), (2, 4), (3, 6).
n = 4, Σx = 6, Σy = 14, Σx² = 14, Σxy = 0 + 3 + 8 + 18 = 29. m = (4·29 − 6·14)/(4·14 − 36) = (116 − 84)/20 = 1.6. c = (14 − 1.6·6)/4 = 1.1. Residuals: −0.1, 0.3, −0.3, 0.1, which sum to 0 ✓.
Common mix-ups
- Least squares only fits straight lines.
✓ It fits any model linear in its parameters, including polynomials and transformed models. - A high R² proves the model is right.
✓ Check the residual pattern; a curved model can fit a straight line well and still be wrong.
6.Watch
→ Strang reaches the same normal equations by projecting y onto the column space. Compare with the calculus derivation here.
Derives the least-squares line by projection, arriving at the same normal equations AᵀA·x = Aᵀb that this term derives with calculus.
Length: see YouTube · captions on YouTube · source
Nothing loads from YouTube until you press play.
The video is optional: everything you need is also in the lesson text and the interactive.
7.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
Fit y = m·x + c by least squares to the points (0, 0.3), (1, -0.3), (2, -1.2), (3, -2.8). What is the intercept c?
Practice 3
At the least-squares solution, the residual vector r satisfies…
Practice 4
Which model can be fitted by linear least squares directly?
Practice 5
One reading is wildly wrong. What happens to the least-squares line?
8.Apply it
In the Code Lab, generate 20 noisy points from y = 2x + 1, fit them with the normal equations, and repeat 200 times. Plot the histogram of fitted slopes: how wide is it, and how does it change with 80 points?
9.Reflect
Concepts: Fitting lines with least squares · Numerical linear algebra: conditioning, LU and power iteration