of differences from the current mean, , here denoted : This algorithm was found by Welford, and it has been thoroughly analyzed. It is also common to denote and . An example Python implementation for Welford's algorithm is given below. # For a new value newValue, compute the new count, new mean, the new M2. # mean accumulates the mean of the entire dataset # M2 aggregates the squared distance from the mean # count aggregates the number of samples seen so far def update(existingAggregate, newValue): (count, mean, M2) = existingAggregate count += 1 delta = newValue - mean mean += delta / count delta2 = newValue - mean M2 += delta * delta2 return (count, mean, M2) # Retrieve the mean, variance and sample variance from an aggregate def finalize(existingAggregate): (count, mean, M2) = existingAggregate if count < 2: return float("nan") else: (mean, variance, sampleVariance) = (mean, M2 / count, M2 / (count - 1)) return (mean, variance, sampleVariance) This algorithm is much less prone to loss of precision due to catastrophic cancellation, but might not be as efficient because of the division operation inside the loop. For a particularly robust two-pass algorithm for computing the variance, one can first compute and subtract an estimate of the mean, and then use this algorithm on the residuals. The parallel algorithm below illustrates how to merge multiple sets of statistics calculated online. Weighted incremental algorithm The algorithm can be extended to handle unequal sample weights, replacing the simple counter n with the sum of weights seen so far. West (1979) suggests this incremental algorithm: def weighted_incremental_variance(data_weight_pairs): w_sum = w_sum2 = mean = S = 0 for x, w in data_weight_pairs: w_sum = w_sum + w w_sum2 = w_sum2 + w**2 mean_old = mean mean = mean_old + (w / w_sum) * (x - mean_old) S = S + w * (x - mean_old) * (x - mean) population_variance = S / w_sum # Bessel's correction for weighted samples # Frequency weights sample_frequency_variance = S / (w_sum - 1) # Reliability weights sample_reliability_variance = S / (w_sum - w_sum2 / w_sum) Parallel algorithm Chan et al. note that Welford's online algorithm detailed above is a special case of an algorithm that works for combining arbitrary sets and : . This may be useful when, for example, multiple processing units may be assigned to discrete parts of the input. Chan's method for estimating