<s>[INST] incremental computation of standard deviation How can I compute the standard deviation in an incremental way (using the new value and the last computed mean and/or std deviation) ?
for the non incremental way, I just do something like:
$$S_N=\sqrt{\frac1N\sum_{i=1}^N(x_i-\overline{x})^2}.$$
mean = Mean(list)
for i = 0 to list.size
   stdev = stdev + (list[i] - mean)^2
stdev = sqrRoot( stdev / list.size )

 [/INST] In case someone has to "decrement" and not only "increment" the standard deviation $\sigma$ (for example, when a result $x_i$ in the set is incorrect and needs to be removed or recalculated), you can use this formula:
$
      \sigma_{\text{without } x_i} = \sqrt{\frac{n}{n -1} \left[ \sigma_n^2 - \frac{(\bar{x}_n-x_i)^2}{n-1} \right]}
$
Here is the derivation:
\begin{equation} \label{varianceDecrementale}
      \begin{split}
      \sigma^2_{\text{without } x_i} & = \sum_{j\in \{0, 1, \cdots, i-1, i+1, \cdots, n\}} \frac{x_j^2}{n-1} - \bar{x}_{n \text{ without } x_i}^2 \\
       & = \frac{x_0^2 + x_1^2 + \cdots + x_{i - 1}^2 + x_{i + 1}^2 + \cdots + x_n^2}{n - 1} - \bar{x}_{n \text{ without } x_i}^2 \\
       & = \frac{n}{n -1} \left[ \frac{x_0^2 + x_1^2 + \cdots + x_{i - 1}^2 + x_{i}^2 + x_{i + 1}^2 + \cdots + x_n^2}{n} - \frac{x_{i}^2}{n}\right] - \bar{x}_{n \text{ without } x_i}^2 \\
       & = \frac{n}{n -1} \left[  \sum_i^n \frac{x_i^2}{n} - \frac{x_{i}^2}{n} \right] - \bar{x}_{n \text{ without } x_i}^2 \\
       & = \frac{n}{n -1} \left[  \sum_i^n \frac{x_i^2}{n} - \frac{x_{i}^2}{n} \right] - \frac{n^2}{(n-1)^2} \left[ \bar{x}_n - \frac{x_i}{n} \right]^2 \\
       & = \frac{n}{n -1} \left[ \sum_i^n \frac{x_i^2}{n} - \frac{x_{i}^2}{n} -  \frac{n}{n-1} \left[ \bar{x}_n - \frac{x_i}{n} \right]^2 \right] \\
       & = \frac{n}{n -1} \left[ \sum_i^n \frac{x_i^2}{n} - \frac{x_{i}^2}{n} -  \frac{n}{n-1} \left[ \bar{x}_n^2 - 2 \bar{x}_n \frac{x_i}{n} + \frac{x_i^2}{n^2} \right] \right] \\
       & = \frac{n}{n -1} \left[ \sum_i^n \frac{x_i^2}{n} - \frac{x_{i}^2}{n} -  \frac{n}{n-1} \bar{x}_n^2 + 2 \bar{x}_n \frac{x_i}{n - 1} - \frac{x_i^2}{n (n - 1)} \right] 
      \end{split}
\end{equation}
We can remark that
\begin{equation} 
      \begin{split}
            -\frac{n}{n-1} \bar{x}_n^2  & = -\frac{n}{n-1} \bar{x}_n^2 + \bar{x}_n^2 - \bar{x}_n^2 \\
            & =  -\bar{x}_n^2 -  \frac{n}{n-1} \bar{x}_n^2 + \frac{n - 1}{n - 1} \bar{x}_n^2 \\
            & =  -\bar{x}_n^2 + \frac{-n \bar{x}_n^2 + n \bar{x}_n^2 - \bar{x}_n^2}{n-1} \\
            & =  - \left (\bar{x}_n^2 + \frac{\bar{x}_n^2}{n-1}\right)
      \end{split}
\end{equation}
thus
\begin{equation}
      \begin{split}
      \sigma^2_{\text{without } x_i} & = \frac{n}{n -1} \left[ \sum_i^n \frac{x_i^2}{n} - \frac{x_{i}^2}{n} -  \frac{n}{n-1} \bar{x}_n^2 + 2 \bar{x}_n \frac{x_i}{n - 1} - \frac{x_i^2}{n (n - 1)} \right] \\
      & = \frac{n}{n -1} \left[ \sum_i^n \frac{x_i^2}{n} - \frac{x_{i}^2}{n} - \left (\bar{x}_n^2 + \frac{\bar{x}_n^2}{n-1}\right) + 2 \bar{x}_n \frac{x_i}{n - 1} - \frac{x_i^2}{n (n - 1)} \right]\\
      & = \frac{n}{n -1} \left[ \sum_i^n \frac{x_i^2}{n} - \bar{x}_n^2 - \frac{x_{i}^2}{n} - \frac{\bar{x}_n^2}{n-1} + 2 \bar{x}_n \frac{x_i}{n - 1} - \frac{x_i^2}{n (n - 1)} \right]\\
      & = \frac{n}{n -1} \left[ \sigma^2_n- \frac{x_{i}^2}{n} - \frac{\bar{x}_n^2}{n-1} + 2 \bar{x}_n \frac{x_i}{n - 1} - \frac{x_i^2}{n (n - 1)} \right] \\
      & = \frac{n}{n -1} \left[ \sigma^2_n - \frac{x_{i}^2}{n} - \frac{\bar{x}_n^2}{n-1} + 2 \bar{x}_n \frac{x_i}{n - 1} - \frac{x_i^2}{n (n - 1)} \right] \\
      & = \frac{n}{n -1} \left[ \sigma^2_n - \frac{x_{i}^2(n-1)}{n(n-1)} - \frac{\bar{x}_n^2}{n-1} + 2 \bar{x}_n \frac{x_i}{n - 1} - \frac{x_i^2}{n (n - 1)} \right] \\
      & = \frac{n}{n -1} \left[ \sigma^2_n - \frac{x_{i}^2{n}}{{n}(n-1)} + {\frac{r^2}{n(n-1)}} - \frac{\bar{x}_n^2}{n-1} + 2 \bar{x}_n \frac{x_i}{n - 1} - {\frac{x_i^2}{n (n - 1)}} \right] \\
      & = \frac{n}{n -1} \left[ \sigma^2_n - \frac{x_i^2}{n-1} - \frac{\bar{x}_n^2}{n-1} + 2\bar{x}_n\frac{x_i}{n-1} \right]\\
      & = \frac{n}{n -1} \left[ \sigma^2_n - \frac{(\bar{x}_n-x_i)^2}{n-1} \right]
      \end{split}
\end{equation}
</s>