# Computing standard deviation in one pass

Given a sequence of real numbers $x_1, x_2, \dots, x_n$, standard deviation of the sequence is

$\displaystyle \sigma = \sqrt{\frac{\sum_{i = 1}^n (x_i - \mu)^2}{n - 1}},$

where

$\displaystyle \mu = \frac{1}{n}\sum_{i = 1}^n x_i$

is the average of all the given values. At first site, a person might think that he or she has to compute the average first, after which he or she has to do another pass in order to compute that funky sum, divide it by $n - 1$ and take the square root of it. This is, however, not quite true, and that’s why:

\displaystyle \begin{aligned} \sum_{i = 1}^n (x_i - \mu)^2 &= \sum_{i = 1}^n (x_i^2 - 2\mu x_i + \mu^2) \\ &= \sum_{i = 1}^n x_i^2 - 2\mu \sum_{i = 1}^n x_i + n \mu^2 \\ &= \sum_{i = 1}^n x_i^2 - 2 \Bigg( \frac{\sum_{i = 1}^n x_i}{n} \Bigg) \sum_{i = 1}^n x_i + n \Bigg( \frac{\sum_{i = 1}^n x_i}{n} \Bigg)^2 \\ &= \sum_{i = 1}^n x_i^2 - 2 \Bigg( \frac{\sum_{i = 1}^n x_i}{n} \Bigg) \sum_{i = 1}^n x_i + \frac{1}{n}\Bigg( \sum_{i = 1}^n x_i \Bigg)^2 \\ &= \sum_{i = 1}^n x_i^2 - \frac{2}{n} \Bigg( \sum_{i = 1}^n x_i \Bigg) \sum_{i = 1}^n x_i + \frac{1}{n}\Bigg( \sum_{i = 1}^n x_i \Bigg)^2 \\ &= \sum_{i = 1}^n x_i^2 - \frac{2}{n} \Bigg( \sum_{i = 1}^n x_i \Bigg)^2 + \frac{1}{n}\Bigg( \sum_{i = 1}^n x_i \Bigg)^2 \\ &= \sum_{i = 1}^n x_i^2 - \frac{1}{n} \Bigg( \sum_{i = 1}^n x_i \Bigg)^2. \end{aligned}

So, whenever computing standard deviation, just keep two sums: one for each value $x_i$ and another one for each $x_i^2$. As soon as you have them, return

$\displaystyle \sqrt{\frac{1}{n - 1}\Bigg( \sum_{i = 1}^n x_i^2 - \frac{1}{n} \Big( \sum_{i = 1}^n x_i \Big)^2 \Bigg)}.$