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(dataWeightPairs): sumweight = 0 mean = 0 M2 = 0 for x, weight in dataWeightPairs: # Alternatively "for x, weight in zip(data, weights):" temp = weight + sumweight delta = x - mean R = delta * weight / temp mean = mean + R M2 = M2 + sumweight * delta * R # Alternatively, "M2 = M2 + weight * delta * (x−mean)" sumweight = temp variance_n = M2/sumweight variance = variance_n * len(dataWeightPairs)/(len(dataWeightPairs) − 1)Read more about this topic: Algorithms For Calculating Variance
Related Phrases
Related Words