9  When the Mean Misbehaves

The chapter that pulls 5 and 6 together into one applied experiment: fit mean, median, and a trimmed_mean against repeated finite samples from both worlds.

def median(xs):
    s = sorted(xs)
    n = len(s)
    mid = n // 2
    return s[mid] if n % 2 else (s[mid-1] + s[mid]) / 2

def trimmed_mean(xs, trim=0.1):
    s = sorted(xs)
    k = int(len(s) * trim)
    return mean(s[k: len(s) - k])

Readers run 500 independent trials of n=1000 samples from Cauchy data, computing all three statistics each time, and plot the spread of the 500 estimates for each. The mean’s spread is enormous; the median’s and trimmed mean’s are tight. The chapter’s closing line: “More data is not automatically the solution — which statistic you compute matters more than how much data you feed it.”