Variance of huge, nearly equal numbers, why the textbook formula blows up
Asked at Two Sigma, HRT
The classic one-pass variance formula is . It is elegant and it is dangerous:
>>> data = [1e9, 1e9 + 1, 1e9 + 2]
>>> mean_sq = sum(x*x for x in data) / 3
>>> mean = sum(data) / 3
>>> mean_sq - mean*mean
-256.0 # a variance cannot be negative; the true value is 2/3
Explain where the precision is destroyed, and implement a stable one-pass variance.
Your answer
This one is open-ended. Work it through, then check your reasoning against the full solution.