# Levenberg–Marquardt algorithm

In mathematics and computing, the **Levenberg–Marquardt algorithm** (**LMA** or just **LM**), also known as the **damped least-squares** (**DLS**) method, is used to solve non-linear least squares problems. These minimization problems arise especially in least squares curve fitting.

The LMA is used in many software applications for solving generic curve-fitting problems. However, as with many fitting algorithms, the LMA finds only a local minimum, which is not necessarily the global minimum. The LMA interpolates between the Gauss–Newton algorithm (GNA) and the method of gradient descent. The LMA is more robust than the GNA, which means that in many cases it finds a solution even if it starts very far off the final minimum. For well-behaved functions and reasonable starting parameters, the LMA tends to be a bit slower than the GNA. LMA can also be viewed as Gauss–Newton using a trust region approach.

The algorithm was first published in 1944 by Kenneth Levenberg,^{[1]} while working at the Frankford Army Arsenal. It was rediscovered in 1963 by Donald Marquardt,^{[2]} who worked as a statistician at DuPont, and independently by Girard,^{[3]} Wynne^{[4]} and Morrison.^{[5]}

## Contents

## The problem[edit]

The primary application of the Levenberg–Marquardt algorithm is in the least-squares curve fitting problem: given a set of empirical datum pairs of independent and dependent variables, find the parameters of the model curve so that the sum of the squares of the deviations is minimized:

- which is assumed to be non-empty.

## The solution[edit]

Like other numeric minimization algorithms, the Levenberg–Marquardt algorithm is an iterative procedure. To start a minimization, the user has to provide an initial guess for the parameter vector . In cases with only one minimum, an uninformed standard guess like will work fine; in cases with multiple minima, the algorithm converges to the global minimum only if the initial guess is already somewhat close to the final solution.

In each iteration step, the parameter vector is replaced by a new estimate . To determine , the function is approximated by its linearization:

where

is the gradient (row-vector in this case) of with respect to .

The sum of square deviations has its minimum at a zero gradient with respect to . The above first-order approximation of gives

or in vector notation,

Taking the derivative of with respect to and setting the result to zero gives

where is the Jacobian matrix, whose -th row equals , and where and are vectors with -th component and respectively. This is a set of linear equations, which can be solved for .

Levenberg's contribution is to replace this equation by a "damped version":

where is the identity matrix, giving as the increment to the estimated parameter vector .

The (non-negative) damping factor is adjusted at each iteration. If reduction of is rapid, a smaller value can be used, bringing the algorithm closer to the Gauss–Newton algorithm, whereas if an iteration gives insufficient reduction in the residual, can be increased, giving a step closer to the gradient-descent direction. Note that the gradient of with respect to equals . Therefore, for large values of , the step will be taken approximately in the direction of the gradient. If either the length of the calculated step or the reduction of sum of squares from the latest parameter vector fall below predefined limits, iteration stops, and the last parameter vector is considered to be the solution.

Levenberg's algorithm has the disadvantage that if the value of damping factor is large, inverting is not used at all. R. Fletcher provided the insight that we can scale each component of the gradient according to the curvature, so that there is larger movement along the directions where the gradient is smaller. This avoids slow convergence in the direction of small gradient. Therefore, Fletcher in his 1971 paper *A modified Marquardt subroutine for non-linear least squares* replaced the identity matrix with the diagonal matrix consisting of the diagonal elements of , thus making the solution scale invariant:

A similar damping factor appears in Tikhonov regularization, which is used to solve linear ill-posed problems, as well as in ridge regression, an estimation technique in statistics.

### Choice of damping parameter[edit]

Various more or less heuristic arguments have been put forward for the best choice for the damping parameter . Theoretical arguments exist showing why some of these choices guarantee local convergence of the algorithm; however, these choices can make the global convergence of the algorithm suffer from the undesirable properties of steepest descent, in particular, very slow convergence close to the optimum.

The absolute values of any choice depend on how well-scaled the initial problem is. Marquardt recommended starting with a value and a factor . Initially setting and computing the residual sum of squares after one step from the starting point with the damping factor of and secondly with . If both of these are worse than the initial point, then the damping is increased by successive multiplication by until a better point is found with a new damping factor of for some .

If use of the damping factor results in a reduction in squared residual, then this is taken as the new value of (and the new optimum location is taken as that obtained with this damping factor) and the process continues; if using resulted in a worse residual, but using resulted in a better residual, then is left unchanged and the new optimum is taken as the value obtained with as damping factor.

## Example[edit]

In this example we try to fit the function using the Levenberg–Marquardt algorithm implemented in GNU Octave as the *leasqr* function. The graphs show progressively better fitting for the parameters , used
in the initial curve. Only when the parameters in the last graph are chosen closest to the original, are the curves fitting exactly. This equation
is an example of very sensitive initial conditions for the Levenberg–Marquardt algorithm. One reason for this sensitivity is the existence of multiple minima — the function has minima at parameter value and .

## See also[edit]

- Trust region
- Nelder–Mead method (aka simplex)
- Variants of the Levenberg–Marquardt algorithm have also been used for solving nonlinear systems of equations.
^{[6]}

## References[edit]

**^**Levenberg, Kenneth (1944). "A Method for the Solution of Certain Non-Linear Problems in Least Squares".*Quarterly of Applied Mathematics*.**2**(2): 164–168. doi:10.1090/qam/10666.**^**Marquardt, Donald (1963). "An Algorithm for Least-Squares Estimation of Nonlinear Parameters".*SIAM Journal on Applied Mathematics*.**11**(2): 431–441. doi:10.1137/0111030.**^**Girard, André (1958). "Excerpt from*Revue d'optique théorique et instrumentale*".*Rev. Opt*.**37**: 225–241, 397–424.**^**Wynne, C. G. (1959). "Lens Designing by Electronic Digital Computer: I".*Proc. Phys. Soc. Lond*.**73**(5): 777–787. Bibcode:1959PPS....73..777W. doi:10.1088/0370-1328/73/5/310.**^**Morrison, David D. (1960). "Methods for nonlinear least squares problems and convergence proofs".*Proceedings of the Jet Propulsion Laboratory Seminar on Tracking Programs and Orbit Determination*: 1–9.**^**Kanzow, Christian; Yamashita, Nobuo; Fukushima, Masao (2004). "Levenberg–Marquardt methods with strong local convergence properties for solving nonlinear equations with convex constraints".*Journal of Computational and Applied Mathematics*.**172**(2): 375–397. doi:10.1016/j.cam.2004.02.013.

## Further reading[edit]

- Moré, Jorge J.; Sorensen, Daniel C. (1983). "Computing a Trust-Region Step".
*SIAM J. Sci. Stat. Comput*.**4**(4): 553–572. doi:10.1137/0904038. - Gill, Philip E.; Murray, Walter (1978). "Algorithms for the solution of the nonlinear least-squares problem".
*SIAM Journal on Numerical Analysis*.**15**(5): 977–992. Bibcode:1978SJNA...15..977G. doi:10.1137/0715063. - Pujol, Jose (2007). "The solution of nonlinear inverse problems and the Levenberg-Marquardt method".
*Geophysics*. SEG.**72**(4): W1–W16. Bibcode:2007Geop...72W...1P. doi:10.1190/1.2732552. - Nocedal, Jorge; Wright, Stephen J. (2006).
*Numerical Optimization*(2nd ed.). Springer. ISBN 978-0-387-30303-1.

## External links[edit]

### Descriptions[edit]

- Detailed description of the algorithm can be found in Numerical Recipes in C, Chapter 15.5: Nonlinear models
- C. T. Kelley,
*Iterative Methods for Optimization*, SIAM Frontiers in Applied Mathematics, no 18, 1999, ISBN 0-89871-433-8. Online copy - History of the algorithm in SIAM news
- A tutorial by Ananth Ranganathan
- Methods for Non-Linear Least Squares Problems by K. Madsen, H. B. Nielsen, O. Tingleff is a tutorial discussing nonlinear least-squares in general and the Levenberg–Marquardt method in particular
- T. Strutz:
*Data Fitting and Uncertainty (A practical introduction to weighted least squares and beyond).*2nd edition, Springer Vieweg, 2016, ISBN 978-3-658-11455-8. - H. P. Gavin,
*The Levenberg-Marquardt method for nonlinear least squares curve-fitting problems*(MATLAB implementation included)

### Implementations[edit]

Levenberg-Marquardt is a built-in algorithm in SciPy, GNU Octave, Scilab, Mathematica , Matlab, NeuroSolutions, Origin, Fityk, IGOR Pro, LabVIEW and SAS numerical computing environments. There also exist numerous software libraries which allow to use LM algorithm in standalone applications. Some of them support only basic unconstrained optimization, whilst other ones support different combinations of box and linear constraints.

Box and linearly constrained implementations:

- ALGLIB has box and linearly constrained implementation of improved LM in C# and C++. Improved algorithm takes less time to converge and can use either Jacobian or exact Hessian.
- Artelys Knitro is a non-linear solver with an implementation of the box-constrained Levenberg–Marquardt algorithm. It is written in C and has interfaces to C++/C#/Java/Python/MATLAB/R.
- ceres is a non-linear minimization library with an implementation of the box-constrained Levenberg–Marquardt algorithm. It is written in C++ and uses eigen.
- IDL, add-on MPFIT supports box constraints.
- levmar is an implementation in C/C++ with support for box and general linear constraints, distributed under the GNU General Public License.
- levmar includes a MEX file interface for MATLAB.
- Perl (PDL), python, Haskell and .NET interfaces to levmar are available: see PDL::Fit::Levmar or PDL::Fit::LM, levmar (for python), HackageDB levmar and LevmarSharp.

- R (programming language) has the minpack.lm package (box constrained).

Unconstrained implementations:

- The oldest implementation still in use is lmdif, from MINPACK, in Fortran, in the public domain. See also:
- lmfit, a self-contained C implementation of the MINPACK algorithm, with an easy-to-use wrapper for curve fitting, liberal licence (freeBSD).
- eigen, a C++ linear-algebra library, includes an adaptation of the minpack algorithm in the "NonLinearOptimization" module.
- The GNU Scientific Library has a C interface to MINPACK.
- C/C++ Minpack includes the Levenberg–Marquardt algorithm.
- Python library scipy, module
`scipy.optimize.leastsq`

provides wrapper for the MINPACK routines.

- sparseLM is a C implementation aimed at minimizing functions with large, arbitrarily sparse Jacobians. Includes a MATLAB MEX interface. Unconstrained only.
- SMarquardt.m is a stand-alone routine for Matlab or Octave.
- InMin library contains a C++ implementation of the algorithm based on the eigen C++ linear-algebra library. It has a pure C-language API, as well as a Python binding.
- NMath has an implementation for the .NET Framework.
- gnuplot uses its own implementation gnuplot.info.
- Java programming language implementations: 1) Javanumerics, 2) Apache Commons Math, 3) finmath lib.
- OOoConv implements the L–M algorithm as an OpenOffice.org Calc spreadsheet.
- LMAM/OLMAM Matlab toolbox implements Levenberg–Marquardt with adaptive momentum for training feedforward neural networks.
- GADfit is a Fortran implementation of global fitting based on a modified Levenberg–Marquardt. Uses automatic differentiation. Allows fitting functions of arbitrary complexity, including integrals.