Search

Search papers and lessons

Nonlinear Least Squares

Level 3 · 8 min read

Nonlinear least squares fits a model whose parameters enter nonlinearly by repeatedly linearising the residuals and solving a small least-squares problem, with Levenberg-Marquardt damping to keep each step safe.

On this page

  • Colour = topic mix
  • Calculus
  • Linear algebra
  • Probability
  • Classical models
  • Losses & regularization
  • Discrete math
  • Sign in to see your progress

TL;DR

Many models are not straight lines in their parameters: an exponential decay, a sine wave with a free frequency, a physical law with a rate constant. Nonlinear least squares finds the parameters that make the sum of squared errors smallest, even though no closed-form answer exists. The usual method linearises the errors at the current guess, solves an ordinary least-squares problem for the step, and repeats. Levenberg-Marquardt adds damping so that a bad step gets shrunk instead of accepted.

Where you'll see it: curve fitting in science and engineering, camera calibration and bundle adjustment in computer vision, and the Gauss-Newton and Levenberg-Marquardt solvers behind many libraries.

Intuition

Linear least squares has one bowl-shaped error surface, and a single linear solve finds its bottom. Nonlinear models give a bumpy surface with many valleys. You cannot solve for the bottom in one step, so you walk downhill. Each step replaces the hard problem with an easy one: near the current guess, the model looks like a straight line, so the error surface looks like a bowl. Solve for the bottom of that local bowl, move there, and repeat.

Why not just run gradient descent? It works, but the path zig-zags when the valley is narrow in some directions and wide in others. Least squares has extra structure: the error is a sum of squares of residuals. That structure lets the method use the residuals' slopes directly, which is what the normal equations below do. The result often converges in a handful of steps where gradient descent needs hundreds.

flowchart TD
  A[Start with a guess theta] --> B[Compute residuals r and Jacobian J at theta]
  B --> C[Solve J-transpose J plus lambda I, times delta, equals minus J-transpose r]
  C --> D{Sum of squares went down?}
  D -- yes --> E[Accept step, lower lambda]
  E --> B
  D -- no --> F[Reject step, raise lambda]
  F --> C

The math

Let the data be (xi,yi)(x_i, y_i) for i=1,…,mi = 1, \dots, m, and let the model be f(x;θ)f(x; \boldsymbol{\theta}) with parameters θ∈Rn\boldsymbol{\theta} \in \mathbb{R}^n. The residuals are

ri(θ)=yi−f(xi;θ),S(θ)=∑i=1mri(θ)2=∥r(θ)∥2.r_i(\boldsymbol{\theta}) = y_i - f(x_i; \boldsymbol{\theta}), \qquad S(\boldsymbol{\theta}) = \sum_{i=1}^{m} r_i(\boldsymbol{\theta})^2 = \lVert \mathbf{r}(\boldsymbol{\theta}) \rVert^2.

The goal is to minimise SS. The Jacobian J\mathbf{J} of the residual vector is the m×nm \times n matrix with entries Jij=∂ri/∂θjJ_{ij} = \partial r_i / \partial \theta_j. (See Jacobian Matrix for the general definition.)

Linearising the residuals

Taylor's theorem (see Taylor Series) gives, for a small step δ\boldsymbol{\delta},

r(θ+δ)≈r(θ)+J δ.\mathbf{r}(\boldsymbol{\theta} + \boldsymbol{\delta}) \approx \mathbf{r}(\boldsymbol{\theta}) + \mathbf{J}\,\boldsymbol{\delta}.

Choosing δ\boldsymbol{\delta} to minimise the squared norm of the right-hand side is an ordinary linear least-squares problem, the same one solved in Linear Regression. Its normal equations are

J⊤J δ=−J⊤r(θ).\mathbf{J}^\top \mathbf{J}\,\boldsymbol{\delta} = -\mathbf{J}^\top \mathbf{r}(\boldsymbol{\theta}).

Solving this and setting θ←θ+δ\boldsymbol{\theta} \leftarrow \boldsymbol{\theta} + \boldsymbol{\delta} is the Gauss-Newton method. It needs no second derivatives. It ignores the term that involves the residuals times the curvature of the model, which is small when the model fits well and the residuals are small.

Levenberg-Marquardt damping

Gauss-Newton can overshoot when the linear approximation is poor. Levenberg-Marquardt replaces the normal equations with

(J⊤J+λI)δ=−J⊤r(θ),λ>0.\left(\mathbf{J}^\top \mathbf{J} + \lambda \mathbf{I}\right)\boldsymbol{\delta} = -\mathbf{J}^\top \mathbf{r}(\boldsymbol{\theta}), \qquad \lambda \gt 0.

For large λ\lambda the step becomes δ≈−J⊤r/λ\boldsymbol{\delta} \approx -\mathbf{J}^\top \mathbf{r}/\lambda, a short gradient-descent step. For small λ\lambda it approaches Gauss-Newton. The algorithm accepts a step when SS falls and then lowers λ\lambda; otherwise it raises λ\lambda and tries again. Marquardt's variant replaces λI\lambda \mathbf{I} with λdiag⁡(J⊤J)\lambda \operatorname{diag}(\mathbf{J}^\top \mathbf{J}), which scales the damping to each parameter.

Convergence

Near a minimum where the residuals are small, Gauss-Newton converges quadratically in the zero-residual case and roughly linearly when residuals are large. Neither statement is a guarantee for every starting point. The method finds a local minimum, and the answer depends on the starting guess.

Worked examples

Example
One Gauss-Newton step with an overshoot

Fit the one-parameter model f(x;θ)=eθxf(x; \theta) = e^{\theta x} to two points, (x,y)=(1,2)(x, y) = (1, 2) and (2,3)(2, 3). Start at θ0=0\theta_0 = 0.

  1. At θ0=0\theta_0 = 0: f(1;0)=1f(1; 0) = 1 and f(2;0)=1f(2; 0) = 1.
  2. Residuals: r1=2−1=1r_1 = 2 - 1 = 1 and r2=3−1=2r_2 = 3 - 1 = 2. Sum of squares S=1+4=5S = 1 + 4 = 5.
  3. Jacobian: ∂ri/∂θ=−xieθxi\partial r_i / \partial \theta = -x_i e^{\theta x_i}, so at θ=0\theta = 0 the entries are J=(−1,−2)⊤J = (-1, -2)^\top.
  4. Normal equation: J⊤J=1+4=5\mathbf{J}^\top \mathbf{J} = 1 + 4 = 5 and J⊤r=(−1)(1)+(−2)(2)=−5\mathbf{J}^\top \mathbf{r} = (-1)(1) + (-2)(2) = -5.
  5. Gauss-Newton step: δ=−(−5)/5=1\delta = -(-5)/5 = 1, so θ1=1\theta_1 = 1.
  6. Check the new point: f(1;1)=e≈2.718f(1; 1) = e \approx 2.718 and f(2;1)=e2≈7.389f(2; 1) = e^2 \approx 7.389. Residuals are about −0.718-0.718 and −4.389-4.389, so S≈0.516+19.26=19.78S \approx 0.516 + 19.26 = 19.78. The full step made the fit worse than the start (S=5S = 5).
  7. Damped step with λ=5\lambda = 5: (5+5) δ=5(5 + 5)\,\delta = 5, so δ=0.5\delta = 0.5 and θ1=0.5\theta_1 = 0.5.
  8. Check: f(1;0.5)=e0.5≈1.6487f(1; 0.5) = e^{0.5} \approx 1.6487 and f(2;0.5)=e≈2.7183f(2; 0.5) = e \approx 2.7183. Residuals are about 0.35130.3513 and 0.28170.2817, so S≈0.1234+0.0794=0.2028S \approx 0.1234 + 0.0794 = 0.2028, down from 5. The damped step is accepted.

Interpretation: the linear model promised a drop to zero at θ=1\theta = 1, but the true curve grows far faster than its tangent, so the step overshot. Damping cut the step in half and the fit improved.

Example
A plain Gauss-Newton step near the fit

Keep the same model and data, but now start at θ0=0.5\theta_0 = 0.5, the point the damped step reached.

  1. At θ0=0.5\theta_0 = 0.5: f(1)=1.6487f(1) = 1.6487 and f(2)=2.7183f(2) = 2.7183, residuals r=(0.3513,0.2817)r = (0.3513, 0.2817), and S=0.2028S = 0.2028.
  2. Jacobian entries: J1=−1⋅e0.5=−1.6487J_1 = -1 \cdot e^{0.5} = -1.6487 and J2=−2⋅e1=−5.4366J_2 = -2 \cdot e^{1} = -5.4366.
  3. J⊤J=1.64872+5.43662=2.7181+29.557=32.275\mathbf{J}^\top \mathbf{J} = 1.6487^2 + 5.4366^2 = 2.7181 + 29.557 = 32.275.
  4. J⊤r=(−1.6487)(0.3513)+(−5.4366)(0.2817)=−0.5792−1.5315=−2.1107\mathbf{J}^\top \mathbf{r} = (-1.6487)(0.3513) + (-5.4366)(0.2817) = -0.5792 - 1.5315 = -2.1107.
  5. Gauss-Newton step: δ=2.1107/32.275≈0.0654\delta = 2.1107 / 32.275 \approx 0.0654, so θ1≈0.5654\theta_1 \approx 0.5654.
  6. At θ1≈0.5654\theta_1 \approx 0.5654: f(1)≈1.7601f(1) \approx 1.7601 and f(2)≈3.0981f(2) \approx 3.0981, so r≈(0.2399,−0.0981)r \approx (0.2399, -0.0981) and S≈0.0576+0.0096=0.0672S \approx 0.0576 + 0.0096 = 0.0672, down from 0.20280.2028. The next step would be smaller again, as the residuals shrink.

Interpretation: near a fit, the linear model is accurate and plain Gauss-Newton steps are small and reliable. The damping is needed only at the start, when the guess is far off. The numbers here only show the early steps of one local search.

Tradeoffs and failure modes

SituationProblemBetter choice
Good starting guess, small residualsNone; Gauss-Newton converges fastGauss-Newton
Poor starting guess, large residualsFull steps overshoot and S risesLevenberg-Marquardt
Residuals large at the solution (poor fit)Ignored curvature term matters; convergence slowsUse the full Hessian (Newton) or a trust-region method
Many parameters, expensive JacobianEach step builds and solves an n-by-n systemQuasi-Newton or finite-difference Jacobians, with care
Several minimaResult depends on the startMulti-start with random initial guesses
Parameters with very different scalesDamping treats them unequallyMarquardt scaling, or reparameterise

Two practical traps. First, an exponential or power-law model can fit a few points almost exactly and say nothing about the rest, so check residuals on held-out data. Second, a Jacobian computed by finite differences is only as accurate as the step size allows; an analytic Jacobian is better when available.

History

Least squares itself is usually credited to Legendre and Gauss, and the idea of replacing a nonlinear model by its linearisation at the current guess, giving what is now called Gauss-Newton, is a natural extension of it. Gauss-Newton can overshoot or fail when the linear approximation is poor. Kenneth Levenberg proposed adding a damping term to the normal equations in 1944, and Donald Marquardt independently arrived at a similar damped method in 1963, adding the diagonal scaling. The combined Levenberg-Marquardt method became a default because it is simple to implement and robust when the starting guess is far from the solution.

Check yourself

QuestionWhy is the Gauss-Newton step the solution of a linear least-squares problem?Show answer

Replace each residual with its linear approximation r+Jδ\mathbf{r} + \mathbf{J}\boldsymbol{\delta}. The sum of squares of that expression is a quadratic in δ\boldsymbol{\delta}. Setting its gradient to zero gives the normal equations J⊤Jδ=−J⊤r\mathbf{J}^\top \mathbf{J}\boldsymbol{\delta} = -\mathbf{J}^\top \mathbf{r}, which is the same form as linear regression with design matrix J\mathbf{J}. So each step is an ordinary least-squares solve.

QuestionFor the example with θ=0\theta = 0, the residuals are (1,2)(1, 2) and the Jacobian is (−1,−2)⊤(-1, -2)^\top. Compute the Gauss-Newton step and the new sum of squares if you took it.Show answer
  1. J⊤J=5\mathbf{J}^\top \mathbf{J} = 5 and J⊤r=−5\mathbf{J}^\top \mathbf{r} = -5.
  2. δ=−(−5)/5=1\delta = -(-5)/5 = 1.
  3. At θ=1\theta = 1 the residuals are about −0.718-0.718 and −4.389-4.389.
  4. The new sum of squares is about 19.7819.78, up from 55. The full step overshoots because the model's slope at θ=0\theta = 0 is much smaller than its slope at θ=1\theta = 1.
QuestionWhat does the damping parameter λ\lambda do when it becomes very large?Show answer

The matrix J⊤J+λI\mathbf{J}^\top\mathbf{J} + \lambda\mathbf{I} is dominated by λI\lambda\mathbf{I}, so δ\boldsymbol{\delta} is roughly −J⊤r/λ-\mathbf{J}^\top\mathbf{r}/\lambda. That is a small step in the steepest-descent direction. The method behaves like gradient descent with a small learning rate, which is safe far from the minimum, and it becomes Gauss-Newton as λ\lambda shrinks.

QuestionYou fit y=aebxy = a e^{bx} to ten points and the solver reports a tiny sum of squares but the residuals alternate in sign along xx. What could explain this, and what would you check?Show answer

A tiny sum of squares only means the curve passes near the points, not that the model is right. Alternating residuals suggest the model is missing structure, such as a second term or noise that is not zero mean. Check the residuals against xx with a plot, try a richer model, and compare on held-out points. Also confirm the solver converged to a true minimum by checking that the gradient J⊤r\mathbf{J}^\top\mathbf{r} is near zero, and rerun from several starting values.

Sources