A13 · Lesson 37 · Least squares and curve fitting
Fitting a model to noisy data, linear and nonlinear
A capacitor discharges and a scope records eight noisy points. What's the time constant? A thermistor is measured at a dozen temperatures; what are its B and ? Fitting a model to data means choosing parameters that make the misfit small, and the standard measure of misfit is the sum of squared residuals. When the model is linear in its parameters (a line, a polynomial), that's a linear least-squares problem, solved stably by QR (A7). When it isn't (an exponential, a resonance curve), Gauss–Newton linearizes and repeats, and Levenberg–Marquardt adds a safety brake. Both are what scipy.optimize.curve_fit runs.
- Eight points from an RC discharge with noise, and the model . Move A and τ to make the sum of squared residuals as small as you can.
- The two parameters trade off: a larger A with a shorter τ can fit the early points similarly. The minimum, 0.0352 V², is at A = 5.050 V and τ = 1.980 ms.
- Starting from A = 4, τ = 1.5 (SSR 5.66), Gauss–Newton reaches it in three steps: 0.050, 0.0352, 0.0352.
Predict
You solve a least-squares fit through the normal equations , and . About how many digits can you trust?
About 4: , so forming throws away twice the digits. A QR factorization of A works with κ(A) itself and keeps about 10.
Name it
Linear least squares
For a model linear in its parameters, . Three ways to solve it:
| Method | Conditioning | Notes |
|---|---|---|
| normal equations | cheap; fine when κ is small | |
| QR: , solve | the standard choice | |
| SVD | handles rank deficiency; most expensive |
For a straight line through : slope , intercept , and with noise σ on each y the slope's standard error is .
Nonlinear least squares
For residuals with Jacobian :
- Gauss–Newton: solve the linear least-squares problem (that is, , preferably by QR) and set . Near a good fit it converges almost quadratically.
- Levenberg–Marquardt: solve . A large λ gives short, safe steps downhill; a small λ is Gauss–Newton. Raise λ when a step makes things worse, lower it when steps succeed.
At the solution, the parameters' covariance is about , with σ² estimated as SSR/(m − n) for m points and n parameters.
Don't linearize by transforming the data
Fitting a line to does give an exponential, but it weights the noisy small values far more heavily and biases the result. Use it for a starting guess, then fit the real model.
Worked example
Fit to the eight points above by Gauss–Newton from A = 4 V, τ = 1.5 ms.
Show the solution
- The start. At A = 4 V and τ = 1.5 ms the SSR is 5.663 V².
- Iterate. Gauss–Newton takes (A, τ) to (5.023, 2.049), (5.050, 1.979) and (5.050, 1.980), and the SSR falls to 0.0501, 0.03520 and 0.035191 V².
- Compare with the log-linear fit. It gives A = 5.038 V and τ = 1.987 ms: close, but not the least-squares answer for the actual data.
Answer A = 5.050 V and τ = 1.980 ms, with an SSR of 0.035191 V².
Worked example
A line is fitted to 11 points with x = 0, 1, ..., 10 and noise σ = 0.2 on each y. What's the slope's standard error?
Show the solution
- The spread of x.
- The standard error.
Answer about 0.019.
Worked example
Fit a line to (0, 1), (1, 3), (2, 4), (3, 4) and estimate the noise.
Show the solution
- The sums. n = 4, Σx = 6, Σx² = 14, Σy = 12 and Σxy = 23.
- Slope.
- Intercept.
- Residuals. They are −0.5, 0.5, 0.5, −0.5, so SSR = 1.
- Noise. With two fitted parameters the noise variance estimate is a standard deviation of 0.71.
- A hint in the residuals. Their pattern, negative at the ends and positive in the middle, hints that a straight line isn't the right model; with more points, a plot of the residuals would show it.
Answer the line is , and the noise's standard deviation is about 0.71.
Check
Least squares by the normal equations with : about how many digits survive?
Check
What's the intercept of the least-squares line through (0, 1), (1, 3), (2, 9)?
Check
A line is fitted to 6 points at x = 0, 1, 2, ..., each y with noise σ = 0.1. What's the slope's standard error?
Check
For the model with A = 5, τ = 2, what's the Jacobian entry at t = 3?
Check
A fit of 3 parameters to 8 points leaves SSR = 0.035. What's the estimated noise variance σ²?
Check
In Levenberg–Marquardt, a step just increased the SSR. What should happen to λ?
Check
Fit a line to (0, 1), (1, 3), (2, 5), (3, 7).
(a)
What is its slope?
(b)
What is its intercept?
Check
Fit through the origin to the points (1, p) and (2, q).
Step 1
What is k, ?
Type it the way you would write it, like 3sin(2πft) or 3*sin(2*pi*f*t). Angles can be in radians or carry a degree sign, like 45°.
Step 2
What is the sum of squared residuals?
Explain why
Why is solving least squares through the normal equations less accurate than through QR?
Forming squares the condition number: if A's columns are nearly dependent, with , then has , and solving with it loses about 12 digits instead of 6. The information that distinguishes the nearly parallel columns is in the small singular values, which get squared and pushed toward the level of rounding error when is formed. QR works on A directly with orthogonal transformations, which don't amplify errors, so its accuracy is governed by κ(A) alone. The normal equations are fine when A is well-conditioned, which is why they're still used for small, well-posed fits.
Practice
Keep practicing
9 kinds of problem on this topic, dealt one at a time with new numbers every time, for as long as you like.