Expectation for Densities
For continuous random variables, sums become integrals. Everything from the discrete case carries over: linearity, the variance formula, the rules for sums. Only the computation changes.
Definitions
If has density :
and as before. The mean is the balance point of the density: if you cut the region under the curve out of cardboard, it balances on a knife edge at .
from sympy import symbols, integrate, exp, oo, sqrt, pi, simplify
x = symbols("x", real=True)
lam = symbols("lambda", positive=True)
f_exp = lam * exp(-lam * x)
m1 = integrate(x * f_exp, (x, 0, oo))
m2 = integrate(x ** 2 * f_exp, (x, 0, oo))
print("exponential: E[X] =", m1, " Var =", simplify(m2 - m1 ** 2))
f_tri = 2 * x # density on [0, 1]
m1 = integrate(x * f_tri, (x, 0, 1))
m2 = integrate(x ** 2 * f_tri, (x, 0, 1))
print("density 2x on [0,1]: E[X] =", m1, " Var =", m2 - m1 ** 2)
phi = exp(-x ** 2 / 2) / sqrt(2 * pi)
print("standard normal: E[X^2] =", integrate(x ** 2 * phi, (x, -oo, oo)))exponential: E[X] = 1/lambda Var = lambda**(-2)
density 2x on [0,1]: E[X] = 2/3 Var = 1/18
standard normal: E[X^2] = 1
The tail integral
For a non-negative random variable, , the continuous version of the tail sum. For the exponential, at once. The formula is handy for maxima and minima, whose tails are easy: for the maximum of uniforms, and .
Heavy tails
Some densities have no finite mean. The Cauchy density is symmetric about 0, but . Sample averages of Cauchy variables never settle down: the average of of them has the same Cauchy distribution as a single one. Pareto densities on have a finite mean only if and a finite variance only if .
import math
import random
rng = random.Random(3)
def cauchy():
return math.tan(math.pi * (rng.random() - 0.5)) # inverse CDF of the Cauchy
total = 0.0
for n in range(1, 1_000_001):
total += cauchy()
if n in (100, 1000, 10_000, 100_000, 1_000_000):
print(f"n={n:8d} running mean of Cauchy samples: {total / n:9.3f}")n= 100 running mean of Cauchy samples: 3.452
n= 1000 running mean of Cauchy samples: -0.110
n= 10000 running mean of Cauchy samples: -0.134
n= 100000 running mean of Cauchy samples: -0.317
n= 1000000 running mean of Cauchy samples: 0.695
Heavy tails are common in practice: file sizes, request latencies, city populations and wealth follow power laws. Averages of such data are dominated by a few huge values and can be misleading; medians and quantiles are more stable.
Mean versus median
For a skewed density, the mean and median differ. The exponential with mean 1 has median : most lifetimes are shorter than average, because a few very long ones pull the mean up. "The average user waits 2 seconds" can coexist with "most users wait under 1 second".
import numpy as np
rng = np.random.default_rng(1)
lat = rng.lognormal(mean=0.0, sigma=1.0, size=200_000) # a typical skewed latency shape
print("mean:", round(lat.mean(), 3), " median:", round(float(np.median(lat)), 3),
" p99:", round(float(np.quantile(lat, 0.99)), 3))
print("fraction below the mean:", round((lat < lat.mean()).mean(), 3))mean: 1.644 median: 0.998 p99: 10.173
fraction below the mean: 0.693
In software
Expectations of continuous variables are computed by numerical integration or, in high dimensions, by Monte Carlo averaging (Monte Carlo Methods). Service-level objectives are written with percentiles precisely because latency means are distorted by heavy tails.
Exercises
Exercise 1: Uniform moments
Compute and for uniform on , and the variance.
Answer
, , .
Solution
Density : , ; variance .
Exercise 2: Pareto mean
For the Pareto density on , find the mean.
Answer
.
Solution
.
Exercise 3: Minimum by tail integral (code)
Use with SymPy to find the expected minimum of 4 independent uniforms on , and check by simulation.
Answer
.
Solution
on .
from sympy import symbols, integrate
import random
t = symbols("t")
print(integrate((1 - t) ** 4, (t, 0, 1)))
rng = random.Random(2)
print(round(sum(min(rng.random() for _ in range(4)) for _ in range(100_000)) / 100_000, 4))1/5
0.2001
Test yourself
For a continuous random variable with density , equals…
Why do running averages of Cauchy samples never settle down?
- The generator is broken
- The Cauchy distribution has no finite mean
- The samples are dependent
- The variance is zero