Generalizing Waiting Times: Negative Binomial & Gamma Distributions

Joel Sanqui (Appalachian State University)

Overview & Roadmap

  1. Discrete Waiting Times: Geometric \(\rightarrow\) Negative Binomial
    • Derivation of PMF, CDF, Quantiles, Mean, and Variance
    • Parameter visualizations & R implementation
  2. Continuous Waiting Times: Exponential \(\rightarrow\) Gamma
    • Derivation of PDF, CDF, Quantiles, Mean, and Variance
    • Parameter visualizations & R implementation
  3. Applied Problems: Real-world examples solved analytically and in R

Part I: Discrete Waiting Times

From Geometric to Negative Binomial

  • Bernoulli Trial: Independent trials with success probability \(p \in (0, 1)\) and failure probability \(q = 1 - p\).
  • Geometric Distribution: Models the number of failures \(X\) before the 1st success (or total trials \(Y = X + 1\)).
  • Negative Binomial Distribution: Generalizes the Geometric by modeling the number of failures \(X\) before the \(r\)-th success (\(r \in \mathbb{Z}^+\)).

\[X \sim \text{NB}(r, p) \quad \text{where } r=1 \implies X \sim \text{Geom}(p)\]

(Note: R uses the “number of failures before \(r\) successes” parameterization).

Derivation of the Negative Binomial PMF

To observe \(x\) failures before the \(r\)-th success, we require \(x + r\) total trials:

  1. The final trial (\(x + r\)) must be a success (probability \(p\)).
  2. The preceding \(x + r - 1\) trials must contain exactly \(r - 1\) successes and \(x\) failures in any order.

Multiplying the combinations by the independent trial probabilities yields the PMF:

\[P(X = x) = \binom{x + r - 1}{r - 1} p^r (1 - p)^x = \binom{x + r - 1}{x} p^r (1 - p)^x, \quad x = 0, 1, 2, \dots\]

(Extension: Using the Gamma function \(\binom{x+r-1}{x} = \frac{\Gamma(x+r)}{x!\Gamma(r)}\) generalizes \(r\) to any positive real number \(r > 0\)).

Negative Binomial: CDF & Quantile Function

Cumulative Distribution Function (CDF)

The CDF sums the PMF up to \(k\) failures, which relates directly to the regularized incomplete beta function \(I_p(r, k+1)\):

\[F(k) = P(X \le k) = \sum_{x=0}^{k} \binom{x + r - 1}{x} p^r (1 - p)^x = I_p(r, k + 1)\]

Quantile Function

Because \(X\) is discrete, the quantile function for probability \(u \in (0, 1)\) is the smallest integer \(x\) satisfying the CDF threshold:

\[Q(u) = \inf \{ x \in \mathbb{Z}_{\ge 0} : F(x) \ge u \}\]

Negative Binomial: Mean & Variance

Since \(X\) is the sum of \(r\) independent \(\text{Geom}(p)\) random variables (\(X = \sum_{i=1}^r G_i\)), we use linearity of expectation and independence:

  1. Single Geometric (\(G_i\)): \[E[G_i] = \frac{1 - p}{p}, \quad \text{Var}(G_i) = \frac{1 - p}{p^2}\]

  2. Negative Binomial (\(X\)): \[E[X] = \sum_{i=1}^r E[G_i] = \frac{r(1 - p)}{p}\] \[\text{Var}(X) = \sum_{i=1}^r \text{Var}(G_i) = \frac{r(1 - p)}{p^2}\]

Visualizing the Negative Binomial PMF

Code
library(ggplot2)

x <- 0:25
df_nb <- expand.grid(x = x, r = c(1, 3, 6), p = c(0.3, 0.6))
df_nb$pmf <- dnbinom(df_nb$x, size = df_nb$r, prob = df_nb$p)
df_nb$params <- paste0("r = ", df_nb$r, ", p = ", df_nb$p)

ggplot(df_nb, aes(x = x, y = pmf, color = params)) +
  geom_line(alpha = 0.6) + geom_point(size = 2) +
  labs(title = "Negative Binomial PMF Across Parameters (r = 1 is Geometric)",
       x = "Number of Failures (x)", y = "P(X = x)", color = "Parameters") +
  theme_minimal(base_size = 13)

Figure 1

Simulating Negative Binomial in R

We can simulate \(N = 10,000\) experiments using rnbinom()

Code
set.seed(42)
N <- 10000; r <- 4; p <- 0.20

# Negative Binomial simulation
sim_nb <- rnbinom(N, size = r, prob = p)



# Compare empirical metrics against theoretical values
rbind(
  Theoretical    = c(Mean = r*(1-p)/p, Var = r*(1-p)/p^2, Prob_LE_10 = pnbinom(10, r, p)),
  Direct_rnbinom = c(Mean = mean(sim_nb), Var = var(sim_nb), Prob_LE_10 = mean(sim_nb <= 10))
)
                  Mean      Var Prob_LE_10
Theoretical    16.0000 80.00000  0.3018101
Direct_rnbinom 16.0861 80.45033  0.2976000

Visualizing Simulated vs. Theoretical NB

Code
sim_df <- as.data.frame(prop.table(table(sim_nb)))
names(sim_df) <- c("x", "Empirical")
sim_df$x <- as.numeric(as.character(sim_df$x))
sim_df$Theoretical <- dnbinom(sim_df$x, size = r, prob = p)

ggplot(sim_df[sim_df$x <= 40, ], aes(x = x)) +
  geom_col(aes(y = Empirical), fill = "steelblue", alpha = 0.6) +
  geom_line(aes(y = Theoretical), color = "darkred", linewidth = 1) +
  geom_point(aes(y = Theoretical), color = "darkred", size = 1.8) +
  labs(title = "Simulated Histogram vs. Theoretical PMF (r = 4, p = 0.20)",
       x = "Number of Failures (x)", y = "Probability") +
  theme_minimal(base_size = 13)

Figure 2

Applied Example: Negative Binomial

Problem: A quality control engineer inspects microchips until finding \(r = 4\) defective chips. Historically, the defect rate is \(p = 0.20\).

    1. What is the probability that 10 non-defective chips (failures) are inspected before the 4th defective chip is found?
    1. What is the probability of inspecting at most 10 non-defective chips?
    1. How many non-defective chips are expected, and what is the 90th percentile? What is the variance?

R Implementation: Negative Binomial

Code
r <- 4
p <- 0.20

# 1. PMF: P(X = 10)
pmf_10 <- dnbinom(10, size = r, prob = p)

# 2. CDF: P(X <= 10)
cdf_10 <- pnbinom(10, size = r, prob = p)

# 3. Quantile: 90th percentile
q_90 <- qnbinom(0.90, size = r, prob = p)

# Mean and Variance
mean_nb <- r * (1 - p) / p
var_nb  <- r * (1 - p) / (p^2)

c(PMF = pmf_10, CDF = cdf_10, Quantile_90 = q_90, Mean = mean_nb, Variance = var_nb)
        PMF         CDF Quantile_90        Mean    Variance 
 0.04913443  0.30181012 28.00000000 16.00000000 80.00000000 

Part II: Continuous Waiting Times

From Exponential to Gamma

  • Poisson Process: Events occur continuously and independently at a constant average rate \(\lambda > 0\).
  • Exponential Distribution: Models the continuous waiting time \(T_1\) until the 1st event.
  • Gamma Distribution: Generalizes the Exponential by modeling the total waiting time \(Y = \sum_{i=1}^{\alpha} T_i\) until the \(\alpha\)-th event (shape \(\alpha > 0\), rate \(\lambda > 0\)).

\[Y \sim \text{Gamma}(\alpha, \lambda) \quad \text{where } \alpha = 1 \implies Y \sim \text{Exp}(\lambda)\]

(Note: Scale parameterization uses \(\theta = 1/\lambda\)).

Derivation of the Gamma PDF

For an integer shape \(\alpha = n\), \(Y\) is the waiting time for the \(n\)-th Poisson event. Using the survival function \(P(Y > y)\):

\[P(Y > y) = P(\text{fewer than } n \text{ events in } [0, y]) = \sum_{k=0}^{n-1} \frac{(\lambda y)^k e^{-\lambda y}}{k!}\]

Differentiating \(F(y) = 1 - P(Y > y)\) with respect to \(y\) telescopes the sum:

\[f(y) = \frac{d}{dy}\left[1 - \sum_{k=0}^{n-1} \frac{(\lambda y)^k e^{-\lambda y}}{k!}\right] = \frac{\lambda^n y^{n-1} e^{-\lambda y}}{(n-1)!}\]

Replacing \((n-1)!\) with the Gamma function \(\Gamma(\alpha) = \int_0^\infty t^{\alpha-1}e^{-t}dt\) generalizes to any \(\alpha > 0\):

\[f(y) = \frac{\lambda^\alpha}{\Gamma(\alpha)} y^{\alpha - 1} e^{-\lambda y}, \quad y > 0\]

Gamma: CDF & Quantile Function

Cumulative Distribution Function (CDF)

Integrating the PDF from \(0\) to \(y\) gives the regularized lower incomplete gamma function:

\[F(y) = P(Y \le y) = \int_0^y \frac{\lambda^\alpha}{\Gamma(\alpha)} t^{\alpha - 1} e^{-\lambda t} \, dt = \frac{\gamma(\alpha, \lambda y)}{\Gamma(\alpha)}\]

(For integer \(\alpha = n\), this simplifies to \(F(y) = 1 - \sum_{k=0}^{n-1} \frac{(\lambda y)^k e^{-\lambda y}}{k!}\)).

Quantile Function

Because \(F(y)\) has no closed-form elementary inverse for general \(\alpha\), \(Q(u) = F^{-1}(u)\) for \(u \in (0, 1)\) is computed numerically via root-finding algorithms.

Gamma: Mean & Variance

Using the definition of \(\Gamma(\alpha)\) and the property \(\Gamma(\alpha + 1) = \alpha\Gamma(\alpha)\):

Mean (\(E[Y]\))

\[E[Y] = \int_0^\infty y \frac{\lambda^\alpha}{\Gamma(\alpha)} y^{\alpha-1} e^{-\lambda y} \, dy = \frac{\lambda^\alpha}{\Gamma(\alpha)} \frac{\Gamma(\alpha+1)}{\lambda^{\alpha+1}} = \frac{\alpha}{\lambda}\]

Variance (\(\text{Var}(Y)\))

\[E[Y^2] = \int_0^\infty y^2 \frac{\lambda^\alpha}{\Gamma(\alpha)} y^{\alpha-1} e^{-\lambda y} \, dy = \frac{\Gamma(\alpha+2)}{\lambda^2 \Gamma(\alpha)} = \frac{\alpha(\alpha+1)}{\lambda^2}\]

\[\text{Var}(Y) = E[Y^2] - (E[Y])^2 = \frac{\alpha(\alpha+1)}{\lambda^2} - \left(\frac{\alpha}{\lambda}\right)^2 = \frac{\alpha}{\lambda^2}\]

Visualizing the Gamma PDF

Code
y <- seq(0.01, 20, length.out = 300)
df_gam <- expand.grid(y = y, alpha = c(1, 2, 5), rate = c(0.5, 1))
df_gam$pdf <- dgamma(df_gam$y, shape = df_gam$alpha, rate = df_gam$rate)
df_gam$params <- paste0("α = ", df_gam$alpha, ", λ = ", df_gam$rate)

ggplot(df_gam, aes(x = y, y = pdf, color = params)) +
  geom_line(linewidth = 1) +
  ylim(0, 0.6) +
  labs(title = "Gamma PDF Across Parameters (α = 1 is Exponential)",
       x = "Waiting Time (y)", y = "Density f(y)", color = "Parameters") +
  theme_minimal(base_size = 13)

Figure 3

Simulating the Gamma Distribution with rgamma()

Using rgamma(n, shape, rate), we generate \(n = 10,000\) random waiting times for \(\alpha = 5\) events arriving at rate \(\lambda = 3\) per hour, and compare the simulated sample to theoretical values:

Code
set.seed(123)

# Parameters
n      <- 10000
alpha  <- 5   # shape
lambda <- 3   # rate

# 1. Simulate 10,000 Gamma waiting times
sim_gamma <- rgamma(n, shape = alpha, rate = lambda)

# 2. Compare empirical simulation results vs. theoretical values
comparison <- rbind(
  Theoretical = c(
    Mean     = alpha / lambda,
    Variance = alpha / (lambda^2),
    CDF_1.5  = pgamma(1.5, shape = alpha, rate = lambda),
    Q95      = qgamma(0.95, shape = alpha, rate = lambda)
  ),
  Simulated = c(
    Mean     = mean(sim_gamma),
    Variance = var(sim_gamma),
    CDF_1.5  = mean(sim_gamma <= 1.5),
    Q95      = as.numeric(quantile(sim_gamma, 0.95))
  )
)
round(comparison, 4)
              Mean Variance CDF_1.5    Q95
Theoretical 1.6667   0.5556  0.4679 3.0512
Simulated   1.6543   0.5420  0.4756 3.0176

Visualizing the rgamma() Simulation

Code
library(ggplot2)

ggplot(data.frame(y = sim_gamma), aes(x = y)) +
  geom_histogram(aes(y = after_stat(density)), bins = 40,
                 fill = "steelblue", color = "white", alpha = 0.7) +
  stat_function(fun = dgamma, args = list(shape = alpha, rate = lambda),
                color = "darkred", linewidth = 1.2) +
  labs(
    title = "Simulated Gamma Draws (rgamma) vs. Theoretical PDF",
    subtitle = "n = 10,000 draws | shape (alpha) = 5, rate (lambda) = 3",
    x = "Simulated Waiting Time (y)",
    y = "Density"
  ) +
  theme_minimal(base_size = 13)

Figure 4

Applied Example: Gamma Distribution

Problem: Customer support calls arrive at a help desk according to a Poisson process at a rate of \(\lambda = 3\) calls per hour.

    1. What is the probability density at \(y = 1.5\) hours for the \(\alpha = 5\)-th call?
    1. What is the probability that the 5th call arrives within 1.5 hours?
    1. By what time is there a 95% probability that the 5th call has arrived?
    1. What are the expected waiting time and variance?

R Implementation: Gamma Distribution

Code
alpha  <- 5
lambda <- 3

# 1. PDF: f(1.5)
pdf_1.5 <- dgamma(1.5, shape = alpha, rate = lambda)

# 2. CDF: P(Y <= 1.5)
cdf_1.5 <- pgamma(1.5, shape = alpha, rate = lambda)

# 3. Quantile: 95th percentile
q_95 <- qgamma(0.95, shape = alpha, rate = lambda)

# 4. Mean and Variance
mean_gam <- alpha / lambda
var_gam  <- alpha / (lambda^2)

c(PDF = pdf_1.5, CDF = cdf_1.5, Quantile_95 = q_95, Mean = mean_gam, Variance = var_gam)
        PDF         CDF Quantile_95        Mean    Variance 
  0.5694229   0.4678964   3.0511730   1.6666667   0.5555556 

Summary Comparison

Property Negative Binomial \(\text{NB}(r, p)\) Gamma \(\text{Gamma}(\alpha, \lambda)\)
Base Distribution Geometric (\(r = 1\)) Exponential (\(\alpha = 1\))
Domain \(x \in \{0, 1, 2, \dots\}\) \(y \in (0, \infty)\)
PMF / PDF \(\binom{x+r-1}{x}p^r(1-p)^x\) \(\frac{\lambda^\alpha}{\Gamma(\alpha)}y^{\alpha-1}e^{-\lambda y}\)
Mean \(\frac{r(1-p)}{p}\) \(\frac{\alpha}{\lambda}\)
Variance \(\frac{r(1-p)}{p^2}\) \(\frac{\alpha}{\lambda^2}\)
R Functions dnbinom, pnbinom, qnbinom , rnbinom dgamma, pgamma, qgamma, rgamma

References

  • More about the Distributions Ross, S. M. (2014). A First Course in Probability (9th ed.). Pearson.

  • Computational Software: R Core Team (2026). R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing. Vienna, Austria. Available at https://www.R-project.org/.

About This Presentation

  • Content Design: Developed by the instructor to introduce the Negative Binomial Distribution and the Gamma Distribution.
  • AI Assistance: Google Gemini was provided with a detailed structural prompt to generate the foundational Quarto layout, technical R code chunks, and mathematical explanations and examples.
  • Verification: All statistical definitions, R code logic, and visualizations were reviewed, verified, and finalized by the instructor for educational accuracy.