Standard Deviation
Jack L. Vevea
vevea at paideia.uchicago.edu
Fri Mar 24 15:36:32 AEST 1989
In article <2221 at maccs.McMaster.CA> cs3b3aj at maccs.UUCP (Stephen M. Dunn) writes:
>
> Well, of course, the standard textbook methods of calculating the
>standard deviation are as follows:
>
>1) assuming values are in array x, there are n values, and the mean is
> known and is in the variable mean:
>
>temp = 0;
>for (i = 1; i <= n; i++)
> temp += (x [i] - mean) ^ 2;
>std_dev = sqrt (temp / n);
And this is precisely what Doug Gwynn was warning against in his
earlier posting; if your numbers have variations of small magnitude,
you will end up with more rounding error than meaning. Don't use this.
>2) assuming that the SQUARES of the values are in array x, there are n
> values, and the mean is known and is in the variable mean:
>
>temp = 0;
>for (i = 1; i <= n; i++)
> temp += x [i] ^ 2;
>std_dev = sqrt ((temp - (mean ^ 2) / n) / n);
>
And if the array already contains the squares, why square them again, pray?
Try this:
for(i=0;i<n;i++) {
temp1 += x * x;
temp2 += x; }
sd = sqrt(temp1 - (temp2 * temp2 / n)) / (n-1);
(Note division by n-1, not n; although the original poster didn't
give much information on what he planned to do with this, he almost
certainly wants _sample_ sd's, not population as the above quoted
formulae assume.)
Saepe fidelis.
More information about the Comp.lang.c
mailing list