Last change
on this file since 63 was
18,
checked in by ole, 20 years ago
|
Added old pytools module from CVS
|
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 | |
---|
11 | def mean(x): |
---|
12 | import Numeric |
---|
13 | |
---|
14 | sum = Numeric.sum(x) |
---|
15 | avg = float(sum)/len(x) |
---|
16 | |
---|
17 | return(avg) |
---|
18 | |
---|
19 | |
---|
20 | def cov(x,y=None): |
---|
21 | import Numeric |
---|
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 = Numeric.innerproduct(cx,cy) / N |
---|
32 | |
---|
33 | |
---|
34 | return(p) |
---|
35 | |
---|
36 | |
---|
37 | def 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 | |
---|
63 | def norm(x): |
---|
64 | """2-norm of x |
---|
65 | """ |
---|
66 | |
---|
67 | import Numeric |
---|
68 | y = Numeric.ravel(x) |
---|
69 | p = Numeric.sqrt(Numeric.innerproduct(y,y)) |
---|
70 | return p |
---|
71 | |
---|
72 | |
---|
73 | def 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.