lmfit Model is not evaluating as expected, variables are not being calculated (remain initial guess)

292 views
Skip to first unread message

Dakota Rose

unread,
Jul 16, 2019, 5:14:20 PM7/16/19
to lmfit-py
Hello,

I recently found lmfit, and am attempting to use it to fit to data in the attached Excel file. I am starting very simple, and following the Gaussian fit tutorial, but using my function and data. The code is as appears below. I am hoping to get a fit comparable to that in the Excel workbook, where the values of the coefficients were simply guessed to give the best fit.

import numpy as np
from numpy import exp, linspace, random, sqrt, pi, append
import matplotlib.pyplot as plt
import xlrd


wb = xlrd.open_workbook("HDDA_Eric.xlsx")
sheet = wb.sheet_by_index(0)
xdata=[]
for i in range(12,sheet.nrows):
    xd=sheet.cell_value(i,0)
    xdata.append(xd)
    
measuredBTDF=[]
for i in range(12,sheet.nrows):
    yd=sheet.cell_value(i,1)
    measuredBTDF.append(yd)

xarray = np.asarray(xdata)

BTDFarray = np.asarray(measuredBTDF)


incident_angle = sheet.cell_value(0,1)

def BSDF2(theta_s,b0,l,s):
    theta_0_radx = incident_angle*pi/180 
    theta_s_radx = theta_s*pi/180
    betax = np.sin(theta_0_radx) # this is the sin of the incident angle
    beta_0x = np.sin(theta_s_radx) # this is the sin of the measured angle
    
    BSDF2 = b0*(1+((abs(betax - beta_0x))/l)**2)**(-s/2)
    
    return BSDF2

gmodel2 = Model(BSDF2)
#print(gmodel2.param_names)
#print(gmodel2.independent_vars)

params3 = gmodel2.make_params(b0=7.0,l=0.01,s=2.1)
yeval = gmodel2.eval(params3,theta_s=xarray)
result2 = gmodel2.fit(yeval,params3,theta_s=xarray)
print(result2.fit_report())

The problem is that the fit report shows that there is no attempt at finding new values for the coefficients. They are being returned unaltered, at their initial values.

[[Model]]
    Model(BSDF2)
[[Fit Statistics]]
    # fitting method   = leastsq
    # function evals   = 4
    # data points      = 193
    # variables        = 3
    chi-square         = 0.00000000
    reduced chi-square = 0.00000000
    Akaike info crit   = -inf
    Bayesian info crit = -inf
##  Warning: uncertainties could not be estimated:
    b0:  at initial value
    l:   at initial value
    s:   at initial value
[[Variables]]
    b0:  7.00000000 +/- 0.00000000 (0.00%) (init = 7)
    l:   0.01000000 +/- 0.00000000 (0.00%) (init = 0.01)
    s:   2.10000000 +/- 0.00000000 (0.00%) (init = 2.1)

At this point I really do not know where I am going wrong, but I have a feeling it is probably with the BSDF equation itself, and how it uses the absolute value of the angle data. Any light that could be shed on this would be greatly appreciated!

Best,
Dakota Rose
HDDA_Eric.xlsx

Laurence Lurio

unread,
Jul 16, 2019, 8:36:18 PM7/16/19
to lmfit-py
You may want to check the data type of your input.  I once had a similar problem and found that my data was a float32, and I had to convert it to float64 before things would fit.

Matt Newville

unread,
Jul 16, 2019, 10:54:46 PM7/16/19
to lmfit-py
Is it possible that you mean to fit the `BTDFarray` instead of `yeval` -- which evaluated from the model (so will match it exactly)?

That is, maybe you meant to do something like (cleaning up a bit):

    gmodel = Model(BSDF2)

    params = gmodel.make_params(b0=7.0,l=0.01,s=2.1)
    yinit  = gmodel.eval(params, theta_s=xarray)

    result = gmodel.fit(BTDFarray, params, theta_s=xarray)
    print(result.fit_report())

    plt.plot(xarray, BTDFarray)
    plt.plot(xarray, result.best_fit)
    plt.gca().set_xlim((-10, 10))
    plt.show()

Hope that helps,

--Matt

Dakota Rose

unread,
Jul 17, 2019, 12:51:41 PM7/17/19
to lmfit-py
This worked, thank you so much! I was curious as well though, this was using a least squares method. I saw that the 'eval' function has a 'method' argument, but haven't (yet) found alternatives to try for better fit results (maybe). Can you recommend any alternative methods to try as well? Thanks again!

On Tuesday, July 16, 2019 at 7:54:46 PM UTC-7, Matt Newville wrote:
Reply all
Reply to author
Forward
0 new messages