我正在尝试使用 LMFIT 拟合模型,我可以轻松地执行以下操作:
def loss_function(params):
residuals = []
for x, measured in ...:
y = predict(x, params)
residuals.append(y - measured)
return residuals
params = Parameters()
params.add(...)
model = Minimizer(loss_function, params)
result = model.minimize(method='leastsq')
并得到非常合理的结果
现在我还有一些与我的measured
变量相关的不确定性(例如测量误差),所以我想通过与之相关的标准误差对残差中的点进行加权(假设它始终是测量值的 20%)。代码现在变成了这样:
def loss_function(params):
residuals = []
for x, measured in ...:
y = predict(x, params)
residuals.append((y - measured) / (measured * 0.2))
return residuals
params = Parameters()
params.add(...)
model = Minimizer(loss_function, params)
result = model.minimize(method='leastsq')
问题是现在我得到完全不可靠的拟合结果。为什么?我怎样才能解决这个问题?