Lite / Independent Engineering Research / 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?
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.
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…
- Halves (roughly)
- Stays the same
- Quarters
- Doubles
Guess first, then reveal
SE(slope) = σ/√Σ(xᵢ − x̄)². Four times as many evenly spread points makes Σ(xᵢ − x̄)² roughly four times larger, so the SE roughly halves.
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.
What the interactive shows (text version)
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.
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
Intuitive
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.
Technical
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.
Mathematical
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.
Applied
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.
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
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?
Reflect
Least squares assumes the x-values are exact. What would change if timestamps were noisy too?
Write your answer on paper or in your own notes. Lite never stores what you write.