Consider a Poisson model for count data,
y∼Poisson(θ),θ≥0.
The parameter θ can be interpreted as the rate of arrivals, and importantly, E[y]=Var(y)=θ. An unfortunate property of this Poisson model is that it cannot model overdispersed data or data in which the variance is greater than the mean. This is because Poisson regression has one free parameter. However, if we place a gamma prior on θ,
yθ∼Poisson(θ)∼gamma(r,1−pp),
and then marginalize out θ, we get a negative binomial (NB) distribution, which has the useful property that its variance can be greater than its mean. The derivation is
p(y)=∫0∞p(y∣θ)p(θ)dθ=∫0∞(y!θye−θ)(Γ(r)(1−pp)r1θr−1e−θ(1−p)/p)dθ=y!Γ(r)(1−p)rp−r∫0∞θr+y−1e−θ/pdθ=⋆y!Γ(r)(1−p)rp−rpr+yΓ(r+y)=Γ(r)y!Γ(r+y)py(1−p)r=†(r−1)!y!(r+y−1)!py(1−p)r=(yy+r−1)py(1−p)r=NB(r,p).
Step ⋆ holds because of the following equality,
∫0∞xbe−axdx=ab+1Γ(b+1).
Wikipedia claims that this is part of the usefulness of the gamma function: integrals of expressions of the form f(x)e−g(x), which model exponential decay, can be sometimes solved in closed form using the above equation.
Step † uses the fact that Γ(x)=(x−1)!.