### 7.4.2 The negative binomial distribution

#### The probability density function for the negative binomial distribution: negbinomial

If you repeatedly perform an experiment with probability of success p, then, given an integer n, the probability of k failures that occur before you have n successes is given by the negative binomial distribution, and can be computed with negbinomial(n,k,p). It is given by the formula (kn+k−1)pn(1−p)k. If you enter

negbinomial(4,2,0.5)

you will get

0.15625

Note that

⎛
⎝

n
k

⎞
⎠

=

n!
k! (n−k)!

=

n (n−1 ) … ( n−k+1)
k!

The second formula makes sense even if n is negative, and you can write negbinomial(n,k,p) = (k−n)pn (p−1)k, from which the name negative binomial distribution comes from. This also makes it simple to determine the mean (n(1−p)/p) and variance (n(1−p)/p2). The negative binomial is also called the Pascal distribution (after Blaise Pascal) or the Pólya distribution (after George Pólya).

#### The cumulative distribution function for the negative binomial distribution: negbinomial_cdf

The cumulative distribution function for the negative binomial distribution is given by the negbinomial_cdf command. Given parameters n and p, as above, then negbinomial_cdf(n,p,x) = Prob(X ≤ x) = negbinomial(n,0,p) + …+ negbinomial(n,floor(x),p), and negbinomial_cdf(n,p,x,y) = Prob(x ≤ X ≤ y) = negbinomial(n,ceil(x),p) + ⋯ + negbinomial(n,floor(y),p). If you enter

negbinomial_cdf(4,0.5,2)

for example, you will get

0.34375

#### The inverse distribution function for the negative binomial distribution: negbinomial_icdf

Given a value h, the inverse distribution function gives the smallest value of x so that Prob(X ≤ x) ≥ h. The negbinomial_icdf gives the inverse distribution function for the negative binomial distribution. If you enter

negbinomial_icdf(4,0.5,0.9)

for example, you will get

8