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 for , and let the model be with parameters . The residuals are
The goal is to minimise . The Jacobian of the residual vector is the matrix with entries . (See Jacobian Matrix for the general definition.)
Linearising the residuals
Taylor's theorem (see Taylor Series) gives, for a small step ,
Choosing 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
Solving this and setting 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
For large the step becomes , a short gradient-descent step. For small it approaches Gauss-Newton. The algorithm accepts a step when falls and then lowers ; otherwise it raises and tries again. Marquardt's variant replaces with , 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
Fit the one-parameter model to two points, and . Start at .
- At : and .
- Residuals: and . Sum of squares .
- Jacobian: , so at the entries are .
- Normal equation: and .
- Gauss-Newton step: , so .
- Check the new point: and . Residuals are about and , so . The full step made the fit worse than the start ().
- Damped step with : , so and .
- Check: and . Residuals are about and , so , down from 5. The damped step is accepted.
Interpretation: the linear model promised a drop to zero at , but the true curve grows far faster than its tangent, so the step overshot. Damping cut the step in half and the fit improved.
Keep the same model and data, but now start at , the point the damped step reached.
- At : and , residuals , and .
- Jacobian entries: and .
- .
- .
- Gauss-Newton step: , so .
- At : and , so and , down from . 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
| Situation | Problem | Better choice |
|---|---|---|
| Good starting guess, small residuals | None; Gauss-Newton converges fast | Gauss-Newton |
| Poor starting guess, large residuals | Full steps overshoot and S rises | Levenberg-Marquardt |
| Residuals large at the solution (poor fit) | Ignored curvature term matters; convergence slows | Use the full Hessian (Newton) or a trust-region method |
| Many parameters, expensive Jacobian | Each step builds and solves an n-by-n system | Quasi-Newton or finite-difference Jacobians, with care |
| Several minima | Result depends on the start | Multi-start with random initial guesses |
| Parameters with very different scales | Damping treats them unequally | Marquardt 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 . The sum of squares of that expression is a quadratic in . Setting its gradient to zero gives the normal equations , which is the same form as linear regression with design matrix . So each step is an ordinary least-squares solve.
QuestionFor the example with , the residuals are and the Jacobian is . Compute the Gauss-Newton step and the new sum of squares if you took it.Show answer
- and .
- .
- At the residuals are about and .
- The new sum of squares is about , up from . The full step overshoots because the model's slope at is much smaller than its slope at .
QuestionWhat does the damping parameter do when it becomes very large?Show answer
The matrix is dominated by , so is roughly . 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 shrinks.
QuestionYou fit to ten points and the solver reports a tiny sum of squares but the residuals alternate in sign along . 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 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 is near zero, and rerun from several starting values.
Sources
- Non-linear least squares (Wikipedia)
- Levenberg-Marquardt algorithm (Wikipedia)
- Nocedal and Wright, Numerical Optimization, chapter 10 (Gauss-Newton and Levenberg-Marquardt). Publisher page