Skip to content

Instantly share code, notes, and snippets.

@luckythandel
Created January 28, 2022 13:27
Show Gist options
  • Save luckythandel/1b5665ba5979695a7dbe8d0bd2ad9ca5 to your computer and use it in GitHub Desktop.
Save luckythandel/1b5665ba5979695a7dbe8d0bd2ad9ca5 to your computer and use it in GitHub Desktop.
math IInd year regression
#!/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 bxy(n, total_xy, total_x_total_y, total_sqr_y, sqr_total_y):
denom = (n*total_xy) - (total_x_total_y)
nume = (n*total_sqr_y) - (sqr_total_y)
return denom/nume
def byx(n, total_xy,total_x_total_y, total_sqr_x, sqr_total_x):
denom = (n*total_xy) - (total_x_total_y)
nume = (n*total_sqr_x) - (sqr_total_x)
return denom/nume
def regression():
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)
bxy_result = bxy(n, total_xy, total_x * total_y, total_sqr_y, total_y * total_y)
byx_result = byx(n, total_xy, total_x * total_y, total_sqr_x, total_x * total_x)
print("bxy:",bxy_result)
print("byx:", byx_result)
mean_x = mean(total_x, n)
mean_y = mean(total_y, n)
print("mean x:", mean_x)
print("mean y:", mean_y)
print("----- x on y -----")
print(f"x - {mean_x} = {bxy_result}(y - {mean_y})")
print("----- y on x -----")
print(f"y - {mean_y} = {byx_result}(x - {mean_x})")
regression()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment