School/✦ Research Engineer/Week 11
Least squares as an estimator
A rover logs its position every second. Fitting a line gives its speed. How sure can you be of that speed?
About 45 min · 3 goals · Checked by automated tests
1.Why should I care?
Calibrating a sensor, estimating a drift rate, fitting a motor model: each is a least-squares problem. Reporting the fitted value without its uncertainty is half an answer.
- ◇ Derive the normal equations for a straight-line fit.
- ◇ Compute the least-squares slope and intercept from sums.
- ◇ Quantify the standard error of a fitted slope.
2.Discover
In the Code Lab, generate y = 1.5x + 2 plus Gaussian noise (σ = 0.3) at x = 0…4. Fit the line with the normal equations, repeat 1,000 times with new noise, and compare the spread of your fitted slopes with σ/√Σ(x − x̄)² = 0.3/√10 ≈ 0.095.
Can't use the interactive? Read a description instead
The Code Lab task is a calculation that can be done by hand with the sums Σx, Σy, Σxy and Σx²; the expected spread of slopes, 0.095, is stated in the instructions.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
A line is fitted at x = 0, 1, 2, 3, 4 with noise σ = 0.5. What is the standard error of the slope?
4.Apply it
Fit a line to five odometer logs from the generator, estimate σ from the residuals (divide by n − 2), and report the speed as m̂ ± 2·SE. Is the rover's speed significantly different from the commanded 1.5 m/s?
5.Understand
The model and the criterion
Model: yᵢ = m·xᵢ + c + εᵢ. Least squares picks m, c to minimise S = Σ(yᵢ − mxᵢ − c)². Setting ∂S/∂m = ∂S/∂c = 0 gives the normal equations; their solution is m̂ = Σ(xᵢ − x̄)(yᵢ − ȳ) / Σ(xᵢ − x̄)² and ĉ = ȳ − m̂x̄.
Matrix form
Stack the data as y = Aθ + ε with A's rows [xᵢ, 1] and θ = [m, c]ᵀ. The estimate is θ̂ = (AᵀA)⁻¹Aᵀy, the same formula for any linear-in-parameters model (polynomials, sensor calibration curves). With noise variance σ², Cov(θ̂) = σ²(AᵀA)⁻¹.
How certain is the slope?
From that covariance, SE(m̂) = σ/√Σ(xᵢ − x̄)². Spreading the x-values out, or taking more points, shrinks it. If σ is unknown, estimate it from the residuals: s² = Σ rᵢ²/(n − 2), dividing by n − 2 because two parameters were fitted.
Explain it another way
Hang the line on springs attached to every point, each spring pulling with force proportional to the vertical gap. Where the line settles is the least-squares fit: big gaps pull hardest.
Gauss–Markov: with zero-mean, uncorrelated, equal-variance errors, θ̂ is the best linear unbiased estimator (lowest variance among linear unbiased estimators), even if the noise is not Gaussian.
x = 0…4: x̄ = 2, Σ(x − x̄)² = 10. With σ = 0.3, SE(m̂) = 0.3/√10 ≈ 0.095. Intercept variance σ²(1/n + x̄²/Σ(x − x̄)²) = 0.09 × (0.2 + 0.4) = 0.054, SE ≈ 0.23.
A gyro's bias drift is estimated by fitting a line to its output while stationary. The slope's SE tells you whether the drift is real or just noise.
Derivation: The least-squares slope
S(m, c) = Σ(yᵢ − mxᵢ − c)².
∂S/∂c = −2Σ(yᵢ − mxᵢ − c) = 0 ⇒ c = ȳ − m x̄.
Substitute: S = Σ((yᵢ − ȳ) − m(xᵢ − x̄))².
∂S/∂m = −2Σ(xᵢ − x̄)((yᵢ − ȳ) − m(xᵢ − x̄)) = 0.
So m̂ = Σ(xᵢ − x̄)(yᵢ − ȳ) / Σ(xᵢ − x̄)², which equals (nΣxy − ΣxΣy)/(nΣx² − (Σx)²).
Worked example 1
Points (0, 2.1), (1, 3.4), (2, 5.2), (3, 6.4), (4, 8.1). Find the slope.
x̄ = 2, ȳ = 5.04. Σ(x − x̄)(y − ȳ) = (−2)(−2.94) + (−1)(−1.64) + 0 + (1)(1.36) + (2)(3.06) = 5.88 + 1.64 + 1.36 + 6.12 = 15.0. Σ(x − x̄)² = 10. m̂ = 1.50.
Common mix-ups
- More points always help equally wherever they go.
✓ Points far from x̄ constrain the slope much more than points near the middle. - A good-looking fit means a precise slope.
✓ Report SE(m̂). A line through 3 tightly clustered points can look perfect and still be very uncertain.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2 · a fresh version every time
A rover logs its odometer distance y (m) at times x = 0, 1, 2, 3, 4 s: y = 4.9, 5.8, 7.2, 8.2, 8.8. What is the least-squares slope (speed estimate) in m/s?
Practice 3
To estimate a drift rate as precisely as possible with 10 measurements over a fixed hour, you should take them…
Practice 4
Why divide the residual sum of squares by n − 2 to estimate σ² in a straight-line fit?
7.Predict
You fit a line to 5 points, then to 20 points spread over the same time span with the same noise. The slope's standard error…
Guess first. Being wrong here is how the surprise works.
8.Reflect
Concepts: Statistical estimation: bias, variance and likelihood · Fitting lines with least squares