source: misc/tools/pytools/stats.py @ 7276

Last change on this file since 7276 was 7276, checked in by ole, 15 years ago

Merged numpy branch back into the trunk.

In ~/sandpit/anuga/anuga_core/source
svn merge -r 6246:HEAD ../../branches/numpy .

In ~/sandpit/anuga/anuga_validation
svn merge -r 6417:HEAD ../branches/numpy_anuga_validation .

In ~/sandpit/anuga/misc
svn merge -r 6809:HEAD ../branches/numpy_misc .

For all merges, I used numpy version where conflicts existed

The suites test_all.py (in source/anuga) and validate_all.py passed using Python2.5 with numpy on my Ubuntu Linux box.

File size: 1.4 KB
Line 
1#def mean(x):
2#  import Numeric, caching
3#  reload(caching)#
4#
5#  sum = caching.cache(Numeric.sum,x,verbose=0)
6#  #sum = Numeric.sum(x) 
7#  avg = float(sum)/len(x)
8#
9#  return(avg)
10
11def mean(x):
12  import numpy as num
13
14  sum = num.sum(x) 
15  avg = float(sum)/len(x)
16
17  return(avg)
18
19
20def cov(x,y=None):
21  import numpy as num
22  if y is None:
23    y = x
24
25  assert(len(x)==len(y))
26  N = len(x)
27 
28  cx = x - mean(x) 
29  cy = y - mean(y) 
30
31  p = num.inner(cx,cy) / N
32
33
34  return(p)
35
36
37def err(x, y=0, n=2):
38  """Relative error of ||x-y|| to ||y||
39     n = 2:    Two norm
40     n = None: Max norm
41
42     If denominator evaluates to zero, absolute error is returned
43  """
44
45 
46  if n == 2:
47      err = norm(x-y)
48      try:
49          err = err/norm(y)
50      except:
51          pass
52
53  else:
54      err = max(abs(x-y))
55      try:
56          err = err/max(abs(y))   
57      except:
58          pass
59       
60  return err
61 
62
63def norm(x):
64  """2-norm of x
65  """
66 
67  import numpy as num 
68  y = num.ravel(x)
69  p = num.sqrt(num.inner(y,y))
70  return p
71   
72 
73def corr(x,y=None):
74  import caching 
75  from math import sqrt
76  if y is None:
77    y = x
78
79  varx = caching.cache(cov,x,verbose=0)
80  vary = caching.cache(cov,y,verbose=0)
81  #varx = cov(x)
82  #vary = cov(y)
83
84  if varx == 0 or vary == 0:
85    C = 0
86  else: 
87    C = cov(x,y)/ sqrt(varx * vary)   
88
89  return(C)
Note: See TracBrowser for help on using the repository browser.