Skip to content
VibeFormer
Advanced26 min

Gamma and Beta Distributions

Sums of exponentials, the gamma function, and the beta distribution as a conjugate prior over probabilities.

Gamma and Beta Distributions

Intuition first

Two flexible families that fill important gaps.

The gamma generalises the exponential. An exponential times a single event; a gamma times the kk-th event. Waiting for one customer is exponential, waiting for the tenth is gamma. It lives on (0,)(0, \infty), is right-skewed, and is the natural model for durations, insurance claim sizes and rainfall totals.

The beta lives on [0,1][0, 1], which makes it the distribution for modelling a probability. If you are uncertain about a conversion rate, the beta describes that uncertainty. Its two shape parameters let it be flat, bell-shaped, U-shaped or J-shaped.

The beta's real importance is that it is the conjugate prior for the binomial: start with a beta belief about pp, observe some successes and failures, and your updated belief is again a beta with the counts simply added on. Bayesian updating reduces to arithmetic.

The gamma function

Γ(α)=0tα1etdt\Gamma(\alpha) = \int_0^{\infty} t^{\alpha - 1}e^{-t}\,dt

Key properties:

Γ(n)=(n1)! for positive integers,Γ(α+1)=αΓ(α),Γ(12)=π\Gamma(n) = (n-1)! \text{ for positive integers}, \qquad \Gamma(\alpha + 1) = \alpha\Gamma(\alpha), \qquad \Gamma(\tfrac12) = \sqrt{\pi}

It interpolates the factorial to non-integer arguments, which is what lets the gamma and beta distributions have continuous shape parameters.

The gamma distribution

XGamma(α,β),f(x)=1Γ(α)βαxα1ex/β,x>0X \sim \text{Gamma}(\alpha, \beta), \qquad f(x) = \frac{1}{\Gamma(\alpha)\beta^\alpha}x^{\alpha-1}e^{-x/\beta}, \qquad x > 0 E[X]=αβ,Var(X)=αβ2,MX(t)=(1βt)α\E[X] = \alpha\beta, \qquad \Var(X) = \alpha\beta^2, \qquad M_X(t) = (1 - \beta t)^{-\alpha}
Notation used in this lesson
SymbolMeaning
αShape — controls skewness; α = 1 gives the exponential
βScale — stretches the distribution
λ = 1/βRate, the alternative parametrisation
Γ(α)Gamma function, the normalising constant
The gamma as a sum of exponentialsAdvanced

Let X1,,XαX_1, \dots, X_\alpha be independent Exponential(rate λ)\text{Exponential}(\text{rate } \lambda), each with MGF

MXi(t)=λλt=(1t/λ)1M_{X_i}(t) = \frac{\lambda}{\lambda - t} = (1 - t/\lambda)^{-1}

MGFs of independent sums multiply, so for S=i=1αXiS = \sum_{i=1}^{\alpha} X_i:

MS(t)=[(1t/λ)1]α=(1βt)α,β=1/λM_S(t) = \left[(1 - t/\lambda)^{-1}\right]^{\alpha} = (1 - \beta t)^{-\alpha}, \qquad \beta = 1/\lambda

which is exactly the gamma MGF. So a sum of α\alpha i.i.d. exponentials is Gamma(α,β)\text{Gamma}(\alpha, \beta) — sometimes called the Erlang distribution when α\alpha is an integer.

Mean and variance follow immediately without integration:

E[S]=α1λ=αβ,Var(S)=α1λ2=αβ2\E[S] = \alpha \cdot \frac{1}{\lambda} = \alpha\beta, \qquad \Var(S) = \alpha \cdot \frac{1}{\lambda^2} = \alpha\beta^2

Interpretation: in a Poisson process at rate λ\lambda, the waiting time until the α\alpha-th event is Gamma(α,1/λ)\text{Gamma}(\alpha, 1/\lambda). The gamma is to the exponential what the negative binomial is to the geometric.

Two special cases worth recognising:

  • α=1\alpha = 1: the exponential.
  • α=k/2\alpha = k/2, β=2\beta = 2: the chi-squared distribution with kk degrees of freedom, which is why the next lesson's distributions are all gamma relatives.

The beta distribution

XBeta(α,β),f(x)=xα1(1x)β1B(α,β),0x1X \sim \text{Beta}(\alpha, \beta), \qquad f(x) = \frac{x^{\alpha-1}(1-x)^{\beta-1}}{B(\alpha, \beta)}, \qquad 0 \leq x \leq 1

where B(α,β)=Γ(α)Γ(β)Γ(α+β)B(\alpha,\beta) = \frac{\Gamma(\alpha)\Gamma(\beta)}{\Gamma(\alpha+\beta)} normalises it.

E[X]=αα+β,Var(X)=αβ(α+β)2(α+β+1)\E[X] = \frac{\alpha}{\alpha + \beta}, \qquad \Var(X) = \frac{\alpha\beta}{(\alpha+\beta)^2(\alpha+\beta+1)}
α,β\alpha, \betaShapeInterpretation
1,11, 1Flat — uniform on [0,1][0,1]No information about pp
2,22, 2Symmetric bumppp probably near 0.5
5,25, 2Left-skewed, mass near 1pp probably high
2,52, 5Right-skewed, mass near 0pp probably low
0.5,0.50.5, 0.5U-shapedpp probably extreme
50,5050, 50Tight around 0.5Strong belief p0.5p \approx 0.5

Beta–binomial conjugacy

Deriving the posteriorAdvanced

Prior: pBeta(α,β)p \sim \text{Beta}(\alpha, \beta), so

π(p)pα1(1p)β1\pi(p) \propto p^{\alpha-1}(1-p)^{\beta-1}

Observe kk successes in nn trials. The likelihood is binomial in pp:

L(p)pk(1p)nkL(p) \propto p^{k}(1-p)^{n-k}

By Bayes' theorem the posterior is proportional to the product:

π(pdata)pα1(1p)β1pk(1p)nk=p(α+k)1(1p)(β+nk)1\pi(p \mid \text{data}) \propto p^{\alpha-1}(1-p)^{\beta-1} \cdot p^{k}(1-p)^{n-k} = p^{(\alpha+k)-1}(1-p)^{(\beta+n-k)-1}

That is the kernel of a Beta distribution. Hence

pdataBeta(α+k,  β+nk)\boxed{p \mid \text{data} \sim \text{Beta}(\alpha + k,\; \beta + n - k)}

The update is pure addition: add successes to α\alpha, failures to β\beta. No integration is needed, because the beta and binomial have matching functional forms in pp — which is what "conjugate" means.

The posterior mean is a weighted average of prior and data:

E[pdata]=α+kα+β+n=α+βα+β+nweight on priorαα+β  +  nα+β+nweight on datakn\E[p \mid \text{data}] = \frac{\alpha + k}{\alpha + \beta + n} = \underbrace{\frac{\alpha+\beta}{\alpha+\beta+n}}_{\text{weight on prior}}\cdot\frac{\alpha}{\alpha+\beta} \;+\; \underbrace{\frac{n}{\alpha+\beta+n}}_{\text{weight on data}}\cdot\frac{k}{n}

As nn grows the data weight tends to 1 and the prior is progressively ignored — the mathematical statement that evidence eventually overwhelms prior belief.

Solved problem 1 · Updating a conversion rate

You believe a landing page's conversion rate is around 10%, with moderate uncertainty, modelled as Beta(2,18)\text{Beta}(2, 18). You then observe 47 conversions in 300 visitors.

Find the prior mean, the posterior distribution, the posterior mean, and compare with the raw sample proportion.

Step 1 — prior mean and strength

E[p]=αα+β=220=0.10  \E[p] = \frac{\alpha}{\alpha+\beta} = \frac{2}{20} = 0.10 \;\checkmark

Prior strength α+β=20\alpha + \beta = 20, so the prior carries the weight of about 20 observations.

Step 2 — posterior parameters

k=47k = 47 successes, nk=30047=253n - k = 300 - 47 = 253 failures.

α=2+47=49,β=18+253=271\alpha' = 2 + 47 = 49, \qquad \beta' = 18 + 253 = 271pdataBeta(49,271)p \mid \text{data} \sim \text{Beta}(49, 271)

Step 3 — posterior mean

E[pdata]=4949+271=49320=0.153125\E[p \mid \text{data}] = \frac{49}{49 + 271} = \frac{49}{320} = 0.153125

Step 4 — compare with the raw proportion

p^MLE=47300=0.156667\hat{p}_{\text{MLE}} = \frac{47}{300} = 0.156667

The posterior mean 0.15310.1531 sits slightly below the sample proportion, pulled towards the prior mean of 0.100.10.

Step 5 — check the weighting decomposition

weight on prior=2020+300=0.0625,weight on data=300320=0.9375\text{weight on prior} = \frac{20}{20 + 300} = 0.0625, \qquad \text{weight on data} = \frac{300}{320} = 0.93750.0625×0.10+0.9375×0.156667=0.00625+0.14688=0.15313  0.0625 \times 0.10 + 0.9375 \times 0.156667 = 0.00625 + 0.14688 = 0.15313 \;\checkmark

With 300 observations against a prior worth 20, the data dominates at 94% weight — which is why the shrinkage is only 0.35 percentage points.

Step 6 — posterior uncertainty

Var(pdata)=49×2713202×321=13,27932,870,4004.039×104\Var(p \mid \text{data}) = \frac{49 \times 271}{320^2 \times 321} = \frac{13{,}279}{32{,}870{,}400} \approx 4.039\times10^{-4}SD0.0201\text{SD} \approx 0.0201

So a rough 95% credible interval is 0.153±2(0.020)=(0.113,0.193)0.153 \pm 2(0.020) = (0.113,\, 0.193).

Answer

Posterior Beta(49,271)\text{Beta}(49, 271), mean 0.15310.1531, SD 0.02010.0201. The sample proportion was 0.15670.1567; the prior shrank it towards 0.10 by a modest amount because 300 observations outweigh a prior worth 20.

python
import numpy as np
from scipy import stats

# Gamma: sum of exponentials.
rng = np.random.default_rng(0)
alpha, beta_scale = 5, 2.0
sums = rng.exponential(beta_scale, size=(200_000, alpha)).sum(axis=1)
G = stats.gamma(a=alpha, scale=beta_scale)
print(f"sum of {alpha} exponentials: mean {sums.mean():.4f}  var {sums.var():.4f}")
print(f"Gamma({alpha}, {beta_scale}):        mean {G.mean():.4f}  var {G.var():.4f}")

# Beta-binomial conjugacy.
a0, b0 = 2, 18
k, n = 47, 300
a1, b1 = a0 + k, b0 + (n - k)
prior, post = stats.beta(a0, b0), stats.beta(a1, b1)

print(f"\nprior  Beta({a0},{b0})   mean {prior.mean():.4f}  sd {prior.std():.4f}")
print(f"post   Beta({a1},{b1}) mean {post.mean():.4f}  sd {post.std():.4f}")
print(f"MLE                   {k/n:.4f}")
print(f"95% credible interval ({post.ppf(0.025):.4f}, {post.ppf(0.975):.4f})")

# Shrinkage is strong when n is small.
print(f"\n{'n':>5} {'k':>3}  {'MLE':>7}  {'posterior mean':>15}")
for n_, k_ in ((3, 1), (30, 10), (300, 100), (3000, 1000)):
    print(f"{n_:5d} {k_:3d}  {k_/n_:7.4f}  {(a0+k_)/(a0+b0+n_):15.4f}")

The final table is the point: the MLE is 0.3330.333 at every sample size, while the posterior mean climbs from 0.1300.130 towards 0.3330.333 as evidence accumulates.

Exercise 1

Calls arrive at 4 per hour as a Poisson process. Find the mean and standard deviation of the waiting time until the 3rd call, and name the distribution.

Show solution

Waiting time until the α\alpha-th event in a Poisson process of rate λ\lambda is Gamma(α,1/λ)\text{Gamma}(\alpha, 1/\lambda).

Here α=3\alpha = 3 and λ=4\lambda = 4 per hour, so β=1/4=0.25\beta = 1/4 = 0.25 hours:

TGamma(3,  0.25)T \sim \text{Gamma}(3,\; 0.25)E[T]=αβ=3×0.25=0.75 hours=45 minutes\E[T] = \alpha\beta = 3 \times 0.25 = 0.75 \text{ hours} = 45 \text{ minutes}Var(T)=αβ2=3×0.0625=0.1875SD=0.18750.4330 hours26 minutes\Var(T) = \alpha\beta^2 = 3 \times 0.0625 = 0.1875 \quad\Longrightarrow\quad \text{SD} = \sqrt{0.1875} \approx 0.4330 \text{ hours} \approx 26 \text{ minutes}

Sanity check by the sum-of-exponentials view: each gap has mean 1/41/4 hour, and three gaps give 3/43/4 hour ✓. The standard deviation is 3\sqrt{3} times a single gap's SD of 15 minutes, giving 1532615\sqrt{3} \approx 26 minutes ✓ — variances add, so SDs grow like 3\sqrt{3}, not 3.

Exercise 2

You use a Beta(1,1)\text{Beta}(1,1) prior for a coin's bias and observe 8 heads in 10 flips. Give the posterior, its mean, and explain why the mean is not 0.80.8.

Show solution

Beta(1,1)\text{Beta}(1,1) is the uniform prior on [0,1][0,1]. Updating with k=8k = 8 successes and nk=2n - k = 2 failures:

pdataBeta(1+8,  1+2)=Beta(9,3)p \mid \text{data} \sim \text{Beta}(1 + 8,\; 1 + 2) = \text{Beta}(9, 3)E[pdata]=99+3=912=0.75\E[p \mid \text{data}] = \frac{9}{9 + 3} = \frac{9}{12} = 0.75

The MLE is 8/10=0.808/10 = 0.80, so the posterior mean is lower.

The reason is that Beta(1,1) is uniform but not weightless. It contributes the equivalent of one prior success and one prior failure, so the posterior mean is

k+1n+2=912=0.75\frac{k + 1}{n + 2} = \frac{9}{12} = 0.75

which is Laplace's rule of succession — the classical add-one smoothing used in naive Bayes and n-gram language models. Its purpose is exactly this: to avoid assigning probability 0 or 1 on the basis of a small sample. With 0 heads in 2 flips the MLE would be 00, claiming heads is impossible, while the posterior mean gives 1/41/4.

A truly weightless prior would be Beta(0,0)\text{Beta}(0,0), which is improper — it does not integrate to a finite value — and recovers the MLE in the limit.


Next: t, Chi-Squared and F Distributions, all of which are gamma relatives built from normal samples.