8 Moments and generating functions
Means and variances summarize only a small part of a distribution. Generating functions organize infinitely many numerical properties in a single function. Their most valuable property is that an independent sum, which normally requires convolution, becomes multiplication.
8.1 Moments as derivatives
The \(k\)th raw moment is \(E[X^k]\) when the expectation exists. The \(k\)th central moment is \(E[(X-\mu)^k]\) for \(\mu=E[X]\). Variance is the second central moment. For positive variance, the standardized third moment measures skewness:
\[ \gamma_1=\frac{E[(X-\mu)^3]}{\sigma^3}. \]
These summaries require the relevant moments to be finite. A Cauchy random variable has a symmetric density, but its first absolute moment diverges, so saying its mean is zero is incorrect.
The moment-generating function (MGF) of \(X\) is
\[ M_X(t)=E[e^{tX}]. \]
In this chapter, we say the MGF exists if it is finite on an open interval \((-a,a)\) for some \(a>0\). Clearly \(M_X(0)=1\). Differentiation yields
\[ M_X^{(k)}(0)=E[X^k]. \]
To justify the interchange of derivatives and expectation, choose any \(b<a\) and consider \(|t|\le b\). For a small \(\epsilon>0\) with \(b+\epsilon<a\), the absolute derivative \(|X|^ke^{tX}\) is bounded by a constant multiple of \(e^{(b+\epsilon)X}+e^{-(b+\epsilon)X}\): polynomial growth is dominated by exponential growth at infinity. This bound is integrable by our hypothesis, so dominated convergence justifies differentiation. In particular,
\[ E[X]=M'_X(0),\qquad \operatorname{Var}(X)=M''_X(0)-[M'_X(0)]^2. \]
For example, the second derivative produces the raw second moment, not the variance directly.
The parameter \(t\) is an auxiliary real number. It is not time, a sample value, or a probability. The shape of \(M_X\) near zero contains information about the moments.
An important uniqueness theorem states that if two probability distributions have MGFs finite and equal throughout an open interval about zero, those distributions are identical. The theorem depends on the existence condition; equality of just a few moments does not identify a distribution. We use it to recognize distributions of sums.
8.2 MGFs of common families
For a Bernoulli variable with success probability \(p\),
\[ M_B(t)=(1-p)+pe^t. \]
Its first derivative at zero is \(p\), its second derivative is \(p\), and the variance is \(p-p^2\).
For \(X\sim\operatorname{Poisson}(\lambda)\),
\[ \begin{aligned} M_X(t) &=\sum_{k=0}^\infty e^{tk}e^{-\lambda}\frac{\lambda^k}{k!}\\ &=e^{-\lambda}\exp(\lambda e^t)\\ &=\exp(\lambda(e^t-1)). \end{aligned} \]
Differentiating gives \(M'_X(t)=\lambda e^tM_X(t)\) and \(M''_X(t)=(\lambda e^t+\lambda^2e^{2t})M_X(t)\). Thus the mean and variance both equal \(\lambda\).
For \(T\sim\operatorname{Exponential}(\lambda)\),
\[ M_T(t)=\int_0^\infty\lambda e^{-(\lambda-t)x}\,dx =\frac{\lambda}{\lambda-t},\qquad t<\lambda. \]
The gamma family generalizes this calculation. If \(X\sim\operatorname{Gamma}(\alpha,\lambda)\), with shape \(\alpha\) and rate \(\lambda\), then substituting \(u=(\lambda-t)x\) into the integral gives
\[ \begin{aligned} M_X(t) &=\frac{\lambda^\alpha}{\Gamma(\alpha)} \int_0^\infty x^{\alpha-1}e^{-(\lambda-t)x}\,dx\\ &=\left(\frac{\lambda}{\lambda-t}\right)^\alpha,\qquad t<\lambda. \end{aligned} \]
The first two derivatives give \(E[X]=\alpha/\lambda\) and \(E[X^2]=\alpha(\alpha+1)/\lambda^2\). Hence \(\operatorname{Var}(X)=\alpha/\lambda^2\).
8.2.1 The normal MGF by completing the square
For \(Z\sim N(0,1)\),
\[ \begin{aligned} M_Z(t) &=\frac{1}{\sqrt{2\pi}}\int_{\mathbb R} e^{tz-z^2/2}\,dz\\ &=e^{t^2/2}\frac{1}{\sqrt{2\pi}}\int_{\mathbb R}e^{-(z-t)^2/2}\,dz\\ &=e^{t^2/2}. \end{aligned} \]
The integral in the second line is one after translating the variable. For a general \(X=\mu+\sigma Z\),
\[ M_X(t)=e^{\mu t}M_Z(\sigma t) =\exp\left(\mu t+\frac{\sigma^2t^2}{2}\right). \]
8.3 Independent sums
If \(X,Y\) are independent and their MGFs exist in a common neighborhood of zero,
\[ M_{X+Y}(t)=E[e^{tX}e^{tY}] =M_X(t)M_Y(t). \]
This follows from factorization of the expectation of a product of functions of independent variables. More generally, for constants \(a,b\),
\[ M_{aX+b}(t)=e^{bt}M_X(at). \]
8.3.1 Three consequences
- For independent \(B_i\sim\operatorname{Bernoulli}(p)\), the sum has MGF \((1-p+pe^t)^n\). Expanding by the binomial theorem recovers the binomial probabilities.
- For independent Poisson variables of rates \(\lambda_1,\lambda_2\), the product is \(\exp((\lambda_1+\lambda_2)(e^t-1))\). By uniqueness their sum is \(\operatorname{Poisson}(\lambda_1+\lambda_2)\).
- For independent gamma variables with a shared rate \(\lambda\), the product \((\lambda/(\lambda-t))^{\alpha_1+\alpha_2}\) identifies a gamma variable with summed shapes. Different rates do not generally produce a gamma sum.
For independent normal \(X_i\sim N(\mu_i,\sigma_i^2)\), constants \(a_i\), and constant \(b\),
\[ \begin{aligned} M_{b+\sum a_iX_i}(t) &=e^{bt}\prod_i\exp(a_i\mu_i t+a_i^2\sigma_i^2t^2/2)\\ &=\exp\left[ \left(b+\sum_i a_i\mu_i\right)t+ \frac{t^2}{2}\sum_i a_i^2\sigma_i^2\right]. \end{aligned} \]
The linear combination is normal (or degenerate if its variance is zero), with exactly these mean and variance parameters.
8.4 The cumulant-generating function
When the MGF exists, \(K_X(t)=\log M_X(t)\) is the cumulant generator. Since \(M(0)=1\),
\[ K_X'(0)=E[X],\qquad K_X''(0)=\operatorname{Var}(X). \]
Indeed \(K'=M'/M\) and \(K''=M''/M-(M'/M)^2\). Independent sums add their cumulants, because their MGFs multiply. For a normal variable \(K_X(t)=\mu t+\sigma^2t^2/2\), so cumulants of all orders above two are zero.
8.5 Probability and characteristic generators
For a nonnegative integer-valued variable, the probability-generating function (PGF) is
\[ G_X(z)=E[z^X]=\sum_{k\ge0}P(X=k)z^k,\qquad |z|\le1. \]
Inside the unit disk this power series can be differentiated, giving \(p(k)=G^{(k)}(0)/k!\). If the mean is finite, \(G'(1^-)=E[X]\) by monotone convergence. Unlike an MGF, this generator exists throughout \(0\le z\le1\) even for heavy-tailed counts.
The characteristic function is \(\varphi_X(t)=E[e^{itX}]\), where \(i^2=-1\) and \(t\) is real. Since \(|e^{itX}|=1\), it always exists, including for the Cauchy law. The same product rule holds. Characteristic functions uniquely determine distributions and will be central to our proof of the central limit theorem.
8.5.1 Where the MGF fails
Let \(X=e^Z\), \(Z\sim N(0,1)\), so \(X\) is lognormal. Every positive integer moment is finite: \(E[X^k]=e^{k^2/2}\). Nevertheless, for any \(t>0\),
\[ E[e^{tX}]=\frac{1}{\sqrt{2\pi}}\int_{\mathbb R} \exp(te^z-z^2/2)\,dz=\infty. \]
As \(z\to\infty\), \(te^z-z^2/2\to\infty\), so the integrand is eventually bounded below by a positive constant over an infinite-length interval. Thus the existence of all moments does not guarantee the existence of an MGF near zero.
8.6 Computing MGFs from data
A simulation-based estimate uses the same Monte Carlo idea as an ordinary sample mean:
import numpy as np
import matplotlib.pyplot as plt
rng = np.random.default_rng(808)
x = rng.poisson(2.5, size=60_000)
t = np.linspace(-.4, .4, 81)
estimate = np.array([np.exp(a*x).mean() for a in t])
exact = np.exp(2.5*np.expm1(t))
plt.plot(t, exact, label="Poisson(2.5) MGF")
plt.plot(t, estimate, "--", label="Monte Carlo estimate")
plt.xlabel("t")
plt.ylabel("M(t)")
plt.legend()
plt.show()
print("Largest absolute discrepancy:", np.max(abs(estimate-exact)))The estimator of \(M_X(t)\) has variance \([M_X(2t)-M_X(t)^2]/n\) when \(M_X(2t)\) is finite. It may be unstable at positive \(t\) if a few large observations dominate the exponential average.
8.7 Practice problems
Work in your browser with Google Colab, or download the notebooks for Jupyter.
Practice notebook
Worked solutions
8.7.1 Exercise 1: Identify a distribution
A random variable has MGF \(M(t)=\exp(7(e^t-1))\). Identify its distribution and find its mean and variance by differentiation.
It matches the Poisson MGF with \(\lambda=7\) on all of \(\mathbb R\). The uniqueness theorem identifies the law. Differentiating, \(M'(0)=7\) and \(M''(0)=7+49=56\). Therefore the variance is \(56-49=7\).
8.7.2 Exercise 2: Sum and scaling
Suppose \(X,Y\) are independent exponentials with rate four. Find the MGF and distribution of \(W=2(X+Y)\).
\(X+Y\) has MGF \((4/(4-t))^2\) and \(\operatorname{Gamma}(2,4)\) law. Scaling by two replaces \(t\) with \(2t\): \[ M_W(t)=\left(\frac{4}{4-2t}\right)^2 =\left(\frac{2}{2-t}\right)^2,\quad t<2. \] Thus \(W\sim\operatorname{Gamma}(2,2)\), with mean one and variance one-half.
8.7.3 Exercise 3: Linear Gaussian model
Let \(X\sim N(1,4)\) and \(Y\sim N(3,1)\) be independent. Determine the law of \(V=2X-Y+5\) and express \(P(V>8)\) using \(\Phi\).
The MGF is \(\exp(4t+17t^2/2)\), so \(V\sim N(4,17)\). Standardization yields \(P(V>8)=1-\Phi(4/\sqrt{17})\).
8.7.4 Exercise 4: Verify the addition rule in Python
Generate 50,000 independent pairs of Poisson(1.5) and Poisson(3.5) counts. Compare the empirical PMF of their sum with Poisson(5). Also compare E[exp(0.2 S)] with the theoretical MGF.
import numpy as np
from scipy import stats
import matplotlib.pyplot as plt
rng = np.random.default_rng(811)
s = rng.poisson(1.5, 50_000)+rng.poisson(3.5, 50_000)
k = np.arange(s.max()+1)
emp = np.bincount(s, minlength=len(k))/len(s)
mgf = np.exp(5*np.expm1(.2))
print("MGF", np.exp(.2*s).mean(), mgf)
plt.bar(k, emp, alpha=.5, label="Empirical")
plt.plot(k, stats.poisson.pmf(k, 5), "o", label="Theory")
plt.legend()
plt.show()
assert abs(s.mean()-5) < .05The empirical PMF approaches the Poisson(5) curve; the sum is identified without convolution.
8.7.5 Exercise 5: An empirical MGF can mislead (Python)
Draw nested samples of 1,000, 10,000 and 100,000 lognormal variables. Estimate \(E[e^{0.15X}]\) on each prefix in log space using SciPy’s logsumexp. Interpret changes without concluding that a finite sample proves existence.
import numpy as np
from scipy.special import logsumexp
rng = np.random.default_rng(812)
x = rng.lognormal(0, 1, 100_000)
for n in [1000, 10_000, 100_000]:
a = .15*x[:n]
log_mean = logsumexp(a)-np.log(n)
share = np.exp(a.max()-logsumexp(a))
print(n, "log estimate", log_mean, "largest share", share)Even an enormous finite sample produces a finite estimate. The theory says the expectation is infinite; an unseen tail can dominate the sum. Log-space computation prevents avoidable overflow but cannot fix this statistical issue.
8.7.6 Exercise 6: Differentiate the log MGF numerically (Python)
For a gamma sample with shape three and rate two, estimate the first two cumulants using centered differences of the empirical log MGF at \(-h,0,h\) for \(h=0.01\). Compare with their theoretical values and the sample mean and variance.
import numpy as np
from scipy.special import logsumexp
rng = np.random.default_rng(813)
x = rng.gamma(3, .5, size=80_000)
h = .01
def k(t):
return logsumexp(t*x)-np.log(x.size)
first = (k(h)-k(-h))/(2*h)
second = (k(h)-2*k(0)+k(-h))/h**2
print("Mean:", first, x.mean(), 1.5)
print("Variance:", second, x.var(), .75)
assert abs(first-x.mean()) < .002
assert abs(second-x.var()) < .002The derivative approximations are accurate because the empirical generator is a smooth finite sum. For very small \(h\), subtractive cancellation can instead undermine accuracy.