Skip to content

Instantly share code, notes, and snippets.

@fourohfour
Created March 28, 2017 19:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fourohfour/0fbecdddfa1e4caced6e78cb11dc7e4e to your computer and use it in GitHub Desktop.
Save fourohfour/0fbecdddfa1e4caced6e78cb11dc7e4e to your computer and use it in GitHub Desktop.
Calculate Product Moment Correlation Coefficient (Pearson Coefficient, r)
import math
import random
class Buffer:
def __init__(self, one, two):
self.one = str(one)
self.two = str(two)
self.length = max((len(self.one), len(self.two))) + 1
def __call__(self, obj):
return " " * (self.length - len(str(obj)))
class Bivariate:
def __init__(self, x, y):
self.x = x
self.y = y
self.data = []
def add_point(self, x, y):
self.data.append((x, y))
def pearson(self):
n = len(self.data)
sig_x = sum((d[0] for d in self.data))
sig_x_sq = sum((d[0] ** 2 for d in self.data))
sig_y = sum((d[1] for d in self.data))
sig_y_sq = sum((d[1] ** 2 for d in self.data))
sig_x_y = sum((d[0] * d[1] for d in self.data))
Sxx = sig_x_sq - ((sig_x ** 2) / n)
Syy = sig_y_sq - ((sig_y ** 2) / n)
Sxy = sig_x_y - ((sig_x * sig_y) / n)
r = Sxy / math.sqrt(Sxx * Syy)
return r
def pretty_print(self):
row1 = self.x + Buffer(self.x, self.y)(self.x)
row2 = self.y + Buffer(self.x, self.y)(self.y)
for datum in self.data:
b = Buffer(datum[0], datum[1])
row1 += str(datum[0]) + b(datum[0])
row2 += str(datum[1]) + b(datum[1])
print(row1)
print(row2)
## Answer Question
data = Bivariate("Rainfall", "Discharge")
data.add_point(1.3, 2.9)
data.add_point(1.9, 2.7)
data.add_point(4.2, 7.9)
data.add_point(3.7, 6.8)
data.add_point(1.8, 2.8)
data.add_point(2.5, 3.9)
data.add_point(2.6, 5.2)
data.add_point(0.5, 1.9)
data.pretty_print()
print("r = {}".format(data.pearson()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment