TactivergeLite

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?

About 45 min · 3 goals · Checked by automated tests · Full interactive version

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.

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…

  1. Halves (roughly)
  2. Stays the same
  3. Quarters
  4. 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.

You can also open the interactive on the full site when you have a faster connection.

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
  1. S(m, c) = Σ(yᵢ − mxᵢ − c)².

  2. ∂S/∂c = −2Σ(yᵢ − mxᵢ − c) = 0 ⇒ c = ȳ − m x̄.

  3. Substitute: S = Σ((yᵢ − ȳ) − m(xᵢ − x̄))².

  4. ∂S/∂m = −2Σ(xᵢ − x̄)((yᵢ − ȳ) − m(xᵢ − x̄)) = 0.

  5. 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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

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?

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.

← Maximum likelihoodRandom and systematic error, and confidence intervals →