Quant Memo
Coding/●●●●

Variance of huge, nearly equal numbers, why the textbook formula blows up

Asked at Two Sigma, HRT

The classic one-pass variance formula is Var=1nxi2(1nxi)2\operatorname{Var} = \frac{1}{n}\sum x_i^2 - \left(\frac{1}{n}\sum x_i\right)^2. 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.

More Coding questions