def online_variance(data):
 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)

def online_variance ( data ) : 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 )
a numerically stable algorithm for calculating variance URL#on-line_algorithm .