Reputation: 1
I'm using lmfit to fit a bunch of lorentzians. For reasons of boundaries, I can't use scipy.optimize functions directly. With lmfit the fits work well, but res.covar is always None, res.errorbars is False, res.uvars is undefined and res.params['parameter_name'].stderr is also None. I need the errorbars. Here's my code (without data):
def to_minimize(params, x, y, uncertainties=None):
x0 = params["x0"]
ampl0 = params["ampl0"]
ampl = params["ampl"]
sig = params["sig"]
model = lorentzian(x, x0, ampl0, ampl, sig)
if uncertainties is not None:
return (y- model) / uncertainties
else:
return y - model
params = lmfit.Parameters()
params.add("x0", value=guess[0], min=guess[0] - 1000, max=guess[0] + 1000)
params.add("ampl0", value=guess[1], min=0)
params.add("ampl", value=guess[2], min=0)
params.add("sig", value=guess[3], min=0)
out = lmfit.minimize(to_minimize, params, args=(x_data, y_data))
Here, lorentzian is a function that calculates the lorentzian and guess is a list of initial values for the guess. x_data and y_data are 1d arrays of 50000+ size. Here's the output that I get when I type out in a jupyter cell:
Fit Result
Fit Statistics
fitting method leastsq
# function evals 156
# data points 51201
# variables 4
chi-square 9.8949e-23
reduced chi-square 1.9327e-27
Akaike info crit. -3149414.19
Bayesian info crit. -3149378.82
Parameters
name value initial value min max vary
x0 2424682.92 2424725.9731872166 2423725.97 2425725.97 True
ampl0 1.6720e-13 1.6717888361052836e-13 0.00000000 inf True
ampl 1.2557e-12 1.2557626695414776e-12 0.00000000 inf True
sig 105.300229 4.296879768371582 0.00000000 inf True
print(out.covar) gives None. out.errorbars is False. out.uvars raises AttributeError. I need these errors in the parameters, no matter how small. But I get no warnings during the fit, and numdiftools is installed. What am I missing here?
My lmfit version is 1.3.2 and numdifftools is 0.9.41. Python is 3.11.7
If it's impossible to get them as outputs, can you suggest a way to calculate these by hand? Thanks a lot
Upvotes: 0
Views: 40