# How to add measurement errors (or uncertainties) on NonlinearModelFit (with Weights)

I'd like to fit a Nonlinear curve to a dataset, which has an uncertainties vector associated. The problem is that I'd also like to add Weights to the fit. Lets say that my dataset is:

data={{-2., 1.00884}, {-1.5, 2.75486}, {-1., 4.00577}, {-0.5,
4.75401}, {0., 5.00771}, {0.5, 4.7533}, {1., 4.00055}, {1.5,
2.75478}, {2., 1.0017}}


and my measurement errors (or uncertainties) are:

u={0.041, 0.0235, 0.011, 0.0035, 0.001, 0.0035, 0.011, 0.0235, 0.041}


There is some documentation at wolfram's website that show me how to handle it. But I'd like to add Weights to my fit. Lets say:

weights={0.4, 1.1, 1.6, 1.9, 2., 1.9, 1.6, 1.1, 0.4}


Meaning that my central is more important for my fit. Any idea of how can I do this? I know how to do the easy part of the job:

• NonlinearModelFit[...., Weights -> weights]? Dec 29, 2017 at 21:23
• Writing an explicit statistical model that describes the situation where you need both weights and "uncertainties" would be helpful. Usually weights are associated with varying uncertainties so to have "both" seems a bit odd.
– JimB
Dec 29, 2017 at 23:19
• Depending on what the "uncertainties" are (2 standard deviations, one standard deviation, variances; these might be well-defined in one field but not in others) you might try what @corey979 suggests but with Weights -> weights/u or Weights -> weights/u^2.
– JimB
Dec 30, 2017 at 2:06

Both NonlinearModelFit and LinearModelFit accept Weights as an optional parameter.

data = {{-2., 1.00884}, {-1.5, 2.75486}, {-1., 4.00577}, {-0.5,
4.75401}, {0., 5.00771}, {0.5, 4.7533}, {1., 4.00055}, {1.5,
2.75478}, {2., 1.0017}}

ListPlot[data, PlotStyle -> Red]


u = {0.041, 0.0235, 0.011, 0.0035, 0.001, 0.0035, 0.011, 0.0235,
0.041}

weights = {0.4, 1.1, 1.6, 1.9, 2., 1.9, 1.6, 1.1, 0.4}


You have a linear relationship between the weights and uncertainties, the higher the uncertainty the lower the weight and vice-versa.

ListPlot[Transpose@{u, weights}, PlotStyle -> Red,
AxesLabel -> {"Uncertainty", "Weight"}]


Let's say that you decide to use LinearModelFit for the solver.

lm = LinearModelFit[data, {x, x^2}, x, Weights -> weights]

Normal@lm

(* 5.00461 - 0.00120898 x - 1.00009 x^2 *)

Show[
ListPlot[data, PlotStyle -> Red],
Plot[lm[x], {x, -2, 2}, PlotStyle -> Blue]
]