Skip to content

Commit

Permalink
Correlation and Regression Lines - A quick recap#3
Browse files Browse the repository at this point in the history
Solution Hackerrank
  • Loading branch information
ramesh-chandra-saini authored Mar 3, 2017
1 parent 46edb91 commit b5781f5
Showing 1 changed file with 44 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Correlation and Regression Lines - A quick recap #3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@

#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)
#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
r = float(A)/math.sqrt(B*C)
#slope b = r*(sd_x/sd_y)
#s_d and s_y are standard deviations of x and y respectively
std_x = math.sqrt( (float(B)/(len(x))))
std_y = math.sqrt( (float(C)/(len(y))))
#final slop
b = float(r*std_y)/std_x

#regression line
# y = a+ bx

# a = mean_y - b*mean_x
a = mean_y - b*mean_x

#regression line
# y = a+ bx
ans = a+b*10
print("%.1f"%ans)

#for more info....go to this link https://www.youtube.com/watch?v=GhrxgbQnEEU

0 comments on commit b5781f5

Please sign in to comment.