Search code examples
pythonnumpyoptimizationscipyleast-squares

Difference between scipy.leastsq and scipy.least_squares


I was wondering what the difference between the two methods scipy.optimize.leastsq and scipy.optimize.least_squares is?

When I implement them they yield minimal differences in chi^2:

>>> solution0 = ((p0.fun).reshape(100,100))
>>> # p0.fun are the residuals of my fit function np.ravel'ed as returned by least_squares
>>> print(np.sum(np.square(solution0))) 
0.542899505806

>>> solution1 = np.square((median-solution1))
>>> # solution1 is the solution found by least_sq, it does not yield residuals thus I have to subtract it from the median to get the residuals (my special case)
>>> print(np.sum((solution1)))
0.54402852325

Could anybody expand on that or point out where I can find an alternative documentation, the one from scipy is a bit cryptic.


Solution

  • From the docs for least_squares, it would appear that leastsq is an older wrapper.

    See also

    leastsq   A legacy wrapper for the MINPACK implementation of the Levenberg-Marquadt algorithm.

    So you should just use least_squares. It appears that least_squares has additional functionality. Foremost among them is that the default "method" (i.e. algorithm) used is different:

    • trf : Trust Region Reflective algorithm, particularly suitable for large sparse problems with bounds. Generally robust method.
    • dogbox : dogleg algorithm with rectangular trust regions, typical use case is small problems with bounds. Not recommended for problems with rank-deficient Jacobian.
    • lm : Levenberg-Marquardt algorithm as implemented in MINPACK. Doesn’t handle bounds and sparse Jacobians. Usually the most efficient method for small unconstrained problems.

    Default is trf. See Notes for more information.

    The old leastsq algorithm was only a wrapper for the lm method, which—as the docs say—is good only for small unconstrained problems.

    The difference you see in your results might be due to the difference in the algorithms being employed.