Skip to content

Instantly share code, notes, and snippets.

@luckythandel
Created January 28, 2022 13:50
Show Gist options
  • Save luckythandel/886d8fe5b48514588fd0544b5c136635 to your computer and use it in GitHub Desktop.
Save luckythandel/886d8fe5b48514588fd0544b5c136635 to your computer and use it in GitHub Desktop.
coefficient of correlation
#!/bin/env python3
import math
def mul(x, y):
xy = []
for a,b in zip(x,y):
xy.append(a*b)
return xy
def total(arr):
total = 0
for e in arr:
total+=e;
return total
def mean(total, n):
return total/n
def squre(arr):
sqr = []
for e in arr:
sqr.append(e**2)
return sqr
def corre(n, total_xy, total_x_total_y, total_sqr_x, sqr_total_x, total_sqr_y, sqr_total_y):
denom = (n*total_xy)-(total_x_total_y)
nome = math.sqrt((n*total_sqr_x)-sqr_total_x) * math.sqrt((n*total_sqr_y)-sqr_total_y)
return denom/nome
def main():
n = int(input("N: "))
x = []; y = []
print("-- x values --")
for i in range(n):
num = input("value: ")
try:
x.append(int(num))
except:
x.append(float(num))
print("-- y values --")
for i in range(n):
num = input("value: ")
try:
y.append(int(num))
except:
y.append(float(num))
xy = mul(x, y)
total_x = total(x)
total_y = total(y)
total_xy = total(xy)
sqr_x = squre(x)
sqr_y = squre(y)
total_sqr_x = total(sqr_x)
total_sqr_y = total(sqr_y)
print("---------------------------")
print("total_x:", total_x)
print("total_y:", total_y)
print("total_xy:", total_xy)
print("total_sqr_x:", total_sqr_x)
print("total_sqr_y:", total_sqr_y)
r = corre(n, total_xy, total_x * total_y, total_sqr_x, total_x * total_x, total_sqr_y, total_y * total_y)
print("r:", r)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment