pythonscipystatsmodelsglmbinomial-cdf

CDF of a Negative Binomial with mu and alpha in python


I have some count data that somewhat resembles a poisson distribution but is overdispersed. I have fitted a negative binomial glm model in python using statsmodels with a chose value for alpha (i.e.dispersion parameter). I have then applied the model to a test set to make some predictions. I now want to calculate the cumulative distributiion function so that I can calculate the probability that a random variable X<=9 given my outputted prediction mu (i.e.7.8) and pre-determined value for alpha (i.e. 0.2).

The scipy documentation (https://docs.scipy.org/doc/scipy/reference/generated/scipy.stats.nbinom.html) suggests that I can calculate this in python with nbinom.cdf(k, n, p) but how can i obtain the values of n and p, when all i have is mu and alpha?


Solution

  • p and n are related to alpha and mu and the relation is given in the documentation you provide:

    sigma_squared = mu + alpha * mu**2
    p = mu / sigma_squared
    n = mu**2 / (sigma_squared - mu)
    

    which is equivalent to:

    p = 1 / (1 + alpha * mu)
    n = 1 / alpha