https://www.hackerrank.com/challenges/computing-the-correlation
You are given the scores of N students in three different subjects - Mathematics,*Physics* andChemistry; all of which have been graded on a scale of 0 to 100. Your task is to compute the Pearson product-moment correlation coefficient between the scores of different pairs of subjects (Mathematics and Physics, Physics and Chemistry, Mathematics and Chemistry) based on this data. This data is based on the records of the CBSE K-12 Examination - a national school leaving examination in India, for the year 2013.
Pearson product-moment correlation coefficient
This is a measure of linear correlation described well on this Wikipedia page. The formula, in brief, is given by:
where x and y denote the two vectors between which the correlation is to be measured.
def correlation(x, y):
n = len(x)
d1 = sqrt(n * sum(map(lambda i: i ** 2, x)) - (sum (x)) ** 2)
d2 = sqrt(n * sum(map(lambda i: i ** 2, y)) - (sum (y)) ** 2)
n1 = 0
for i in range(n):
n1 += n * x[i] * y[i]
n2 = (sum (x)) * (sum (y))
return (n1 - n2) / (d1 * d2)
if __name__ == "__main__":
N = int(raw_input())
x, y, z = [], [], []
for i in range(N):
a, b, c = map(float, raw_input().split())
x.append(a)
y.append(b)
z.append(c)
print '%.2f' % correlation(x, y)
print '%.2f' % correlation(y, z)
print '%.2f' % correlation(x, z)