# Strange behavior of TF1 with ROOT::Math::negative_binomial_pdf

Dear rooters:

I am trying to generate a bunch of negative binomial distribution with TF1 using “r” and “p” parameters that I have extracted from a set of data. From the data I extract a mean and a variance term that I use to calculate “r” and “p” and construct the functions. I just took one example where I have a mean of 1.63 from where I infer an r =18.9242 and a p=0.920519. Now the problem is that I create the TF1 using `ROOT::Math::negative_binomial_pdf(x,0.920519,18.9242)` and when I plot this function I get a mean of 2.13252 (from `Mean(min,max)`) and the function looks like shifted to the right (it never reaches x=0). I have cross-checked my calculation of the mean using `std::negative_binomial_distribution<int> d(round(18.9242),0.920519)` and the result is correct (1.63774). I am not sure what I am doing wrong. I attach a simple macro that illustrates the problem.

Thank you very much in advance
F.
test_NBD.C (2.0 KB)

ROOT Version 6.06/06):
Platform, compiler (Ubuntu 16 gcc and Mac El Capitan):

Hi,

I think the problem is due to the fact that the negative binomial distribution is an integer distribution.
TF1::GetRandom makes some interpolation inside and generate values assuming a continuous distribution.
Converting the TF1 to a TH1D and using correct binning (having bin center equal to integer values fixes the problem), as you see in the attached macro.

However I would use directly the function NegativeBinomial of ROOT::Math::GSLRandom.

``````ROOT::Math::GSLRngMT r;
r.NegativeBinomial(0.920519,18.9242);
``````

Lorenzo
test_NBD.C (2.0 KB)

Hi,

Thank you very much, I figured it out the way you mention. And it is nice to see that there is an alternative and simpler way to sample from the distribution.

Cheers
F.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.