Created
September 5, 2017 08:35
-
-
Save volf52/eab434dacca8f164663521762aa38024 to your computer and use it in GitHub Desktop.
Pearson Correlation Coefficient, Covariance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def main(): | |
n = int(raw_input().strip()) | |
X = map(float, raw_input().strip().split(' ')) | |
Y = map(float, raw_input().strip().split(' ')) | |
x_mean = sum(X) / len(X) | |
y_mean = sum(Y) / len(Y) | |
s_devX = std(X, x_mean) | |
s_devY = std(Y, y_mean) | |
covariance = cov(X, Y, x_mean, y_mean) | |
print round(covariance / (s_devX * s_devY), 3) | |
# standard deviation | |
def std(arr, mean): | |
return pow(sum([pow((x - mean), 2) for x in arr])/len(arr), 0.5) | |
# covariance | |
def cov(X, Y, x_mean, y_mean): | |
return (sum([ (x-x_mean)*(y-y_mean) for x,y in zip(X, Y)]) / len(X)) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment