# Choosing reasonable parameters for a negative binomial distribution

My data is a list of observations and a count for each observation. The data is overdispersed, the mean is ~1,200 and the variance is ~18,000,000. I want to use a negative binomial model to assign p-values for each observation. I tried doing this with a Poisson model (I know the number of trials and the probability of success for each trial) but the p-values became so small for many of the observations that python interpreted the number as 0. Values that were just a couple hundred from the mean were called highly significant because the variance was being underestimated by the model.

Is there an easy to implement way to estimate the parameters for the negative binomial using my data in python? I also need help developing an intuition for what r and p represent in the negative binomial when it is really a mixed Poisson Gamma distribution.

• After reading this blog post it appears that r=beta and p = alpha/(1-alpha) where alpha and beta are the parameters from the gamma distribution. So I guess I need to estimate alpha and beta somehow. – elsherbini Mar 29 '15 at 21:38
• Not quite sure what you mean by the p-value of a single observation. – Scortchi - Reinstate Monica Mar 30 '15 at 11:07
• Sorry, my data is a list of observations with counts asociated with each one. Something like [100, 42000, 1300, ...]. Once I have a ML estimate of the parameters for the distribution, I want to ask what the chances are of seeing 42000, for instance, by random chance. – elsherbini Mar 30 '15 at 12:26
• Not a p-value then, just a probability under an assumed distribution. And note that small probabilities of individual observations aren't in themselves evidence that the assumed distribution's not a good fit (think of the distribution of winning combinations in a lottery). Using logarithms would let you deal with them more easily. – Scortchi - Reinstate Monica Mar 30 '15 at 12:53
• In python numbers 0.0 < x < 1e323 are interpreted as 0.0, so taking the log yields -infinity. I rejected the poisson values because the mean and variance were three orders of magnitude apart, and knowing the underlying process it is likely inappropriate to identify the counts of so many observations as significant. – elsherbini Mar 30 '15 at 13:01

For maximum-likelihood estimation, you'll need to solve the score equations numerically: see http://en.wikipedia.org/wiki/Negative_binomial_distribution#Maximum_likelihood_estimation. (Or directly maximize the log-likelihood.)

For method-of-moments estimation, following e.g. the parametrization given here, substitute the sample mean & variance for the population mean $\operatorname{E}Y$ & variance $\operatorname{Var}Y$, & solve for the parameters $\mu$ & $\theta$. In this case the estimates are

$$\tilde\mu = \bar y$$

$${\tilde\theta}= \frac{\bar y^2}{s_y^2 - \bar y}$$

where $\bar y$ is the sample mean, & $s_y^2$ the sample variance.

• This follow-up may betray my ignorance, but why do I want to do method-of-moments estimation after getting the maximum likelihood estimate of the parameters? I started to implement MLE for the parameters but am not having any success. stackoverflow.com/questions/29338152/… – elsherbini Mar 30 '15 at 12:31
• You don't: they're alternative estimators. (I gave the MOM estimator as you asked for something easy.) – Scortchi - Reinstate Monica Mar 30 '15 at 12:46
• Re your MLE implementation: plot the likelihood to see what's going on. In the linked post you seem to be estimating three parameters, which you haven't mentioned here; you also have some rather extreme outliers. – Scortchi - Reinstate Monica Mar 30 '15 at 13:00
• Ah, great idea, I'll plot the likelihood. The third parameter "loc" should be fixed at 0, and I think I just figured out a way to do that. I'm only interested in the negative binomial parameters r and p. – elsherbini Mar 30 '15 at 13:03
• I think one of the main problems I am having is that for the Poisson model, I understood what n and p represented, but I don't have an intuition what r and p mean for the negative binomial when it is being used for these data. – elsherbini Mar 30 '15 at 13:07