Calculating the Mean and Standard Deviation
[Summary] [Algorithm] [Example]
-
Summary
-
We want to calculate both the mean and standard deviation in one pass through
the data without round-off errors.
-
This is the kind of algorithm used
in pocket calculators.
-
It takes more paper but has several advantages.
-
This method also allows you to notice peculiar observations as soon as
they appear.
-
We avoid round-off errors by carrying the maximum possible precision.
-
Algorithm
-
Make a table with five (5) columns and one row for each observation.
-
Label the first column n, the second column X, the third column d,
the fourth column M, and the fifth column SS.
-
Write "1" in the n column, the 1st observation in the X column and
the M column, and 0 in the d and SS columns.
-
For each observation after the first, write
-
the observation sequence number in the n column. (This column will have the entries 1,2,... when finished.)
-
the next observation in the X column
-
(X - M)/n in the d column. (using the M from the previous row)
-
M + d in the M column, using the M from the previous row again.
-
SS + n(n-1)d in the SS column. (using the SS from the previous
row)
-
When you have finished, divide the SS from the last row by (n-1). This result
is the variance of X from this sample.
-
Take its square root. This number is the standard deviation of X from this
sample.
-
Example
| n | X | d | M | SS |
| 1 | 7 | 0 | 0 | 0 |
| 2 | 9 | 1 | 8 | 2 |
| 3 | 8 | 0 | 8 | 2 |
| 4 | 8 | 0 | 8 | 2 |
| 5 | 7 | -0.2 | 7.8 | 2.8 |
| 6 | 9 | 0.2 | 8 | 4.0 |
SS / (n-1) = 4.0 / 5 = 0.8
and the square root of 0.8 is (approximately) 0.894 which is the sd.
DickBeldin@prdigital.com