Negative binomial distribution (source code)

= Negative binomial distribution
{wiki}

= Negative-binomial
{synonym}

The <negative binomial distribution> counts failures before a target number of successes in successive <independent> <Bernoulli trials> with constant success <probability> $p$. In this stopping model, $r$ is a positive <integer> and $0<p\leq1$. On the failures-before-the-$r$th-success convention, the <random variable> $X$ takes values $0,1,\ldots$; for $0<p<1$, its <probability mass function> is
$$
\mathbb P(X=x)=\binom{x+r-1}{x}(1-p)^x p^r,
\qquad x=0,1,\ldots.
$$
The <binomial coefficient> counts possible arrangements before the final success, as explained by the <negative binomial stopping argument>. At $p=1$, every trial succeeds: $X=0$ with <probability> one, so its <expected value> and <variance> are both zero. This handles the endpoint without needing to interpret $0^0$. At $p=0$, the target is never reached and there is no proper finite waiting-count law.

For a worked example, toss a fair coin until the second head, calling heads $S$ and tails $F$. Thus $r=2$ and $p=1/2$. Exactly two failures means that the second success is on trial four. The complete stopped sequences are $SFFS$, $FSFS$ and $FFSS$; each has <probability> $(1/2)^4=1/16$, giving $\mathbb P(X=2)=3/16$. The first three masses are
$$
\mathbb P(X=0)=\frac14,\qquad \mathbb P(X=1)=\frac14,\qquad \mathbb P(X=2)=\frac3{16}.
$$
Both $0$ and $1$ are <modes>, each with mass $1/4$: there is no unique <mode> here. Later masses decrease, since $\mathbb P(X=x+1)/\mathbb P(X=x)=(x+2)/(2x+2)<1$ for $x\geq1$.

\Image[/negative-binomial-stopping-sequences.png]
{title=Exactly two failures before the second success: three stopped sequences, each of probability 1/16}

The <expected value> and <variance> of the failures count are
$$
\mathbb E X=\frac{r(1-p)}p,\qquad \operatorname{Var}(X)=\frac{r(1-p)}{p^2}.
$$
If instead $N=X+r$ counts all trials, including the $r$ successes, then $N\geq r$ and
$$
\mathbb E N=\frac rp,\qquad \operatorname{Var}(N)=\frac{r(1-p)}{p^2}.
$$
For the fair-coin example, $\mathbb E X=2$, $\operatorname{Var}(X)=4$, and $\mathbb E N=4$. Shifting the count changes its <expected value> but preserves its <variance>. When $r=1$, $X$ has the failures-count <geometric distribution>, while $N=X+1$ has the all-trials <geometric distribution>. The two conventions and the moments are treated in https://www.randomservices.org/random/bernoulli/NegativeBinomial.html[Kyle Siegrist, The Negative Binomial Distribution, Random Services].

\Image[/negative-binomial-failure-counts.png]
{title=Negative binomial failure counts for success probability 1/2 and targets of one, two and five successes}

The <negative binomial distribution> is useful when a procedure runs until it collects a target number of successes: for example, obtaining $r$ usable samples from <independent> attempts with a stable acceptance <probability>. A <binomial distribution> instead counts successes in a fixed number of trials. The attractive connection is that these two experimental questions can be answered from the same trial sequence, using the <negative binomial stopping argument>. For the failures count and $0<p<1$, the positive <mean> $\mu=r(1-p)/p$ satisfies $\operatorname{Var}(X)=\mu/p>\mu$, so the law allows <overdispersion> relative to a <Poisson distribution>. At $p=1$, both quantities vanish and the inequality is not strict. The separate <Poisson-gamma mixture> explains another way this count law arises; its noninteger shape parameters do not represent a literal target number of successes.