Moment Generating Functions
PGFs work for variables with values in . For general random variables, including continuous ones, the analogous tool is the moment generating function (MGF). It packs all the moments into one function, turns sums of independent variables into products, and gives the cleanest proof of the central limit theorem.
Definition
whenever the expectation is finite for near 0. The -th derivative at 0 is the -th moment: . For a discrete variable on , .
| Distribution | MGF |
|---|---|
| Bernoulli() | |
| Poisson() | |
| Exponential() | for |
| Normal(, ) | |
| Uniform on |
from sympy import symbols, exp, integrate, oo, sqrt, pi, simplify, diff, series
t, x = symbols("t x", real=True)
lam = symbols("lambda", positive=True)
M_exp = simplify(integrate(lam * exp(-lam * x) * exp(t * x), (x, 0, oo), conds="none"))
print("exponential MGF:", M_exp)
print("moments:", [simplify(diff(M_exp, t, k).subs(t, 0)) for k in range(1, 5)])
M_norm = exp(t ** 2 / 2)
print("standard normal moments:", [diff(M_norm, t, k).subs(t, 0) for k in range(1, 7)])
print(series(M_norm, t, 0, 7))exponential MGF: lambda/(lambda - t)
moments: [1/lambda, 2/lambda**2, 6/lambda**3, 24/lambda**4]
standard normal moments: [0, 1, 0, 3, 0, 15]
1 + t**2/2 + t**4/8 + t**6/48 + O(t**7)
The exponential's moments are ; the standard normal's odd moments are 0 and its even moments are .
Sums and uniqueness
If and are independent, . And when MGFs exist near 0, they determine the distribution. Together these prove closure results in one line:
- normals: , a normal;
- exponentials: is the MGF of the gamma distribution.
The CLT via MGFs
Let be i.i.d. with mean 0, variance 1 and MGF . The standardized sum has MGF
which is the MGF of the standard normal. A continuity theorem (Lévy's) turns convergence of MGFs (or characteristic functions) into convergence of distributions. That is the central limit theorem.
import math
# standardized Bernoulli(1/2): values +-1 with prob 1/2, MGF cosh(t)
for n in [1, 10, 100, 10_000]:
vals = [round(math.cosh(t / math.sqrt(n)) ** n, 5) for t in (0.5, 1.0, 2.0)]
print(f"n={n:6d} M_n(0.5), M_n(1), M_n(2) = {vals}")
print("limit e^(t^2/2):", [round(math.exp(t * t / 2), 5) for t in (0.5, 1.0, 2.0)])n= 1 M_n(0.5), M_n(1), M_n(2) = [1.12763, 1.54308, 3.7622]
n= 10 M_n(0.5), M_n(1), M_n(2) = [1.13256, 1.63539, 6.54931]
n= 100 M_n(0.5), M_n(1), M_n(2) = [1.13309, 1.64735, 7.29221]
n= 10000 M_n(0.5), M_n(1), M_n(2) = [1.13315, 1.64871, 7.38807]
limit e^(t^2/2): [1.13315, 1.64872, 7.38906]
Chernoff bounds from MGFs
For any , Markov's inequality applied to gives . Optimizing over yields Chernoff bounds, which for sums of independent variables decay exponentially, far stronger than Chebyshev.
import math
# P(S >= 0.6 n) for n fair coin flips: Chernoff bound min_t e^{-0.6 n t} ((1 + e^t)/2)^n
for n in [50, 100, 500]:
log_bound = min(-0.6 * n * t + n * math.log((1 + math.exp(t)) / 2) for t in [i / 1000 for i in range(1, 2000)])
bound = math.exp(log_bound)
exact = sum(math.comb(n, k) for k in range(math.ceil(0.6 * n), n + 1)) / 2 ** n
print(f"n={n:3d} Chernoff {bound:.2e} exact {exact:.2e}")n= 50 Chernoff 3.65e-01 exact 1.01e-01
n=100 Chernoff 1.34e-01 exact 2.84e-02
n=500 Chernoff 4.24e-05 exact 4.47e-06
The bound overestimates by a factor that grows only slowly (about 10 at ), and it has the right exponential rate, unlike Markov and Chebyshev.
In software
Chernoff bounds size sketches and sampling schemes (how many samples make an estimate reliable with probability ), and they analyse load balancing and randomized rounding. Cumulants (derivatives of ) appear in risk measures and in the analysis of large deviations in queueing systems.
Exercises
Exercise 1: From an MGF
A random variable has MGF . What is its distribution, mean and variance?
Answer
Poisson(3); mean 3, variance 3.
Solution
It matches with .
Exercise 2: Variance from an MGF
For the exponential MGF , use and to find the variance.
Answer
.
Solution
, . So , , variance .
Exercise 3: Uniform moments (code)
Use SymPy to expand the uniform MGF as a series and read off ; compare with .
Answer
.
Solution
from sympy import symbols, exp, series, factorial, Rational
t = symbols("t")
s = series((exp(t) - 1) / t, t, 0, 6).removeO()
print([s.coeff(t, k) * factorial(k) for k in range(6)])
print([Rational(1, k + 1) for k in range(6)])[1, 1/2, 1/3, 1/4, 1/5, 1/6]
[1, 1/2, 1/3, 1/4, 1/5, 1/6]
Test yourself
If and are independent, equals…
What is the MGF of the standard normal?