Variance, moments, and moment generating functions
Define variance and higher moments, and use the MGF to recover them.
The mean says where a distribution sits; it says nothing about how spread out it is. Two distributions with the same mean can be a tight spike or a broad smear. Variance measures that spread, and the moment generating function packages the mean, variance, and every higher moment into a single function.
Variance and standard deviation
The variance of is the expected squared deviation from the mean [1]:
Squaring makes deviations positive and penalizes large ones more. The standard deviation returns to the original units.
A feel for the scale, on familiar spreads:
- Adult heights scatter with cm, so .
- IQ scores are built to points, so .
- A tight instrument reading with has .
The squared units expose a confusion worth naming: variance and standard deviation are not interchangeable, because they live in different units. Variance is in squared units (cm), so it is awkward to interpret directly; standard deviation is in the data's own units (cm), so "most adults fall within , roughly cm of the mean" is a statement you can picture, while " variance" is not. They also scale differently, the next point makes this exact: a unit change from cm to m divides by 100 but by .
Computational formula. Expand the square and use linearity:
So variance is "the mean of the square minus the square of the mean". Both and come from LOTUS.
Scaling. For constants , since has mean ,
Shifting by does not change spread; scaling by scales the variance by .
Moments
The -th moment of is , and the -th central moment is . The first moment is the mean; the second central moment is the variance. The third and fourth central moments, suitably normalized, give skewness (asymmetry) and kurtosis (tail heaviness). The moments, taken together, often pin down the entire distribution.
The moment generating function
The moment generating function (MGF) of is[2]
defined for in a neighborhood of 0 where the expectation is finite. The name comes from the way it generates moments by differentiation. Expand the exponential as a power series and apply linearity term by term:
The coefficient of is the -th moment. Differentiating times and setting kills every other term:
So and , from which .
Two key uses
Uniqueness. If two random variables have the same MGF on an interval around 0, they have the same distribution. The MGF is a fingerprint.
Sums of independent variables. If and are independent, then
where the middle step uses independence to split the expectation of the product. Convolving distributions becomes multiplying their MGFs, the cleanest route to proving, for example, that a sum of independent Gaussians is Gaussian.
Check it on arrays
import numpy as np
rng = np.random.default_rng(3)
N = 1_000_000
# Exponential(rate=2): mean 1/2, variance 1/4, MGF M(t) = 2/(2 - t) for t < 2
X = rng.exponential(scale=0.5, size=N) # numpy uses scale = 1/rate
print("mean:", X.mean(), " exact 0.5")
print("Var (E[X^2]-mu^2):", (X**2).mean() - X.mean()**2, " exact 0.25")
print("Var (direct):", X.var(), " same")
# MGF at t = 0.5 by sampling vs closed form 2/(2 - t)
t = 0.5
print("MGF(0.5) sampled:", np.mean(np.exp(t * X)), " exact", 2 / (2 - t))
# Moments from finite-difference derivatives of the empirical MGF
def Mhat(t):
return np.mean(np.exp(t * X))
h = 1e-3
m1 = (Mhat(h) - Mhat(-h)) / (2 * h) # ~ E[X]
m2 = (Mhat(h) - 2 * Mhat(0) + Mhat(-h)) / h**2 # ~ E[X^2]
print("M'(0) ~ E[X]:", m1)
print("M''(0) ~ E[X^2]:", m2, " variance:", m2 - m1**2)The two variance formulas agree, the sampled MGF matches its closed form, and numerical derivatives of the MGF recover the first two moments.
Exercises
Exercise 1. A fair die shows each with probability . Compute using the computational formula .
Solution
Step 1. The mean is .
Step 2. The second moment is
Step 3. Subtract the squared mean:
The standard deviation is , a spread of under two faces around the center .
Exercise 2. The MGF of a Poisson() variable is . Use it to recover and .
Solution
Step 1. Differentiate once. With and ,
At : , so .
Step 2. Differentiate again, using the product rule on and :
At : .
Step 3. Combine:
For a Poisson, mean and variance both equal .
Exercise 3 (code). Confirm the MGF product rule for sums of independent variables: draw independent Poisson() and Poisson() samples and check that the empirical MGF of matches at a chosen .
Solution
import numpy as np
rng = np.random.default_rng(8)
N = 2_000_000
lam1, lam2 = 2.0, 3.0
X = rng.poisson(lam1, N)
Y = rng.poisson(lam2, N)
t = 0.3
mgf_sum = np.mean(np.exp(t * (X + Y))) # empirical M_{X+Y}(t)
mgf_product = np.mean(np.exp(t * X)) * np.mean(np.exp(t * Y))
# Closed form: each Poisson MGF is exp(lam (e^t - 1))
closed = np.exp(lam1 * (np.exp(t) - 1)) * np.exp(lam2 * (np.exp(t) - 1))
assert np.isclose(mgf_sum, mgf_product, rtol=2e-3)
print("M_{X+Y}(t):", round(mgf_sum, 4),
" M_X(t) M_Y(t):", round(mgf_product, 4),
" closed:", round(closed, 4))The MGF of the sum matches the product of the individual MGFs, the independence identity that makes summing Poissons reduce to adding their rates.
With spread defined for one variable, the next lesson handles several at once: joint distributions.
References
- [1]Wasserman, L. (2004). All of Statistics. Springer. Ch. 3-4, variance, moments, and moment generating functions. doi:10.1007/978-0-387-21736-9↩
- [2]Casella, G., & Berger, R. L. (2002). Statistical Inference (2nd ed.). Duxbury. §2.3, moments and moment generating functions.↩