I am trying to calculate the regression coefficient of 3 to 15 companies.
I want to enter at least 3 companies. Problem, the program ends after entering 3 companies, which I suspect is due to the while... <3. But I don't know how to combine at least 3 with more than 3 should the user wish to do so. Would that require true/false instead of while true? Or can the existing code be altered?
I am calculating the regression of the opening and closing stocks of companies (in pairs of 2) of 3 to 15 companies. The user can choose any unique combination of companies. This means that there are anywhere from 3 to 105 possible combinations. The list of companies that the user has chosen is stored in another module def read_company(). I'm stuck with the def calculate_combinations(companies) module that has to generate the pairs and return them for def calculate_sommations. I'm not even sure if my reasoning is correct with the pair combination.
import math
def read_company():
companies = []
while len(companies) < 3:
company = input("Enter at least three companies.")
if company in companies:
print("Company already entered. Enter a new company.")
elif not path.exists(company + '.csv'):
print(company + " does not exist. Enter a new company.")
else:
companies.append(company)
return companies
def calculate_combinations(companies):
companies = []
output = []
for x in range(0,len(companies)):
for y in range(0,len(companies)):
output.append((company[x],company[y]))
return combinations_companies
def calculate_sommations(range_x, range_y):
som_x = 0
som_y = 0
som_xsquared = 0
som_ysquared = 0
som_xy = 0
number = len(range_x)
for i in range(number):
som_x += range_x[i]
som_y += range_y[i]
som_xsquared += range_x[i] ** 2
som_ysquared += range_y[i] ** 2
som_xy += range_x[i] * range_y[i]
return number, som_x, som_y, som_xsquared, som_ysquared, som_xy
def calculate_correlationcoefficient(number, som_x, som_y, som_xsquared, som_ysquared, som_xy):
return (number * som_xy - (som_x * som_y)) / \
math.sqrt(
(number * som_xsquared - som_x ** 2) * (number * som_ysquared - som_y ** 2)
)