Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

14. Nonlinear least squares

School of Engineering, Nazarbayev University

Track: advanced  ·  Prerequisites: Chapters 11 to 13

Reading: Bevington & Robinson, ch. 8, 9.1–9.2 and 11.5 · Press et al., Numerical Recipes, ch. 15.4–15.6 · James, Statistical Methods in Experimental Physics, ch. 9

Chapters 11 and 13 fitted models that are linear in the parameters, such as y=a+bx+cx2y = a + bx + cx^2. Then χ2\chi^2 is an exact quadratic function of the parameters, the minimum is found in one step by solving linear equations, and the covariance (ATWA)−1(A^TWA)^{-1} is exact. Many models are not of this kind: a peak whose position and width are to be found, an exponential decay with an unknown rate, a dose–response curve. For these, χ2\chi^2 is minimised iteratively, and three new questions arise: will the iteration find the minimum, is it the right minimum, and does the covariance at the minimum still describe the uncertainty?

The running example is a resonance: a Lorentzian peak on a quadratic background (Bevington, ch. 9.1),

y(x)=b0+b1u+b2u2+H (Γ/2)2(x−x0)2+(Γ/2)2,u=x−5,y(x) = b_0 + b_1 u + b_2 u^2 + \frac{H\,(\Gamma/2)^2}{(x - x_0)^2 + (\Gamma/2)^2}, \qquad u = x - 5 ,

with six parameters: three background coefficients, the peak height HH, position x0x_0 and full width at half maximum Γ\Gamma. The background polynomial is written about the middle of the range to reduce the correlation between its coefficients (Section 13.3). The model is linear in b0,b1,b2,Hb_0, b_1, b_2, H but not in x0x_0 and Γ\Gamma.

The data are simulated with known uncertainties σi\sigma_i, equal to the square root of the expected value, as for counts. Two data sets are used: a strong peak, 150 above a background of about 40, and a weak peak of height 40 on the same background. (Estimating σi\sigma_i from the counts themselves introduces a bias of its own; that is the subject of Chapter 15.)

14.1 Gauss–Newton and Levenberg–Marquardt

Near a trial point p\mathbf p, the model can be linearised, y(xi;p+δ)≈y(xi;p)+∑kJikδky(x_i; \mathbf p + \boldsymbol\delta) \approx y(x_i;\mathbf p) + \sum_k J_{ik}\delta_k, with the Jacobian Jik=∂y(xi)/∂pkJ_{ik} = \partial y(x_i)/\partial p_k. In this approximation the problem is a linear fit for the step δ\boldsymbol\delta, solved by the normal equations

(JTWJ) δ=JTW (y−y(p)),(J^T W J)\,\boldsymbol\delta = J^T W\,(\mathbf y - \mathbf y(\mathbf p)) ,

with W=diag(1/σi2)W = \mathrm{diag}(1/\sigma_i^2). Repeating this step is the Gauss–Newton method. Close to the minimum it converges very fast; far from it the linearisation can be poor, and the step can overshoot so badly that the iteration diverges.

The Levenberg–Marquardt method (Levenberg 1944; Marquardt 1963; Bevington, ch. 8.6) adds a damping term, (JTWJ+λ diag JTWJ) δ=JTW(y−y(p))(J^TWJ + \lambda\,\mathrm{diag}\,J^TWJ)\,\boldsymbol\delta = J^TW(\mathbf y - \mathbf y(\mathbf p)). A large λ\lambda gives a short step along the steepest descent of χ2\chi^2; λ→0\lambda \to 0 gives the Gauss–Newton step. The algorithm decreases λ\lambda after each successful step and increases it when χ2\chi^2 would rise, so it never goes uphill. The cell below implements both in a few lines and starts them from the same rough guess.

start A, Gauss-Newton:        chi2 after 1, 3, 25 steps = 547, 2.98e+07, 2.62e+13
start A, Levenberg-Marquardt: chi2 after 1, 3, 25 steps = 428.3, 134.5, 50.1;  x0 = 5.629, Gamma = 1.182
start B, Levenberg-Marquardt: chi2 after 25 steps = 467.7;  H = 109.8, x0 = 0.157, |Gamma| = 2.344

From start A, Gauss–Newton diverges while Levenberg–Marquardt reaches the minimum in a handful of steps. From start B, Levenberg–Marquardt converges too, but to a different point: a local minimum with a much higher χ2\chi^2, where a broad “peak” sits at the edge of the range and, together with the background polynomial, imitates the rise of the data there. No minimiser can tell a local minimum from the global one by local information alone.

A smaller trap: Γ\Gamma enters the model only as Γ2\Gamma^2, so −Γ-\Gamma fits exactly as well as Γ\Gamma, and an unconstrained fit may return a negative width. The fit is not wrong; report ∣Γ∣|\Gamma|, or write the model in terms of a parameter that cannot change sign, such as ln⁡Γ\ln\Gamma.

Starting values and multistart. The remedy is good starting values and a check. Starting values come from a plot: the peak position from the highest point, the width from where the data fall to half height, the background from the ends of the range. The check is to restart from many different points and confirm that the best minimum is found repeatedly. The cell below compares 200 random starting points with the plot-based guess.

random starting points reaching the global minimum: 36%
chi2 at some of the other minima: [394. 415. 468. 560.]
plot-based guess x0 = 5.67, Gamma = 1.00  ->  chi2 = 50.05 (global minimum 50.05)

Only a minority of random starting points find the global minimum here; the others end in one of many local minima, with the peak at the edge of the range, outside it, or turned into a dip (H<0H < 0). The plot-based guess goes straight to it. In a routine analysis, a fit that converges is not yet a fit that is right: plot the fitted curve over the data, and look at the residuals.

14.2 The result and its covariance

At the minimum, the parameter covariance is estimated as for a linear fit, with the Jacobian in place of the design matrix (Bevington, ch. 8.5):

Cp=(JTWJ)−1.C_p = (J^T W J)^{-1} .

This is the covariance of the linearised problem: it is exact only if the model is linear over the region of parameter space allowed by the data. errprop.nonlinear_fit wraps scipy.optimize.least_squares and returns the parameters, this covariance, χ2\chi^2 and its p-value.

  b0     =   39.272 ± 3.285    (true 40)
  b1     =   -1.561 ± 0.325    (true -1.5)
  b2     =    0.221 ± 0.189    (true 0.25)
  H      =  157.401 ± 7.773    (true 150)
  x0     =    5.629 ± 0.025    (true 5.6)
  Gamma  =    1.182 ± 0.098    (true 1.2)
chi2 = 50.1 for 55 degrees of freedom, p = 0.66
correlation matrix:
[[ 1.    0.28 -0.9  -0.1   0.07 -0.73]
 [ 0.28  1.   -0.28 -0.03 -0.13 -0.25]
 [-0.9  -0.28  1.    0.14 -0.07  0.58]
 [-0.1  -0.03  0.14  1.   -0.02 -0.44]
 [ 0.07 -0.13 -0.07 -0.02  1.   -0.04]
 [-0.73 -0.25  0.58 -0.44 -0.04  1.  ]]

The fit is acceptable (χ2\chi^2 close to the number of degrees of freedom) and every parameter is within 1.2 standard uncertainties of its true value. The correlation matrix carries the physics of the model: the width Γ\Gamma is strongly correlated with the background level b0b_0 and the curvature b2b_2, because a wider peak and a lower background describe the wings of the data almost equally well; it is anticorrelated with the height HH. The position x0x_0 is nearly uncorrelated with everything, because the model is symmetric about it.

14.3 Is the parabola good enough? Δχ2\Delta\chi^2 and profiles

A more general statement of the uncertainty does not rely on the linearisation. For one parameter pkp_k, the profile χmin⁡2(pk)\chi^2_{\min}(p_k) is the minimum of χ2\chi^2 over all other parameters with pkp_k held fixed. The set of pkp_k with

Δχ2=χmin⁡2(pk)−χmin⁡2≤1\Delta\chi^2 = \chi^2_{\min}(p_k) - \chi^2_{\min} \le 1

is a 68 % confidence interval, and Δχ2≤4\Delta\chi^2 \le 4 gives 95 %. For a joint region of two parameters, the threshold is the 68 % quantile of χ2\chi^2 with two degrees of freedom, Δχ2=2.30\Delta\chi^2 = 2.30 (Avni 1976; Bevington, ch. 11.5). When the model is linear, the profile is an exact parabola, and the interval is exactly p^k±σk\hat p_k \pm \sigma_k; any departure from a parabola shows the linearisation failing. This is the least-squares form of the likelihood intervals of Section 7.4, and it is the method used by the MINOS routine of the CERN library MINUIT (James 2006).

strong peak: Gamma = 1.182 ± 0.098;  Delta chi2 = 1 interval [1.090, 1.283] = 1.182 -0.093 +0.100
weak   peak: Gamma = 1.139 ± 0.272;  Delta chi2 = 1 interval [0.882, 1.461] = 1.139 -0.257 +0.322

For the strong peak the two intervals agree to within 5 % of the uncertainty: the parabola is adequate. For the weak peak the width is known only to about 25 %, and the profile is lopsided: the interval extends further upwards than downwards, because the data cannot rule out a broad, low peak blending into the background as easily as a narrow one. Panel (d) of the figure shows both profiles, and panel (c) the joint region of x0x_0 and Γ\Gamma for the weak peak, computed on a grid of profile fits and compared with the ellipse implied by CpC_p.

weak peak: grid of 1221 profile fits computed; minimum Delta chi2 on grid = 0.005

14.4 Derived quantities: the peak area

The quantity of physical interest is often a function of the fitted parameters rather than a parameter itself. For a Lorentzian the area is A=πHΓ/2A = \pi H \Gamma / 2. Three ways of finding its uncertainty can be compared:

  1. propagate with the full covariance, σA2=gTCp g\sigma_A^2 = \mathbf g^T C_p\,\mathbf g with g=∂A/∂p\mathbf g = \partial A/\partial\mathbf p (Section 12.1);

  2. propagate ignoring the correlation of HH and Γ\Gamma;

  3. rewrite the model with AA itself as a parameter and fit again.

Methods 1 and 3 are equivalent to first order (Tellinghuisen 2001): reparametrising the model only changes variables in the same χ2\chi^2 surface. Method 3 is convenient, since the fit then reports the uncertainty and the correlations directly, and it is the natural starting point for a profile interval of AA.

area = 292.3  (true 282.7)
  1. full covariance:          u = 22.08
  2. correlation ignored:      u = 28.22
  3. refit with area as param: A = 292.3 ± 22.08
correlation of H and Gamma: -0.44

The negative correlation of HH and Γ\Gamma (a higher peak is fitted narrower) partly cancels in the product, so ignoring it overstates the uncertainty of the area by about 30 %. The refit reproduces the full propagation exactly.

14.5 Checking coverage by simulation

The final test of any uncertainty is whether it covers the truth at the claimed rate. The cell below repeats the experiment 1000 times for each peak, fits each data set, and records the pull (p^−p)/σp(\hat p - p)/\sigma_p and whether the true width lies inside the Δχ2≤1\Delta\chi^2 \le 1 profile interval.

strong peak, 1000 data sets:
   fraction with |pull| < 1: b0 0.69, b1 0.67, b2 0.69, H 0.68, x0 0.68, Gamma 0.68
   Gamma: pull mean -0.06, sd 1.00;  symmetric interval misses: true value above 0.179, below 0.142
   Gamma, profile interval: covers 0.690;  misses: true value above 0.160, below 0.150
weak peak, 1000 data sets:
   fraction with |pull| < 1: b0 0.66, b1 0.69, b2 0.69, H 0.67, x0 0.69, Gamma 0.65
   Gamma: pull mean -0.25, sd 1.24;  symmetric interval misses: true value above 0.220, below 0.132
   Gamma, profile interval: covers 0.662;  misses: true value above 0.180, below 0.158

For the strong peak, every parameter has pulls with mean near 0 and spread near 1, and both kinds of interval cover close to 68 %, with the misses split roughly evenly between the two sides: the linear theory is adequate. For the weak peak, ±σ\pm\sigma still covers about two-thirds of the cases, but the misses are lopsided: the true width lies above the interval nearly twice as often as below it, and the pulls of Γ\Gamma are wider than 1 and shifted. The profile interval removes most of the lopsidedness, just as the likelihood interval did for the lifetime in Section 7.4, though with so little signal both kinds cover slightly less than 68 %. When the profile and the parabola disagree, quote the asymmetric profile interval and say so.

<Figure size 1012x836 with 4 Axes>

(a) Simulated data for the strong and weak peaks with the fitted curves; dashed: the fitted background of the strong-peak data. (b) χ2\chi^2 during the iteration (dotted: the number of degrees of freedom). Gauss–Newton diverges from start A; Levenberg–Marquardt converges from it, and from start B to a local minimum. (c) Joint 68 % and 95 % regions for x0x_0 and Γ\Gamma of the weak peak: profile contours against the ellipses from CpC_p; the star is the true value. (d) Profile Δχ2\Delta\chi^2 of the width against the parabola of linear theory.

Summary

  • Nonlinear models are fitted iteratively. Levenberg–Marquardt is robust where Gauss–Newton diverges, but neither can distinguish a local minimum from the global one.

  • Take starting values from a plot of the data, and confirm the minimum by restarting from different points; always plot the fit over the data.

  • The covariance (JTWJ)−1(J^TWJ)^{-1} at the minimum is the linearised result. The correlation matrix often reflects the physics of the model and must be kept for derived quantities.

  • The profile Δχ2=1\Delta\chi^2 = 1 interval (2.30 for two parameters jointly) does not depend on the linearisation. Where it differs from ±σ\pm\sigma, quote it.

  • Derived quantities can be obtained by propagation with the full covariance or, equivalently, by reparametrising the model.

  • Check coverage by simulation when the data are weak.

References

  • Y. Avni, “Energy spectra of X-ray clusters of galaxies”, Astrophys. J. 210, 642 (1976).

  • F. James, Statistical Methods in Experimental Physics, 2nd ed. (World Scientific, 2006).

  • K. Levenberg, “A method for the solution of certain non-linear problems in least squares”, Q. Appl. Math. 2, 164 (1944).

  • D. W. Marquardt, “An algorithm for least-squares estimation of nonlinear parameters”, J. Soc. Ind. Appl. Math. 11, 431 (1963).

  • W. H. Press, S. A. Teukolsky, W. T. Vetterling and B. P. Flannery, Numerical Recipes, 3rd ed. (Cambridge University Press, 2007).

  • J. Tellinghuisen, “Statistical error propagation”, J. Phys. Chem. A 105, 3917 (2001).

Exercises

  1. Fit the strong-peak data with the background written in powers of xx instead of u=x−5u = x - 5. Compare the correlation matrices and the number of iterations.

  2. Replace the Lorentzian by a Gaussian of the same width. Which parameter correlations change, and why?

  3. Reduce the peak height step by step from 150 to 20 and plot, against the height, the asymmetry of the profile interval of Γ\Gamma and the fraction of random starting points that find the global minimum.

  4. Find the profile Δχ2=1\Delta\chi^2 = 1 interval of the area for the weak peak, using the reparametrised model, and compare it with ±σA\pm\sigma_A.

  5. Derive the Gauss–Newton step from the condition ∂χ2/∂δ=0\partial\chi^2/\partial\boldsymbol\delta = 0 for the linearised model.