When the textbook variance formula returns a negative number
A metrics service computes variance with the classic one-pass identity . On a stream of prices all near , 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.