lmfit / lmfit-py

Non-Linear Least Squares Minimization, with flexible Parameter settings, based on scipy.optimize, and with many additional classes and methods for curve fitting.
https://lmfit.github.io/lmfit-py/
Other
1.07k stars 275 forks source link

method selection does not work when calling minimize directly #25

Closed ghost closed 10 years ago

ghost commented 12 years ago

Hi there,

great work. This interface really improves the handling of numpy.optimize. The issue: When passing method="nelder" to minimize, a leastsquare fit is performed. This is due to the code fragment line 498 in minimizer.py. The method argument is only parsed if it refers to a scalar function (HAS_SCALAR_MIN is true).

I suggest replacing

    if not found:
        fitter.leastsq()

by

    if not found:
        if meth in _methods:
            func = 'fitter.'+_methods[meth]+'(**fit_kws)'
            found = True
            exec(func)
        else:
            fitter.leastsq(**fit_kws)

Best regards,

Alois

newville commented 12 years ago

Hi Alois, thanks for the message.

I believe this is already fixed in the git repo. That is, specifying 'nelder' and the other scaler minimization procedures works for me.

ghost commented 12 years ago

Hey,

I checked only on the newest version in the master branch which pops up immediately on the github side (is it the newest version?). There was a commit one month ago which resulted in a deletion of a respective code block:

commit d02e8c7e1ec733ce7ed842b8b0e8b24b19049b55

Am I getting something wrong here?

Best regards,

Alois

On Wed, Oct 17, 2012 at 4:54 PM, Matt Newville notifications@github.comwrote:

Hi Alois, thanks for the message.

I believe this is already fixed in the git repo. That is, specifying 'nelder' and the other scaler minimization procedures works for me.

— Reply to this email directly or view it on GitHubhttps://github.com/newville/lmfit-py/issues/25#issuecomment-9530235.

newville commented 12 years ago

I added tests/fit_pvoight_NelderMead2.py This works for me from the master branch. If something isn't working for you, please send a small script that fails on the master branch. That's much better than trying to guess what is going wrong or posting partial code.

ghost commented 12 years ago

Hey there,

sorry to bother you again, but: I installed the master branch from the repo and tested your script. It looks like it still executes the leastsquare algorithm.

I added report_errors(myfit.params) to your script and there are error values on each parameter which are not given by the fmin algorithm.

To verify, I added

print "I'm a least square fit!"

to the leastsq function in minimize. This is the output:

$:~/phd/somebody/trunk/tools/lmfit-master/lmfit-py/tests$ python fit_pvoigt_NelderMead2.py I am a least square fit! (' Nfev = ', 50) amp_g: 21.096483 +/- 0.052981 (0.25%) initial = 10.000000 amp_l: 21.096483 +/- 0.000000 (0.00%) == 'amp_g' cen_g: 8.097970 +/- 0.002799 (0.03%) initial = 9.000000 cen_l: 8.097970 +/- 0.000000 (0.00%) == 'cen_g' frac: 0.399482 +/- 0.013247 (3.32%) initial = 0.500000 line_off: -1.126788 +/- 0.035736 (3.17%) initial = 0.000000 line_slope: 0.623733 +/- 0.001930 (0.31%) initial = 0.000000 wid_g: 1.600233 +/- 0.004338 (0.27%) initial = 1.000000 wid_l: 1.600233 +/- 0.000000 (0.00%) == 'wid_g' Correlations: C(line_off, line_slope) = -0.802 C(frac, line_off) = -0.723 C(amp_g, frac) = 0.704 C(amp_g, line_off) = -0.616 C(amp_g, wid_g) = -0.505 C(frac, line_slope) = 0.361 C(frac, wid_g) = -0.343 C(amp_g, line_slope) = 0.328 C(cen_g, line_slope) = -0.204 C(cen_g, line_off) = 0.161 C(cen_g, frac) = -0.072 C(amp_g, cen_g) = -0.065 C(line_slope, wid_g) = 0.048 C(line_off, wid_g) = -0.026 C(cen_g, wid_g) = -0.009 None

Btw thanks for the work. I use it for 3d data evaluation (extracting lineshapes and fitting functional dependencies).

Alois

On Wed, Oct 17, 2012 at 8:37 PM, Matt Newville notifications@github.comwrote:

I added tests/fit_pvoight_NelderMead2.py This works for me from the master branch. If something isn't working for you, please send a small script that fails on the master branch. That's much better than trying to guess what is going wrong or posting partial code.

— Reply to this email directly or view it on GitHubhttps://github.com/newville/lmfit-py/issues/25#issuecomment-9538632.

newville commented 12 years ago

Well, for me it definitely runs the Nelder-Mead algorithm and does not produce uncertainties.

Two things to check: 1) that you're running scipy 0.11 or later (otherwise HAS_SCALAR_MIN will be False), and 2) that you're not running some older version of lmfit (remove all older versions from your site-packages/).

And, again, actual code that runs and shows a problem is far better than describing what you did in words.

newville commented 10 years ago

I think this is resolved.