我正在使用 lmfit 来查找适合的置信区间,但它经常给我一个错误:ValueError: f(a) and f(b) must have different signs
这是一个最小的工作示例(运行几次以获取错误):
import lmfit
import numpy as np
def residual(p, X):
a1, a2, t1, t2 = [i.value for i in p.values()]
return a1*np.exp(-x/t1)+a2*np.exp(-x/t2)-y
if __name__ == '__main__':
x = np.linspace(0.3,10,100)
y = 3*np.exp(-x/2.)-5*np.exp(-x/10.)+0.2*np.random.randn(x.size)
p = lmfit.Parameters()
p.add_many(('a1', 5), ('a2', -5), ('t1', 2), ('t2', 5))
mi = lmfit.minimize(residual, p, args=(x,))
lmfit.printfuncs.report_fit(mi.params, show_correl=False)
ci, trace = lmfit.conf_interval(mi, sigmas=[0.68,0.95], trace=True, verbose=False)
lmfit.printfuncs.report_ci(ci)
为什么这样做?有解决办法吗?
谢谢