Hi @mailinglist,
I have started using lmfit in order to get some condensed information along with my fits (especially confidence intervals) but I am running into an error on some of my fits.
To make it short, I have 10 data points (with y_err) per fit, and 12 independent experiments. Each experiment are described by the same mathematical equation, so I loop over the 12 experiments and create a new lmfit model instance each time then fit the model instance with the 10 experimental data points associated to the experiment.
In 9 out of 12 cases, I can use ci = result.conf_interval() to get my confidence intervals. However, and for no obvious reason (as they all look kind of similar), 3 out of 12 give me a ValueError: f(a) and f(b) must have different signs, but f(4.09...e-00) = 3.151...e-01, f(4.12...e-00) = 3.136...e-01
I was wondering if you had an idea as to how I could get this confidence interval computation to work, as for now I just have it in a try... except block so that the error is not fatal.
Note that depending on how the input data was preprocessed, some of the experiments used to not generate that error...
I read from the documentation and from online question boards that this may happen if the parameter being evaluated is close to its boundary but none of my 3 parameters have boundaries and their initial guess is somewhat close to the expected fit value.
Below a reduced sample of the code I'm using, with the data from a pathological case:
from lmfit import Model, printfuncs
def invrecov(Ti, a, b, T1):
if T1 * 1e3 < 50: # Force T1 in valid range
return 1e7
return np.abs(a + b * np.exp(-Ti/T1))
fullMeans = array(
[2965.61052096, 1743.81963565, 1067.84200776,
482.80343508, 59.56659829, 1540.88095421,
4019.87643396, 4134.87512142, 4137.39869382,
4145.13411215]
)
fullSEMs = array(
[0.13002311890348395, 0.09963669736078981, 0.07788739802154705,
0.07584347244366009, 0.031136842850181763, 0.09370153130305435,
0.15141882065887413, 0.15357232888620656, 0.1536191959417515,
0.1537628769325479]
)
mod = Model(
invrecov)
params = mod.make_params(a=4, b=-8, T1=0.1)
result = mod.fit(
np.array(fullMeans),
params,
Ti=np.array(time)/1e3,
weights=1.0/np.array(fullSEMs)
)
print(result.fit_report())
try:
ci = result.conf_interval()
print("[[Confidence Intervals]]")
printfuncs.report_ci(ci)
except ValueError: