STATISTICS BASICS IN PYTHON

COVARIANCE, PEARSON CORRELATION
1

PYTHON 3

MEDIUM

last hacked on Aug 05, 2018

`@python`, `@statistics`
import math

x = [2,0,5,8,10]
y = [3,4,5,8,10]

def vector_sum(vector):
# takes vector and returns the sum of its elements
s = 0
for i in vector:
s = s + i
return s

print(vector_length(x))

def vector_length(vector):
# takes vector and returns its length
l = len(vector)
return l

print(vector_length(x))

def vector_average(vector):
# takes vector and returns its average
s = vector_sum(vector)
l = vector_length(vector)
a = s / l
return a

print(vector_average(x))

def covariance_of_2_vectors(vector1, vector2):
# takes 2 vectors and returns their covariance
n = vector_length(vector1)
v1_average = vector_average(vector1)
v2_average = vector_average(vector2)
cum_sum = 0
for i in range(n):
cum_sum = cum_sum + ((vector1[i] - v1_average) * (vector2[i] - v2_average))
return cum_sum

print(covariance_of_2_vectors(x, y))

def pearson_r(vector1, vector2):
n = vector_length(vector1)
v1_average = vector_average(vector1)
v2_average = vector_average(vector2)
cov = covariance_of_2_vectors(vector1, vector2)
ss1 = 0
for i in range(n):
ss1 = ss1 + ((vector1[i] - v1_average)*(vector1[i] - v1_average))
ss2 = 0
for i in range(n):
ss2 = ss2 + ((vector2[i] - v2_average)*(vector2[i] - v2_average))
ss_1_2 = ss1 * ss2
r = cov / math.sqrt(ss_1_2)
return r

print(pearson_r(x, y))