Passing keyword method to least_squares

78 views
Skip to first unread message

Andreas Var

unread,
Jul 11, 2022, 8:55:30 AM7/11/22
to lmfit-py
Hi everybody,

I would like to use the Levenberg-Marquardt method of "least_squares" instead of using "leastsq" . (One advantage of least_squares is the option diff_step.)

When using scipy.optimize.least_squares you may choose the method with
least_squares(fun, x0, method='lm')
In lmfit.minimize you can pass **fit_kws to the optimizer. But you cannot use the keyword "method" since this is also a keyword for the minimize-function itself.

Is there a way to pass the keyword anyway?

Thanks,
Andreas

Matt Newville

unread,
Jul 13, 2022, 1:44:23 AM7/13/22
to lmfit-py
Hi Andreas, 

Yeah, it is a bit messy.  We try to expose the function arguments for the scipy.optimize routines, but it is not super-easy.  Frankly, their API is a mess. While the algorithms that make up "least_squares" are clearly very good, the complex mix of options is pretty frustrating to work with.  I think that scrapping the whole idea of "exposing the scipy API as much as possible" would actually be a viable option - but that would clearly be disruptive and a lot of work. 

For the time being, it should be that doing something like:

       fitter = Minimizer(my_residual, my_params, fcn_kws=dict(xdata=xdata, ydata=ydata)) 
       result = fitter.least_squares(method='lm')

Should run `least_squares` with its "method='lm'" argument.

Hope that helps,
--Matt


--
You received this message because you are subscribed to the Google Groups "lmfit-py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lmfit-py+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/eaca0433-900b-4c43-b37f-d77fab86a441n%40googlegroups.com.


--
--Matt Newville <newville at cars.uchicago.edu630-327-7411

Andreas Var

unread,
Jul 13, 2022, 8:11:07 AM7/13/22
to lmfit-py
Hi Matt,

thank you very much for your help. That looks like a good solution!

As I understand it the wrapper for Minimizer.least_squares does not add a "bounds functionality" like it does for minimize(method='leastsq'). Is that correct?

(scipy.optimize.least_squares only has a built-in support for bounds when using 'trf'.)

Thank you,
Andreas
Reply all
Reply to author
Forward
0 new messages