Moment Generating Functions

The moment generating function (MGF) is a single function that encodes an entire probability distribution. As its name promises, it manufactures the moments of a random variable through differentiation, and it turns sums of independent variables into products.

Definition#

For a random variable XX, the moment generating function is MX(t)=E[etX],M_X(t) = \mathbb{E}[e^{tX}], defined for values of tt in a neighborhood of 00 where this expectation is finite. For a discrete variable MX(t)=ketkP(X=k)M_X(t)=\sum_k e^{tk}P(X=k), and for a continuous one MX(t)=etxf(x)dxM_X(t)=\int e^{tx} f(x)\,dx. Not every distribution has an MGF (heavy-tailed ones may fail to converge), but when it exists near 00 it is extremely useful.

It generates moments#

Expand the exponential as a Taylor series, etX=n0tnXnn!e^{tX}=\sum_{n\ge 0} \frac{t^n X^n}{n!}, and take expectations term by term: MX(t)=n=0E[Xn]n!tn.M_X(t) = \sum_{n=0}^{\infty} \frac{\mathbb{E}[X^n]}{n!}\, t^n. The moments E[Xn]\mathbb{E}[X^n] are exactly the Taylor coefficients, so differentiating and setting t=0t=0 picks them off: MX(n)(0)=E[Xn].M_X^{(n)}(0) = \mathbb{E}[X^n]. In particular MX(0)=E[X]M_X'(0)=\mathbb{E}[X] and MX(0)=E[X2]M_X''(0)=\mathbb{E}[X^2], from which the variance follows as Var(X)=MX(0)(MX(0))2.\operatorname{Var}(X) = M_X''(0) - \big(M_X'(0)\big)^2.

Two defining properties#

Uniqueness. When the MGF exists in an open interval around 00, it uniquely determines the distribution: two variables with the same MGF have the same law. This makes the MGF a fingerprint for identifying distributions.

Sums become products. If XX and YY are independent, then MX+Y(t)=E[et(X+Y)]=E[etX]E[etY]=MX(t)MY(t).M_{X+Y}(t) = \mathbb{E}[e^{t(X+Y)}] = \mathbb{E}[e^{tX}]\,\mathbb{E}[e^{tY}] = M_X(t)\,M_Y(t). Adding independent variables corresponds to multiplying their MGFs. This factorization is the engine behind one standard proof of the central limit theorem: the MGF of a standardized sum converges to et2/2e^{t^2/2}, the MGF of the standard normal.

MGFs of standard distributions#

DistributionMGF MX(t)M_X(t)Valid for
Normal N(μ,σ2)\mathcal{N}(\mu,\sigma^2)eμt+σ2t2/2e^{\mu t + \sigma^2 t^2/2}all tt
Poisson (λ)(\lambda)eλ(et1)e^{\lambda(e^{t}-1)}all tt
Exponential (λ)(\lambda)λλt\dfrac{\lambda}{\lambda - t}t<λt<\lambda
Gamma (shape α\alpha, rate λ\lambda)(λλt)α\left(\dfrac{\lambda}{\lambda - t}\right)^{\alpha}t<λt<\lambda

See the normal and Poisson distributions for the parent densities. The exponential is the gamma with shape α=1\alpha = 1, and a gamma is a sum of α\alpha independent exponentials — which is exactly why its MGF is the exponential’s raised to the α\alpha (sums become products).

The cumulant generating function#

Taking logs gives the cumulant generating function K(t)=logMX(t),K(t) = \log M_X(t), whose derivatives at 00 generate the cumulants: K(0)=E[X]K'(0)=\mathbb{E}[X] is the mean and K(0)=Var(X)K''(0)=\operatorname{Var}(X) is the variance. Cumulants are often more convenient than moments because K(t)K(t) simply adds over independent variables. The convex function K(t)K(t) is also the object whose Legendre transform yields the rate function of large-deviations theory.

The discrete analogue#

For nonnegative integer counts, the parallel tool is the probability generating function G(s)=E[sX]=k0P(X=k)sk.G(s) = \mathbb{E}[s^{X}] = \sum_{k\ge 0} P(X=k)\, s^{k}. It relates to the MGF by G(et)=MX(t)G(e^t)=M_X(t), and it is the workhorse for branching processes, where composing GG with itself tracks successive generations.

Worked example: Poisson mean and variance#

Let XPoisson(λ)X\sim\mathrm{Poisson}(\lambda) with M(t)=eλ(et1)M(t)=e^{\lambda(e^{t}-1)}. Differentiate using the chain rule: M(t)=λeteλ(et1).M'(t) = \lambda e^{t}\, e^{\lambda(e^{t}-1)}. At t=0t=0, e0=1e^{0}=1 and the exponential factor is e0=1e^{0}=1, so M(0)=λ=E[X].M'(0) = \lambda = \mathbb{E}[X]. Differentiate again: M(t)=λeteλ(et1)+(λet)2eλ(et1),M''(t) = \lambda e^{t}\,e^{\lambda(e^{t}-1)} + \big(\lambda e^{t}\big)^2 e^{\lambda(e^{t}-1)}, so M(0)=λ+λ2=E[X2]M''(0) = \lambda + \lambda^2 = \mathbb{E}[X^2]. Therefore Var(X)=M(0)(M(0))2=(λ+λ2)λ2=λ,\operatorname{Var}(X) = M''(0) - \big(M'(0)\big)^2 = (\lambda + \lambda^2) - \lambda^2 = \lambda, recovering the familiar fact that a Poisson variable has equal mean and variance.

Worked example: the gamma second moment#

The gamma MGF is a clean case where the second moment E[X2]=M(0)\mathbb{E}[X^2] = M''(0) falls out with two applications of the chain rule. Let XGamma(α,λ)X \sim \mathrm{Gamma}(\alpha, \lambda) (shape α\alpha, rate λ\lambda) with M(t)=(1t/λ)αM(t) = \left(1 - t/\lambda\right)^{-\alpha} for t<λt < \lambda.

Show the derivation of E[X2]\mathbb{E}[X^2]

Write the MGF as a power so the chain rule is mechanical:

M(t)=(1tλ)α,t<λ.M(t) = \left(1 - \frac{t}{\lambda}\right)^{-\alpha}, \qquad t < \lambda .

First derivative. Differentiate the outer power and multiply by the inner derivative ddt ⁣(1t/λ)=1/λ\frac{d}{dt}\!\left(1 - t/\lambda\right) = -1/\lambda:

M(t)=α(1tλ)α1 ⁣(1λ)=αλ(1tλ)α1.M'(t) = -\alpha\left(1 - \frac{t}{\lambda}\right)^{-\alpha - 1}\!\left(-\frac{1}{\lambda}\right) = \frac{\alpha}{\lambda}\left(1 - \frac{t}{\lambda}\right)^{-\alpha - 1} .

Setting t=0t = 0 (where 1t/λ=11 - t/\lambda = 1) gives the mean:

M(0)=αλ=E[X].M'(0) = \frac{\alpha}{\lambda} = \mathbb{E}[X] .

Second derivative. Differentiate M(t)M'(t) the same way — the exponent drops by one again and another factor of 1/λ-1/\lambda appears:

M(t)=αλ(α1)(1tλ)α2 ⁣(1λ)=α(α+1)λ2(1tλ)α2.M''(t) = \frac{\alpha}{\lambda}\cdot(-\alpha - 1)\left(1 - \frac{t}{\lambda}\right)^{-\alpha - 2}\!\left(-\frac{1}{\lambda}\right) = \frac{\alpha(\alpha + 1)}{\lambda^{2}}\left(1 - \frac{t}{\lambda}\right)^{-\alpha - 2} .

Evaluating at t=0t = 0 reads off the second moment:

E[X2]=M(0)=α(α+1)λ2.\mathbb{E}[X^2] = M''(0) = \frac{\alpha(\alpha + 1)}{\lambda^{2}} .

Sanity check via the variance. Subtracting the squared mean recovers the known gamma variance:

Var(X)=M(0)(M(0))2=α(α+1)λ2α2λ2=αλ2.\operatorname{Var}(X) = M''(0) - \big(M'(0)\big)^2 = \frac{\alpha(\alpha + 1)}{\lambda^{2}} - \frac{\alpha^{2}}{\lambda^{2}} = \frac{\alpha}{\lambda^{2}} .

Setting α=1\alpha = 1 collapses everything to the exponential: E[X2]=2/λ2\mathbb{E}[X^2] = 2/\lambda^2 and Var(X)=1/λ2\operatorname{Var}(X) = 1/\lambda^2.

In code#

R#

R
# Check Poisson moments from the MGF against a large sample
lambda <- 4
set.seed(1)
x <- rpois(1e6, lambda)
mean(x)  # ~ 4.00  -> M'(0)  = lambda
var(x)   # ~ 4.00  -> M''(0) - M'(0)^2 = lambda

# Numerical derivatives of M(t) = exp(lambda*(exp(t)-1)) at t = 0
M <- function(t) exp(lambda * (exp(t) - 1))
h <- 1e-4
(M(h)  - M(-h)) / (2 * h)          # ~ 4  (mean)
(M(h)  - 2 * M(0) + M(-h)) / h^2   # ~ 20 (E[X^2] = lambda + lambda^2)

Python#

Python
import sympy as sp

t, lam = sp.symbols('t lambda', positive=True)
M = sp.exp(lam * (sp.exp(t) - 1))          # Poisson MGF
mean = sp.diff(M, t).subs(t, 0)            # lambda
EX2  = sp.diff(M, t, 2).subs(t, 0)         # lambda + lambda**2
var  = sp.simplify(EX2 - mean**2)          # lambda
print(mean, sp.simplify(EX2), var)         # lambda  lambda**2 + lambda  lambda
lambda lambda*(lambda + 1) lambda

Julia#

Julia
using Symbolics

@variables t λ
M = exp(λ * (exp(t) - 1))                    # Poisson MGF
D = Differential(t)
mean = substitute(expand_derivatives(D(M)), Dict(t => 0))        # λ
EX2  = substitute(expand_derivatives(D(D(M))), Dict(t => 0))     # λ^2 + λ
println(mean, "  ", simplify(EX2 - mean^2))                      # λ   λ

Why it matters#

The MGF is a Swiss-army knife: it extracts means, variances, and higher moments by differentiation, identifies distributions by uniqueness, and collapses convolutions of independent variables into simple products. Those properties make it the cleanest route to results like the central limit theorem and the additivity of normal and Poisson variables. Through its logarithm, the cumulant generating function, it also opens the door to large-deviations theory and the Legendre-transform duality at the heart of statistical mechanics.