Created:October 4, 2024
Last Updated:March 29, 2025
This calculator helps you compute the probabilities of a negative binomial distribution given the number of successes needed (r) and the probability of success (p). You can find the probability of achieving a certain number of failures before the r-th success. The distribution chart shows the probability mass function (PMF) of the negative binomial distribution.
Calculator
Parameters
Interactive Distribution Chart
Enter parameters and calculate to view the distribution
Learn More
What is the Negative Binomial Distribution?
The negative binomial distribution, also known as the Pascal distribution, is a discrete probability distribution that models the number of failures in a sequence of independent Bernoulli trials before a specified number of successes occurs. 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:
How to Calculate Negative Binomial Distribution in R
R
library(tidyverse)
r <- 3 # number of successes needed
p <- 0.4 # probability of success on each trial
# P(X = )
prob_exact <- dnbinom(7, size = r, prob = p) # 0.06449725
print(str_glue("P(X = 7): {prob_exact}"))
# P(X <= 9)
prob_cumulative <- pnbinom(9, size = r, prob = p) # 0.9165567
print(str_glue("P(X <= 9): {prob_cumulative}"))
# mean and variance
mean <- r * (1-p)/p
variance <- r * (1-p)/(p^2)
print(str_glue("Mean: {mean}")) # 4.5
print(str_glue("Variance: {variance}")) # 11.25
# plot
x <- 0:20
pmf <- dnbinom(x, size = r, prob = p)
pmf_df <- data.frame(x = x, pmf = pmf)
ggplot(pmf_df, aes(x = x, y = pmf)) +
geom_col() +
labs(
x = "Number of failures before r successes",
y = "Probability",
title = "Negative Binomial Distribution PMF"
) +
theme_minimal()
How to Calculate Negative Binomial Distribution in Python
Python
import scipy.stats as stats
import numpy as np
import matplotlib.pyplot as plt
r = 3 # number of successes needed
p = 0.4 # probability of success on each trial
# P(X = 7)
prob_exact = stats.nbinom.pmf(7, r, p)
print(f"P(X = 7): {prob_exact:.4f}")
# P(X <= 9)
prob_cumulative = stats.nbinom.cdf(9, r, p)
print(f"P(X <= 9): {prob_cumulative:.4f}")
# mean and variance
mean = r/p
variance = r * (1-p)/(p**2)
print(f"Mean: {mean:.4f}")
print(f"Variance: {variance:.4f}")
# plot
x = np.arange(0, 21)
pmf = stats.nbinom.pmf(x, r, p)
plt.figure(figsize=(10, 6))
plt.vlines(x, 0, pmf, colors="b", lw=2)
plt.plot(x, pmf, "bo", ms=8)
plt.xlabel("Number of failures before r successes")
plt.ylabel("Probability")
plt.title("Negative Binomial Distribution PMF")
plt.grid(True)
plt.show()