I'm having a little problem with the NonLinearModelFit function. I'm hoping someone can point me in the right direction please. I have this data points: https://www.dropbox.com/s/5hflg2nvko26hpz/question.nb

When I plot the data points I have this:


Then I use a Rice (Rician) Distribution Curve as model for fitting. For this, I used $$\frac{x \exp \left(-\frac{v^2+x^2}{2 \sigma ^2}\right) I_0\left(\frac{x v}{\sigma ^2}\right)}{\sigma ^2}$$

But when I plot both curves together I get something like this figure where the fit is the very tiny one below: enter image description here

I checked the Residual fit and I got something really disturbing. Showing that my fit has a large error margin:

enter image description here

Honestly, I'm not sure what's wrong. I've spent some time on this and I think I need an outside eye to help look at it. Any idea will be much appreciated. Thanks friends.

  • 4
    $\begingroup$ I think you will need an extra parameter (an amplitude) in order to match your data (which does not have an area of 1). $\endgroup$
    – chuy
    Jul 18, 2013 at 16:38
  • $\begingroup$ When I added the 3rd parameter $m$ as @PatoCriollo suggested, it doesn't seem to work well with the data. I've provided the data above maybe you may want to play around with it. The plot of the data point itself [Fig 1.] is smooth so I'm not sure what's going on. Thanks $\endgroup$
    – Afloz
    Jul 19, 2013 at 3:23
  • $\begingroup$ I think you're making some confusion. The plot of the data themselves does not have to match the plot of their distribution function. $\endgroup$ Jul 19, 2013 at 6:22
  • $\begingroup$ What do the 20…40 range and the values in the data represent? Is there any particular reason why you decided to use a Rice density? I'm getting reasonably good fits with a shifted gamma density, but if that's not allowed then there's little point in pursuing it. $\endgroup$ Jul 21, 2013 at 1:56
  • $\begingroup$ @RayKoopman, apology for late response. I considered Rice a good fit for the original curve however, if it's not too late, I'd love to see your idea on the shifted gamma function. $\endgroup$
    – Afloz
    Jul 26, 2013 at 8:28

2 Answers 2


@Chuy makes a valid point in the comments section of the OP. Here's the fit with a "fudge" factor, using the equation for f in the notebook you reference and assigning your data to the variable data.

xfit = NonlinearModelFit[data, fudge x/s^2 Exp[-((x^2 + v^2)/(2 s^2))] BesselI[0, (x v)/
    s^2] , {{fudge, 1}, {s, 0.5}, {v, 30}}, x]
Plot[xfit[x], {x, 0, 100}, Epilog -> {Red, PointSize[0.011], 
   Point@Transpose[{Range[Length[data]], data}]}]
(* {fudge -> 49.3597, s -> 10.3848, v -> 43.0833} *)

The result: a better but still not great fit.

enter image description here

  • $\begingroup$ Yes, this is the best I came up with as well using Rician distr. but I had to normalize the data with data/Total[data] before I got this. Would you please explain the fudge parameter means? It appears this is what @Chuy was talking about. $\endgroup$
    – Afloz
    Jul 26, 2013 at 8:31
  • $\begingroup$ Oh by "fudge" you mean something like an "enabler" or a"heuristic" a "weighting factor"? is that what you mean? $\endgroup$
    – Afloz
    Jul 26, 2013 at 8:49
  • $\begingroup$ @Methyl yes. I chose the more 'technical' term :-). There are cases when you may not want to normalize the data and this is one option in such cases. $\endgroup$ Jul 26, 2013 at 12:21
  • $\begingroup$ Ahaha. That makes sense. I'll call it the "Fudge Fit Factor". Thanks a lot B. You've done more that fit my data ;) $\endgroup$
    – Afloz
    Jul 26, 2013 at 15:55

sorry I can't access your notebook file. We got a firewall restriction at work to access online storage site.

I hope you find the following code helpful.

sample = RandomVariate[RiceDistribution[2.5, 3], 1000];
(*simulates your data*)
mymodel = EstimatedDistribution[sample, RiceDistribution[m, \[Alpha], \[Beta]]]
Show[Histogram[sample, 20, "ProbabilityDensity"], Plot[PDF[mymodel, x], {x, 0, 14}]]
  • $\begingroup$ Your last line is not going to work; you can use this, straight from the docs : Histogram[data, Automatic, "PDF", Epilog -> First@Plot[PDF[mymodel, x], {x, 0, 2}, PlotStyle -> Red]]. $\endgroup$ Jul 19, 2013 at 6:23
  • $\begingroup$ Thanks for the tip @b.gatessucks $\endgroup$
    – Zviovich
    Jul 19, 2013 at 13:13

Your Answer

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

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