1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20  import copy 
 21  import math 
 22   
 23  try: 
 24      from numpy import maximum, minimum, sqrt 
 25  except ImportError: 
 26      maximum = max 
 27      minimum = min 
 28      sqrt = math.sqrt 
 29   
 30   
 32   
 34          self.n = 0L     
 35          self.mu = 0.0   
 36          self.m2 = 0.0   
 37          self.maxValue = float("-inf") 
 38          self.minValue = float("inf") 
 39   
 40          for v in values: 
 41              self.merge(v) 
  42   
 43       
 45          delta = value - self.mu 
 46          self.n += 1 
 47          self.mu += delta / self.n 
 48          self.m2 += delta * (value - self.mu) 
 49          self.maxValue = maximum(self.maxValue, value) 
 50          self.minValue = minimum(self.minValue, value) 
 51   
 52          return self 
  53   
 54       
 56          if not isinstance(other, StatCounter): 
 57              raise Exception("Can only merge Statcounters!") 
 58   
 59          if other is self:   
 60              self.merge(copy.deepcopy(other))   
 61          else: 
 62              if self.n == 0: 
 63                  self.mu = other.mu 
 64                  self.m2 = other.m2 
 65                  self.n = other.n 
 66                  self.maxValue = other.maxValue 
 67                  self.minValue = other.minValue 
 68   
 69              elif other.n != 0: 
 70                  delta = other.mu - self.mu 
 71                  if other.n * 10 < self.n: 
 72                      self.mu = self.mu + (delta * other.n) / (self.n + other.n) 
 73                  elif self.n * 10 < other.n: 
 74                      self.mu = other.mu - (delta * self.n) / (self.n + other.n) 
 75                  else: 
 76                      self.mu = (self.mu * self.n + other.mu * other.n) / (self.n + other.n) 
 77   
 78                  self.maxValue = maximum(self.maxValue, other.maxValue) 
 79                  self.minValue = minimum(self.minValue, other.minValue) 
 80   
 81                  self.m2 += other.m2 + (delta * delta * self.n * other.n) / (self.n + other.n) 
 82                  self.n += other.n 
 83          return self 
  84   
 85       
 87          return copy.deepcopy(self) 
  88   
 91   
 94   
 96          return self.n * self.mu 
  97   
100   
103   
104       
106          if self.n == 0: 
107              return float('nan') 
108          else: 
109              return self.m2 / self.n 
 110   
111       
112       
113       
114       
116          if self.n <= 1: 
117              return float('nan') 
118          else: 
119              return self.m2 / (self.n - 1) 
 120   
121       
124   
125       
126       
127       
128       
131   
133          return ("(count: %s, mean: %s, stdev: %s, max: %s, min: %s)" % 
134                  (self.count(), self.mean(), self.stdev(), self.max(), self.min())) 
  135