import math


def avestd(vals):
    """return an (average, stddev) tuple computed from the supplied list of values"""
    (sum, sum2) = (0.,0.)
    N = float(len(vals))
    for val in vals:
        sum  = sum  + float(val)
        sum2 = sum2 + float(val)*float(val)
    if N == 1:
        ave = sum
        std = 0
    else:
        ave = sum /  N
        std = math.sqrt( (sum2-(N*ave*ave)) / (N-1.0) )
    return ave,std