How to set the iteration limit for lmfit?

2,070 views
Skip to first unread message

Jinyi Shangguan

unread,
May 5, 2016, 7:05:22 AM5/5/16
to lmfit-py
Hi there,

       I am new to lmfit. I find that when I fit some data, I usually find that when I use the best fit parameter to re-fit the data, I can get smaller chi square. Is this due to the iteration limit of lmfit is too small? But I cannot find a parameter to change the iteration limit...


PS: Since I need to calculate the chi square myself in order to incorporate upperlimits, I use the method "nelder", instead of "leastsq".


best,
Shangguan

Matt Newville

unread,
May 5, 2016, 7:37:11 AM5/5/16
to Jinyi Shangguan, lmfit-py
On Thu, May 5, 2016 at 6:05 AM, Jinyi Shangguan <darkbe...@gmail.com> wrote:
Hi there,

       I am new to lmfit. I find that when I fit some data, I usually find that when I use the best fit parameter to re-fit the data, I can get smaller chi square. Is this due to the iteration limit of lmfit is too small? But I cannot find a parameter to change the iteration limit...


You can set the maximum number of function evaluations with 'maxfev'.    To do this, you make a 'fit_kws' dictionary of parameters to send to the underlying scipy minimize routine.  So, something like

   result = minimize(myresidual, ...., fit_kws={'maxfev': 50} )

The fit_kws dictionary might contain other parameters too, depending on the solver used.    The Model.fit() function also takes a fit_kws dictionary.


PS: Since I need to calculate the chi square myself in order to incorporate upperlimits, I use the method "nelder", instead of "leastsq".


Sorry, that doesn't really make sense to me.  Why should Chi-square incorporate upper limits?

--Matt

Jinyi Shangguan

unread,
May 5, 2016, 7:49:41 AM5/5/16
to lmfit-py, newv...@cars.uchicago.edu
Thanks a lot Matt!

There are upperlimits in my data and the upperlimits are 3 (Gaussian) sigma. Therefore, we expect the upperlimits could also constrain our model. From the method people obtain the Chi-square formalism, we could incorporate the upperlimits consistently into Chi-square, if I understand right. My main reference is the appendix of the attached paper. Please correct me if I do not understand right. I am actually not very familiar with statistics.


best,
Shangguan

在 2016年5月5日星期四 UTC+8下午7:37:11,Matt Newville写道:
Sawicki_2012PASJ_SEDfitting.pdf

Jinyi Shangguan

unread,
May 5, 2016, 8:08:02 AM5/5/16
to lmfit-py, darkbe...@gmail.com, newv...@cars.uchicago.edu
Hi Matt,

    When I add the fit_kws dict in to minimize, I got an error: TypeError: minimize() got an unexpected keyword argument 'fit_kws'

Please find the screen shot of the error in the attachment. Do I make anything wrong?


best,
Shangguan


在 2016年5月5日星期四 UTC+8下午7:37:11,Matt Newville写道:
Screen Shot 2016-05-05 at 20.04.00.png

Matt Newville

unread,
May 5, 2016, 8:48:31 AM5/5/16
to Jinyi Shangguan, lmfit-py
On Thu, May 5, 2016 at 7:08 AM, Jinyi Shangguan <darkbe...@gmail.com> wrote:
Hi Matt,

    When I add the fit_kws dict in to minimize, I got an error: TypeError: minimize() got an unexpected keyword argument 'fit_kws'

Please find the screen shot of the error in the attachment. Do I make anything wrong?


Oh, my mistake.  With minimize(), you can just pass maxfev directly,


   result = minimize(myfunc, ....., maxfev=100)

when using Model.fit() or creating a Minimizer instance, you use fit_kws dictionary.

Sorry, I have no idea why (or how) one would apply upper limits for chi-square, and no time to read the paper you reference.

--Matt

Jinyi Shangguan

unread,
May 5, 2016, 9:19:18 AM5/5/16
to Matt Newville, lmfit-py
There are still some problems… I find when I write like this:
    
    fit_kws = {'maxfev':500}
    out = minimize(ChiSquare_LMFIT, pars, kws=kws, method='nelder', **fit_kws)

It does not report the error of expected argument “fit_kws”. And I find the formalism above work well using “leastsq”, however, it cannot take the keyword “maxfev” for “nelder”… Please see the attachment.


I think the method to incorporate upper limits is widely used in some fields and my code work reasonably good with it anyway. So don’t worry about that :)


best,
Shangguan

Matt Newville

unread,
May 5, 2016, 10:30:11 AM5/5/16
to Jinyi Shangguan, lmfit-py
On Thu, May 5, 2016 at 8:19 AM, Jinyi Shangguan <darkbe...@gmail.com> wrote:
There are still some problems… I find when I write like this:
    
    fit_kws = {'maxfev':500}
    out = minimize(ChiSquare_LMFIT, pars, kws=kws, method='nelder', **fit_kws)

It does not report the error of expected argument “fit_kws”. And I find the formalism above work well using “leastsq”, however, it cannot take the keyword “maxfev” for “nelder”… Please see the attachment.


Hmm, you're right that it doesn't seem to work for scalar minimizers.  What's more, the scalar minimizers actually want "maxiter" instead of "maxfev".   Sheesh.  I'll add an Issue to try to unify this so that there is a uniform way to set the this (and also "tol" and "xtol").

For now, you can do this with an explicit creation of Minimizer() and calling its scalar_minimize() method:

    mini = Minimizer(fcn2min, params, fcn_args=(x, data), fcn_kws={})
    result = mini.scalar_minimize(method='Nelder-Mead', options={'maxiter': 150})

Hope that helps,

 
I think the method to incorporate upper limits is widely used in some fields and my code work reasonably good with it anyway. So don’t worry about that :)


You mean like to suppress the importance of outliers?   There are lots of reasons to use something other than sum-of-squares for the cost to be minimized, and suppressing outliers is one of them.  I just would consider that to be something other than chi-square.
 
--Matt
Reply all
Reply to author
Forward
0 new messages