lmfit.eval() producing wrong values of my model functions

13 views
Skip to first unread message

ethan koren

unread,
Apr 22, 2024, 11:05:55 PMApr 22
to lmfit-py
I have a problem which requires fitting and I wanted to test different fitting methods by using the lmfit library. I used numpy.curvefit (which worked) to produce initial fitting parameters for lmfit, but when I tried to fit using lmfit, I got nan values in all of my outputs. Evaluating the function normally in python with the parameters from numpy.curvefit did not produce nan values. Would anybody have an idea as to why this is happening?  

Thank you in advance, 
Ethan

Matthew Newville

unread,
Apr 22, 2024, 11:15:21 PMApr 22
to lmfi...@googlegroups.com
Hi Ethan, 

Did you read https://lmfit.github.io/lmfit-py/faq.html#i-get-errors-from-nan-in-my-fit-what-can-i-do?     That gives several scenarios for getting nans.

We always suggest giving a minimal example code that shows the problem.  

--Matt




From: lmfi...@googlegroups.com <lmfi...@googlegroups.com> on behalf of ethan koren <ethan...@gmail.com>
Sent: Monday, April 22, 2024 8:05 PM
To: lmfit-py <lmfi...@googlegroups.com>
Subject: lmfit.eval() producing wrong values of my model functions
 
I have a problem which requires fitting and I wanted to test different fitting methods by using the lmfit library. I used numpy.curvefit (which worked) to produce initial fitting parameters for lmfit, but when I tried to fit using lmfit, I got nan values in all of my outputs. Evaluating the function normally in python with the parameters from numpy.curvefit did not produce nan values. Would anybody have an idea as to why this is happening?  

Thank you in advance, 
Ethan
--
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/54544e7f-7ee5-4c93-ac51-3503ffc68c2cn%40googlegroups.com.

ethan koren

unread,
Apr 23, 2024, 9:18:55 AMApr 23
to lmfi...@googlegroups.com
Thank you for the speedy response Matt.
I have read that link, and the strange thing is that I only get the
nan values when I use model.eval() or model.fit(). If I simply
evaluate the function I get finite values as I expect (see below).
I'll attach the code.
What my function does is a convolution of a theoretical spectrum by
some Lorentzians and this covolved spectrum is fitted to an
experimental one.
When I use numpy.curvefit, this is fitted without problems. When I try
to use lmfit I run into the issue I mentioned above. My function is
composed of a few functions which call my Lorentzian function (maybe
this causes a problem?).
Here is some of the code.

def CauchyDist(x,Gamma): #Gamma is the full width
b = Gamma/2.0 #Half width
m = np.max(x) - (np.max(x)-np.min(x))/2.0 #Location of the mean
(in the middle of the x array)
return np.pi**-1 * (abs(b)/((x-m)**2.0+b**2.0))

def TotalConvolutionFunction5(Energy,x0,x1,x2,x3,x4,debye,hole):

#Remove the atom background from the FDMNES output
Chi = (xanes-Atom)/Atom

#Apply the convolutions
Chi = DebyeWaller(imfp5Conv(Chi,E,hole,x0,x1,x2,x3,x4),E,debye)
#The function imfp5Conv performs the convolutions.

#Interpolation function for spectrum
f = scipy.interpolate.interp1d(E,Chi,kind = 'cubic')
return f(Energy)

#Trying to use lmfit to explore some different fitting methods
model = lmfit.Model(TotalConvolutionFunction5)
params5 = model.make_params(x0=poptConv5[0],
x1=poptConv5[1],
x2=5.,
x3=poptConv5[3],
x4=poptConv5[4],
debyeWallerFactor=poptConv5[5],
Gamma_hole=poptConv5[6])

test = model.eval(Energy=E[E<MaxEnFit],
params = params5)
print(np.isnan(TotalConvolutionFunction5(E[E<MaxEnFit],*poptConv5)))
print(np.isnan(test))


Here is the output

RuntimeWarning: invalid value encountered in divide return np.pi**-1 *
(abs(b)/((x-m)**2.0+b**2.0)) (This is the line from the CauchyDist
function)

[False False... ... ...False False]

[ True True... ... ...True True True]


I am truly confused. What do you think?
> To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/MN6PR11MB821986B3C06416CE09B0334496112%40MN6PR11MB8219.namprd11.prod.outlook.com.

Laurence Lurio

unread,
Apr 23, 2024, 11:45:28 AMApr 23
to lmfi...@googlegroups.com
Have you tried params.pretty_print().  It could be a case that you are feeding it NaN parameter values.

Larry 

ethan koren

unread,
Apr 23, 2024, 7:00:20 PMApr 23
to lmfi...@googlegroups.com
Yes that was the problem!
Thank you very much. It is all working now :)
> To view this discussion on the web visit https://groups.google.com/d/msgid/lmfit-py/CAK4HuLEHg%3D3ZCamgV_Y0tB%2BOwXXbDN4HJY6DKKsneuKMXOvScg%40mail.gmail.com.
Reply all
Reply to author
Forward
0 new messages