User:Ggenellina/zdp

Rapid calculation methods
The following two formulas can represent a running (continuous) standard deviation. A set of three power sums s0, s1, s2 are each computed over a set of N values of x, denoted as x1, ..., xN:


 * $$\ s_j=\sum_{k=1}^N{x_k^j}.$$

Note that s0 raises x to the zero power, and since x0 is always 1, s0 evaluates to N.

Given the results of these three running summations, the values s0, s1, s2 can be used at any time to compute the current value of the running standard deviation:


 * $$\sigma = \frac{\sqrt{s_0s_2-s_1^2} }{s_0}$$

Similarly for sample standard deviation,


 * $$s = \sqrt{\frac{s_0s_2-s_1^2}{s_0(s_0-1)}}.$$

In a computer implementation, as the three sj sums become large, we need to consider round-off error, arithmetic overflow, and arithmetic underflow. The method below calculates the running sums method with reduced rounding errors:
 * $$\begin{align}

A_0 &= 0\\ A_i &= A_{i-1}+\frac{x_i-A_{i-1}}{i} \end{align}$$

where A is the mean value.
 * $$\begin{align}

Q_0 &= 0\\ Q_i &= Q_{i-1}+\frac{i-1}{i} (x_i-A_{i-1})^2 = Q_{i-1}+ (x_i-A_{i-1})(x_i-A_i) \end{align}$$

Sample variance:
 * $$s^2_n=\frac{Q_n}{n-1}$$

Standard variance:
 * $$\sigma^2_n=\frac{Q_n}{n}$$