C++ boost library to generate negative binomial random variables

103 Views Asked by At

I'm new to C++ and I'm using the boost library to generate random variables. I want to generate random variables from a negative binomial distribution.

The first parameter of boost::random::negative_binomial_distribution<int> freq_nb(r, p); has to be an integer. I want to expand that to a real value. Therefore I would like to use a poisson-gamma mixture, but I fail to.

Here's an excerpt from my code:

    int nr_sim = 1000000;
    double mean = 2.0;
    double variance = 15.0;
    double r = mean * mean / (variance - mean);
    double p = mean / variance;
    double beta = (1 - p) / p;

    typedef boost::mt19937 RNGType;
    RNGType rng(5);

    boost::random::gamma_distribution<double> my_gamma(r, beta);
    boost::random::poisson_distribution<int> my_poi(my_gamma(rng));

    int simulated_mean = 0;
    for (int i = 0; i < nr_sim; i++) {
        simulated_mean += my_poi(rng);
    }
    double my_result = (double)simulated_mean / (double)nr_sim;

With my_result == 0.5 there is definitly something wrong. Is it my_poi(my_gamma(rng))? If so, what is the correct way to solve that problem?

0

There are 0 best solutions below