1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17   
 18   
 19   
 20   
 21   
 22   
 23  """Module for calculating simple statistics.""" 
 24   
 25   
 26  from math import exp, pi, sqrt 
 27   
 28   
 29 -def bucket(values=None, lower=0.0, upper=200.0, inc=100, verbose=False): 
  30      """Generate a discrete probability distribution for the given values. 
 31   
 32      @keyword values:    The list of values to convert. 
 33      @type values:       list of float 
 34      @keyword lower:     The lower bound of the distribution. 
 35      @type lower:        float 
 36      @keyword upper:     The upper bound of the distribution. 
 37      @type upper:        float 
 38      @keyword inc:       The number of discrete increments for the distribution between the lower and upper bounds. 
 39      @type inc:          int 
 40      @keyword verbose:   A flag which if True will enable printouts. 
 41      @type verbose:      bool 
 42      @return:            The discrete probability distribution. 
 43      @rtype:             list of lists of float 
 44      """ 
 45   
 46       
 47      bin_width = (upper - lower)/float(inc) 
 48   
 49       
 50      dist = [] 
 51      for i in range(inc): 
 52          dist.append([bin_width*i+lower, 0]) 
 53   
 54       
 55      for val in values: 
 56           
 57          bin = int((val - lower)/bin_width) 
 58   
 59           
 60          if bin < 0 or bin >= inc: 
 61              if verbose: 
 62                  print("Outside of the limits: '%s'" % val) 
 63              continue 
 64   
 65           
 66          dist[bin][1] = dist[bin][1] + 1 
 67   
 68       
 69      total_pr = 0.0 
 70      for i in range(inc): 
 71          dist[i][1] = dist[i][1] / float(len(values)) 
 72          total_pr = total_pr + dist[i][1] 
 73   
 74       
 75      if verbose: 
 76          print("Total Pr: %s" % total_pr) 
 77   
 78       
 79      return dist 
  80   
 81   
 83      """Calculate the probability for a Gaussian probability distribution for a given x value. 
 84   
 85      @keyword x:     The x value to calculate the probability for. 
 86      @type x:        float 
 87      @keyword mu:    The mean of the distribution. 
 88      @type mu:       float 
 89      @keyword sigma: The standard deviation of the distribution. 
 90      @type sigma:    float 
 91      @return:        The probability corresponding to x. 
 92      @rtype:         float 
 93      """ 
 94   
 95       
 96      return exp(-(x-mu)**2 / (2.0*sigma**2)) / (sigma * sqrt(2.0 * pi)) 
  97   
 98   
 99 -def std(values=None, skip=None, dof=1): 
 100      """Calculate the standard deviation of the given values, skipping values if asked. 
101   
102      @keyword values:    The list of values to calculate the standard deviation of. 
103      @type values:       list of float 
104      @keyword skip:      An optional list of booleans specifying if a value should be skipped.  The length of this list must match the values.  An element of True will cause the corresponding value to not be included in the calculation. 
105      @type skip:         list of bool or None. 
106      @keyword dof:       The degrees of freedom, whereby the standard deviation is multipled by 1/(N - dof). 
107      @type dof:          int 
108      @return:            The standard deviation. 
109      @rtype:             float 
110      """ 
111   
112       
113      n = 0 
114      for i in range(len(values)): 
115           
116          if skip != None and not skip[i]: 
117              continue 
118   
119           
120          n = n + 1 
121   
122       
123      Xsum = 0.0 
124      for i in range(len(values)): 
125           
126          if skip != None and not skip[i]: 
127              continue 
128   
129           
130          Xsum = Xsum + values[i] 
131   
132       
133      if n == 0: 
134          Xav = 0.0 
135      else: 
136          Xav = Xsum / float(n) 
137   
138       
139      sd = 0.0 
140      for i in range(len(values)): 
141           
142          if skip != None and not skip[i]: 
143              continue 
144   
145           
146          sd = sd + (values[i] - Xav)**2 
147   
148       
149      if n <= 1: 
150          sd = 0.0 
151      else: 
152          sd = sqrt(sd / (float(n) - float(dof))) 
153   
154       
155      return sd 
 156