Single pass standard deviation

The usual way to calculate standard deviation is two step: one pass through the data points to calculate the mean, and then another pass to get the deviations from the mean.

However, in scenarios with obscenely large data sets which you stream from the database, there’s a better approach backed up by math.

Formula

Let’s rewrite the formula for sample standard deviation:

However, . Therefore,

Implementation

Here’s a little C# gist to illustrate the solution:

Conclusion