2
$\begingroup$

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:

$\endgroup$
3
  • 1
    $\begingroup$ NonlinearModelFit[...., Weights -> weights]? $\endgroup$
    – corey979
    Dec 29, 2017 at 21:23
  • $\begingroup$ 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. $\endgroup$
    – JimB
    Dec 29, 2017 at 23:19
  • $\begingroup$ 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. $\endgroup$
    – JimB
    Dec 30, 2017 at 2:06

1 Answer 1

1
$\begingroup$

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]

Mathematica graphics

Your uncertainties and weights are

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"}]

Mathematica graphics

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]
 ]

Mathematica graphics

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.