AI Knowledge Tree

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 XX is the expected squared deviation from the mean μ=E[X]\mu = \mathbb{E}[X][1]:

Var⁡(X)=E ⁣[(X−μ)2].\operatorname{Var}(X) = \mathbb{E}\!\left[(X - \mu)^2\right].

Squaring makes deviations positive and penalizes large ones more. The standard deviation σ=Var⁡(X)\sigma = \sqrt{\operatorname{Var}(X)} returns to the original units.

A feel for the scale, on familiar spreads:

  • Adult heights scatter with σ≈7\sigma \approx 7 cm, so Var⁡=49 cm2\operatorname{Var} = 49\ \text{cm}^2.
  • IQ scores are built to σ=15\sigma = 15 points, so Var⁡=225 points2\operatorname{Var} = 225\ \text{points}^2.
  • A tight instrument reading with σ=0.1\sigma = 0.1 has Var⁡=0.01\operatorname{Var} = 0.01.

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 (cm2^2), so it is awkward to interpret directly; standard deviation is in the data's own units (cm), so "most adults fall within ±2σ\pm 2\sigma, roughly ±14\pm 14 cm of the mean" is a statement you can picture, while "±2\pm 2 variance" is not. They also scale differently, the next point makes this exact: a unit change from cm to m divides σ\sigma by 100 but Var⁡\operatorname{Var} by 1002=10,000100^2 = 10{,}000.

Computational formula. Expand the square and use linearity:

Var⁡(X)=E[X2−2μX+μ2]=E[X2]−2μ E[X]+μ2=E[X2]−μ2.\operatorname{Var}(X) = \mathbb{E}[X^2 - 2\mu X + \mu^2] = \mathbb{E}[X^2] - 2\mu\,\mathbb{E}[X] + \mu^2 = \mathbb{E}[X^2] - \mu^2.

So variance is "the mean of the square minus the square of the mean". Both E[X2]\mathbb{E}[X^2] and μ\mu come from LOTUS.

Scaling. For constants a,ba, b, since aX+baX + b has mean aμ+ba\mu + b,

Var⁡(aX+b)=E[(aX+b−aμ−b)2]=a2 E[(X−μ)2]=a2Var⁡(X).\operatorname{Var}(aX + b) = \mathbb{E}[(aX + b - a\mu - b)^2] = a^2\,\mathbb{E}[(X-\mu)^2] = a^2 \operatorname{Var}(X).

Shifting by bb does not change spread; scaling by aa scales the variance by a2a^2.

Moments

The kk-th moment of XX is E[Xk]\mathbb{E}[X^k], and the kk-th central moment is E[(X−μ)k]\mathbb{E}[(X - \mu)^k]. 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 XX is[2]

MX(t)=E ⁣[etX],M_X(t) = \mathbb{E}\!\left[e^{tX}\right],

defined for tt 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:

MX(t)=E ⁣[∑k=0∞(tX)kk!]=∑k=0∞tkk! E[Xk].M_X(t) = \mathbb{E}\!\left[\sum_{k=0}^\infty \frac{(tX)^k}{k!}\right] = \sum_{k=0}^\infty \frac{t^k}{k!}\,\mathbb{E}[X^k].

The coefficient of tk/k!t^k/k! is the kk-th moment. Differentiating kk times and setting t=0t = 0 kills every other term:

MX(k)(0)=E[Xk].M_X^{(k)}(0) = \mathbb{E}[X^k].

So MX′(0)=E[X]M_X'(0) = \mathbb{E}[X] and MX′′(0)=E[X2]M_X''(0) = \mathbb{E}[X^2], from which Var⁡(X)=MX′′(0)−MX′(0)2\operatorname{Var}(X) = M_X''(0) - M_X'(0)^2.

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 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),

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.

Variance is the spread around the mean: three Gaussians share mean 0 but, with different σ, look completely different.
Variance is the spread around the mean: three Gaussians share mean 0 but, with different σ, look completely different.

Check it on arrays

python · numpy
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 X∈{1,2,3,4,5,6}X \in \{1, 2, 3, 4, 5, 6\} each with probability 1/61/6. Compute Var⁡(X)\operatorname{Var}(X) using the computational formula E[X2]−μ2\mathbb{E}[X^2] - \mu^2.

Solution

Step 1. The mean is μ=1+2+3+4+5+66=216=3.5\mu = \frac{1 + 2 + 3 + 4 + 5 + 6}{6} = \frac{21}{6} = 3.5.

Step 2. The second moment is

E[X2]=1+4+9+16+25+366=916≈15.1667.\mathbb{E}[X^2] = \frac{1 + 4 + 9 + 16 + 25 + 36}{6} = \frac{91}{6} \approx 15.1667.

Step 3. Subtract the squared mean:

Var⁡(X)=916−3.52=15.1667−12.25=2.9167=3512.\operatorname{Var}(X) = \frac{91}{6} - 3.5^2 = 15.1667 - 12.25 = 2.9167 = \frac{35}{12}.

The standard deviation is 35/12≈1.708\sqrt{35/12} \approx 1.708, a spread of under two faces around the center 3.53.5.

Exercise 2. The MGF of a Poisson(λ\lambda) variable is MX(t)=eλ(et−1)M_X(t) = e^{\lambda(e^t - 1)}. Use it to recover E[X]\mathbb{E}[X] and Var⁡(X)\operatorname{Var}(X).

Solution

Step 1. Differentiate once. With g(t)=λ(et−1)g(t) = \lambda(e^t - 1) and g′(t)=λetg'(t) = \lambda e^t,

MX′(t)=λet eλ(et−1).M_X'(t) = \lambda e^t\, e^{\lambda(e^t - 1)}.

At t=0t = 0: MX′(0)=λ⋅1⋅e0=λM_X'(0) = \lambda \cdot 1 \cdot e^0 = \lambda, so E[X]=λ\mathbb{E}[X] = \lambda.

Step 2. Differentiate again, using the product rule on λet\lambda e^t and MX(t)M_X(t):

MX′′(t)=λet eg(t)+λet⋅λet eg(t)=(λet+λ2e2t)eg(t).M_X''(t) = \lambda e^t\, e^{g(t)} + \lambda e^t \cdot \lambda e^t\, e^{g(t)} = \big(\lambda e^t + \lambda^2 e^{2t}\big)e^{g(t)}.

At t=0t = 0: MX′′(0)=λ+λ2=E[X2]M_X''(0) = \lambda + \lambda^2 = \mathbb{E}[X^2].

Step 3. Combine:

Var⁡(X)=MX′′(0)−MX′(0)2=(λ+λ2)−λ2=λ.\operatorname{Var}(X) = M_X''(0) - M_X'(0)^2 = (\lambda + \lambda^2) - \lambda^2 = \lambda.

For a Poisson, mean and variance both equal λ\lambda.

Exercise 3 (code). Confirm the MGF product rule for sums of independent variables: draw independent Poisson(λ1\lambda_1) and Poisson(λ2\lambda_2) samples and check that the empirical MGF of X+YX + Y matches MX(t) MY(t)M_X(t)\,M_Y(t) at a chosen tt.

Solution
python · numpy
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. [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. [2]Casella, G., & Berger, R. L. (2002). Statistical Inference (2nd ed.). Duxbury. §2.3, moments and moment generating functions.↩