def online_variance(data):
    """
        A numerically stable algorithm for calculating variance
        http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance#On-line_algorithm
    """

    n = 0
    mean = 0
    M2 = 0

    for x in data:
        n = n + 1
        delta = x - mean
        mean = mean + delta/n
        M2 = M2 + delta*(x - mean)

    variance_n = M2/n
    variance = M2/(n - 1)
    return (variance, variance_n)