python 3.6 assignment
''' Q1 ''' def calculate_median(numbers): N=len(numbers) numbers.sort() if (N%2==0): # the modulo operation finds the remainder after division of one number # by another (sometimes called modulus) # if N is even m1 = N/2 m2 = (N/2)+1 # Convert to integer and match position m1 = int(m1)-1 m2 = int(m2)-1 median=(numbers[m1]+numbers[m2])/2 else: # if N is odd m = (N+1)/2 # Convert to integer and match position m = int(m)-1 median=numbers[m] return median list_of_numbers = [100 ,60 ,70 ,900 ,100 ,200 ,500 ,500 ,503 ,600 ,1000 ,1200] median = calculate_median (list_of_numbers) length_of_num = len(list_of_numbers) print ('Median number of the {0} numbers is {1}'.format(length_of_num,median)) ''' Q2 ''' import numpy as np from xlrd import open_workbook from datetime import datetime def read_from_excel(): # creation of an xlrd book and sheet based on the path to the excel file book = open_workbook (r'C:\Users\tao24\Dropbox\PhD\Seminars\Programming for Finance 2019\Seminar 3\excel_key_events.xlsx') sheet = book.sheet_by_index(0) # the excel file has three variables (dates, prices, key_events) which we append with values # from the file dates = [] prices = [] key_events = [] for i in range (1, sheet.nrows): dates.append(sheet.cell_value(i,0)) prices.append(sheet.cell_value(i,1)) key_events.append(sheet.cell_value(i,2)) # after the for loop, the results are returned to the user return dates, prices, key_events def calculate_returns_from_prices(input_prices): # define a list called "returns" with the first value "--" returns = ["-‐"] for i in range (1,len(input_prices)): return_in_position_i = np.log(input_prices[i]/input_prices[i-1]) returns.append(return_in_position_i) return returns def f_returns_on_key_events_dates (all_returns, key_events_variable): # keep returns only for key events dates # by checking whether the value in the key events variable column is equal to 1.0 returns_on_key_events_dates = [] for i in range (0,len(all_returns)): if key_events_variable[i]==1.0: returns_on_key_events_dates.append(all_returns[i]) return returns_on_key_events_dates [dates, prices, key_events] = read_from_excel() returns = calculate_returns_from_prices(prices) returns_on_key_events_dates = f_returns_on_key_events_dates(returns,key_events) median_return_of_key_events_dates = calculate_median(returns_on_key_events_dates) print("All returns on key event dates are: ", returns_on_key_events_dates) print("Median return on key events dates is: ", median_return_of_key_events_dates) # We did put the dash as the first value of returns. We cannot call the median function with #this list, because the sort command will fail thus we keep all the elements from the list, # starting from the second element print("Returns before deleting the first line: ", returns) returns = returns[1:] median_return_of_all_dates = calculate_median(returns) print("Returns on all dates are: ", returns) print("Median return from all returns is: ", median_return_of_all_dates) ''' Q3 ''' def elements_sum (input_list_2): # Function that calculates and returns the sum of the inputted list sum_of_list = 0 for element in input_list_2: sum_of_list += element return sum_of_list def elements_average (input_list_3): # Function that calculates and returns the average of the inputted list sum_of_list = elements_sum (input_list_3) average_of_list = sum_of_list / float(len(input_list_3)) return average_of_list def elements_variance(input_list_4): # Function that calculates and returns the variance of the inputted list average = elements_average(input_list_4) variance = 0 for element in input_list_4: diff_sq = (element - average)**2 variance = variance + diff_sq variance = variance / len(input_list_4) return variance list_for_input = [15, 25, 12, 37, 42, 56, 124, 1, 3, 7, 20.4] print("The variance of the inputted list is: ", elements_variance(list_for_input)) ''' Q4 ''' def elements_std_deviation (variance_1): variance_1 = np.power(variance_1, 0.5) return variance_1 list_for_input = [15, 25, 12, 37, 42, 56, 124, 1, 3, 7, 20.4] var = elements_variance(list_for_input) print("The variance of the inputted list is: ", elements_std_deviation(var)) #print("The variance of the inputted list is: ", elements_variance(list_for_input)**0.5)