+ All Categories
Home > Documents > Computing standard deviations: accuracy

Computing standard deviations: accuracy

Date post: 17-Dec-2016
Category:
Upload: john-gregg
View: 218 times
Download: 4 times
Share this document with a friend
6
Scientific F.N. Fritsch Applications Editor Computing Deviations: Standard Accuracy Tony F. Chan and John Gregg Lewis Stanford University Four algorithms for the numerical computation of the standard deviation of (unweighted) sampled data are analyzed. Two of the algorithms are well-known in the statistical and computational literature; the other two are new algorithms specifically intended for auto- matic computation. Our discussion is expository, with emphasis on reaching a suitable definition of "accuracy." Each of the four algorithms is analyzed for the condi- lions under which it will be accurate. We conclude that all four algorithms will provide accurate answers for many problems, but two of the algorithms, one new, one old, are substantially more accurate on difficult problems than are the other two. Key Words and Phrases: mean, standard deviation, least squares, updating estimates, rounding error analysis, condition number CR Categories: 5.5, 5.II, 5.19 Permission to copy without fee all or part of this material is granted provided that the copies are not made or distributed for direct commercial advantage, the ACM copyright notice and the title of the publication and its date appear, and notice is given that copying is by permission of the Association for Computing Machinery. To copy otherwise, or to republish, requires a fee and/or specific permission. Authors' address: T.F. Chan, Dept. of Computer Science, Yale University, 10 Hillhouse Ave., New Haven, CT 06520; J.G. Lewis, Boeing Computer Services Co., 565 Andover Park West, Mail Stop 9C-01, Tukwila, WA 98188. Professor Chan's work was funded by ERDA E(04-3)326 PA~30, Dr. Lewis' by ERDA E(04-3)326 PA#30 and NSF DCR 71-01996 A04-05. © 1979 ACM 0001-0782/79/0900-0526 $00.75 526 Introduction Two recent communications to this journal (Hanson [3] and Cotton [2]) have discussed the problem of com- puting numerically the unweighted mean X and standard deviation S of sampled data. The question has been raised, but not answered: What is the best algorithm for computing X and S? "Best" has many meanings; some common interpretations for computer algorithms are: highest accuracy, least computational cost, minimal in- put/output requirements, and least storage require- ments. We address primarily the first of these by defining accuracy in a way which allows us to make comparisons between algorithms in general (as opposed to comparing results on specific data). In a companion note [7], D. West addresses some of the latter questions of efficiency. A synthesis of the two papers will show that there are algorithms which are both efficient and accurate. (We shall concern ourselves only with the calculation of S; the calculation of X is satisfactory in all of these algo- rithms. ) I. Accuracy We have available an automatic calculator or com- puter; we want to compute the sample standard devia- tion S of data X1, X,, .... , X,,,. We define I X= ~ Xi/m, i=1 S = (X, -- X)'-'/(m -- 1) . (1) \/,=1 A typical modern computer allows us to perform arithmetic on numbers with a fixed, limited number of significant digits (in some base), usually referred to as finite precision floating point arithmetic. Various pro- gramming techniques can use the basic operations to provide arithmetic with higher precision (e.g. doubled or arbitrarily flexible precision), but these techniques are usually complicated and expensive. Accordingly, we are often willing to accept the results of calculations which utilize the basic operations only in standard ways; these results are necessarily often somewhat inaccurate. If we want to compute S inexpensively, how accurate an an- swer can we expect? Suppose, then, that we use some formula to compute an answer S. When is S sufficiently close to the true standard deviation S? Two naive and simplistic require- ments are: (a) allow S to differ from S in the least significant digit of the computer representation of S (relative error); (b) require S to be within e of S, where e is a computer dependent quantity, usually related to the least sig- nificant digit of the representation of 1.00... 0 (ab- solute error). (For example, e might be 10 -~ for a decimal computer with 6-digit arithmetic.) Communications September 1979 of Volume 22 the ACM Number 9
Transcript
Page 1: Computing standard deviations: accuracy

Scientific F.N. Fritsch Applications Editor

Computing Deviations:

Standard Accuracy

T o n y F. C h a n a n d J o h n G r e g g L e w i s S t a n f o r d U n i v e r s i t y

Four algorithms for the numerical computation of the standard deviation of (unweighted) sampled data are analyzed. Two of the algorithms are wel l -known in the statistical and computational literature; the other two are new algorithms specifically intended for auto- matic computation. Our discussion is expository, with emphasis on reaching a suitable definition of "accuracy." Each of the four algorithms is analyzed for the condi- lions under which it will be accurate. We conclude that all four algorithms will provide accurate answers for many problems, but two of the algorithms, one new, one old, are substantially more accurate on difficult problems than are the other two.

Key Words and Phrases: mean, standard deviation, least squares, updating estimates, rounding error analysis, condition number

CR Categories: 5.5, 5 . I I , 5.19

Permission to copy without fee all or part of this material is granted provided that the copies are not made or distributed for direct commercial advantage, the ACM copyright notice and the title of the publication and its date appear, and notice is given that copying is by permission of the Association for Computing Machinery. To copy otherwise, or to republish, requires a fee a n d / o r specific permission.

Authors ' address: T.F. Chan, Dept. of Computer Science, Yale University, 10 Hillhouse Ave., New Haven, CT 06520; J.G. Lewis, Boeing Computer Services Co., 565 Andover Park West, Mail Stop 9C-01, Tukwila, WA 98188.

Professor Chan's work was funded by ERDA E(04-3)326 PA~30, Dr. Lewis' by ERDA E(04-3)326 PA#30 and NSF DCR 71-01996 A04-05. © 1979 ACM 0001-0782/79/0900-0526 $00.75

526

Introduction

Two recent communications to this journal (Hanson [3] and Cotton [2]) have discussed the problem of com- puting numerically the unweighted mean X and standard deviation S of sampled data. The question has been raised, but not answered: What is the best algorithm for computing X and S? "Best" has many meanings; some common interpretations for computer algorithms are: highest accuracy, least computational cost, minimal in- put /output requirements, and least storage require- ments. We address primarily the first of these by defining accuracy in a way which allows us to make comparisons between algorithms in general (as opposed to comparing results on specific data). In a companion note [7], D. West addresses some of the latter questions of efficiency. A synthesis of the two papers will show that there are algorithms which are both efficient and accurate. (We shall concern ourselves only with the calculation of S; the calculation of X is satisfactory in all of these algo- rithms. )

I. Accuracy

We have available an automatic calculator or com- puter; we want to compute the sample standard devia- tion S of data X1, X,, . . . . , X,,,. We define

I X = ~ X i / m , i = 1

S = ( X , - - X ) ' - ' / ( m -- 1) . (1) \ / , = 1

A typical modern computer allows us to perform arithmetic on numbers with a fixed, limited number of significant digits (in some base), usually referred to as finite precision floating point arithmetic. Various pro- gramming techniques can use the basic operations to provide arithmetic with higher precision (e.g. doubled or arbitrarily flexible precision), but these techniques are usually complicated and expensive. Accordingly, we are often willing to accept the results of calculations which utilize the basic operations only in standard ways; these results are necessarily often somewhat inaccurate. If we want to compute S inexpensively, how accurate an an- swer can we expect?

Suppose, then, that we use some formula to compute an answer S. When is S sufficiently close to the true standard deviation S? Two naive and simplistic require- ments are: (a) allow S to differ from S in the least significant digit

of the computer representation of S (relative error); (b) require S to be within e of S, where e is a computer

dependent quantity, usually related to the least sig- nificant digit of the representation of 1 . 0 0 . . . 0 (ab- solute error) . (For example, e might be 10 -~ for a decimal computer with 6-digit arithmetic.)

Communicat ions September 1979 of Volume 22 the ACM Number 9

Page 2: Computing standard deviations: accuracy

The first requirement is unreasonable because even so simple a calculation as a + b + c could not be guaran- teed to meet this requirement without the use of extra precision. The second requirement is unreasonable be- cause it does not take into account the magnitude of S or the scale of the data.

We can arrive at a more realistic notion of accuracy if we realize that our goal usually is to compute inexact answers f rom uncertain data. We shall leave the compu- tation of exact answers f rom exact data to special algo- rithms. But in the practical, uncertain setting, a com- puted answer S may differ f rom the exact answer S, which itself differs f rom the "real" populat ion standard deviation o- because of measurement and sampling er- rors. We should be satisfied if the error made in the computa t ion is smaller than the possible change due to the statistical errors. For most practical problems, the comput ing error S -- S will be much smaller than the statistical errors represented by S -- or.

Defini t ion. If some algorithm on some computer computes S as the (approximate) s tandard deviation of X~, X ~ , . . . , X,,,, we shall call S (acceptably) accurate if

IS -- SI ~ max6 IS -- S~I (2)

where S~ is the s tandard deviation of X, + 8~, X~ + 8z, . . . . X,,, + 8 .... for shifts {8~} which still result in data points which lie within the statistical uncertainty of the data { X d .

If we can compute how large IS -- S~I can possibly be for data which lies within the uncertainty of the given data X~ . . . . . X~,,, then S is accurate enough if IS -- SI is known to be much smaller than the maximum of IS -- $61. Note that max6 IS -- $61 is an upper bound on the sta- tistical error IS -- crl.

The size of a vector is the usual (Eucl idean) length:

llXll = (X~ -t- 2?2. q- . . q- X~,,.) 1/-' 11811 (8~ q- 87, + . ~t- 8;~) 1/'-'.

Then we can show that the maximum effect of uncer- tainty in the data is

1 S ~ = S ( 1 + A )

where 11811 ( ( l l ~11) z - - ) . (3)

The approximation in (3) holds whenever 11811/ (m -- 1) 1/-~ is small compared to S; that is, whenever the uncertainty in the data is much smaller than the s tandard deviation of the sample.

The meaning of eq. (3) is more obvious in the com- mon situation where I~1 < 7 IX~I for all i; that is, when there is a c o m m o n upper bound 7 on the relative un- certainty in each datum. Then (3) can be replaced by

} S~----S(1 + A )

{ IAIwhere_< ( ( v I ? [ I ) 2 IlXll ;~/.,s + O - - ). ( 4 )

(m -- 1

The quanti ty IlXll/((m -- 1)~/=S) is always at least unity because of the identity:

[IXll "° = (m -- 1)S 2 -4- m X 2 > = ( m -- 1)SL

Hence, the relative sensitivity of S can be larger by a factor IIXll/((m -- 1)w-'S) than the uncertainty of the data. If IIXII is much larger than S, or if ..Y is much larger than S, we may find large relative uncertainties in S.

Thus, we find it useful to make the following defini- tion.

Defini t ion. The condition number ~ of S is

2. The Uncertainty of S [IXll

K = v r - ~ _ _ l S "

Suppose we know how certain (uncertain) the data X , , . . . , X,,~ are. Can we estimate how close S must be to the true sample s tandard deviation? For example, as- sume that each datum is given to three significant digits; we choose ~1, ~2 . . . . . ~,,~ such that [8il < 10 :~ IXil and compute S~, the s tandard deviation of X~ + ~1 . . . . , X,~ + 8m. Can we say that IS6 - SI _< 10-3S? The an- swer is no - the relative change in S can be far greater than the relative change in any individual datum. The standard deviation can be very sensitive to changes. The results below provide a perspective f rom which we can predict the sensitivity of S. (A complete exposition of the results is given in [1 ]. )

We shall use the notation below:

the vector X is ( X , , X2 . . . . . X ,D 'e

the vector 8 is (~1, ~ . . . . . 8m)L

The condit ion number measures the extra sensitivity of S," if the data has relative uncertainty v, then S has rela- tive uncertainty KT, where K is always at least 1. In many practical situations K is much bigger than 1.

The essence of this result is the effect of K on the sensitivity of S. Consider a sample of 100 points, with mean 100 and standard deviation 1. Then

IlXll ~ m ~ / - ' X = 1000; S = I ; ( m - - 1 ) ~ / ~ 1 0

and so

K ~ 100.

There are, in fact, sets of data X1 + 8 1 , . . . , X~, + 8,,~ which differ f rom the given data by at most 1 unit in the third digit, but which have standard deviations through- out the interval .9 _< S~ _< 1.1.

527 Communications September 1979 of Volume 22 the ACM Number 9

Page 3: Computing standard deviations: accuracy

Table I. Algorithms for Variance S e.

Algorithm Mathematical Computational

V1 Two-Pass .~ = ~ X i / m Aluorithm i : a

S'-' = ~ ( X i - ~ ) ' - ' / ( r n - 1) i = 1

V2 West's Algorithm

V3 Textbook Algorithm

V4 Cotton's Algorithm

M 1 = X1

(k = 2 . . . . . m) ..~ = Mm Tz = 0 Tk = Tk-1 + ( k - - 1) * <1)

(k = 2 . . . . . m) S-' = T r n / ( m -- 1)

= ~ X J m i = 1

1 [ m X ~

m - 1

M 1 = X1

T I = O l Mk = M k - 1 q- ( X k - - Mk-1)/k T k = T k - l + X ~ +

(/," -- l) Mg-1 -- kMl~

(k = 2, 3 . . . . . m) ,~ = M m S z = T , , , / ( m -- 1)

.~BAR = 0 F o r i = 1,2 . . . . . m do

XBAR = XBAR + Xi XBAR = X B A R / m $2 = 0 F o r i = 1 , 2 , . . . , m do

$2 = $2 + (Xi -- XBAR)'-' $ 2 = S 2 / ( m -- 1)

M = X 1

T = 0 F o r i = 2,3 . . . . . m do

I Q = Xi - - M R = Q / i

M = M + R

T = T + ( i - - 1) * Q * R

$ 2 = T / ( m -- 1) XBAR = M

Tk ,~1 (Xi -- Mk) '-'

SUMX = 0 SUMX2 = 0 F o r i = 1,2 . . . . . m do

SUMX = SUMX + Xi SUMX2 = SUMX2 + (Xi)'-'

XBAR = S U M X / m

$2 = (SUMX2 -- XBAR * SUMX) / ( m -- 1)

M = X1

T = 0 For i = 2, 3 . . . . . m do

= Q + ( X i - Q ) / i

= r + x ~ + ( i - - 1) Q" - i M 2

XBAR = M $ 2 = T / ( m - - l )

3 . T h e R e l a t i v e A c c u r a c y o f S o m e C o m p u t e r

A l g o r i t h m s

In this sec t ion we g ive u p p e r b o u n d s on the c o m p u -

t a t iona l e r ro r s wh ich can be e n c o u n t e r e d in the use of

any of f o u r ways to c o m p u t e the s t a n d a r d d e v i a t i o n S.

F o r each a l g o r i t h m we s h o w h o w to c o m p u t e a n u m b e r

1' such tha t

IS - SI < J',

w h e r e 1' d e p e n d s on K, m, and the p rec i s ion of the c o m -

puter . W e k n o w o u r c o m p u t a t i o n is as a c c u r a t e as the

u n c e r t a i n t y of S just if ies if we find tha t 1" ~< K • S • ~,,

w h e r e ~, is the r e l a t ive u n c e r t a i n t y in the data .

T h e a l g o r i t h m s are all m a t h e m a t i c a l l y e q u i v a l e n t to

528

t he def in i t ion - t hey a re o n l y c o m p u t a t i o n a l r e a r r a n g e -

m e n t s of f o r m u l a ( 1 ) , bu t they dif fer in the i r a c c u r a c y

and cost . In t he de sc r ip t i on in T a b l e I, e a c h a l g o r i t h m is

r e p r e s e n t e d by a m a t h e m a t i c a l f o r m u l a , and by an in fo r -

ma l ou t l i ne of a c o m p u t e r a lgo r i t hm. N o t e tha t we as-

s u m e all s u m m a t i o n s a re p e r f o r m e d f r o m lef t to r ight .

T h e first of t hese a lgo r i t hms , V1 , is jus t a r ea l i za t ion

of the m a t h e m a t i c a l def in i t ion . I ts m a j o r v i r t ue is the

h ighes t a c c u r a c y of the f o u r a lgo r i t hms . H o w e v e r , it suf-

fers as a c o m p u t a t i o n a l s c h e m e b e c a u s e the m e a n m u s t

be c o m p u t e d first, b e f o r e the s u m m a t i o n of the dev i a -

t ions can begin . T h e v a l u e of e a c h d a t u m m u s t be ava i l -

ab le at t w o d i f fe ren t t imes ; hence , the d a t a mus t be r e a d

twice if the m e m o r y is no t la rge e n o u g h to s tore all of

the X~'s. T h e r e m a i n i n g th ree a lgo r i t hms , V2 , V3 , and V4 ,

Communications September 1979 of Volume 22 the ACM Number 9

Page 4: Computing standard deviations: accuracy

are mathematical rearrangements of eq. ( 1 ), which per- mit the computation of S in a single reading or pass over the data. Each requires memory for two sums in process, and a few auxiliary scalars, but not for the vector X. They are implicitly "updating" algorithms, since the computation of the variance of the current sample plus one new additional datum can be accomplished simply by proceeding with one more iteration of the summation. (Algorithm V1 as stated would require that the second summation be recomputed completely.)

Algorithm V2 is due to West [7]; it is an optimized version of the algorithm given by Hanson in [3]. (This unweighted algorithm is also given by Welford in [6].) It successfully preserves most of the accuracy of the Two-Pass Algorithm (V1) without requiring more than a small amount of memory. It is a very good algorithm for machine calculation and should probably be used in many of the situations in which the Textbook Algorithm (V3) is now used.

Algorithm V3 is the algorithm given in many statis- tics textbooks because it is well-suited for pencil and paper calculation. It is also a good algorithm for exact calculations (using special arithmetic facilities). When used with finite precision arithmetic, it may compute very poor answers to difficult problems, even to the point of computing a negative number as the variance. This algorithm is, unfortunately, very widely used on computers.

The last algorithm, V4, is due to Cotton [2]. We in- clude it here only to illustrate the danger of rearrange- ments without attention to rounding errors. Its similarity to V2 and V3 is only superficial; it is a poor algorithm for either hand calculation or machine computation. We know of no situation in which it should be used.

J.H. Wilkinson has shown in [9] how to analyze an algorithm when reasonable floating point arithmetic is used.We have carried out such analyses in [1] for each of the four algorithms given here. The analyses are straight- forward, albeit tedious, derivations of bounds from sim- ple axioms describing the floating point arithmetic. Spe- cifically, we require knowledge of a quantity ~ which bounds the relative error in any single elementary opera- tion. The quantity ~ depends on the computer, but it is usually the same as the smallest number which, when added to unity on the computer, produces a number larger than one. Only an order of magnitude estimate is needed. As concrete examples, we can take ~ = 10-" for an HP25 calculator, ~/ = 10 -11 for a TI57 calculator,

= 16 -'~ ~ 10 -'~ for IBM 370 computers (short preci- sion).

Table I I above contains the bounds derived in [1]. These are upper bounds on the ~ ax i m um errors which could be encountered using a computer with relative pre- cision ~l, on a sample of size m with condition number ~. For contrast, the first entry in Table II is the maximum change which could occur in data with relative uncer- tainty ~,.

Table II. Upper bounds for IS -- S[ for samples with m points, S

condition number x. on a computer with relative precision (high order terms eliminated).

Theoretical perturbations from relative uncertainty y

xy

Algorithm Vl (Two-Pass)

2x~ + ( @ + 1)7

(West's updating) + 7m~/2 + l)xr/

Algorithm V3 (Textbook)

( @ m + 1)x2~ + ,q

Algorithm V4 (Cotton's updating)

5 ,,

+(32--! - 1 )Tj

To evaluate the appropriateness of a particular algo- rithm on a particular computer for a particular set of data, a user needs to have knowledge of y, the relative uncertainty in his data; r l, the relative precision of his computer; and K, the condition number of the data. The condition number K is usually not known by the user, but it can easily be derived from the results of the calcula- tion, especially since the quantity X / S is a good estimate whenever K is large. Most statistical packages routinely compute the coefficient of variation S/X; whenever this quantity is much less than one, the user should be aware of possible trouble. Several things are clear from the bounds in Table II. First, the condition number di- rectly measure~ the difficulty of the problem. When K is large, we can expect the Textbook Algorithm (V3) to perform badly because of the squaring of K. Secondly, the error bound for V3 is never less than for the Two- Pass Algorithm (V1) . (This does not say that V3 might not be more accurate on a particular set of data, only that its guarantee is less good.) Third, we can always find adequately accurate answers by using higher pre- cision arithmetic (reducing r/). The standard precision of most computers is adequate for most problems.

It is not obvious from Table I ! whether the bounds are overly pessimistic or are close to the actual errors. For example, all of the bounds grow as functions of 177; so do the relative errors observed in actual calculation. However, the rate of increase rarely approaches the pre- dicted rate. On the other hand, the dependence on K or K 2 is usually seen as predicted, except on very easy prob- lems. (However, the bound for VI , and V1 alone, is act- ually independent of K if the computer carriers a guard digit during arithmetic operations. Bounds similar to Table I I for this special type of arithmetic are given in [1 ].)

529 Communications September 1979 of Volume 22 the ACM Number 9

Page 5: Computing standard deviations: accuracy

4. Numerical Examples

In the previous section we have presented theoretical error bounds for the four algorithms. The bounds indi- cate the relative performance of the algorithms, but they do not give the actual errors for any given set of data. To demonstrate that our bounds are significant, we have computed standard deviations for a series of pseudo- random samples drawn from a normally distributed pop- ulation with mean unity. The effect of the condition number K on the accuracy is shown by choosing samples of the same size from populations with different vari- ances. Similarly, the effect of sample size is shown by choosing samples of different sizes from the same popu- lation. Tables III , IV, and V provide both of these varia- tions. The results confirm the dependence of the error on K or K 2. However, the growth rate as a function of m is slower than the bounds. (The actual growth rate for our samples is less than m ' /2 for all of the algorithms.)

Similar numerical examples, based on populations which are not normally distributed, are given in [5], [10], and extensively in [4]. The comparative results are con- sistent with ours. However, two anomalies have been observed and can be predicted on technical grounds. The Two-Pass Algorithm V! (only) has a particularly small bound on computers which use at least one guard digit in arithmetic computation. Recent IBM computers and the DEC System 10 do provide this facility, which explains the excellent results above. The Textbook Algorithm (V3) does unexpectedly well on whole or round number data, at least when K is not quite large. (Our samples do not show this effect because of the method of generating the data. )

5. Conclusions

All four a lgor i thms p e r f o r m accep tab ly for da ta which is much less precise than the computer and when S is not small compared to I IXII. An algorithm for general use should work even when these conditions are vio- lated. We can recommend particular settings in which each algorithm is appropriate:

V1 : high accuracy, when X can be stored in memory; V2: high accuracy, when storage is limited and for

sequential sampling and quality control; V3: exact computation with rational arithmetic; V4: never.

We close with several observations about related problems. For the unweighted case we have considered, the quantity Y~X~ is often of statistical interest. The calculation Y~X~ = (m -- 1 )S 2 + m.Y ~ will be accu- rate if S and .Y are accurate. Hence, the use of V1 or V2 makes this information available. Good algorithms for the weighted case are given in [3] and [7]. Similar algo- rithms also can be derived for linear regression, which

Table llI. Data chosen randomly from N (1.0, o-). Accuracy results are the number of correct leading digits averaged over 20 samples, each with 10 points.*

Number of correct leading digits in S ~ when calculated by:

West's Cotton's Average Two-Pass updating Textbook updating condition

o- (V1) (V2) (V3) (V4) number K

1.0 8.3 8.0 7.7 7.8 1.31 0.1 8.3 7.7 5.8 5.8 1.01 X 101 0.01 8.1 6.8 3.9 3.7 1.00 X 102 0.001 8.1 5.6 1.7 1.7 1.07 × 10 :~ 0.0001 7.9 4.5 0.0 0.0 1.05 X 104

':'Computations on a DEC System 10 in single precision, (8D).

Table IV. Data chosen randomly from N ( 1.0, o-). Accuracy results are the number of correct leading digits averaged over 20 samples, each with 100 points.*

Number of correct leading digits in S'-' when calculated by:

West's Cotton's Average Two-Pass updating Textbook updating condition

o- (V1) (V2) (V3) (V4) number x

1.0 7.8 7.7 7.5 7.3 1.40 0.I 7.7 7.6 5.5 5.4 1.01 X 101 0.01 7.8 6.7 3.4 3.5 1.01 X 102 0.001 7.8 5.7 1.4 1.4 0.99 X 103 0.0001 7.3 4.8 0.0 0.0 1.00 X 104

*Computations on a DEC System 10 in single precision, (8D).

Table V. Data chosen randomly from N ( 1.0. cr). Accuracy results are the number of correct leading digits averaged over 20 samples, each with 1000 points.*

Number of correct leading digits in S ~ when calculated by:

West's Cotton's Average Two-Pass updating Textbook updating condition

o- (V1 (V2) (V3) (V4) number

1.0 7.3 7.3 7.0 6.9 1.38 0.1 7.5 7.2 5.0 5.1 1.01 X 101 0.01 7.3 6.7 3.1 3.0 1.00 X 102 0.001 7.4 5.6 1.1 0.8 1.00 X 103 0.0001 6.2 4.6 0.0 0.0 1.0l X 104

*Computations on a DEC System 10 in single precision, (8D).

give higher accuracy in difficult situations than do the algorithms most often used [8]. Results like those pre- sented here hold for the weighted algorithms, with an important exception. The weighted algorithms have the formal capability to use negative weights to remove (er- roneous) data from the calculation. "Downdating" or the removal of "blunder points" can destroy all accu- racy in S even in the unit weight case. This can be dem- onstrated easily on any of the popular calculators with this feature by entering and removing a single datum whose magnitude is much larger than the other data in the calculation. It can also be seen in the degradation in accuracy shown in the downdating tables in [7]. The accuracy results here and the stability arguments in Han- son [3] apply only to the positive weight case.

530 Conamunications September 1979 of Volume 22 the ACM Number 9

Page 6: Computing standard deviations: accuracy

Table VI. Mean Variance

Updating Updating West [7], lb 2a Ling [4] Youngs & Cramer [10], la 2b Nelson [11] Hanson [3], lc 2a Ling [4], Welford [6]

A d d e n d u m

The inaccuracies of the Tex tbook A l g o r i t h m ( V 3 ) and Cot ton ' s A lgo r i t hm ( V 4 ) i l lustrate the dangers of rear ranging the opera t ions in calculat ing the var iance of a sample. However , there are cer ta in permiss ib le re- a r rangements which give rise to a small family of accu- rate upda t ing algori thms. Specifically, assume that we know ei ther

Mk = k i

or

k

Sk = ~ x i = k ' M k , i = l

and also

k

TI~ = 5;(x~ - - Mk) z. i=1.

Given a new da tum xk + 1, we can accura te ly compute Tk + and ei ther Mk + 1 or Sk + 1, using any of the fo rmulae below:

forSk+~,

for Mk+ 1, ei ther of

for Tk+i, any of

Sk+, = Sk + xk+~ ( l a )

t Mk+l = Mk + (xk+l - -

M k ) / ( k + 1) ( l b )

I Mk+l ( k ' M k + x~+l ) /

(k q- 1) ( l c ) Tk+l = T~ + k ' ( x ~ + l - -

M k ) 2 / ( k + 1) (2a) Tk+l = Tk + (k + 1).

(xk+l - - M k + l ) Z / k (2b) Tk+l = Tk + (k + l ) .

k . ( m k + l - - Mk) 2 (2c)

The re are nine combina t ions which can be bui l t f rom eqs. ( l a ) , ( l b ) , o r ( l c ) with ( 2 a ) , ( 2 b ) , or (2c). In addi t ion, there are rea r rangements to exchange divisions for mul t ip l ica t ions in equat ions ( 2 a b c ) in combina t ion with ( l a ) (see [10]) . Wi th at least twelve choices, which a lgor i thm should we use? The er ror bounds for a lgo- r i thms which use ei ther ( t c ) or (2c ) are worse than for the o ther a lgor i thms; in our numer ica l exper iments the five a lgor i thms using one or both of these equat ions have been somewhat less accurate. The best e r ror bounds are for ( l a ) and ( 2 a ) , so the best choice would appea r to be (1 a) with ( 2 a ) . However , there is essent ial ly no dif-

ference in accuracy be tween this a lgor i thm and the com- b ina t ion ( l b ) with (2a) which requires one fewer opera - t ion per da tum ( two fewer opera t ions if the new mean is requi red at each s t ep) . This is the a lgor i thm by West, which we r e c o m m e n d as the bes t genera l choice among these var iat ions. The var ia t ions which have been pub- l ished are given in Tab le VI.

A c k n o w l e d g m e n t s . G. Golub , W.M. Kahan , and A. BjSrck have all p rov ided insight into the numer ica l as- pects of the calcula t ions . I. Olkin suggested that we adop t s t andard stat is t ical notat ions, which we have done in an a t tempt to reach a wider audience.

Received September 1975; revised April 1979

References 1. Chan, T.F.C., and Lewis, J.G. Rounding error analysis of algorithms for computing means and standard deviations. Tech. Rep. No. 289, Dept. of Mathematical Sciences, The Johns Hopkins U., Baltimore, Md., April 1978. 2. Cotton, I.W. Remark on stably updating mean and standard deviation of data. Comm. A C M 18, 8 (Aug. 1975), 458. 3. Hanson, R.J. Stably updating mean and standard deviation of data. Comm. A C M 18, 1 (Jan. 1975), 57-58. 4. Ling, R.F. Comparison of several algorithms for computing means and variances. J. Amer. Statistical Assoc. 69, 348 (Dec. 1974), 859-866. 5. Neely, P.M. Comparison of several algorithms for computation of means, standard deviations and correlation coefficients. Comm. A C M 9, 7 (July 1966), 496-499. 6. Welford, B.P. Note on a method for calculating corrected sums of squares and products. Technometrics 4 (Aug. 1962), 419 -420. 7. West, D.H.D. Updating mean and variance estimates: An improved method. Comm. A C M 22, 9 (Sept. 1979), 532-535. 8. West, D.H.D. Incremental least squares and the approximate separation of exponentials. Nuclear Instruments and Methods 136 (1976), 137-143. 9. Wilkinson, J.H. Rounding errors in algebraic processes. Prentice-Hall, Englewood Cliffs, N.J., 1963. 10. Youngs, E.A., and Cramer, E.M. Some results relevant to choice of sum and sum-of-product algorithms. Technometrics 13 (Aug. 1971 ), 657-665. 11. Nelson, L.S. Further remarks on stably updating mean and standard deviation estimates. Comm. A C M 22, 8 (Aug. 1979), 483.

531 Communications September 1979 of Volume 22 the ACM Number 9


Recommended