TExcelTAD
Home › Probability Theory › Expectation for Densities

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 XX has density ff:

E[X]=∫−∞∞xf(x) dx,E[g(X)]=∫−∞∞g(x)f(x) dxE[X] = \int_{-\infty}^{\infty} x f(x)\,dx, \qquad E[g(X)] = \int_{-\infty}^{\infty} g(x) f(x)\,dx

and Var⁡(X)=E[X2]−E[X]2\operatorname{Var}(X) = E[X^2] - E[X]^2 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 E[X]E[X].

python · try it yourself
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, E[X]=∫0∞Pr⁡[X>t] dtE[X] = \int_0^\infty \Pr[X > t]\,dt, the continuous version of the tail sum. For the exponential, ∫0∞e−λt dt=1/λ\int_0^\infty e^{-\lambda t}\,dt = 1/\lambda at once. The formula is handy for maxima and minima, whose tails are easy: for the maximum of nn uniforms, Pr⁡[M>t]=1−tn\Pr[M > t] = 1 - t^n and E[M]=∫01(1−tn) dt=nn+1E[M] = \int_0^1 (1 - t^n)\,dt = \frac{n}{n+1}.

Heavy tails

Some densities have no finite mean. The Cauchy density f(x)=1π(1+x2)f(x) = \frac{1}{\pi(1 + x^2)} is symmetric about 0, but ∫∣x∣f(x) dx=∞\int \lvert x \rvert f(x)\,dx = \infty. Sample averages of Cauchy variables never settle down: the average of nn of them has the same Cauchy distribution as a single one. Pareto densities f(x)=αx−α−1f(x) = \alpha x^{-\alpha - 1} on [1,∞)[1, \infty) have a finite mean only if α>1\alpha > 1 and a finite variance only if α>2\alpha > 2.

python · try it yourself
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 ln⁡2≈0.693\ln 2 \approx 0.693: 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".

python · try it yourself
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 E[X]E[X] and E[X2]E[X^2] for XX uniform on [0,2][0, 2], and the variance.

Answer

E[X]=1E[X] = 1, E[X2]=4/3E[X^2] = 4/3, Var⁡=1/3\operatorname{Var} = 1/3.

Solution

Density 1/21/2: ∫02x/2 dx=1\int_0^2 x/2\,dx = 1, ∫02x2/2 dx=8/6\int_0^2 x^2/2\,dx = 8/6; variance 4/3−1=1/3=22/124/3 - 1 = 1/3 = 2^2/12.

Exercise 2: Pareto mean

For the Pareto density f(x)=3x−4f(x) = 3x^{-4} on [1,∞)[1, \infty), find the mean.

Answer

3/23/2.

Solution

∫1∞x⋅3x−4 dx=3∫1∞x−3 dx=3⋅12\int_1^\infty x \cdot 3x^{-4}\,dx = 3\int_1^\infty x^{-3}\,dx = 3 \cdot \frac{1}{2}.

Exercise 3: Minimum by tail integral (code)

Use E[X]=∫0∞Pr⁡[X>t] dtE[X] = \int_0^\infty \Pr[X > t]\,dt with SymPy to find the expected minimum of 4 independent uniforms on [0,1][0, 1], and check by simulation.

Answer

1/51/5.

Solution

Pr⁡[min⁡>t]=(1−t)4\Pr[\min > t] = (1 - t)^4 on [0,1][0, 1].

python · try it yourself
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 ff, E[X]E[X] equals…

  1. ∫f(x) dx\int f(x)\,dx
  2. ∫xf(x) dx\int x f(x)\,dx
  3. f(mean)f(\text{mean})
  4. max⁡f\max f

Why do running averages of Cauchy samples never settle down?

  1. The generator is broken
  2. The Cauchy distribution has no finite mean
  3. The samples are dependent
  4. The variance is zero