# Notes on the Negative Binomial Distribution in Ecology

There are many different formulations of the negative binomial distribution (NBD).

## Default Formulation

In the default formulation, it is the number of failures to occur in a series of Bernoulli trials before a target number of successes is reached.

For example consider the number of failures that occur before 10 successes ($$N$$) are achieved where the probability of success ($$\rho$$) is 0.5.

With R this can be achieved as follows.

N <- 10
rho <- 0.5
hist(rnbinom(1000, size = N, prob = rho))

## Ecological Formulation

A common use of the NBD in ecology is to model over-dispersion in count data. In this situation a more useful formulation is in terms of the mean number of counts ($$\mu$$) and the dispersion ($$\phi$$) where the standard deviation of the distribution is given by

$\sigma = \sqrt{\mu + \mu^2 \cdot \phi}$

It is worth noting that if $$\phi = 0$$ then the NBD is equivalent to the Poisson distribution.

#### R

In R, this alternative formulation is parameterised in terms of size ($$1/\phi$$)

mu <- 100
phi <- 10
x <- rnbinom(1000, mu = mu, size = 1/phi)
mean(x)
# 100.954
sd(x)
# 33.07028

#### TMB

In TMB, the default formulation dnbinom() follows the default convention of using $$N$$ (size) and $$\rho$$ (prob). However, an alternative formulation, dnbinom2(), uses the mean ($$\mu$$) and variance ($$\sigma^2$$). To reparameterise in terms of $$\mu$$ and $$\phi$$, the variance should be set to be $$\mu + \mu^2 \cdot \phi$$.

#### STAN

In STAN, there is a formulation neg_binomial_2() which uses the alternative R formulation except the second parameter (which is equivalent to $$1/\phi$$) is called phi.

For examples of the NBD in action see https://github.com/joethorley/bioRxiv-028274/blob/master/model-lek.R.