Created
May 24, 2019 14:59
-
-
Save eLtronicsVilla/27820a6bd1e0e7f128c6e9f0c639ba55 to your computer and use it in GitHub Desktop.
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
import math | |
import numpy as np | |
data_X = [5,6,8,12,15,18,10] | |
data_Y = [1,4,6,8,10,12,14] | |
def mean(x): | |
return sum(x)/len(x) | |
def de_mean(data): | |
# convert x by subtracting its mean ( so the result has mean 0) | |
x_bar = mean(data) | |
return [x_i - x_bar for x_i in data] | |
def covariance(x,y): | |
n = len(x) | |
return np.dot(de_mean(x), de_mean(y)) / (n-1) | |
print(covariance(data_X,data_Y)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment