A1337 of 55
0 XP
  1. Hook
  2. Play
  3. Predict
  4. Name it
  5. Check
  6. Explain

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 R25? 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 Ae−t/τ. 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 ATAx=ATb, and κ(A)=106. About how many digits can you trust?

Name it

Linear least squares

For a model linear in its parameters, minx⁡‖Ax−b‖2. Three ways to solve it:

MethodConditioningNotes
normal equations ATAx=ATbκ(A)2cheap; fine when κ is small
QR: A=QR, solve Rx=QTbκ(A)the standard choice
SVDκ(A)handles rank deficiency; most expensive

For a straight line through (xi,yi): slope =∑(xi−x¯)(yi−y¯)∑(xi−x¯)2, intercept =y¯−slope⋅x¯, and with noise σ on each y the slope's standard error is σ∑(xi−x¯)2.

Nonlinear least squares

For residuals ri(p)=yi−f(ti;p) with Jacobian Jij=∂f(ti)/∂pj:

  • Gauss–Newton: solve the linear least-squares problem JΔ≈r (that is, JTJΔ=JTr, preferably by QR) and set p←p+Δ. Near a good fit it converges almost quadratically.
  • Levenberg–Marquardt: solve (JTJ+λD)Δ=JTr. 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 σ2(JTJ)−1, with σ² estimated as SSR/(m − n) for m points and n parameters.

Don't linearize by transforming the data

Fitting a line to ln⁡v 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 Ae−t/τ to the eight points above by Gauss–Newton from A = 4 V, τ = 1.5 ms.

Show the solution
  1. The start. At A = 4 V and τ = 1.5 ms the SSR is 5.663 V².
  2. 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².
  3. 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
  1. The spread of x. ∑(xi−x¯)2=n(n2−1)12=11×12012=110
  2. The standard error. 0.2/110=0.019

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
  1. The sums. n = 4, Σx = 6, Σx² = 14, Σy = 12 and Σxy = 23.
  2. Slope. m=4×23−6×124×14−36=2020=1
  3. Intercept. b=12−64=1.5
  4. Residuals. They are −0.5, 0.5, 0.5, −0.5, so SSR = 1.
  5. Noise. With two fitted parameters the noise variance estimate is 14−2=0.5 a standard deviation of 0.71.
  6. 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 y=1.5+x, and the noise's standard deviation is about 0.71.

Check

Least squares by the normal equations with κ(A)=105: 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 Ae−t/τ with A = 5, τ = 2, what's the Jacobian entry ∂f/∂τ 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 y=kx through the origin to the points (1, p) and (2, q).

Step 1

What is k, ∑xy∑x2?

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

Explain why

Why is solving least squares through the normal equations less accurate than through QR?

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.