Reputation: 37
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)
)
Upvotes: 0
Views: 39
Reputation: 940
Your flow could be something like:
Enter Company 1:
Enter Company 2:
Enter Company 3:
Enter Company 4 (leave blank to stop):
The code would be:
def read_company():
companies = []
while len(companies) < 3:
company = input(f"Enter Company {len(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)
while True:
company = input(f"Enter Company {len(companies} (leave blank to stop): ")
if not company:
break
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
Note how after 3 companies, you infinitely ask for more company names UNTIL the company entered is a false-y value (for example, the empty string ""
). This means until the user enters nothing in the company input prompt, it keeps prompting the user for companies.
There are definitely many ways to improve this code (remove repeated code for example), but for now it does what it needs to do.
You have the right idea, but your code has quite a few mistakes. You're overwriting companies
(the argument to your function), and also using company
variables that don't exist. Here's an improved version that does what you want.
def calculate_combinations(companies):
output = {}
for company in companies:
for other_company in companies:
if company == other_company or (other_company, company) in output:
continue
output.add((company, other_company))
return output
Note: The pair is only added to the set if both companies are different, and if the pair isn't already added in reverse order.
Here's a more efficient version from itertools
: (credit)
import itertools
def calculate_combinations(companies):
return itertools.combinations(companies, 2)
Upvotes: 0