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

VoigtModel params do not vary when symmetric min and max constraints are used #700

Closed xxyxxyxyx1 closed 3 years ago

xxyxxyxyx1 commented 3 years ago

If a constraint window that is symmetric about center is provided for sigma or gamma of the builtin VoigtModel, that parameter will not be varied in the fit, even if vary=True is included explicitly.

Here is an example:

from pprint import pprint
from lmfit.models import VoigtModel
import numpy as np

data = np.loadtxt('test_peak.dat') # from github lmfit/lmfit-py/examples/
x = data[:,0]
y = data[:,1]

fun = VoigtModel()
params = fun.make_params()
params['center'].set(value=9, vary=True)
params['amplitude'].set(value=25, vary=True)

sigma = 0.7
gamma = 0.5
variability = 0.45
params['sigma'].set(
        value=sigma,
        # min=sigma-variability,
        # max=sigma+variability,
        vary=True,
        )
params['gamma'].set(
        value=gamma,
        # min=gamma-variability,
        # max=gamma+variability,
        vary=True,
        )

fit = fun.fit(y, params, x=x)
pprint(fit.best_values)

This code, with the min and max kwargs commented out as above, finds the following good fit:

{'v0_amplitude': 34.191471987858264,
 'v0_center': 9.243748480259875,
 'v0_gamma': 0.5254015957109053,
 'v0_sigma': 0.8951895000000183}

The same fit is found if either min or max is provided for sigma and/or gamma. However, if both constraints are provided for the same parameter sigma or gamma, that parameter is no longer varied in the fit, resulting in fits like:

{'v0_amplitude': 37.17310478600008,
 'v0_center': 9.244460290632759,
 'v0_gamma': 0.9347693655134416,
 'v0_sigma': 0.7}

{'v0_amplitude': 33.990849747046866,
 'v0_center': 9.243706672519501,
 'v0_gamma': 0.5,
 'v0_sigma': 0.9130631363487098}

Making the constraints slightly asymmetric, such as with

min = sigma-variability+0.001
max = sigma+variability

results in the correct fit, at least in my limited testing.

Version information

Python: 3.7.7 (default, Mar 26 2020, 10:32:53) [Clang 4.0.1 (tags/RELEASE_401/final)] lmfit: 1.0.1, scipy: 1.4.1, numpy: 1.19.2, asteval: 0.9.18, uncertainties: 3.1.2

newville commented 3 years ago

@xxyxxyxyx1 I must say that it continues to surprise me when people who are clearly literate and intelligent enough to write python code, understand enough mathematics and to pursue the task of modeling and fitting data, and who can navigate github issues are unable to read and follow clear instructions.

It just is not credible to think that you somehow missed all of the advice on how to ask for help and you clearly saw AND REMOVED the notification in creating this issue that clearly and loudly states how to ask for help with this library and to not use github issues for that.

Use the mailing list.

xxyxxyxyx1 commented 3 years ago

"do NOT use GitHub Issues for questions, it is only for bugs!... However, if you discovered a bug, please do proceed"

My issue is a bug, not a question. I do not need help, I only want to report a bug. I included a working example.