Gamma and Beta Distributions
Sums of exponentials, the gamma function, and the beta distribution as a conjugate prior over probabilities.
Assumes you know
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 -th event. Waiting for one customer is exponential, waiting for the tenth is gamma. It lives on , is right-skewed, and is the natural model for durations, insurance claim sizes and rainfall totals.
The beta lives on , 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 , 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
Key properties:
It interpolates the factorial to non-integer arguments, which is what lets the gamma and beta distributions have continuous shape parameters.
The gamma distribution
| Symbol | Meaning | Read aloud |
|---|---|---|
| α | Shape — controls skewness; α = 1 gives the exponential | alpha |
| β | Scale — stretches the distribution | beta |
| λ = 1/β | Rate, the alternative parametrisation | lambda |
| Γ(α) | Gamma function, the normalising constant | gamma of alpha |
The gamma as a sum of exponentialsAdvanced
Let be independent , each with MGF
MGFs of independent sums multiply, so for :
which is exactly the gamma MGF. So a sum of i.i.d. exponentials is — sometimes called the Erlang distribution when is an integer.
Mean and variance follow immediately without integration:
Interpretation: in a Poisson process at rate , the waiting time until the -th event is . The gamma is to the exponential what the negative binomial is to the geometric.
Two special cases worth recognising:
- : the exponential.
- , : the chi-squared distribution with degrees of freedom, which is why the next lesson's distributions are all gamma relatives.
The beta distribution
where normalises it.
| Shape | Interpretation | |
|---|---|---|
| Flat — uniform on | No information about | |
| Symmetric bump | probably near 0.5 | |
| Left-skewed, mass near 1 | probably high | |
| Right-skewed, mass near 0 | probably low | |
| U-shaped | probably extreme | |
| Tight around 0.5 | Strong belief |
Beta–binomial conjugacy
Deriving the posteriorAdvanced
Prior: , so
Observe successes in trials. The likelihood is binomial in :
By Bayes' theorem the posterior is proportional to the product:
That is the kernel of a Beta distribution. Hence
The update is pure addition: add successes to , failures to . No integration is needed, because the beta and binomial have matching functional forms in — which is what "conjugate" means.
The posterior mean is a weighted average of prior and data:
As 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 . 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
Prior strength , so the prior carries the weight of about 20 observations.
Step 2 — posterior parameters
successes, failures.
Step 3 — posterior mean
Step 4 — compare with the raw proportion
The posterior mean sits slightly below the sample proportion, pulled towards the prior mean of .
Step 5 — check the weighting decomposition
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
So a rough 95% credible interval is .
Answer
Posterior , mean , SD . The sample proportion was ; the prior shrank it towards 0.10 by a modest amount because 300 observations outweigh a prior worth 20.
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 at every sample size, while the posterior mean climbs from towards 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 solutionHide solution
Waiting time until the -th event in a Poisson process of rate is .
Here and per hour, so hours:
Sanity check by the sum-of-exponentials view: each gap has mean hour, and three gaps give hour ✓. The standard deviation is times a single gap's SD of 15 minutes, giving minutes ✓ — variances add, so SDs grow like , not 3.
Exercise 2
You use a 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 .
Show solutionHide solution
is the uniform prior on . Updating with successes and failures:
The MLE is , 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
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 , claiming heads is impossible, while the posterior mean gives .
A truly weightless prior would be , 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.