(Series Part 1) Gamma Distribution Deep Dive: The Foundation of Related Distributions

| November 25, 2024

The Gamma distribution's PDF, its recursive and special-case identities, and how Erlang, Chi-squared, Exponential, Weibull and Poisson derive from it.

This post contains affiliate links for tools I use in production. If you buy through them I earn a commission at no extra cost to you. Recommendations are based on my own experience.

Table of Contents

Introduction

The Gamma distribution is a continuous probability distribution with two parameters: the shape parameter (α\alpha) and the rate parameter (β\beta). Several other distributions you already use, Erlang, Chi-squared, Exponential, and (via a different derivation) Weibull and Poisson, fall out of it through specific parameter substitutions. Knowing the substitution is often faster than looking up each distribution’s PDF from scratch.

Probability Density Function (PDF) of Gamma distribution

The probability density function (PDF) of the Gamma distribution is given by:

f(x;α,β)=βαΓ(α)xα−1e−βx,x>0f(x; \alpha, \beta) = \frac{\beta^\alpha}{\Gamma(\alpha)} x^{\alpha - 1} e^{-\beta x}, \quad x > 0

where Γ(α)\Gamma(\alpha) is:

Γ(α)=∫0∞tα−1e−t dt,for α>0\Gamma(\alpha) = \int_0^\infty t^{\alpha - 1} e^{-t} \, dt, \quad \text{for } \alpha > 0

has the following properties that will be used to derive the related distributions:

  1. Recursive Relation:

    Γ(α+1)=α Γ(α)\Gamma(\alpha + 1) = \alpha \, \Gamma(\alpha)
  2. Special Case for Positive Integers (Used in Erlang Distribution):

    Γ(n)=(n−1)!,for n∈N\Gamma(n) = (n - 1)!, \quad \text{for } n \in \mathbb{N}
  3. Value at α=12\alpha = \frac{1}{2} (Used in Chi-Squared Distribution):

    Γ(12)=π\Gamma\left(\frac{1}{2}\right) = \sqrt{\pi}

Erlang Distribution

The Erlang distribution is a special case of the Gamma distribution where the shape parameter (kk) is a positive integer.

  • PDF:

    f(x;k,β)=βkxk−1e−βx(k−1)!,x>0f(x; k, \beta) = \frac{\beta^k x^{k - 1} e^{-\beta x}}{(k - 1)!}, \quad x > 0
  • Parameters:

    • Shape parameter k∈Nk \in \mathbb{N}
    • Rate parameter β\beta
  • Substitution:

    • Set α=k\alpha = k

Chi-Squared Distribution

The Chi-squared distribution is another special case of the Gamma distribution, widely used in hypothesis testing and confidence interval estimation.

  • PDF:

    f(x;k)=12k/2Γ(k/2)x(k/2)−1e−x/2,x>0f(x; k) = \frac{1}{2^{k/2} \Gamma(k/2)} x^{(k/2) - 1} e^{-x/2}, \quad x > 0
  • Parameters:

    • Degrees of freedom kk
  • Substitution:

    • Set α=k2\alpha = \frac{k}{2}
    • Set β=12\beta = \frac{1}{2}

Exponential Distribution

The Exponential distribution is a special case of the Gamma distribution with the shape parameter set to 1.

  • PDF:

    f(x;β)=βe−βx,x>0f(x; \beta) = \beta e^{-\beta x}, \quad x > 0
  • Parameters:

    • Rate parameter β\beta
  • Substitution:

    • Set α=1\alpha = 1

Weibull Distribution

While not a direct parameter substitution from the Gamma distribution, the Weibull distribution is a separate continuous probability distribution used primarily in reliability engineering and failure analysis.

  • PDF:

    f(x;λ,k)=kλ(xλ)k−1e−(xλ)k,x≥0f(x; \lambda, k) = \frac{k}{\lambda} \left( \frac{x}{\lambda} \right)^{k - 1} e^{-\left(\frac{x}{\lambda}\right)^k}, \quad x \geq 0
  • Parameters:

    • Scale parameter λ\lambda
    • Shape parameter kk

Poisson Distribution

The Poisson distribution is a discrete probability distribution expressing the probability of a given number of events occurring in a fixed interval of time or space.

  • Probability Mass Function (PMF): P(X=k)=λke−λk!,k=0,1,2,…P(X = k) = \frac{\lambda^k e^{-\lambda}}{k!}, \quad k = 0, 1, 2, \dots

Relationship with Gamma Distribution

In Bayesian statistics, the Poisson and Gamma distributions are connected through the concept of conjugate priors. If the number of events follows a Poisson distribution with an unknown rate parameter λ\lambda, and λ\lambda is assumed to follow a Gamma distribution as a prior, then the posterior distribution of λ\lambda given the observed data remains a Gamma distribution. This conjugate prior relationship simplifies the updating of beliefs about λ\lambda after observing data.


Examples

Gamma Distribution

Waiting Time for Multiple Events

Suppose the time between events follows an Exponential distribution with rate β\beta. The time until the α\alpha-th event occurs follows a Gamma distribution with parameters α\alpha (shape) and β\beta (rate).


Erlang Distribution

Telephone Call Center

The time until the kk-th call arrives at a call center can be modeled using the Erlang distribution with shape parameter kk and rate parameter β\beta.


Poisson Distribution

Number of Emails Received

The number of emails a person receives in an hour can be modeled by a Poisson distribution with parameter λ\lambda representing the average number of emails per hour.

Derivation

The Poisson distribution is a discrete probability distribution that expresses the probability of a given number of events occurring in a fixed interval of time or space. It can be derived as a limit of the Binomial distribution under specific conditions.

The Binomial distribution models the number of successes in nn independent trials, each with probability pp of success:

P(X=k)=(nk)pk(1−p)n−k,k=0,1,2,…,nP(X = k) = \binom{n}{k} p^k (1 - p)^{n - k}, \quad k = 0, 1, 2, \dots, n

Limit Conditions:

  • Let n→∞n \to \infty
  • Let p→0p \to 0
  • Such that λ=np\lambda = np remains constant

Using the approximations for large nn and small pp:

(nk)≈nkk!\binom{n}{k} \approx \frac{n^k}{k!}

and

(1−p)n≈e−np=e−λ(1 - p)^n \approx e^{-np} = e^{-\lambda}

Substituting these into the Binomial PMF:

P(X=k)≈λke−λk!P(X = k) \approx \frac{\lambda^k e^{-\lambda}}{k!}

This is the Poisson PMF, demonstrating that under the conditions of a large number of trials and a small probability of success per trial, the Binomial distribution converges to the Poisson distribution with parameter λ=np\lambda = np.

Relationship Between Poisson and Gamma Distributions

As mentioned earlier, in Bayesian statistics, the Poisson and Gamma distributions form a conjugate pair. This means that when the rate parameter λ\lambda of a Poisson distribution is assigned a Gamma prior, the posterior distribution of λ\lambda after observing data remains a Gamma distribution. This property facilitates the updating of beliefs about λ\lambda in a computationally efficient manner.

Derivation: Gamma as a Conjugate Prior for Poisson

Assume the number of events XX follows a Poisson distribution with rate parameter λ\lambda:

P(X=k∣λ)=λke−λk!,k=0,1,2,…P(X = k | \lambda) = \frac{\lambda^k e^{-\lambda}}{k!}, \quad k = 0, 1, 2, \dots

Suppose the prior distribution of λ\lambda is a Gamma distribution with parameters α\alpha (shape) and β\beta (rate):

p(λ)=βαΓ(α)λα−1e−βλ,λ>0p(\lambda) = \frac{\beta^\alpha}{\Gamma(\alpha)} \lambda^{\alpha - 1} e^{-\beta \lambda}, \quad \lambda > 0

Posterior Distribution:

Using Bayes’ theorem, the posterior distribution of λ\lambda given X=kX = k is:

p(λ∣X=k)∝P(X=k∣λ)⋅p(λ)p(\lambda | X = k) \propto P(X = k | \lambda) \cdot p(\lambda)

Substituting the expressions:

p(λ∣X=k)∝λke−λk!⋅βαΓ(α)λα−1e−βλp(\lambda | X = k) \propto \frac{\lambda^k e^{-\lambda}}{k!} \cdot \frac{\beta^\alpha}{\Gamma(\alpha)} \lambda^{\alpha - 1} e^{-\beta \lambda}

Simplifying (constants can be absorbed into the proportionality):

p(λ∣X=k)∝λk+α−1e−(β+1)λp(\lambda | X = k) \propto \lambda^{k + \alpha - 1} e^{-(\beta + 1)\lambda}

This is the kernel of a Gamma distribution with updated parameters:

  • Shape parameter: α′=α+k\alpha' = \alpha + k
  • Rate parameter: β′=β+1\beta' = \beta + 1

Therefore, the posterior distribution is:

p(λ∣X=k)∼Γ(α+k,β+1)p(\lambda | X = k) \sim \Gamma(\alpha + k, \beta + 1)

Interpretation:

After observing kk events, the updated belief about the rate parameter λ\lambda is captured by a Gamma distribution with increased shape and rate parameters. This conjugate relationship simplifies Bayesian updating, as the posterior remains in the same family as the prior.


Chi-Squared Distribution

The Chi-squared distribution is a special case of the Gamma distribution and is widely used in statistical hypothesis testing and confidence interval estimation.

Goodness-of-Fit Test

In a Chi-squared goodness-of-fit test, the test statistic follows a Chi-squared distribution with degrees of freedom equal to the number of categories minus one. This test assesses whether observed frequencies differ from expected frequencies under a specific hypothesis.


Weibull Distribution

The Weibull distribution is a continuous probability distribution used extensively in reliability engineering and failure analysis. It is characterized by its scale parameter (λ\lambda) and shape parameter (kk).

Time to Failure of a Mechanical Component

The lifespan of a mechanical component can be modeled using the Weibull distribution. The shape parameter kk indicates the failure rate behavior:

  • k<1k < 1: Failure rate decreases over time (infant mortality).
  • k=1k = 1: Corresponds to the Exponential distribution (constant failure rate).
  • k>1k > 1: Failure rate increases over time (wear-out failures).

Example:

A component with k=2k = 2 and λ=1000\lambda = 1000 hours suggests that the failure rate increases with time, modeling wear-out behavior.


Conclusion

The Gamma distribution is the parent form behind Erlang, Chi-squared, Exponential, and the Poisson-Gamma conjugate pair, and Weibull shares its use case even though it needs a separate derivation. Once you can map a problem onto one of these substitutions, you get closed-form parameter updates instead of a numerical fit.

If you are putting a model like this into production, the next steps below cover the data and observability layer, and the newsletter is where the production playbook for generative and statistical models ships first.


Next steps: scaling to production

If you take this into production, these are the pieces I would add first.

  • Supabase Supabase is a hosted Postgres platform with authentication and storage built in. Postgres with pgvector for embeddings, so you do not run a separate vector store.
  • Datadog Datadog aggregates metrics, logs, and traces for infrastructure monitoring. Traces and cost metrics across model calls, so latency and spend are visible per request.
  • Vercel Vercel hosts frontend applications with a global edge network and CI/CD. Deploys the frontend and edge functions that sit in front of the model API.

Deploying generative AI models to production

Get the free playbook on shipping generative AI models to production.