Quant Memo
Coding/●●●●●

Streaming variance without the sum of squares

The textbook variance formula Var=1nxi2xˉ2\operatorname{Var} = \frac{1}{n}\sum x_i^2 - \bar{x}^2 is a numerical trap: it forms xi2\sum x_i^2, which can overflow a fixed-width integer and, in floating point, subtracts two large nearly-equal numbers, cancelling away your precision.

Compute the running mean and (sample) variance of a stream in a single pass, without ever forming xi2\sum x_i^2. Handle the case where values are large but their spread is small.

Your answer

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

More Coding questions