show/hide this revision's text 7 deleted 11 characters in body

For the variance (square of std), personally I would use this simple one:

( n (sum x*x ) - (sum x)*(sum x) ) / (n*n) orbetter: ( (sum x*x ) - (sum x)*(sum x) / n ) / n

[ For the case of relatively small variance ("catastrophic cancellation") check also: http://www.daheiser.info/excel/notes/NOTE%20P.pdf ]

Edit:

A viki page also reports a nice discussion: http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#cite_note-1

In particular, it mentions: A numerically stable algorithm is given below. It also computes the mean. This algorithm is due to Knuth,[1] who cites Welford.[2] [...] This algorithm is much less prone to loss of precision due to massive cancellation, but might not be as efficient because of the division operation inside the loop.

About the 2-pass algo, it says: "This algorithm is often more numerically reliable than the naïve algorithm for large sets of data, although it can be worse if much of the data is very close to but not precisely equal to the mean and some are quite far away from it."

I'd say that if the variance is not small (as often happens) and ease and speed of computation are a priority, the simpler version (closer to the definition) could still be considered.

show/hide this revision's text 6 added 987 characters in body; added 2 characters in body; added 14 characters in body

For the variance (square of std), personally I would use this simple one:

( n (sum x^2 x*x ) - (sum x)^2 x)*(sum x) ) / n^2(n*n)

or better:

( (sum x*x ) - (sum x)*(sum x) / n ) / n

[ For the case of relatively small variance ("catastrophic cancellation") check also: http://www.daheiser.info/excel/notes/NOTE%20P.pdf ]

Edit:

A viki page also reports a nice discussion: http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#cite_note-1

In particular, it mentions: A numerically stable algorithm is given below. It also computes the mean. This algorithm is due to Knuth,[1] who cites Welford.[2] [...] This algorithm is much less prone to loss of precision due to massive cancellation, but might not be as efficient because of the division operation inside the loop.

About the 2-pass algo, it says: "This algorithm is often more numerically reliable than the naïve algorithm for large sets of data, although it can be worse if much of the data is very close to but not precisely equal to the mean and some are quite far away from it."

I'd say that if the variance is not small (as often happens) and ease and speed of computation are a priority, the simpler version (closer to the definition) could still be considered.

show/hide this revision's text 5 deleted 284 characters in body; added 30 characters in body

For the variance (square of std), personally I would use this simple one:

( n (sum x^2 ) - (sum x)^2 ) / n^2

I think it should be intuitive to perceive that the above will be more precise than the suggested recurrences, which frankly look awkward, (besides, the (sum x) often comes from previous, or simultaneous, computation of avg).

I leave to the professionals the proof or disproof ...

[ For the case of relatively small variance ("catastrophic cancellation") check also: http://www.daheiser.info/excel/notes/NOTE%20P.pdf ]

show/hide this revision's text 4 added 90 characters in body; added 18 characters in body
show/hide this revision's text 3 added 1 characters in body
show/hide this revision's text 2 added 2 characters in body
show/hide this revision's text 1