[Python-Dev] Accumulation module

Kevin Jacobs jacobs at penguin.theopalgroup.com
Tue Jan 13 15:26:43 EST 2004


On Tue, 13 Jan 2004, Raymond Hettinger wrote:
> * Is there a way to compute the standard deviation without multiple
> passes over the data (one to compute the mean and a second to sum the
> squares of the differences from the mean?

Yes --

def one_pass_stddev(l):
  n  = 0
  x  = 0. 
  xx = 0.

  for y in l:
    n  += 1
    x  += y
    xx += y*y

  x  /= n
  xx /= n
  var = max(0,xx - x*x)
  dev = var**0.5
 
  return dev

Skewness and kurtosis can also be computed in one pass, though numerical
stability problems can occur (even with std.dev) with these kinds of
methods.

-Kevin




More information about the Python-Dev mailing list