# - - - m e a n S i g m a def meanSigma(samples): '''Compute the mean and standard deviation of a set of samples. [ samples is a sequence of two or more floats -> return (mean of samples, standard deviation of samples) ] '''
The method is from a Wikipedia
article. See the one-pass method under
“Naï ve algorithm,” implementing the note
about computing the variance of a finite population by dividing
Just to be sure, we checked this method against the
.var() method on the
array type in the
numpy (Numeric Python) package. For every test
we tried, dividing by
did not match
numpy's value, but
matched it exactly.
#-- 1 # [ sumx := sum of the values in samples # sumSquares := sum of the squares of values in samples # n := float(len(samples)) ] sumx = sumSquares = 0.0 n = float(len(samples)) for x in samples: sumx += x sumSquares += x*x #-- 2 mean = sumx / n sigma = math.sqrt((sumSquares - sumx*mean)/n) return (mean, sigma)