# $CVSHeader: pal8e/src/Correlation.py,v 1.2 2005/02/15 01:10:25 indraneel Rel $

# calculates correlation between two lists of data

import math
import Sigma

class Covariance:
    def __init__(self, xlist, ylist):
        self.xlist = xlist
	self.ylist = ylist

    def covariance(self):
        xmean = sum(self.xlist)/len(self.xlist)
	ymean = sum(self.ylist)/len(self.ylist)
        def difference_square((x, y)):
	    return ((x-xmean)*(y-ymean))
	S = map(difference_square, zip(self.xlist, self.ylist))
	cov = sum(S)/(len(self.xlist)-1)
	return cov

class Correlation:
    def __init__(self, xlist, ylist):
        self.xlist = xlist
	self.ylist = ylist

    def correlation(self):
        covariance = Covariance(self.xlist, self.ylist)
	cov = covariance.covariance()
	xs = Sigma.Sigma(self.xlist)
	ys = Sigma.Sigma(self.ylist)
	xsigma = xs.sigma()
	ysigma = ys.sigma()
	corr = cov/(xsigma*ysigma)
	return corr

