Next / Previous / Contents / TCC Help System / NM Tech homepage

57. meanSigma(): Compute statistics

cbchistlib.py
# - - -   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 by n instead of n-1.

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 n-1 did not match numpy's value, but dividing by n matched it exactly.

cbchistlib.py
    #-- 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)