Message Boards Message Boards


Fitting data with error terms?

Posted 1 month ago
4 Replies
0 Total Likes

I have the following dataset:


which is generated by data:

 {-5, Around[55., 3.42]},
 {-4, Around[37., 2.13]},
 {-3, Around[23., 3.60]},
 {-2, Around[13., 2.69]},
 {-1, Around[7., 2.30]},
 {0, Around[5., 3.87]},
 {1, Around[7., 2.94]},
 {2, Around[13., 2.29]},
 {3, Around[23., 3.48]},
 {4, Around[37., 3.70]},
 {5, Around[55., 3.25]}

This is actually the plot of

enter image description here

plus some random error.

Now I want to find a fit for this dataset. How do I take into consideration the uncertainty of the measurements? I didn't understand it from the documentation.

NonlinearModelFit[Thread[{data[[All, 1]], data[[All, 2]]}], 
 a x^2 + b, {a, b}, x, VarianceEstimatorFunction -> (1 &), 
 Weights -> Map[#[[2]]["Uncertainty"] &, data]]

I should break down this code block for you:

  • Thread[{data[[All, 1]], data[[All, 2]]}] makes a list of {{x1,y1},{x2,y2},...}

  • ax2+b is the model to fit

  • VarianceEstimatorFunction -> (1 &), Weights -> Map[#[[2]]["Uncertainty"] &, data] This is an option I add to NonlinearModelFit that according to the documentation

Using [the above] ∆yi is treated as the known uncertainty of measurement Subscript[y, i], and parameter standard errors are effectively computed only from the weights.

  • Map[#[[2]]["Uncertainty"] &, data] returns a list of the uncertainties for the points yi's.

But the result I get for this line of code is "too good":

(* FittedModel[5. + 2. x^2 ] *)

What is wrong? Were the uncertainties considered at all?

4 Replies
Posted 1 month ago

Hi Ehud,

The weights need to be 1 / uncertainty^2 see this. There is no need for Thread, the data is already in the right form.

errors = data /. {_, u_} :> u["Uncertainty"]
nlm = NonlinearModelFit[data, a x^2 + b, {a, b}, x, Weights -> 1/errors^2]

(* x^2 (2.00±0.10) + (5.0±1.3) *)
Posted 1 month ago

Thanks very much once again for the help and explanation.

Posted 1 month ago

Do you not believe that you didn't add any errors to the response variable?

Around just characterizes the precision of the observation but it is not the error associated with the response variable.

Posted 1 month ago

The problem is that you didn't really add any noise to the response variable. The y's are simply 2*x^2+5. So you end up with a perfect fit no matter what (positive) weights you use.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract