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  central_idx = int((len(xs)-1)/2)
19  if (len(xs) % 2)==0:
20  return (sorted_xs[central_idx]+sorted_xs[central_idx+1])/2.0
21  else:
22  return sorted_xs[central_idx]
23 
24 def StdDev(xs):
25  """
26  Calculate standard-deviation of dataset
27 
28  | sum[xi-<x>]^2 |
29  sigma=sqrt|---------------|
30  | n |
31  """
32  mean=Mean(xs)
33  return math.sqrt(sum([(x-mean)**2 for x in xs])/len(xs))
34 
35 def Min(xs):
36  return min(xs)
37 
38 def Max(xs):
39  return max(xs)
40 
41 def Correl(xs, ys):
42  """
43  Calculates the correlation coefficient between xs and ys as
44 
45  sum[(xi-<x>)*(yi-<y>)]
46  r=----------------------
47  sx*sy
48 
49  where <x>, <y> are the mean of dataset xs and ys, and, sx and sy are the
50  standard deviations.
51  """
52  if len(xs)!=len(ys):
53  raise RuntimeError("Can't calculate correl. Sequence lengths do not match.")
54  if len(xs)==1:
55  raise RuntimeError("Can't calculate correl of sequences with length 1.")
56  mean_x=Mean(xs)
57  mean_y=Mean(ys)
58  sigma_x, sigma_y=(0.0, 0.0)
59  cross_term=0.0
60  for x, y in zip(xs, ys):
61  cross_term+=(x-mean_x)*(y-mean_y)
62  sigma_x+=(x-mean_x)**2
63  sigma_y+=(y-mean_y)**2
64  sigma_x=math.sqrt(sigma_x)
65  sigma_y=math.sqrt(sigma_y)
66  return cross_term/(sigma_x*sigma_y)
67 
68 def Histogram(xs, bounds, num_bins):
69  bins=[0 for i in range(num_bins)]
70  d=1.0*num_bins/(bounds[1]-bounds[0])
71  for x in xs:
72  index=int((x-bounds[0])*d)
73  if index>num_bins-1 or index<0:
74  continue
75  bins[index]+=1
76  return bins
def Histogram
Definition: stutil.py:68
def Correl
Definition: stutil.py:41
def StdDev
Definition: stutil.py:24
def Min
Definition: stutil.py:35
def Median
Definition: stutil.py:11
def Mean
Definition: stutil.py:3
def Max
Definition: stutil.py:38