Assume we have some data of measurements
SeedRandom[9];
data = Table[{i,2*(1 + 5*RandomReal[])*Exp[-0.01*(1 + RandomReal[])*i]}, {i, {0, 100, 200, 400, 700}}];
and measurement errors associated to it
errors = 10^# & /@ Range[0, -4, -1];
Ignoring the weights in a first step, the fitting works very well:
nlm = NonlinearModelFit[data, a*Exp[-b*x], {a, b}, x, Method -> "NMinimize"];
Show[ListPlot[data, PlotStyle -> PointSize -> Medium],
Plot[nlm[x], {x, 0, 700}, PlotRange -> Full], PlotRange -> All]
Now, according to this how-to, we can include the measurement errors to the fitting procedure:
nlm = NonlinearModelFit[data, a*Exp[-b*x], {a, b}, x, Weights -> 1/errors^2, VarianceEstimatorFunction -> (1 &), Method -> "NMinimize"];
Show[ListPlot[data, PlotStyle -> PointSize -> Medium],
Plot[nlm[x], {x, 0, 700}, PlotRange -> Full], PlotRange -> All]
Clearly, the obtained fit is much worse.
My questions are:
What is the objective function in
NonlinearModelFit
when introducing weights? The how-to says: "It is important to note that weights do not change the fitting or error estimates", but obviously the fit is changed. Thus, the weights have to be a part of the objective functions, which is minimized.How can I obtain a better fit with introduced weights? I was playing around with the
NMinimize
options"NelderMead"
,"RandomSearch"
,"SimulatedAnnealing"
and"DifferentialEvolution"
, but up to this point none of them was able to reproduce the fit obtained by ignoring the measurement errors.