This project has moved. For the latest updates, please go here.

Error in sampling from Negative Binomial

Jun 1, 2015 at 9:06 AM
Edited Jun 1, 2015 at 9:06 AM
I'm using Math.Net to sample values from an overdispersed Poisson distribution. I'm doing this using the negative binomial link, as described here:

My code currently looks like this:

private static double ODPoisson(double lambda, double dispersion)
    double p = 1 / (dispersion - 1);
    double r = lambda * p;

    if (dispersion == 1)
        return Poisson.Sample(lambda);
        return NegativeBinomial.Sample(r, p);

What I've found is that this works well for low values of lambda. As soon as I try to sample with a lambda of 1000 and a dispersion parameter of 2, the code simply 'hangs', i.e. method keeps running but no value is returned. I've even looped through this method to test various combinations of input parameters (lambda from 1 to 1000, dispersion = 2), and the code 'hangs' at different combinations every time. Sometimes it'll sample for all combinations up to lambda = 750, other times up to lambda = 500. This happens by simply re-running the console app and making no code changes.

I've included the 'IsValidParameterSet' check before every run, and even though the parameters are considered valid, the sample is still not generated. To further test whether the input parameters are valid, I've tested the same parameters with the NegativeBinomial.CDF method at the 50th percentile, and it returns a value every time.

Is there an error in the NegativeBinomial.Sample method? If not, how do I fix this problem?