Lite / Independent Engineering Research / Week 5
Least squares and the normal equations
Seven measurements, two unknowns, no exact solution. What is the most honest 'best' answer?
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.
Predict
At the least-squares line, what is the sum of the residuals (not squared)?
- Always zero
- Always positive
- It depends on the data
Guess first, then reveal
The intercept's normal equation says Σ rᵢ = 0: residuals above and below balance exactly. (That is true when the model includes an intercept.)
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.
What the interactive shows (text version)
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.
Watch (optional)
Watch for: 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.
“16. Projection Matrices and Least Squares” by MIT OpenCourseWare (opens YouTube; streaming video uses a lot of data)
The video is optional. Everything you need is in this lesson's text.
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
Intuitive
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.
Technical
p̂ = (AᵀA)⁻¹Aᵀy, and the fitted values Ap̂ = Py, where P = A(AᵀA)⁻¹Aᵀ is the projection onto col(A).
Mathematical
Under independent, equal-variance, zero-mean noise, the least-squares estimator is unbiased and has the smallest variance among linear unbiased estimators (Gauss–Markov).
Applied
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.
Practice
Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.
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?
Reflect
When would you prefer a method less sensitive to outliers than least squares?
Write your answer on paper or in your own notes. Lite never stores what you write.