Negative Binomial Distribution Calculator
Calculator
Parameters
Distribution Chart
Enter parameters and calculate to view the distribution
Learn More
What is the Negative Binomial Distribution?
The negative binomial distribution models the number of failures before achieving a specified number of successes in a sequence of independent Bernoulli trials. Each trial has the same probability of success (p).
Probability Mass Function:
Where:
- is the number of successes needed
- is the number of failures before achieving successes
- is the probability of success on each trial
Properties
- Mean:
- Variance:
Applications
1. Quality Control
Used to model the number of defective items encountered before finding a specified number of non-defective items in manufacturing.
2. Clinical Trials
Models the number of unsuccessful trials before achieving a target number of successful treatments in medical research.
3. Marketing
Analyzes the number of non-converting customers before achieving a specified number of sales.
4. Reliability Testing
Models the number of failures before achieving a set number of successful tests in product development.
R Implementation
R
1# Parameters
2r <- 3 # number of successes needed
3p <- 0.4 # probability of success on each trial
4
5# Calculate P(X = 10) - probability of needing exactly 10 trials
6prob_exact <- dnbinom(7, size = r, prob = p)
7print(paste("P(X = 10):", prob_exact))
8
9# Calculate P(X <= 12) - probability of needing 12 or fewer trials
10prob_cumulative <- pnbinom(9, size = r, prob = p)
11print(paste("P(X <= 12):", prob_cumulative))
12
13# Calculate mean and variance
14mean <- r/p
15variance <- r * (1-p)/(p^2)
16print(paste("Mean:", mean))
17print(paste("Variance:", variance))
18
19# Plot PMF
20x <- 0:20
21pmf <- dnbinom(x, size = r, prob = p)
22plot(x, pmf, type = "h",
23 xlab = "Number of failures before r successes",
24 ylab = "Probability",
25 main = "Negative Binomial Distribution PMF")
Python Implementation
Python
1import scipy.stats as stats
2import numpy as np
3import matplotlib.pyplot as plt
4
5# Parameters
6r = 3 # number of successes needed
7p = 0.4 # probability of success on each trial
8
9# Calculate P(X = 10) - probability of needing exactly 10 trials
10prob_exact = stats.nbinom.pmf(7, r, p)
11print(f"P(X = 10): {prob_exact:.4f}")
12
13# Calculate P(X <= 12) - probability of needing 12 or fewer trials
14prob_cumulative = stats.nbinom.cdf(9, r, p)
15print(f"P(X <= 12): {prob_cumulative:.4f}")
16
17# Calculate mean and variance
18mean = r/p
19variance = r * (1-p)/(p**2)
20print(f"Mean: {mean:.4f}")
21print(f"Variance: {variance:.4f}")
22
23# Plot PMF
24x = np.arange(0, 21)
25pmf = stats.nbinom.pmf(x, r, p)
26plt.figure(figsize=(10, 6))
27plt.vlines(x, 0, pmf, colors="b", lw=2)
28plt.plot(x, pmf, "bo", ms=8)
29plt.xlabel("Number of failures before r successes")
30plt.ylabel("Probability")
31plt.title("Negative Binomial Distribution PMF")
32plt.grid(True)
33plt.show()