Quant Memo
Coding/●●●●●

When the textbook variance formula returns a negative number

A metrics service computes variance with the classic one-pass identity Var(X)=E[X2]E[X]2\text{Var}(X) = E[X^2] - E[X]^2. On a stream of prices all near 10,000,00010{,}000{,}000, it occasionally logs a negative variance:

>>> xs = [10_000_000.1, 10_000_000.2, 10_000_000.3]
>>> mean_sq = sum(x*x for x in xs) / len(xs)
>>> sq_mean = (sum(xs) / len(xs)) ** 2
>>> mean_sq - sq_mean      # true variance is 0.01, but ...
-0.015625

Explain why the formula fails, then write a numerically stable streaming variance. What is the cost?

Your answer

This one is open-ended. Work it through, then check your reasoning against the full solution.

More Coding questions