OpenStructure
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
stutil.py
Go to the documentation of this file.
1 import math
2 
3 def Mean(xs):
4  """
5  Calculate mean of dataset
6  """
7  if len(xs)==0:
8  raise RuntimeError("Can't calculate mean of empty sequence")
9  return float(sum(xs))/len(xs)
10 
11 def Median(xs):
12  """
13  Calculate median of dataset
14  """
15  if len(xs)==0:
16  raise RuntimeError("Can't calculate median of empty sequence")
17  sorted_xs=sorted(xs)
18  if (len(xs) % 2)==0:
19  return (sorted_xs[(len(xs)-1)/2]+sorted_xs[(len(xs)-1)/2+1])/2.0
20  else:
21  return sorted_xs[(len(xs)-1)/2]
22 
23 def StdDev(xs):
24  """
25  Calculate standard-deviation of dataset
26 
27  | sum[xi-<x>]^2 |
28  sigma=sqrt|---------------|
29  | n |
30  """
31  mean=Mean(xs)
32  return math.sqrt(sum([(x-mean)**2 for x in xs])/len(xs))
33 
34 def Min(xs):
35  return min(xs)
36 
37 def Max(xs):
38  return max(xs)
39 
40 def Correl(xs, ys):
41  """
42  Calculates the correlation coefficient between xs and ys as
43 
44  sum[(xi-<x>)*(yi-<y>)]
45  r=----------------------
46  sx*sy
47 
48  where <x>, <y> are the mean of dataset xs and ys, and, sx and sy are the
49  standard deviations.
50  """
51  if len(xs)!=len(ys):
52  raise RuntimeError("Can't calculate correl. Sequence lengths do not match.")
53  if len(xs)==1:
54  raise RuntimeError("Can't calculate correl of sequences with length 1.")
55  mean_x=Mean(xs)
56  mean_y=Mean(ys)
57  sigma_x, sigma_y=(0.0, 0.0)
58  cross_term=0.0
59  for x, y in zip(xs, ys):
60  cross_term+=(x-mean_x)*(y-mean_y)
61  sigma_x+=(x-mean_x)**2
62  sigma_y+=(y-mean_y)**2
63  sigma_x=math.sqrt(sigma_x)
64  sigma_y=math.sqrt(sigma_y)
65  return cross_term/(sigma_x*sigma_y)
66 
67 def Histogram(xs, bounds, num_bins):
68  bins=[0 for i in range(num_bins)]
69  d=1.0*num_bins/(bounds[1]-bounds[0])
70  for x in xs:
71  index=int((x-bounds[0])*d)
72  if index>num_bins-1 or index<0:
73  continue
74  bins[index]+=1
75  return bins
def Histogram
Definition: stutil.py:67
def Correl
Definition: stutil.py:40
def StdDev
Definition: stutil.py:23
def Min
Definition: stutil.py:34
def Median
Definition: stutil.py:11
def Mean
Definition: stutil.py:3
def Max
Definition: stutil.py:37