def _welford(data):
    """Welford's method of calculating the running variance.

    This calculates the second moment M2 = sum( (x-m)**2 ) where m=mean of x.
    Returns (n, M2) where n = number of items.
    """
    data = iter(data)
    n = 0
    M2 = 0.0
    try:
        m = next(data)
        n = 1
    except StopIteration:
        pass
    else:
        for n, x in enumerate(data, 2):
            delta = x - m
            m += delta / n
            M2 += delta * (x - m)
    assert M2 >= 0.0
    return n, M2
