I have tried to write this myself so don't think I'm wanting somebody out there to write the whole thing for me. I did however just get frustrated and deleted everything I had.
Remember those old calculators? Some of them could even do some stats
functions (such as the standard deviation). You could find those numbers for
thousands of samples but you can bet on it that those old calculators didn't
have many kilo/mega-bytes of ram avaialble to store those big arrays.
This is how they did it:
the square of the standard deviation is defined as:
sum(((x-m)^2)/n) where m is the average or mean of all values x and n is
the number of samples x. (identical to your formula).
the value m itself is defined as sum(x)/n
Let's fiddle a bit with that formula:
sum(((x-m)^2)/n) --> sum((x^2-2.x.m+m^2)/n) -->
sum(x^2/n)-sum(2.x.m/n)+sum(m^2/n) -->
sum(x^2/n)-m.sum(2.x)/n+sum(m^2/n) -->
sum(x^2/n)-2.m.m+m.m --> sum(x^2/n)-m.m --> sum(x^2)/n-(sum(x)/n)^2
The latest form requires just sum(x^2) and the mean of all those x's squared.
Suppose you have three variables:
-
double sx; // sum of x's
-
double sxx; // sum of squares of x's
-
int n; // number of sampes
-
you can add a new x value as follows:
And you can find the square of the standard deviation as follows:
-
double ssd= ssx/n-(sx/n)*(sx/n);
-
All you need are those three variables sx, sxx and n, keep on adding new
x values and you can have the ssd at any time you want.
kind regards,
Jos