TExcelTAD
Home › Probability Theory › Moment Generating Functions

Moment Generating Functions

PGFs work for variables with values in {0,1,2,…}\{0, 1, 2, \ldots\}. For general random variables, including continuous ones, the analogous tool is the moment generating function (MGF). It packs all the moments E[Xk]E[X^k] into one function, turns sums of independent variables into products, and gives the cleanest proof of the central limit theorem.

Definition

MX(t)=E[etX]=∑k=0∞E[Xk]k!tkM_X(t) = E\left[e^{tX}\right] = \sum_{k=0}^{\infty} \frac{E[X^k]}{k!} t^k

whenever the expectation is finite for tt near 0. The kk-th derivative at 0 is the kk-th moment: MX(k)(0)=E[Xk]M_X^{(k)}(0) = E[X^k]. For a discrete variable on {0,1,2,…}\{0, 1, 2, \ldots\}, MX(t)=GX(et)M_X(t) = G_X(e^t).

Distribution MGF
Bernoulli(pp) q+petq + pe^t
Poisson(λ\lambda) eλ(et−1)e^{\lambda(e^t - 1)}
Exponential(λ\lambda) λλ−t\frac{\lambda}{\lambda - t} for t<λt < \lambda
Normal(μ\mu, σ2\sigma^2) eμt+σ2t2/2e^{\mu t + \sigma^2 t^2/2}
Uniform on [0,1][0, 1] et−1t\frac{e^t - 1}{t}
python · try it yourself
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 k!/λkk!/\lambda^k; the standard normal's odd moments are 0 and its even moments 1,3,151, 3, 15 are (2k−1)!!(2k - 1)!!.

Sums and uniqueness

If XX and YY are independent, MX+Y(t)=E[etXetY]=MX(t)MY(t)M_{X+Y}(t) = E[e^{tX}e^{tY}] = M_X(t)M_Y(t). And when MGFs exist near 0, they determine the distribution. Together these prove closure results in one line:

  • normals: eμ1t+σ12t2/2⋅eμ2t+σ22t2/2=e(μ1+μ2)t+(σ12+σ22)t2/2e^{\mu_1 t + \sigma_1^2 t^2/2} \cdot e^{\mu_2 t + \sigma_2^2 t^2/2} = e^{(\mu_1 + \mu_2)t + (\sigma_1^2 + \sigma_2^2)t^2/2}, a normal;
  • exponentials: (λλ−t)n\left(\frac{\lambda}{\lambda - t}\right)^n is the MGF of the gamma distribution.

The CLT via MGFs

Let XiX_i be i.i.d. with mean 0, variance 1 and MGF MM. The standardized sum Sn/nS_n/\sqrt{n} has MGF

M(tn)n=(1+t22n+o(1n))n  ⟶  et2/2M\left(\frac{t}{\sqrt{n}}\right)^n = \left(1 + \frac{t^2}{2n} + o\left(\frac{1}{n}\right)\right)^n \;\longrightarrow\; e^{t^2/2}

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.

python · try it yourself
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 t>0t > 0, Markov's inequality applied to etXe^{tX} gives Pr⁡[X≥a]≤e−taMX(t)\Pr[X \ge a] \le e^{-ta}M_X(t). Optimizing over tt yields Chernoff bounds, which for sums of independent variables decay exponentially, far stronger than Chebyshev.

python · try it yourself
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 n=500n = 500), 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 1−δ1 - \delta), and they analyse load balancing and randomized rounding. Cumulants (derivatives of log⁡M\log M) 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 M(t)=e3(et−1)M(t) = e^{3(e^t - 1)}. What is its distribution, mean and variance?

Answer

Poisson(3); mean 3, variance 3.

Solution

It matches eλ(et−1)e^{\lambda(e^t - 1)} with λ=3\lambda = 3.

Exercise 2: Variance from an MGF

For the exponential MGF λλ−t\frac{\lambda}{\lambda - t}, use M′(0)M'(0) and M′′(0)M''(0) to find the variance.

Answer

1/λ21/\lambda^2.

Solution

M′(t)=λ(λ−t)2M'(t) = \frac{\lambda}{(\lambda - t)^2}, M′′(t)=2λ(λ−t)3M''(t) = \frac{2\lambda}{(\lambda - t)^3}. So E[X]=1/λE[X] = 1/\lambda, E[X2]=2/λ2E[X^2] = 2/\lambda^2, variance 2/λ2−1/λ22/\lambda^2 - 1/\lambda^2.

Exercise 3: Uniform moments (code)

Use SymPy to expand the uniform MGF et−1t\frac{e^t - 1}{t} as a series and read off E[Xk]E[X^k]; compare with 1k+1\frac{1}{k+1}.

Answer

E[Xk]=1k+1E[X^k] = \frac{1}{k+1}.

Solution
python · try it yourself
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 XX and YY are independent, MX+Y(t)M_{X+Y}(t) equals…

  1. MX(t)+MY(t)M_X(t) + M_Y(t)
  2. MX(t)MY(t)M_X(t) M_Y(t)
  3. MX(MY(t))M_X(M_Y(t))
  4. MX(2t)M_X(2t)

What is the MGF of the standard normal?

  1. ete^{t}
  2. et2/2e^{t^2/2}
  3. 1/(1−t)1/(1 - t)
  4. cosh⁡t\cosh t