Before this lesson:lesson 01
(independence, counting with (kn)) and
lesson 02 (PMF, PDF, CDF,
expectation, variance). The gamma integral uses integration by parts
(calculus, lesson 10).
Time: about 75 minutes, plus the notebook.
When you choose a loss or a prior, you choose a distribution. Binary cross-entropy is the negative
log-likelihood of a Bernoulli; softmax cross-entropy is the categorical; the squared error is the
Gaussian; the Poisson loss is for count targets such as clicks or photon counts. A beta prior on a
click rate and a Dirichlet prior on topic proportions (LDA) are the standard Bayesian choices
(lesson 10).
The relations between the distributions are how samplers are built: a gamma sampler gives beta and
Dirichlet samplers. Queues, server logs, website traffic, radioactive decay and earthquake catalogues
are first modelled as Poisson processes. The moment generating function leads to the central limit
theorem (lesson 06) and to Chernoff
bounds (lesson 07).
Start with one trial with success probability p: a Bernoulli variable, 1 for success and 0 for
failure. Everything in the discrete half of the family comes from repeating it independently.
Count the successes in n trials: binomialBin(n,p).
Count the trials up to the first success: geometricGeom(p).
Let n be large and p small, with np=λ fixed (many chances, each one rare):
PoissonPoisson(λ).
Replace "success or failure" by "one of K classes": categorical.
Now let time be continuous. Events happen at rateλ per unit of time: on average
λ events per unit.
The waiting time to the first event: exponentialExp(λ), the continuous
version of the geometric.
The waiting time to the k-th event: gammaGamma(k,λ), a sum of k
exponential gaps.
Two distributions describe probabilities themselves. A beta variable is a number in (0,1),
for example an unknown click rate. A Dirichlet variable is a probability vector, for example the
shares of K classes. Both come from normalising independent gamma variables. Finally, the
GaussianN(μ,σ2) appears when many small independent effects add up
(lesson 06).
Interactive figure: loading…
Choose a family and move its parameters, then press "Sample 1000". The
orange bars or curve are the formula (PMF or PDF). The dark vertical line marks the
mean, and the bar at the top spans mean ± one standard deviation. The blue
histogram shows 1000 samples; compare the sample mean and variance with the formulas in the side
panel. Try the gamma with k=1 (it is the exponential), and the beta with a=b=1 (it is the
uniform distribution).
A shop's website gets many visitors per hour. Each visitor buys with a small probability. The number
of orders in an hour is binomial with a large n and a small p. In practice, nobody knows n
and p separately; only the average λ=np is easy to measure. The Poisson distribution
needs only λ:
P(N=k)=e−λk!λk,k=0,1,2,…
Interactive figure: loading…
Increase the number of trials n, with the mean λ=np fixed. The
blue bars are Bin(n,λ/n); the orange
bars are Poisson(λ). With n=10 the binomial is visibly narrower. As n grows, the
bars agree. The side panel shows the total variation distance (the largest possible difference
between the probabilities of any event) and Le Cam's bound np2=λ2/n: both shrink like
1/n.
Quick check
A factory makes 5,000 chips per day. Each chip is defective with probability 0.0004, independently. Which statement about the number of defective chips in a day is correct?
Why.
The count of successes in 5,000 independent trials is binomial. The mean is np=5000⋅0.0004=2, and p is tiny, so Poisson(2) is very close: Le Cam's bound is np2=0.0008. The Poisson parameter is the mean count np, not the probability p. A large n with a small p is exactly the case where the approximation is good.
Now spread the events over time. A Poisson process with rate λ is a random set of time
points with two properties: the numbers of events in disjoint time windows are independent, and the
number in a window of length t is Poisson(λt). Equivalently, the gaps between
consecutive events are independent Exp(λ) waiting times. Section 3 proves that the two
descriptions agree.
Interactive figure: loading…
Press "New run", then make the run longer.Blue ticks on the timeline are
events at rate λ. The left histogram collects the gaps between events, the right one the
counts in windows of length 1. The orange curves are the formulas
Exp(λ) and Poisson(λ). Then lower the keep probability q: each event
is kept (aqua) or removed (magenta) by an independent
coin. The kept events again form a Poisson process, with rate qλ (thinning).
Quick check
In the figure, λ=2 and q=1. About what share of the windows of length 1 are empty?
Why.
The count in a window of length 1 is Poisson(2), so P(N=0)=e−2⋅20/0!=e−2≈0.135. It is also the probability that the first gap is longer than 1: P(Exp(2)>1)=e−2, the same event seen from the gaps.
Notation. We use the rate for the exponential and the gamma: Exp(λ) has mean
1/λ, and Gamma(k,β) has mean k/β (here β is a rate; it has nothing to
do with the second parameter b of the beta). N(μ,σ2) is given by its
variance. The gamma function is Γ(a)=∫0∞xa−1e−xdx for a>0.
Integration by parts gives Γ(a+1)=aΓ(a), so Γ(n)=(n−1)! for integers
n≥1. The beta function is B(a,b)=Γ(a)Γ(b)/Γ(a+b).
Distribution
Support
PMF / PDF
Mean
Variance
Models
Bernoulli(p)
{0,1}
px(1−p)1−x
p
p(1−p)
one yes/no outcome (a click, a correct label)
Bin(n,p)
{0,…,n}
(xn)px(1−p)n−x
np
np(1−p)
successes in n independent trials
Geom(p), trials
{1,2,…}
(1−p)x−1p
1/p
(1−p)/p2
trials up to and including the first success
Poisson(λ)
{0,1,…}
e−λλx/x!
λ
λ
counts of rare independent events in a window
Cat(p1,…,pK)
{1,…,K}
px
one-hot: p
one-hot: diag(p)−pp⊤
one class label
Exp(λ)
[0,∞)
λe−λx
1/λ
1/λ2
waiting time to the next event
Gamma(k,β)
(0,∞)
Γ(k)βkxk−1e−βx
k/β
k/β2
waiting time to the k-th event; positive, skewed amounts
Beta(a,b)
(0,1)
B(a,b)xa−1(1−x)b−1
a+ba
(a+b)2(a+b+1)ab
an unknown probability or proportion
N(μ,σ2)
R
2πσ21e−(x−μ)2/(2σ2)
μ
σ2
a sum of many small effects; measurement noise
Dir(α1,…,αK)
simplex
∏iΓ(αi)Γ(α0)∏ixiαi−1
αi/α0
α02(α0+1)αi(α0−αi)
a probability vector (class shares, mixture weights)
All parameters are positive, and p∈(0,1). For the Dirichlet, α0=∑iαi, and
the simplex is the set of vectors with xi>0 and ∑ixi=1. For the categorical, the
mean and covariance are those of the one-hot vector eX (a 1 in position X, 0 elsewhere).
Special cases.Exp(λ)=Gamma(1,λ).Bernoulli(p)=Bin(1,p).Beta(1,1) is the uniform distribution on (0,1).Beta(a,b) is the Dirichlet with
K=2 (the first component). Each mean and variance in the table follows from the definitions of
lesson 02; the MGF below gives most of them in two lines.
Two Dirichlet facts. The components compete for a fixed total of 1, so they are negatively
correlated: Cov(Xi,Xj)=−αiαj/(α02(α0+1)) for
i=j. A large α0 means little spread around the mean α/α0.
The first factor is a product of k terms (n−j)/n, and each one tends to 1 (k is fixed).
(1−λ/n)n→e−λ (the definition of ex as a limit), and (1−λ/n)−k→1.
So the product tends to 1⋅k!λk⋅e−λ⋅1.■
How good is it for a finite n? The total variation distance between two distributions P
and Q on the integers is dTV=21∑k∣P(k)−Q(k)∣. It is the largest difference
∣P(A)−Q(A)∣ over all events A.Le Cam's inequality says that a sum of independent
Bernoulli(pi) variables is within total variation distance ∑ipi2 of
Poisson(∑ipi). For Bin(n,p), the bound is np2. It is small when p is
small, even if np is not. The trials may even have different pi.
Why the bound is ∑ipi2: coupling
Le Cam's inequality is proved by a coupling: build the Bernoulli Bi and a
Poisson(pi) variable Qi on the same probability space so that they are equal most of the
time. One can do it with P(Bi=Qi)≤pi2. Then ∑iQi∼Poisson(∑ipi)
(sums of independent Poissons are Poisson, below), and the two sums differ only if some pair
differs. By the union bound of lesson 01, P(∑Bi=∑Qi)≤∑ipi2, and
dTV is at most this probability.
The survival function is P(X>x)=∫x∞λe−λudu=e−λx for x≥0.
The event {X>s+t} is a subset of {X>s}, so their intersection is {X>s+t}.
By the definition of conditional probability,
P(X>s+t∣X>s)=e−λ(s+t)/e−λs=e−λt=P(X>t).■
In words: if you have already waited s, the remaining wait has the same distribution as a fresh
wait. For a lifetime, it means the item does not age. The geometric distribution has the discrete
version: P(X>n)=(1−p)n (the first n trials fail), so P(X>m+n∣X>m)=(1−p)n.
These are the only memoryless distributions on (0,∞) and on {1,2,…}.
Memorylessness says that the survival function G(x)=P(X>x) satisfies G(s+t)=G(s)G(t).
The only non-increasing solutions with 0<G<1 are G(t)=e−λt on (0,∞) and
G(n)=(1−p)n on the integers.
Interactive figure: loading…
Drag "already waited s", then change the lifetime family. The orange
curve is the survival curve P(X>t) of a new item, with mean lifetime 10 years. The
yellow part is the curve beyond s. Rescaled by 1/P(X>s) and moved back to
time 0, it becomes the aqua curve: the survival curve of an item that has
already lasted s. For the exponential, aqua and orange are the same curve. For a gamma with
k=3 (wear-out), an item that lasted 10 years survives 10 more with probability 0.146 instead of
0.423 for a new one. Press "Simulate" to count 10,000 simulated lifetimes (blue).
The minimum of exponentials. Let X1,…,Xm be independent, Xi∼Exp(λi).
The minimum is larger than t only if every Xi is, so
P(miniXi>t)=∏ie−λit=e−(∑iλi)t. Hence
iminXi∼Exp(i∑λi),P(X1 is the smallest)=∑iλiλ1.
Competing exponential clocks: the first one rings at the total rate, and clock i wins with a
probability proportional to its rate. (The second formula is
∫0∞λ1e−λ1te−(∑i≥2λi)tdt.)
Quick check
A web server fails after an Exp(λ) time with mean 200 days. It has run without failure for 300 days. What is its expected remaining time until failure?
Why.
By memorylessness, given X>300 the remaining time X−300 is again Exp(λ), with mean 1/λ=200 days. The past running time tells nothing about the future. If real servers wear out, the exponential model is wrong for them.
Definition. The moment generating function (MGF) of X is MX(t)=E[etX]. We use
it when it is finite for all t in an open interval (−h,h) around 0, with h>0.
Theorem (three MGF facts). Assume MX is finite on (−h,h).
(a) Moments:E[Xn]=MX(n)(0), the n-th derivative at 0.
(b) Sums: if X and Y are independent, MX+Y(t)=MX(t)MY(t).
(c) Uniqueness: if MX=MY on some interval (−h,h), then X and Y have the same distribution.
Proof of (a) and (b). (c) is a deep result; we use it without proof.
(a) Expand etX=∑n≥0tnXn/n!. When MX is finite near 0, you may take the
expectation term by term: MX(t)=∑n≥0E[Xn]tn/n!.
A power series is the Taylor series of its sum, so the coefficient of tn/n! is MX(n)(0).
Hence E[Xn]=MX(n)(0).
(b) et(X+Y)=etXetY. For independent X and Y, the mean of a product of a function
of X and a function of Y is the product of the means
(lesson 04). ■
It is often easier to use the cumulant generating functionKX(t)=logMX(t). Since
M(0)=1, differentiating twice gives KX′(0)=E[X] and KX′′(0)=Var(X).
Distribution
MGF M(t)
Valid for
Bernoulli(p)
1−p+pet
all t
Bin(n,p)
(1−p+pet)n
all t
Poisson(λ)
exp(λ(et−1))
all t
Geom(p), trials
pet/(1−(1−p)et)
t<−ln(1−p)
Exp(λ)
λ/(λ−t)
t<λ
Gamma(k,β)
(β/(β−t))k
t<β
N(μ,σ2)
exp(μt+σ2t2/2)
all t
Example (moments of the gamma).K(t)=−klog(1−t/β), so K′(t)=k/(β−t) and
K′′(t)=k/(β−t)2. At t=0: mean k/β and variance k/β2. For the Poisson,
K(t)=λ(et−1), so K′(0)=K′′(0)=λ:the mean equals the variance.
Example (sums). Products of MGFs give at once, for independent summands:
Bin(n,p) is a sum of n independent Bernoullis: (1−p+pet)n;
Poisson(λ1)+Poisson(λ2)=Poisson(λ1+λ2);
N(μ1,σ12)+N(μ2,σ22)=N(μ1+μ2,σ12+σ22);
Gamma(a,β)+Gamma(b,β)=Gamma(a+b,β), with the same rateβ.
Not every distribution has an MGF
The Cauchy, the Student-t and the log-normal distributions have E[etX]=∞ for
every t>0: their right tails are too heavy. The log-normal even has all moments
E[Xn], but they do not determine its distribution. The characteristic functionφX(t)=E[eitX] always exists, because ∣eitX∣=1, and it has the same
product rule for sums and the same uniqueness property. Proofs of the central limit theorem often
use it for this reason.
Theorem. If X1,…,Xn are i.i.d. Exp(λ), then S=X1+⋯+Xn∼Gamma(n,λ).
Proof.
For t<λ:MXi(t)=∫0∞etxλe−λxdx=λ∫0∞e−(λ−t)xdx=λ−tλ.
By independence and fact (b), MS(t)=(λ−tλ)n.
The MGF of Gamma(n,λ) is
∫0∞etxΓ(n)λnxn−1e−λxdx=Γ(n)λn⋅(λ−t)nΓ(n),
using ∫0∞xn−1e−cxdx=Γ(n)/cn for c>0 (substitute u=cx).
The two MGFs agree on (−λ,λ), so by uniqueness (c), S∼Gamma(n,λ).■
So the time of the k-th event, when gaps are Exp(λ), is Gamma(k,λ),
with mean k/λ. With integer k the gamma is also called the Erlang distribution.
Quick check
X∼Gamma(2,3) and Y∼Gamma(4,3) are independent (shape, rate). What is the distribution of X+Y, and its mean?
Why.
The MGFs multiply: (3/(3−t))2⋅(3/(3−t))4=(3/(3−t))6, the MGF of Gamma(6,3), with mean 6/3=2. Shapes add; the common rate stays. If the rates were different, the sum would not be gamma (the product of the MGFs has no gamma form).
Relation 4: gamma ratios give the beta and the Dirichlet #
Theorem. Let X∼Gamma(a,1) and Y∼Gamma(b,1) be independent. Then
T=X+YX∼Beta(a,b),S=X+Y∼Gamma(a+b,1),
and T and S are independent.
Proof with a two-dimensional change of variables
We use the change of variables for a pair: the density of the new pair is the old density times
∣detJ∣, where J is the Jacobian of the inverse map. This is the 2-D version of the rule in
lesson 02; lesson 05 treats it fully.
The inverse map is x=st,y=s(1−t), for s>0 and t∈(0,1).
J=(∂x/∂s∂y/∂s∂x/∂t∂y/∂t)=(t1−ts−s), so detJ=−st−s(1−t)=−s and ∣detJ∣=s.
The joint density of (X,Y) is the product Γ(a)xa−1e−x⋅Γ(b)yb−1e−y (independence). Substitute and multiply by s:
The joint density is a density of s times a density of t. So S and T are independent,
with the stated distributions. ■
Both factors integrate to 1, so the proof also shows B(a,b)=Γ(a)Γ(b)/Γ(a+b).
The rate does not matter: with a common rate β, every gamma is scaled by 1/β, and the
scale cancels in X/(X+Y).
Dirichlet. In the same way (with a K-dimensional Jacobian): if Gi∼Gamma(αi,1)
are independent and S=∑iGi, then
(SG1,…,SGK)∼Dir(α1,…,αK).
This is how Dirichlet samples are made in practice (NumPy's rng.dirichlet does it).
Interactive figure: loading…
Move the three α sliders, or try the presets. Each point is one probability vector
(x1,x2,x3), drawn by normalising three independent gammas; each corner is one class with
probability 1. Blue points are samples, and the orange
dot is the mean α/α0. With all α=1 the points fill the triangle evenly. With all
α=0.2 they crowd into the corners and edges (sparse vectors: most mass on one or two classes).
With all α=5 they gather near the centre. The side panel compares the variance and covariance
with the formulas.
Theorem. If X∼Dir(α1,…,αK), then Xi∼Beta(αi,α0−αi).
Proof.
Build X from independent gammas: Xi=Gi/(Gi+R), with R=∑j=iGj.
R is a sum of independent Gamma(αj,1) variables with the same rate, so
R∼Gamma(α0−αi,1) (MGF sums). R is independent of Gi.
Relation 4 with a=αi and b=α0−αi gives
Xi∼Beta(αi,α0−αi).■
The beta mean and variance then give the Dirichlet mean αi/α0 and the variance in the
table. The same argument shows that merging components adds their α's:
(X1+X2,X3,…)∼Dir(α1+α2,α3,…).
Definition A (gaps). Let G1,G2,… be i.i.d. Exp(λ). The event times are
Tk=G1+⋯+Gk, and N(t) is the number of events in [0,t].
Definition B (counts).N(0)=0; the counts in disjoint intervals are independent; and
N(t)−N(s)∼Poisson(λ(t−s)) for s<t.
Why the two definitions agree (sketch).
By relation 3, Tk∼Gamma(k,λ).
There are at least k events by time t exactly when the k-th event happens by time t:{N(t)≥k}={Tk≤t}.
Integration by parts (k−1 times) shows that the Gamma(k,λ) CDF is
P(Tk≤t)=1−∑j<ke−λt(λt)j/j!. That is P(N(t)≥k) for
N(t)∼Poisson(λt).
Independence of disjoint intervals comes from memorylessness: at any time s, the wait for the
next event is again Exp(λ) and does not depend on what happened before s.
Thinning. Keep each event independently with probability q. Then the kept events form a Poisson
process with rate qλ, the removed events form one with rate (1−q)λ, and the two are
independent.
Proof (counts in one window of length t).
Given N(t)=n, the kept count C is Bin(n,q), and the removed count is R=n−C.
So P(C=k,R=r)=P(N(t)=k+r)(kk+r)qk(1−q)r=e−λt(k+r)!(λt)k+r⋅k!r!(k+r)!qk(1−q)r.
Split e−λt=e−qλte−(1−q)λt and (λt)k+r=(λt)k(λt)r:P(C=k,R=r)=e−qλtk!(qλt)k⋅e−(1−q)λtr!((1−q)λt)r.
The joint PMF is a Poisson(qλt) PMF times a Poisson((1−q)λt) PMF, so
C and R are independent Poissons. Disjoint windows stay independent, because the coins are
independent. ■
Superposition. The union of two independent Poisson processes with rates λ1 and
λ2 is a Poisson process with rate λ1+λ2: a sum of independent Poisson counts
is Poisson (MGFs). Each event comes from process 1 with probability λ1/(λ1+λ2),
the probability that its exponential clock rings first.
Conditional uniformity. Given N(T)=n, the n event times are distributed like n i.i.d.
U(0,T) points, sorted. So, given 4 events in an hour, the number in the first 15 minutes is
Bin(4,1/4).
The inspection paradox: why your bus is late
Buses arrive as a Poisson process with on average one bus every 10 minutes. You arrive at a random
time. By memorylessness, your expected wait is 10 minutes, not 5. And the time since the last bus
is also about 10 minutes on average (for a process that has run for a long time). So the gap you land
in has mean 20 minutes, twice the average gap. This is not a contradiction: a random arrival time is
more likely to fall into a long gap than into a short one. The same length-biased sampling
appears when you sample "a random user's session" by picking a random time point.
Answers to section 1. (1) The count is Poisson(2), so P(N=0)=e−2≈0.135.
(2) P(X>20∣X>10)=P(X>10)=e−10/10=e−1≈0.368, exactly the same as for a
new disk. The exponential model says that the disk does not age; if real disks wear out, the
exponential is the wrong model. (3) Poisson(2), because np=1000⋅0.002=2 and
p is small (Le Cam's bound np2=0.004).
A website gets support tickets as a Poisson process with rate λ=2 per day.
Counts.N = tickets in one day, N∼Poisson(2).
P(N=0)=e−2≈0.1353.
P(N≥3)=1−P(N≤2)=1−e−2(1+2+222)=1−5e−2≈0.3233.
The binomial view. Split the day into 1440 minutes. In each minute a ticket arrives with
probability 2/1440, independently (ignore two tickets in the same minute). Then
N∼Bin(1440,2/1440), with P(N=0)=(1−2/1440)1440≈0.13515 and
P(N≥3)≈0.32332. Le Cam's bound is np2=1440⋅(2/1440)2=4/1440≈0.0028;
the true total variation distance is about 0.0003.
Waiting times.
The wait for the first ticket is Exp(2):P(wait>1 day)=e−2. It is
the same number as P(N=0), because it is the same event.
After 12 quiet hours, the remaining wait is again Exp(2), with mean 1/2 day
(memorylessness).
The time of the third ticket is T3∼Gamma(3,2): mean 3/2=1.5 days, variance
3/22=0.75, standard deviation ≈0.866 days.
Check of the link: P(T3≤1)=P(N≥3)≈0.3233.
Thinning. Each ticket is urgent with probability 1/4, independently.
Urgent tickets form a Poisson process with rate 2⋅1/4=0.5 per day.
P(no urgent ticket in a week)=e−0.5⋅7=e−3.5≈0.030.
Given 4 tickets in one day, the expected number in its first 6 hours is 4⋅6/24=1
(conditional uniformity: Bin(4,1/4)).
A Dirichlet. A classifier is trained on data whose class shares are uncertain, modelled as
X∼Dir(2,3,5), so α0=10.
Mean (0.2,0.3,0.5).
X3∼Beta(5,5), with variance 102⋅115⋅5=110025≈0.0227.
Cov(X1,X2)=−102⋅112⋅3=−11006≈−0.0055.
Quick check
In the worked example (tickets at rate 2 per day), what is the probability that the first ticket of the day arrives in the first 6 hours?
Why.
The first wait is Exp(2) (in days), and 6 hours are 0.25 day. P(wait≤0.25)=1−e−2⋅0.25=1−e−0.5≈0.393. Equivalently, the count in 6 hours is Poisson(0.5), and we need at least one event. The value e−0.5 is the probability of no ticket in those 6 hours.
SciPy checks the numbers of the worked example and the relations. Mind SciPy's parametrisation:
scale is 1/rate, and the first positional argument of stats.expon is a shift (loc),
not a rate.
importnumpyasnpfromscipyimportstats,integrate# worked example: 2 tickets per dayN=stats.poisson(2)print(round(N.pmf(0),4),round(N.sf(2),4))# 0.1353 0.3233print(round(stats.binom(1440,2/1440).sf(2),4))# 0.3233 (1440 minutes)T3=stats.gamma(a=3,scale=1/2)# shape 3, SCALE = 1/rateprint(T3.mean(),T3.var(),round(T3.cdf(1),4))# 1.5 0.75 0.3233 = P(N >= 3)print(stats.expon(2).mean(),stats.expon(scale=1/2).mean())# 3.0 0.5 (the first one is loc=2, a shift!)print(round(np.exp(-0.5*7),4))# 0.0302: no urgent ticket in a week# Binomial(n, 2/n) -> Poisson(2): total variation distance and Le Cam's boundfornin(10,100,1000):k=np.arange(n+1)tv=0.5*(np.abs(stats.binom(n,2/n).pmf(k)-N.pmf(k)).sum()+N.sf(n))print(n,f"{tv:.2g}",f"{n*(2/n)**2:.2g}")# 10 0.052 0.4 | 100 0.0046 0.04 | 1000 0.00045 0.004# Dirichlet(2, 3, 5): moments, and the marginal X_3 ~ Beta(5, 5)alpha=np.array([2.0,3.0,5.0])D=stats.dirichlet(alpha)print(D.mean(),np.round(D.var(),4),round(stats.beta(5,5).var(),4))# [0.2 0.3 0.5] [0.0145 0.0191 0.0227] 0.0227rng=np.random.default_rng(0)G=rng.gamma(shape=alpha,size=(50_000,3))# independent Gamma(alpha_i, 1)X=G/G.sum(axis=1,keepdims=True)# normalise: Dirichlet samplesprint(stats.kstest(X[:,2],stats.beta(5,5).cdf).pvalue>0.01,round(np.cov(X[:,0],X[:,1])[0,1],4))# True -0.0055# MGF of Gamma(3, rate 2) by numerical integration: e^{tx} times the# density 2^3 x^2 e^{-2x} / Gamma(3), written as one exponential (no overflow)f=lambdax,t:2**3/2*x**2*np.exp((t-2)*x)M=lambdat:integrate.quad(f,0,np.inf,args=(t,))[0]print(round(M(0.5),6),round((2/(2-0.5))**3,6))# 2.37037 2.37037# moments by finite differencesh=1e-3m1=(M(h)-M(-h))/(2*h)m2=(M(h)-2*M(0)+M(-h))/h**2print(round(m1,4),round(m2-m1**2,4))# 1.5 0.75 (k/beta, k/beta^2)
Losses are log-likelihoods. Bernoulli gives binary cross-entropy, categorical gives softmax
cross-entropy (information theory, lesson 4),
Gaussian with fixed variance gives the squared error, and Poisson gives the Poisson loss for count
targets (torch.nn.PoissonNLLLoss). Lesson 08
derives these from maximum likelihood.
Priors. Beta for a probability (a click rate, the success rate of an arm in
Thompson sampling), Dirichlet for mixture weights
and topic proportions (LDA), gamma for a precision 1/σ2. Lesson 10 shows why these are
conjugate.
Traffic and queues. Requests to a web server, calls to a help desk and jobs on a cluster are
first modelled as Poisson processes. Thinning gives the rate of a subset (errors, urgent calls);
superposition merges sources.
Overdispersion. Real counts (orders per day, earthquakes per month) often have variance much
larger than the mean, because the rate itself varies. A gamma mixture of Poissons, the negative
binomial, is the standard fix. Earthquake catalogues cluster in time (aftershocks) and need
self-exciting models.
Simulation. The Gillespie algorithm for chemical reactions uses the minimum of exponentials:
the next reaction comes after an Exp(total rate) wait, and it is reaction i with
probability λi/∑jλj.
Diffusion models. A sum of independent Gaussians is Gaussian (MGF product). This is why the
forward noising process has the closed form q(xt∣x0)=N(αˉtx0,(1−αˉt)I)
(generative models, lesson 7).
Rate versus scale. NumPy and SciPy use the scale 1/λ:rng.exponential(scale),
rng.gamma(shape, scale), stats.expon(scale=1/lam). PyTorch's Exponential and Gamma use the
rate. stats.expon(2) is a shift by 2, not a rate of 2.
Two geometric conventions. "Trials up to the first success" (support 1,2,…, mean 1/p;
SciPy and NumPy) or "failures before the first success" (support 0,1,…, mean (1−p)/p).
The variance (1−p)/p2 is the same. Always check which one a book or a library uses.
Adding gammas with different rates.Gamma(a,β1)+Gamma(b,β2) is not
gamma when β1=β2.
The Poisson parameter is a mean, not a probability. For 5,000 trials with p=0.0004, use
Poisson(2), not Poisson(0.0004).
Forcing a Poisson on overdispersed counts. Compare the sample variance with the sample mean;
for a Poisson their ratio is close to 1.
Assuming every variable has an MGF. The Cauchy, Student-t and log-normal do not.
Treating Dirichlet components as independent betas. Each one is beta, but they sum to 1 and are
negatively correlated.
Reading memorylessness as "nothing ever changes". It is a property of the waiting times: the
past waiting gives no information about the remaining wait. Items that wear out are not memoryless.
X∼Bin(20,0.1). Give E[X],Var(X),P(X=0) and the Poisson
approximation of P(X=0). Is the approximation as good as in section 4?AnswerMean 20⋅0.1=2, variance 2⋅0.9=1.8,P(0)=0.920≈0.1216.Poisson(2) gives e−2≈0.1353. It is worse: p=0.1 is not small, and Le Cam's bound np2=0.2 is much larger than 0.0028 (the true total variation distance is about 0.024).
Show that the geometric distribution (trials) is memoryless.AnswerP(X>n)=P(the first n trials fail)=(1−p)n. So P(X>m+n∣X>m)=(1−p)m+n/(1−p)m=(1−p)n=P(X>n).
Use MGFs to show that the sum of independent Poisson(λ1) and Poisson(λ2)
variables is Poisson(λ1+λ2).AnswerM(t)=eλ1(et−1)eλ2(et−1)=e(λ1+λ2)(et−1). This is the MGF of Poisson(λ1+λ2), finite for all t; uniqueness finishes the proof.
Get the mean and the variance of Exp(λ) from its MGF.AnswerK(t)=logλ−log(λ−t), so K′(t)=1/(λ−t) and K′′(t)=1/(λ−t)2. At t=0: mean 1/λ, variance 1/λ2. (Or M′(0)=1/λ and M′′(0)=2/λ2, so the variance is 2/λ2−1/λ2.)
X∼Dir(2,3,5). What is the distribution of X1+X2?AnswerMerging components adds the α's: (X1+X2,X3)∼Dir(5,5), so X1+X2∼Beta(5,5), with mean 0.5. (It is also 1−X3, and X3∼Beta(5,5).)
You have a sampler for Gamma(a,1) only. How do you sample Beta(3,7), and what
are its mean and variance?AnswerX/(X+Y) with independent X∼Gamma(3,1) and Y∼Gamma(7,1). Mean 3/10=0.3, variance 102⋅113⋅7=21/1100≈0.0191.
Recall cardsAnswer in your head first, then reveal. Add the cards to your review deck to see them again just before you would forget.
Give the Poisson PMF, its mean and its variance.
P(N=k)=e−λλk/k! for k=0,1,…; mean λ and variance λ.
When is Bin(n,p) close to Poisson(np), and how close?
When p is small. Le Cam: the total variation distance is at most np2.
State memorylessness of the exponential distribution.
P(X>s+t∣X>s)=P(X>t) for all s,t≥0, because P(X>x)=e−λx.
What is the distribution of the minimum of independent Exp(λ1) and Exp(λ2)?
Exp(λ1+λ2); the first one is the smaller with probability λ1/(λ1+λ2).
How does the MGF of a sum of independent variables relate to their MGFs?
It is the product: MX+Y(t)=MX(t)MY(t).
What is the distribution of a sum of k i.i.d. Exp(λ) variables?
Gamma(k,λ) (shape k, rate λ), with mean k/λ and variance k/λ2.
Give the mean of Beta(a,b), and compute it for Beta(2,8).
a/(a+b); for Beta(2,8) it is 2/10=0.2.
How do you sample a Dirichlet from gamma variables?
Draw independent Gi∼Gamma(αi,1) and normalise: xi=Gi/∑jGj.
Give the two equivalent definitions of a Poisson process with rate λ.
(A) i.i.d. Exp(λ) gaps between events. (B) Independent counts in disjoint windows, with Poisson(λt) events in a window of length t.
What is thinning of a Poisson process?
Keep each event independently with probability q; the kept events form a Poisson process with rate qλ, independent of the removed ones.
"Calls arrive as a Poisson process with 10 per hour. What is the probability of no call in 15
minutes?" — The count in 15 minutes is Poisson(2.5), so e−2.5≈0.082.
"What is the distribution of the minimum of two independent exponentials?" —
Exp(λ1+λ2);X1 is the minimum with probability λ1/(λ1+λ2).
"Expected number of die rolls until the first 6?" — Geom(1/6) (trials): mean 6, variance
(5/6)/(1/36)=30.
"Your count data have variance much larger than the mean. What do you do?" — The Poisson model is
wrong (overdispersion). Use a negative binomial, or model the extra variation (covariates, random
effects).
"Buses come as a Poisson process every 10 minutes on average. How long do you wait?" — 10 minutes
on average, by memorylessness. The gap you land in has mean 20 minutes (inspection paradox).
"How do you sample a Beta or a Dirichlet if you only have a gamma sampler?" — Normalise independent
gammas with the same rate.
The Poisson distribution, its link to the binomial and the "Poisson paradigm" for many rare, weakly dependent events.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
Memorylessness and why the exponential is the only continuous memoryless distribution (relation 2).
Open on YouTube · the player loads from youtube-nocookie.com when you press play
Merging and splitting Poisson processes (superposition and thinning) and the inspection paradox.
Open on YouTube · the player loads from youtube-nocookie.com when you press play
Joseph Blitzstein and Jessica Hwang, Introduction to Probability,
2nd edition, free PDF: §3.3 (Bernoulli, binomial), §4.3 (geometric), §4.7–4.8 (Poisson and the
binomial limit), §5.4–5.5 (normal, exponential), §6.4–6.6 (MGFs and sums), §8.3–8.5 (beta, gamma
and their connections), ch. 13 (Poisson processes).