
Parametrization of the negative binomial and gamma distribution
Klaus K. Holst
Source:vignettes/param.Rmd
param.RmdThe negative binomial distribution describes the probability of
seeing a given number of failtures failures before obtaining
successes in iid Bernoulli trials with probability parameter
.
More generally, let
for real
parameters
,
,
then
has distribution given by
The
stats::rbinom function uses this parametrization, and we
have
Further, if
and
are independent then
.
To simulate from a negative binomial distribution with specific mean
and variance we can use the carts::rnb function
## [1] 0.98320 1.92371
The negative binomial distribution can also be constructed as a gamma-poisson mixture. We write when is gamma-distributed with shape and rate parameter , and the density function is given by
This parametrization leads to
We will exploit that the gamma distribution is closed under both convolution and scaling. Let and be independent and then
b <- 2
a1 <- 1
a2 <- 3
r <- 0.3
n <- 1e4
## Shape-rate parametrization
z1 <- rgamma(n, a1, b)
z2 <- rgamma(n, a2, b)
## r(z1+z2) ~ gamma(a1+a2, b/r)
plot(density(r * (z1 + z2)))
curve(dgamma(x, a1 + a2, b / r), add = TRUE, col = "red", lty = 2)
The negative binomial distribution now appears as the gamma-poisson mixture in the following way. If we let with stochastic rate , then .
Consider now , hence and , and let for some fixed , then direct calculations shows that and
## [1] 2.00573 14.21768
x <- seq(0, 30)
mf <- function(y, x) sapply(x, function(x) mean(y == x))
plot(x, mf(z, x), type = "h", ylab = "p(x)")
y <- rpois(length(z), 2 * rgamma(length(z), 1 / 3, 1 / 3))
lines(x + 0.1, mf(y, x), type = "h", col = "red")