Levenberg–Marquardt algorithm

From HandWiki
Short description: Algorithm used to solve non-linear least squares problems

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 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 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]

The LMA is used in many software applications for solving generic curve-fitting problems. By using the Gauss–Newton algorithm it often converges faster than first-order methods.[6] However, like other iterative optimization algorithms, the LMA finds only a local minimum, which is not necessarily the global minimum.

The problem

The primary application of the Levenberg–Marquardt algorithm is in the least-squares curve fitting problem: given a set of [math]\displaystyle{ m }[/math] empirical pairs [math]\displaystyle{ \left (x_i, y_i\right ) }[/math] of independent and dependent variables, find the parameters [math]\displaystyle{ \boldsymbol\beta }[/math] of the model curve [math]\displaystyle{ f\left (x, \boldsymbol\beta\right ) }[/math] so that the sum of the squares of the deviations [math]\displaystyle{ S\left (\boldsymbol\beta\right ) }[/math] is minimized:

[math]\displaystyle{ \hat{\boldsymbol\beta} \in \operatorname{argmin}\limits_{\boldsymbol\beta} S\left (\boldsymbol\beta\right ) \equiv \operatorname{argmin}\limits_{\boldsymbol\beta} \sum_{i=1}^m \left [y_i - f\left (x_i, \boldsymbol\beta\right )\right ]^2, }[/math] which is assumed to be non-empty.

The solution

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 [math]\displaystyle{ \boldsymbol\beta }[/math]. In cases with only one minimum, an uninformed standard guess like [math]\displaystyle{ \boldsymbol\beta^\text{T} = \begin{pmatrix}1,\ 1,\ \dots,\ 1\end{pmatrix} }[/math] 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 [math]\displaystyle{ \boldsymbol\beta }[/math] is replaced by a new estimate [math]\displaystyle{ \boldsymbol\beta + \boldsymbol\delta }[/math]. To determine [math]\displaystyle{ \boldsymbol\delta }[/math], the function [math]\displaystyle{ f\left (x_i, \boldsymbol\beta + \boldsymbol\delta\right ) }[/math] is approximated by its linearization:

[math]\displaystyle{ f\left (x_i, \boldsymbol\beta + \boldsymbol\delta\right ) \approx f\left (x _i, \boldsymbol\beta\right ) + \mathbf J_i \boldsymbol\delta, }[/math]


[math]\displaystyle{ \mathbf J_i = \frac{\partial f\left (x_i, \boldsymbol\beta\right )}{\partial \boldsymbol\beta} }[/math]

is the gradient (row-vector in this case) of [math]\displaystyle{ f }[/math] with respect to [math]\displaystyle{ \boldsymbol\beta }[/math].

The sum [math]\displaystyle{ S\left (\boldsymbol\beta\right ) }[/math] of square deviations has its minimum at a zero gradient with respect to [math]\displaystyle{ \boldsymbol\beta }[/math]. The above first-order approximation of [math]\displaystyle{ f\left (x_i, \boldsymbol\beta + \boldsymbol\delta\right ) }[/math] gives

[math]\displaystyle{ S\left (\boldsymbol\beta + \boldsymbol\delta\right ) \approx \sum_{i=1}^m \left [y_i - f\left (x_i, \boldsymbol\beta\right ) - \mathbf J_i \boldsymbol\delta\right ]^2, }[/math]

or in vector notation,

[math]\displaystyle{ \begin{align} S\left (\boldsymbol\beta + \boldsymbol\delta\right ) &\approx \left \|\mathbf y - \mathbf f\left (\boldsymbol\beta\right ) - \mathbf J\boldsymbol\delta\right \|^2\\ &= \left [\mathbf y - \mathbf f\left (\boldsymbol\beta\right ) - \mathbf J\boldsymbol\delta \right ]^{\mathrm T}\left [\mathbf y - \mathbf f\left (\boldsymbol\beta\right ) - \mathbf J\boldsymbol\delta\right ]\\ &= \left [\mathbf y - \mathbf f\left (\boldsymbol\beta\right )\right ]^{\mathrm T}\left [\mathbf y - \mathbf f\left (\boldsymbol\beta\right )\right ] - \left [\mathbf y - \mathbf f\left (\boldsymbol\beta\right )\right ]^{\mathrm T} \mathbf J \boldsymbol\delta - \left (\mathbf J \boldsymbol\delta\right )^{\mathrm T} \left [\mathbf y - \mathbf f\left (\boldsymbol\beta\right )\right ] + \boldsymbol\delta^{\mathrm T} \mathbf J^{\mathrm T} \mathbf J \boldsymbol\delta\\ &= \left [\mathbf y - \mathbf f\left (\boldsymbol\beta\right )\right ]^{\mathrm T}\left [\mathbf y - \mathbf f\left (\boldsymbol\beta\right )\right ] - 2\left [\mathbf y - \mathbf f\left (\boldsymbol\beta\right )\right ]^{\mathrm T} \mathbf J \boldsymbol\delta + \boldsymbol\delta^{\mathrm T} \mathbf J^{\mathrm T} \mathbf J\boldsymbol\delta. \end{align} }[/math]

Taking the derivative of this approximation of [math]\displaystyle{ S\left (\boldsymbol\beta + \boldsymbol\delta\right ) }[/math] with respect to [math]\displaystyle{ \boldsymbol\delta }[/math] and setting the result to zero gives

[math]\displaystyle{ \left (\mathbf J^{\mathrm T} \mathbf J\right )\boldsymbol\delta = \mathbf J^{\mathrm T}\left [\mathbf y - \mathbf f\left (\boldsymbol\beta\right )\right ], }[/math]

where [math]\displaystyle{ \mathbf J }[/math] is the Jacobian matrix, whose [math]\displaystyle{ i }[/math]-th row equals [math]\displaystyle{ \mathbf J_i }[/math], and where [math]\displaystyle{ \mathbf f\left (\boldsymbol\beta\right ) }[/math] and [math]\displaystyle{ \mathbf y }[/math] are vectors with [math]\displaystyle{ i }[/math]-th component [math]\displaystyle{ f\left (x_i, \boldsymbol\beta\right ) }[/math] and [math]\displaystyle{ y_i }[/math] respectively. The above expression obtained for [math]\displaystyle{ \boldsymbol\beta }[/math] comes under the Gauss–Newton method. The Jacobian matrix as defined above is not (in general) a square matrix, but a rectangular matrix of size [math]\displaystyle{ m \times n }[/math], where [math]\displaystyle{ n }[/math] is the number of parameters (size of the vector [math]\displaystyle{ \boldsymbol\beta }[/math]). The matrix multiplication [math]\displaystyle{ \left (\mathbf J^{\mathrm T} \mathbf J\right) }[/math] yields the required [math]\displaystyle{ n \times n }[/math] square matrix and the matrix-vector product on the right hand side yields a vector of size [math]\displaystyle{ n }[/math]. The result is a set of [math]\displaystyle{ n }[/math] linear equations, which can be solved for [math]\displaystyle{ \boldsymbol\delta }[/math].

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

[math]\displaystyle{ \left (\mathbf J^{\mathrm T} \mathbf J + \lambda\mathbf I\right ) \boldsymbol\delta = \mathbf J^{\mathrm T}\left [\mathbf y - \mathbf f\left (\boldsymbol\beta\right )\right], }[/math]

where [math]\displaystyle{ \mathbf I }[/math] is the identity matrix, giving as the increment [math]\displaystyle{ \boldsymbol\delta }[/math] to the estimated parameter vector [math]\displaystyle{ \boldsymbol\beta }[/math].

The (non-negative) damping factor [math]\displaystyle{ \lambda }[/math] is adjusted at each iteration. If reduction of [math]\displaystyle{ S }[/math] 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, [math]\displaystyle{ \lambda }[/math] can be increased, giving a step closer to the gradient-descent direction. Note that the gradient of [math]\displaystyle{ S }[/math] with respect to [math]\displaystyle{ \boldsymbol\beta }[/math] equals [math]\displaystyle{ -2\left (\mathbf J^{\mathrm T}\left [\mathbf y - \mathbf f\left (\boldsymbol\beta\right )\right ]\right )^{\mathrm T} }[/math]. Therefore, for large values of [math]\displaystyle{ \lambda }[/math], the step will be taken approximately in the direction opposite to the gradient. If either the length of the calculated step [math]\displaystyle{ \boldsymbol\delta }[/math] or the reduction of sum of squares from the latest parameter vector [math]\displaystyle{ \boldsymbol\beta + \boldsymbol\delta }[/math] fall below predefined limits, iteration stops, and the last parameter vector [math]\displaystyle{ \boldsymbol\beta }[/math] is considered to be the solution.

When the damping factor [math]\displaystyle{ \lambda }[/math] is large relative to [math]\displaystyle{ \| \mathbf J^{\mathrm T} \mathbf J \| }[/math], inverting [math]\displaystyle{ \mathbf J^{\mathrm T} \mathbf J + \lambda \mathbf I }[/math] is not necessary, as the update is well-approximated by the small gradient step [math]\displaystyle{ \lambda^{-1} \mathbf J^{\mathrm T}\left [\mathbf y - \mathbf f\left (\boldsymbol\beta\right )\right ] }[/math].

To make the solution scale invariant Marquardt's algorithm solved a modified problem with each component of the gradient scaled according to the curvature. This provides larger movement along the directions where the gradient is smaller, which avoids slow convergence in the direction of small gradient. Fletcher in his 1971 paper A modified Marquardt subroutine for non-linear least squares simplified the form, replacing the identity matrix [math]\displaystyle{ \mathbf I }[/math] with the diagonal matrix consisting of the diagonal elements of [math]\displaystyle{ \mathbf J^\text{T}\mathbf J }[/math]:

[math]\displaystyle{ \left [\mathbf J^{\mathrm T} \mathbf J + \lambda \operatorname{diag}\left (\mathbf J^{\mathrm T} \mathbf J\right )\right ] \boldsymbol\delta = \mathbf J^{\mathrm T}\left [\mathbf y - \mathbf f\left (\boldsymbol\beta\right )\right ]. }[/math]

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

Various more or less heuristic arguments have been put forward for the best choice for the damping parameter [math]\displaystyle{ \lambda }[/math]. 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 [math]\displaystyle{ \lambda_0 }[/math] and a factor [math]\displaystyle{ \nu \gt 1 }[/math]. Initially setting [math]\displaystyle{ \lambda = \lambda_0 }[/math] and computing the residual sum of squares [math]\displaystyle{ S\left (\boldsymbol\beta\right ) }[/math] after one step from the starting point with the damping factor of [math]\displaystyle{ \lambda = \lambda_0 }[/math] and secondly with [math]\displaystyle{ \lambda_0 / \nu }[/math]. If both of these are worse than the initial point, then the damping is increased by successive multiplication by [math]\displaystyle{ \nu }[/math] until a better point is found with a new damping factor of [math]\displaystyle{ \lambda_0\nu^k }[/math] for some [math]\displaystyle{ k }[/math].

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

An effective strategy for the control of the damping parameter, called delayed gratification, consists of increasing the parameter by a small amount for each uphill step, and decreasing by a large amount for each downhill step. The idea behind this strategy is to avoid moving downhill too fast in the beginning of optimization, therefore restricting the steps available in future iterations and therefore slowing down convergence.[7] An increase by a factor of 2 and a decrease by a factor of 3 has been shown to be effective in most cases, while for large problems more extreme values can work better, with an increase by a factor of 1.5 and a decrease by a factor of 5.[8]

Geodesic acceleration

When interpreting the Levenberg–Marquardt step as the velocity [math]\displaystyle{ \boldsymbol{v}_k }[/math] along a geodesic path in the parameter space, it is possible to improve the method by adding a second order term that accounts for the acceleration [math]\displaystyle{ \boldsymbol{a}_k }[/math] along the geodesic

[math]\displaystyle{ \boldsymbol{v}_k + \frac{1}{2} \boldsymbol{a}_k }[/math]

where [math]\displaystyle{ \boldsymbol{a}_k }[/math] is the solution of

[math]\displaystyle{ \boldsymbol{J}_k \boldsymbol{a}_k = -f_{vv} . }[/math]

Since this geodesic acceleration term depends only on the directional derivative [math]\displaystyle{ f_{vv} = \sum_{\mu\nu} v_{\mu} v_{\nu} \partial_{\mu} \partial_{\nu} f (\boldsymbol{x}) }[/math] along the direction of the velocity [math]\displaystyle{ \boldsymbol{v} }[/math], it does not require computing the full second order derivative matrix, requiring only a small overhead in terms of computing cost.[9] Since the second order derivative can be a fairly complex expression, it can be convenient to replace it with a finite difference approximation

[math]\displaystyle{ \begin{align} f_{vv}^i &\approx \frac{f_i(\boldsymbol{x} + h \boldsymbol{\delta}) - 2 f_i(\boldsymbol{x}) + f_i(\boldsymbol{x} - h \boldsymbol{\delta})}{h^2} \\ &= \frac{2}{h} \left( \frac{f_i(\boldsymbol{x} + h \boldsymbol{\delta}) - f_i(\boldsymbol{x})}{h} - \boldsymbol{J}_i \boldsymbol{\delta} \right) \end{align} }[/math]

where [math]\displaystyle{ f(\boldsymbol{x}) }[/math] and [math]\displaystyle{ \boldsymbol{J} }[/math] have already been computed by the algorithm, therefore requiring only one additional function evaluation to compute [math]\displaystyle{ f(\boldsymbol{x} + h \boldsymbol{\delta}) }[/math]. The choice of the finite difference step [math]\displaystyle{ h }[/math] can affect the stability of the algorithm, and a value of around 0.1 is usually reasonable in general.[8]

Since the acceleration may point in opposite direction to the velocity, to prevent it to stall the method in case the damping is too small, an additional criterion on the acceleration is added in order to accept a step, requiring that

[math]\displaystyle{ \frac{2 \left\| \boldsymbol{a}_k \right\|}{\left\| \boldsymbol{v}_k \right\|} \le \alpha }[/math]

where [math]\displaystyle{ \alpha }[/math] is usually fixed to a value lesser than 1, with smaller values for harder problems.[8]

The addition of a geodesic acceleration term can allow significant increase in convergence speed and it is especially useful when the algorithm is moving through narrow canyons in the landscape of the objective function, where the allowed steps are smaller and the higher accuracy due to the second order term gives significant improvements.[8]


Poor fit
Better fit
Best fit

In this example we try to fit the function [math]\displaystyle{ y = a \cos\left (bX\right ) + b \sin\left (aX\right ) }[/math] using the Levenberg–Marquardt algorithm implemented in GNU Octave as the leasqr function. The graphs show progressively better fitting for the parameters [math]\displaystyle{ a = 100 }[/math], [math]\displaystyle{ b = 102 }[/math] 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 [math]\displaystyle{ \cos\left (\beta x\right ) }[/math] has minima at parameter value [math]\displaystyle{ \hat\beta }[/math] and [math]\displaystyle{ \hat\beta + 2n\pi }[/math].

See also


  1. "A Method for the Solution of Certain Non-Linear Problems in Least Squares". Quarterly of Applied Mathematics 2 (2): 164–168. 1944. doi:10.1090/qam/10666. 
  2. "An Algorithm for Least-Squares Estimation of Nonlinear Parameters". SIAM Journal on Applied Mathematics 11 (2): 431–441. 1963. doi:10.1137/0111030. 
  3. Girard, André (1958). "Excerpt from Revue d'optique théorique et instrumentale". Rev. Opt. 37: 225–241, 397–424. 
  4. Wynne, C. G. (1959). "Lens Designing by Electronic Digital Computer: I". Proc. Phys. Soc. Lond. 73 (5): 777–787. doi:10.1088/0370-1328/73/5/310. Bibcode1959PPS....73..777W. 
  5. 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. 
  6. Wiliamowski, Bogdan; Yu, Hao (June 2010). "Improved Computation for Levenberg–Marquardt Training". IEEE Transactions on Neural Networks and Learning Systems 21 (6). https://www.eng.auburn.edu/~wilambm/pap/2010/Improved%20Computation%20for%20LM%20Training.pdf. 
  7. Transtrum, Mark K; Machta, Benjamin B; Sethna, James P (2011). "Geometry of nonlinear least squares with applications to sloppy models and optimization". Physical Review E (APS) 83 (3): 036701. doi:10.1103/PhysRevE.83.036701. PMID 21517619. Bibcode2011PhRvE..83c6701T. 
  8. 8.0 8.1 8.2 8.3 Transtrum, Mark K; Sethna, James P (2012). "Improvements to the Levenberg-Marquardt algorithm for nonlinear least-squares minimization". arXiv:1201.5885 [physics.data-an].
  9. "Nonlinear Least-Squares Fitting". GNU Scientific Library. https://www.gnu.org/software/gsl/doc/html/nls.html. 
  10. 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. Bibcode2004JCoAM.172..375K. 

Further reading

|last      = Pujol
|first     = Jose
|year      = 2007
|title     = The solution of nonlinear inverse problems and the Levenberg-Marquardt method
|journal   = Geophysics
|publisher = SEG
|doi       = 10.1190/1.2732552
|volume    = 72
|number    = 4
|pages     = W1–W16
|url       = http://link.aip.org/link/?GPY/72/W1/1
|bibcode   = 2007Geop...72W...1P
  • Nocedal, Jorge; Wright, Stephen J. (2006). Numerical Optimization (2nd ed.). Springer. ISBN 978-0-387-30303-1. 

External links