8  Variance Is a Stronger Assumption

Builds the moment hierarchy explicitly rather than assuming variance is always available:

def kth_moment(xs, k):
    return mean([x ** k for x in xs])

def variance(xs):
    m = mean(xs)
    return mean([(x - m) ** 2 for x in xs])

For a Pareto(alpha) generator, the chapter derives (and then verifies by simulation) the existence thresholds:

def pareto_sample(x_min, alpha):
    u = random.random()
    return x_min * (1 - u) ** (-1 / alpha)

for alpha in [0.8, 1.5, 3.0]:
    xs = [pareto_sample(1, alpha) for _ in range(20000)]
    print(alpha, mean(xs), variance(xs))

Running this makes the theoretical thresholds tangible: at alpha = 1.5, mean(xs) settles into something sensible-looking while variance(xs) grows without bound as more data is added — a direct, hands-on encounter with “the mean exists but the variance doesn’t.”