# Linear Regression Model with Python 3

I am trying to create a python module calculating correlation and creating regression model for single independent variable and two independent variables.

The program works correctly, I use lots of studies which were done before as for testing my model and results are very good. So my intention for sharing my model is that I assume that my work is not mature enough and it needs valuable feedback for both code and algorithm optimization. If you decide to try my program for testing model for any given values:

1. Parameters for single independent variable and dependent variable:

x1 = [1, 2, 3, 4, 5]
y = [1, 2, 1.30, 3.75, 2.25]

2. Parameters for two independent variable and one dependent variable:

x1 = [16, 21, 21, 34, 18, 17, 35, 31, 19, 27, 20, 19, 28, 24, 25],
x2 = [50, 58, 61, 73, 53, 50, 78, 86, 61.50, 82, 60, 57, 64, 57, 54],
y =  [2.123, 2.222, 2.154, 3, 2.412, 2.315, 3.289, 3.159, 2.158,
3.256, 2.258, 2.136, 2.596, 2.476, 2.487]


def regression_1V(x_data,y_data):#independent variable list and dependent variable list are given as parameter when the function is called

sum_x=sum(x_data)
sum_y=sum(y_data)
sum_x_square=sum(i**2 for i in x_data)
sum_y_square=sum(i**2 for i in y_data)
mean_x=sum(x_data)/len(x_data)
mean_y=sum(y_data)/len(y_data)
sum_xy=sum(i*j for i,j in zip(x_data,y_data))

beta_1=(sum_xy-(len(x_data)*mean_x*mean_y))/((sum_x_square)-( len(x_data)*(mean_x)**2))#beta_1 is factor for independent variable
beta_0=mean_y-(beta_1*mean_x)# beta_0 is constant for linear model

global coefficient
coefficient=(sum_xy-(len(x_data)*mean_x*mean_y))/(((sum_x_square-(len(x_data)*(mean_x)**2))*(sum_y_square-(len(y_data)*(mean_y)**2)))**(1/2))

return (beta_0,beta_1)

def corelation_1V():#corelation is an index that ranges from -1 to 1 that indicate the relationship between denpendent and independent variables

if coefficient<=1 and coefficient>=0.9:
corelation_level="Very Strong - Positive Corelation"
elif coefficient<=0.89 and coefficient>=0.7:
corelation_level="Strong - Positive Corelation"
elif coefficient<=0.69 and coefficient>=0.5:
corelation_level="Medium - Positive Corelation"
elif coefficient<=0.49 and coefficient>=0.3:
corelation_level="Low - Positive Corelation"
elif coefficient<=0.29 and coefficient>0:
corelation_level="Weak - Positive Corelation"
elif coefficient>=-1 and coefficient<=-0.9:
corelation_level="Very Strong - Negative Corelation"
elif coefficient>=-0.89 and coefficient<=-0.7:
corelation_level="Strong - Negative Corelation"
elif coefficient>=-0.69 and coefficient<=-0.5:
corelation_level="Medium - Negative Corelation"
elif coefficient>=-0.49 and coefficient<=-0.3:
corelation_level="Low - Negative Corelation"
elif coefficient>=-0.29 and coefficient<0:
corelation_level="Weak - Negative Corelation"
else:
corelation_level="No Corelation"

return (coefficient,corelation_level)

def regression_2V(x1_data,x2_data,y_data):#two independent variable list and dependent variable list are given as parameter when the function is called

mean_x1=sum(x1_data)/len(x1_data)
mean_x2=sum(x2_data)/len(x2_data)
mean_y=sum(y_data)/len(y_data)

sum_x1=sum(x1_data)
sum_x2=sum(x2_data)
sum_y=sum(y_data)

sum_x1_square=sum(i**2 for i in x1_data)
sum_x2_square=sum(i**2 for i in x2_data)
sum_y_square=sum(i**2 for i in y_data)

sum_x1y=sum(i*j for i,j in zip(x1_data,y_data))
sum_x2y=sum(i*j for i,j in zip(x2_data,y_data))
sum_x1x2=sum(i*j for i,j in zip(x1_data,x2_data))

sx1y=sum_x1y-(sum_x1*sum_y)/len(x1_data)
sx2y=sum_x2y-(sum_x2*sum_y)/len(x1_data)
sx1x2=sum_x1x2-(sum_x1*sum_x2)/len(x1_data)
sx1x1=sum_x1_square-(sum_x1**2)/len(x1_data)
sx2x2=sum_x2_square-(sum_x2**2)/len(x1_data)

beta_1=(sx1y*sx2x2-sx2y*sx1x2)/(sx1x1*sx2x2-(sx1x2)**2)
beta_2=(sx2y*sx1x1-sx1y*sx1x2)/(sx1x1*sx2x2-(sx1x2)**2)
beta_0=mean_y-(beta_1*mean_x1)-(beta_2*mean_x2)

return ("beta0 = {}\nbeta1 = {}\nbeta2 = {}".format(beta_0,beta_1,beta_2))