StatsCalculators.com

Negative Binomial (Pascal) Distribution

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:P(X=k)=(k+r1k)pr(1p)kP(X = k) = \binom{k+r-1}{k} p^r (1-p)^k

Where:

  • rr is the number of successes needed
  • kk is the number of failures before achieving rr successes
  • pp is the probability of success on each trial

Properties

  • Mean: E(X)=r(1p)pE(X) = \frac{r(1 - p)}{p}
  • Variance: Var(X)=r(1p)p2Var(X) = \frac{r(1 - p)}{p ^ 2}

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()