0

I am trying to fit a model using LMFIT, I can easily do the following:

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')

And get very reasonable results

Now I have also some uncertainties associated with my measured variable (e.g. measurement errors) so I would like to weight the points in my residuals by the standard error associated to it (suppose it is constantly 20% of the measured value). The code now becomes something like this:

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')

The problem is that now I get totally unreliable fitting results. Why? How can I fix this?

luke14free
  • 2,529
  • 1
  • 17
  • 25

1 Answers1

0

What is the nature of "totally unreliable"? You would probably want the uncertainties in the data to be strictly positive -- using measured* 0.2 could allow negative values or zeros. Note that if there are NaNs or Infs in the residual, the fit will not work well, and almost certainly leave the parameter values at their starting values.

FWIW, you can pass on arguments to the objective function (for measurements, uncertainties, etc) using the fcn_args argument to Minimizer.

M Newville
  • 7,486
  • 2
  • 16
  • 29