2020import copy
2121import math
2222
23+
2324class StatCounter (object ):
24-
25+
2526 def __init__ (self , values = []):
2627 self .n = 0L # Running count of our values
2728 self .mu = 0.0 # Running mean of our values
2829 self .m2 = 0.0 # Running variance numerator (sum of (x - mean)^2)
2930 self .maxValue = float ("-inf" )
3031 self .minValue = float ("inf" )
31-
32+
3233 for v in values :
3334 self .merge (v )
34-
35+
3536 # Add a value into this StatCounter, updating the internal statistics.
3637 def merge (self , value ):
3738 delta = value - self .mu
@@ -42,15 +43,15 @@ def merge(self, value):
4243 self .maxValue = value
4344 if self .minValue > value :
4445 self .minValue = value
45-
46+
4647 return self
4748
4849 # Merge another StatCounter into this one, adding up the internal statistics.
4950 def mergeStats (self , other ):
5051 if not isinstance (other , StatCounter ):
5152 raise Exception ("Can only merge Statcounters!" )
5253
53- if other is self : # reference equality holds
54+ if other is self : # reference equality holds
5455 self .merge (copy .deepcopy (other )) # Avoid overwriting fields in a weird order
5556 else :
5657 if self .n == 0 :
@@ -59,19 +60,19 @@ def mergeStats(self, other):
5960 self .n = other .n
6061 self .maxValue = other .maxValue
6162 self .minValue = other .minValue
62-
63- elif other .n != 0 :
63+
64+ elif other .n != 0 :
6465 delta = other .mu - self .mu
6566 if other .n * 10 < self .n :
6667 self .mu = self .mu + (delta * other .n ) / (self .n + other .n )
6768 elif self .n * 10 < other .n :
6869 self .mu = other .mu - (delta * self .n ) / (self .n + other .n )
6970 else :
7071 self .mu = (self .mu * self .n + other .mu * other .n ) / (self .n + other .n )
71-
72+
7273 self .maxValue = max (self .maxValue , other .maxValue )
7374 self .minValue = min (self .minValue , other .minValue )
74-
75+
7576 self .m2 += other .m2 + (delta * delta * self .n * other .n ) / (self .n + other .n )
7677 self .n += other .n
7778 return self
@@ -94,7 +95,7 @@ def min(self):
9495
9596 def max (self ):
9697 return self .maxValue
97-
98+
9899 # Return the variance of the values.
99100 def variance (self ):
100101 if self .n == 0 :
@@ -124,5 +125,5 @@ def sampleStdev(self):
124125 return math .sqrt (self .sampleVariance ())
125126
126127 def __repr__ (self ):
127- return "(count: %s, mean: %s, stdev: %s, max: %s, min: %s)" % ( self . count (), self . mean (), self . stdev (), self . max (), self . min ())
128-
128+ return "(count: %s, mean: %s, stdev: %s, max: %s, min: %s)" %
129+ ( self . count (), self . mean (), self . stdev (), self . max (), self . min ())
0 commit comments