Dear all,
I am trying to write a Monte Carlo simulation that needs data that are distributed after the negative binomial probability. ROOT provides the corresponding pdf already, so I thought it must be easy to use this one …
Here’s a minimal ‘working’ example (1.5 KB). That’s how I thought this should be done:
double my_negative_binomial_pdf(double* x, double* par)
{
const unsigned int k = static_cast<unsigned int>(x[0]);
const double p = par[0];
const double n = par[1];
Double_t f = ROOT::Math::negative_binomial_pdf(k, p, n);
return f;
}
void neg_binomial_plot(unsigned int runs=1000)
{
TCanvas *c = new TCanvas("c", "negative binomial", 900, 700);
TH1D* h = new TH1D("h", "negative binomial", 150, 0., 75.);
const double XMin = 0.;
const double XMax = 100.;
const int NParm = 2;
TF1* fNegBinomial = new TF1("fNegBinomial", my_negative_binomial_pdf, XMin, XMax, NParm);
fNegBinomial->SetParameter(0, 0.25); // p
fNegBinomial->SetParameter(1, 2000); // n
for (unsigned int r=0; r<runs; r++)
{
const double val = fNegBinomial->GetRandom();
h->Fill(val);
}
c->cd();
h->Draw();
}
Unfortunately, this will end up in a histogram with 1000 entries in the 0-bin. I am pretty sure it should be a distribution with a mean of ~2000. But I don’t get what I misunderstood about ROOT’s random number generation.
Any insight is highly appreciated! Thanks!
pbiel