#def mean(x): # import Numeric, caching # reload(caching)# # # sum = caching.cache(Numeric.sum,x,verbose=0) # #sum = Numeric.sum(x) # avg = float(sum)/len(x) # # return(avg) def mean(x): import Numeric sum = Numeric.sum(x) avg = float(sum)/len(x) return(avg) def cov(x,y=None): import Numeric if y is None: y = x assert(len(x)==len(y)) N = len(x) cx = x - mean(x) cy = y - mean(y) p = Numeric.innerproduct(cx,cy) / N return(p) def err(x,y=0,n=2): """Relative error of ||x-y|| to ||y|| n = 2: Two norm n = None: Max norm If denominator evaluates to zero, absolute error is returned """ if n == 2: err = norm(x-y) try: err = err/norm(y) except: pass else: err = max(abs(x-y)) try: err = err/max(abs(y)) except: pass return err def norm(x): """2-norm of x """ import Numeric y = Numeric.ravel(x) p = Numeric.sqrt(Numeric.innerproduct(y,y)) return p def corr(x,y=None): import caching from math import sqrt if y is None: y = x varx = caching.cache(cov,x,verbose=0) vary = caching.cache(cov,y,verbose=0) #varx = cov(x) #vary = cov(y) if varx == 0 or vary == 0: C = 0 else: C = cov(x,y)/ sqrt(varx * vary) return(C)