Skip to content

Commit

Permalink
Correlation and Regression Lines - A Quick Recap#1
Browse files Browse the repository at this point in the history
Hackerrank Solution
  • Loading branch information
ramesh-chandra-saini authored Mar 3, 2017
1 parent f8312bf commit f8648aa
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion Correlation and Regression Lines - A Quick Recap #1.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,27 @@

#RAMESH CHANDRA

import math
x = [15 , 12 , 8 , 8 , 7 , 7 , 7 , 6 , 5 , 3]
y = [10 ,25 , 17 ,11 , 13 , 17 , 20 , 13 , 9 , 15]
#calculate mean of x and y
sum_x,sum_y=0,0
for i in xrange(len(x)):
sum_x,sum_y=sum_x+x[i],sum_y+y[i]

mean_x = float(sum_x)/len(x)
mean_y = float(sum_y)/len(y)
A, B , C = 0,0,0;
#now calculate r = A/sqrt(B*C)
# A = sum ((x[i]-x_mean_x)*(y[i]-mean_y))
# B = sum(x[i]-mean_x)^2
# C = sum(y[i]-mean_y)^2

A, B , C ,= 0,0,0;
for i in xrange(len(x)):
A += (x[i]-mean_x)*(y[i]-mean_y)
B += (x[i]-mean_x)**2
C += (y[i]-mean_y)**2
#Co-relation Coefficient
ans = float(A)/math.sqrt(B*C)
print ans
#for more info....go to this link https://www.youtube.com/watch?v=2SCg8Kuh0tE

0 comments on commit f8648aa

Please sign in to comment.