Negative binomial distribution calculator
How long until the r-th success? Probabilities for failures or trials, plus the smallest k for a target.
What is the negative binomial distribution?
It describes how long you wait for the r-th success when you repeat independent tries that each succeed with the same probability p. Count the failures along the way and you get P(X = k) = C(k + r − 1, k) × p^r × (1 − p)^k for k = 0, 1, 2, …; count the trials instead and you get P(N = n) = C(n − 1, r − 1) × p^r × (1 − p)^(n − r) for n = r, r + 1, …. The last try must be a success, and the r − 1 earlier successes can fall anywhere among the other tries, which is where the binomial coefficient comes from.
Failures before the r-th success, or trials until it? Which convention should I pick?
Both are in use, so check what your source means. R’s dnbinom(x, size = r, prob = p) and SciPy’s nbinom(n = r, p) count failures: the mean is r(1 − p) ÷ p and x starts at 0. Many textbooks, including Penn State’s STAT 414, count trials: the mean is r ÷ p and n starts at r. The two are linked by n = k + r, so P(N = n) = P(X = n − r), and the variance, r(1 − p) ÷ p², is the same either way. This calculator lets you choose and always shows the other form of the same event. With r = 3 and p = 0.2 the mean is 12 failures or 15 trials.
Worked example: oil wells
Penn State’s STAT 414 gives an exploratory oil well a 20% chance of striking oil. The chance that the first strike comes on the third well is 0.8² × 0.2 = 0.128 (this is r = 1, the geometric case). The chance that the third strike comes on the seventh well is C(6, 2) × 0.8⁴ × 0.2³ = 0.049 (0.049152 exactly). To set up three producing wells the company expects to drill r ÷ p = 3 ÷ 0.2 = 15 wells, with variance r(1 − p) ÷ p² = 60. Enter r = 3, p = 0.2, trials, n = 7 to check.
Worked example: selling candy bars
Wikipedia’s example has a child who must sell five candy bars, with a 0.6 chance of a sale at each house. The chance that the fifth sale comes at the 7th house is C(6, 4) × 0.6⁵ × 0.4² = 0.18662; at the 10th house it is 0.10033. The chance of being done by the 8th house, the sum of the probabilities for the 5th to the 8th house (0.07776, 0.15552, 0.18662 and 0.17418), is 0.59409. That equals the binomial chance of at least 5 sales in 8 houses: more trials than r are only needed if some tries fail. After 30 houses the chance of still being short of five sales is about 1.8 × 10⁻⁷.
How many trials do I need to be 95% sure?
Switch to “Solve for k” and enter a target. The calculator returns the smallest k with P(X ≤ k) at or above the target, which is how R’s quantile function qnbinom is defined. For r = 3 and p = 0.2, collecting three successes takes at most 27 failures, that is 30 trials, with probability 95.6%; with 29 trials the chance is only 94.8%. The mean of 15 trials is far below that 95% mark because the distribution has a long right tail.
How is it related to the geometric, binomial and Poisson distributions?
With r = 1 it is the geometric distribution (use the geometric calculator for the same numbers in the first-success form). The binomial distribution fixes the number of trials and counts successes; the negative binomial fixes the number of successes and counts trials, and P(N ≤ n) equals the binomial probability of at least r successes in n trials. Whenever p is below 1 the negative binomial’s variance exceeds its mean (r(1 − p) ÷ p² against r(1 − p) ÷ p for failures), so it is often used for count data that are more spread out than a Poisson distribution allows. Its most likely value is ⌊(r − 1)(1 − p) ÷ p⌋ failures; when that number is a whole number, it and the one below tie.
Sources: documentation of R (stats::NegBinomial) and SciPy (scipy.stats.nbinom); Penn State, STAT 414 Introduction to Probability Theory, lesson 11 (sections 11.4–11.6); Wikipedia, “Negative binomial distribution”. Related: geometric distribution calculator, binomial probability calculator, Poisson distribution calculator and probability calculator.