Message Boards Message Boards


[?] Understanding fitting with NonlinearModelFit

Posted 1 month ago
4 Replies
2 Total Likes

I'm trying to get a better handle on fitting (using NonlinearModelFit and Fit), and I can't understand why my test problem doesn't work. I'm generating a set of points on a half circle, with some added noise:

randomNums = Table[{x, Sqrt[5^2 - x^2] + Random[]}, {x, -5, 5, 0.5}]

Next I'm simply trying to fit a nlm to this set of data:

nlm = NonlinearModelFit[  randomNums,  a Sqrt[b r^2 - c x^2] + d,  {a, b, c, d, r},  x  ]

However, this just throws back an error that I don't understand:

NonlinearModelFit::nrlnum: The function value {0.653833 +4.89898 I,-2.09763+4.38748 I,-2.43186+3.87298 I,-2.89856+3.3541 I,-3.65104+2.82843 I,-3.59796+2.29129 I,-3.77146+1.73205 I,-4.11906+1.11803 I,-4.72929+0. I,-3.72242+0. I,-3.8093+0. I,-4.07116+0. I,-4.22766+0. I,-4.74941+1.11803 I,-3.93771+1.73205 I,-4.14349+2.29129 I,-3.93865+2.82843 I,-3.55063+3.3541 I,-2.80604+3.87298 I,-1.3348+4.38748 I,0.0544825 +4.89898 I} is not a list of real numbers with dimensions {21} at {a,b,c,d,r} = {1.,1.,1.,1.,1.}.
4 Replies


 randomNums = Table[{x, Sqrt[5^2 - x^2] + Random[]}, {x, -5, 5, 0.5}];
 nlm = NonlinearModelFit[randomNums, a Re[Sqrt[b r^2 - c x^2]] + d, {a, b, c, d, r}, x] // Quiet
 Show[{ListPlot[randomNums, PlotStyle -> Black], Plot[(nlm // Normal), {x, -5, 5}, PlotStyle -> Red]}]

enter image description here

For more information see: here, here and here.

Posted 1 month ago

The main issue is that the model is overparameterized as there are really only 3 rather than 5 parameters that can be fit. For example, $b$ and $r$ are always together in the single term $b r^2$. Maybe one could estimate $b r^2$ but not $b$ and $r$ separately.

To make the model match the form as to how the data was generated one can rewrite the model as

$$\left(a \sqrt{c}\right) \sqrt{\frac{b r^2}{c}-x^2}+d$$

So $a\sqrt{c}$, $\frac{b r^2}{c}$, and $d$ can be estimated. To rewrite the model so that NonlinearModelFit can work might be

randomNums = Table[{x, Sqrt[5^2 - x^2] + Random[]}, {x, -5, 5, 0.5}];
nlm = NonlinearModelFit[randomNums, {a Re[Sqrt[b - x^2]] + d, b >= 25},
   {a, {b, 30}, d}, x, MaxIterations -> 5000];
(* {a -> 1.05357, b -> 25.0679, d -> 0.240849} *)

I don't know why you chose a uniform distribution from 0 to 1 as the error function. Usually one assumes that the error distribution has a mean (or median) of zero. (And Random has been replaced with RandomReal since version 6.)

Thanks Jim, I realize it was over parameterized, but I was just playing with the fitting functions for practice before working on a real fitting problem.

I don't understand you comment about the error function, are you referring to my "test data" I made up? Again, I was just trying to understand the syntax and workings of the function, the actual data and fitting here was just a quick example choice.

Posted 1 month ago

I understand that this was test data. I just wanted to point out that typically one chooses an error with a mean of zero. Using RandomReal[{-0.5,0.5}] would accomplish this. (Otherwise, the random component also includes a fixed component that probably is best incorporated in the fixed part of the model. Not required but I think easier to interpret.

Overparameterized models do have problems with convergence (even if the predictions are just fine) so examples without overparameterized models don't have that issue confounding the other issues with model fit.

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

Group Abstract Group Abstract