def test_get_daily_ret(self): self.assertEqual(len(investment(100, 1000).get_daily_ret()), investment(100, 1000).num_trials) self.assertTrue( all((i <= 1 for i in investment(100, 1000).get_daily_ret()))) self.assertTrue( all((i >= -1 for i in investment(100, 1000).get_daily_ret())))
def test_constructor(self): """ unit test for the investment costructor that passinput to arguments positions and num_trials """ self.assertEqual( investment([1, 10, 100], 1000).positions, [1, 10, 100]) self.assertEqual(investment([1, 10, 100], 1000).num_trials, 1000)
def test_constructor(self): """ unit test for the investment costructor that passinput to arguments positions and num_trials """ self.assertEqual(investment([1,10,100], 1000).positions,[1,10,100]) self.assertEqual(investment([1,10,100], 1000).num_trials, 1000)
def main(): """ Main program designed to simulate $1000 spread amongst 1, 20, 100, or 1000 positions. For each position size the investment is simulated for 1 day of trading for 10000 trials. The histogram, mean and standard deviations for the percentage returns for the trials associated with each position are output as files. """ positions = [1, 10, 100, 1000] num_trials = 10000 my_investment = investment.investment(positions, num_trials) my_returns = my_investment.multi_day_gamble() file_labels = ["0001", "0010", "0100", "1000"] for i in range(4): my_fig = plt.figure() plt.hist(my_returns[:, i], 100, range=[-1, 1]) plt.xlabel("Daily Return") plt.ylabel("Number of instances") plt.title("Daily returns for investsments of size " + str(my_investment.position_value[i])) plt.savefig("histogram_" + file_labels[i] + "_pos.pdf") results = np.array([my_investment.positions, np.mean(my_returns, axis=0), np.std(my_returns, axis=0)]) fout = open("results.txt", "w") fout.write(" Position Size Mean Return Std Return \n") fout.write(str(results.T))
def main(): ''' Main program designed to simulate $1000 spread amongst 1, 20, 100, or 1000 positions. For each position size the investment is simulated for 1 day of trading for 10000 trials. The histogram, mean and standard deviations for the percentage returns for the trials associated with each position are output as files. ''' positions = [1, 10, 100, 1000] num_trials = 10000 my_investment = investment.investment(positions, num_trials) my_returns = my_investment.multi_day_gamble() file_labels = ['0001', '0010', '0100', '1000'] for i in range(4): my_fig = plt.figure() plt.hist(my_returns[:, i], 100, range=[-1, 1]) plt.xlabel('Daily Return') plt.ylabel('Number of instances') plt.title('Daily returns for investsments of size ' + str(my_investment.position_value[i])) plt.savefig('histogram_' + file_labels[i] + '_pos.pdf') results = np.array([ my_investment.positions, np.mean(my_returns, axis=0), np.std(my_returns, axis=0) ]) fout = open('results.txt', "w") fout.write(' Position Size Mean Return Std Return \n') fout.write(str(results.T))
def main(): ''' main function that generates results ''' position_input = raw_input('Please enter [1,10,100,1000] for simulation') position_input.strip(' ') f = open('results.txt', 'w') # to write in a file position_list = [1,10,100,1000] # there are 4 positions num_trials = 10000 value_to_invest = investment() if position_input == '[1,10,100,1000]': for position in position_list: daily_ret_data = value_to_invest.daily_ret(position,num_trials) # daily return for one position mean = float(np.mean(daily_ret_data)) # mean of daily return for one position std = float(np.std(daily_ret_data)) # std of daily return for one posiion f.write('Mean of daily return for position '+str(position)+' is '+ str(mean) +'\n\n') f.write('Standard deviation of daily return for position '+str(position)+' is '+str(std)+'\n\n') hist_plot = plt.figure() # plot daily return results for one position plt.hist(daily_ret_data,100,range=[-1,1]) plt.xlabel('Results of Daily Return') plt.ylabel('Number of Trials with Results') plt.title('Position' + str(position)) hist_plot.savefig('histogram_'+str(position).zfill(4)+'_pos.pdf',format='pdf') else: print 'Please re-run the program and enter the input in this exact format [1,10,100,1000]' sys.exit()
def investmentTest(self): ''' Tests whether investment positions and position_value are correctly associated ''' position = 10 my_investment = investment.investment(position) self.assertEqual(my_investment.position_value, 100.)
def test_simulation_one(self): i = investment("[1, 10]","10000") positions = 1 trails_1 = 10 trails_2 = 100 trails_3 = 1000 self.assertEqual(len(i.simulation_one(positions,trails_1)),trails_1) self.assertEqual(len(i.simulation_one(positions,trails_2)),trails_2) self.assertEqual(len(i.simulation_one(positions,trails_3)),trails_3)
def test_simulation_one(self): i = investment("[1, 10]", "10000") positions = 1 trails_1 = 10 trails_2 = 100 trails_3 = 1000 self.assertEqual(len(i.simulation_one(positions, trails_1)), trails_1) self.assertEqual(len(i.simulation_one(positions, trails_2)), trails_2) self.assertEqual(len(i.simulation_one(positions, trails_3)), trails_3)
def test_str2num(self): i = investment("[1, 10]", "10000") positions = "[1, 10, 100, 1000]" trails = "10000" positions_result, trails_result = i.str2num(positions, trails) position_true = [1, 10, 100, 1000] trails_true = 10000 self.assertEqual(positions_result, position_true) self.assertEqual(trails_result, trails_true)
def gambleTest(self): ''' Tests whether gamble function outputs are within reasonable bounds. ''' positions = [1, 10, 100, 1000] my_investment = investment.investment(positions) le2000 = np.all(my_investment.gamble() <= 2000) ge0 = np.all(my_investment.gamble() >= 0) self.assertEqual(True, np.all([ge0, le2000]))
def test_simulation(self): i = investment("[1, 10]","10000") positions_1 = [1,10] positions_2 = [1,10,100] positions_3 = [1,10,1000] trails = 10000 self.assertEqual(len(i.simulation(positions_1,trails).keys()),len(positions_1)) self.assertEqual(len(i.simulation(positions_2,trails).keys()),len(positions_2)) self.assertEqual(len(i.simulation(positions_3,trails).keys()),len(positions_3))
def test_str2num(self): i = investment("[1, 10]","10000") positions = "[1, 10, 100, 1000]" trails = "10000" positions_result, trails_result = i.str2num(positions, trails) position_true = [1, 10, 100, 1000] trails_true = 10000 self.assertEqual(positions_result,position_true) self.assertEqual(trails_result, trails_true)
def test_simulation(self): i = investment("[1, 10]", "10000") positions_1 = [1, 10] positions_2 = [1, 10, 100] positions_3 = [1, 10, 1000] trails = 10000 self.assertEqual(len(i.simulation(positions_1, trails).keys()), len(positions_1)) self.assertEqual(len(i.simulation(positions_2, trails).keys()), len(positions_2)) self.assertEqual(len(i.simulation(positions_3, trails).keys()), len(positions_3))
def main(): """ Function main takes a list of the number of shares to buy in parallel as positions and how many times to randomly repeat the test as num_trials """ while True: try: position = input('Please input a list of shares to buy:\n') if (position == 'quit'): sys.exit() position = position.replace(' ', '') positions = position[1:-1].split(',') for i, item in enumerate(positions): positions[i] = int(item) break except ValueError: print('Invalid input') except KeyboardInterrupt: sys.exit() except EOFError: sys.exit() while True: try: num_trials = int( input( 'Please input how many times you want to repeat the test')) break except ValueError: print('Invalid input') except KeyboardInterrupt: sys.exit() except EOFError: sys.exit() # call the class investment constructor to create an object t t = investment(positions, num_trials) # call the function 'present_results' in class investment to do the simulation and # produce the result t.present_results(positions, num_trials)
def main(): """ Function main takes a list of the number of shares to buy in parallel as positions and how many times to randomly repeat the test as num_trials """ while True: try: position = input('Please input a list of shares to buy:\n') if (position == 'quit'): sys.exit() position = position.replace(' ', '') positions = position[1:-1].split(',') for i, item in enumerate(positions): positions[i] = int(item) break except ValueError: print('Invalid input') except KeyboardInterrupt: sys.exit() except EOFError: sys.exit() while True: try: num_trials = int(input('Please input how many times you want to repeat the test')) break except ValueError: print('Invalid input') except KeyboardInterrupt: sys.exit() except EOFError: sys.exit() # call the class investment constructor to create an object t t = investment(positions, num_trials) # call the function 'present_results' in class investment to do the simulation and # produce the result t.present_results(positions, num_trials)
def main(): positions_input = raw_input('a list of the number of shares to buy in parallel: e.g. [1, 10, 100, 1000]? ') positions = [int(x) for x in positions_input.strip('[]').split(',')] num_trials = int(raw_input('how many times to randomly repeat the test? ')) mean_list = [] std_list = [] results = open('results.txt','w') for position in positions: daily_ret = investment.investment(position,num_trials) mean = np.mean(daily_ret) std = np.std(daily_ret) results.write('{} mean:{},std:{} \n'.format(position,mean,std)) p = plt.figure() plt.hist(daily_ret,100,range=[-1,1]) plt.title('Histogram of Daily Return with position {}'.format(position)) plt.xlabel('daily return') p.savefig('histogram_{}_pos.pdf'.format(str(position).zfill(4))) results.close()
def trial_investments(positions, num_trials) : """This function takes in a list of positions and a number of trials to run and creates a pdf with a histogram for each position and a results text file with the mean and standard deviation of each position""" results = [] #Here the program runs the trials for each positions for position in positions : curr_inv = investment(position) cumu_ret = [0] * num_trials daily_ret = [0] * num_trials for trial in range(num_trials) : cumu_ret[trial] = curr_inv.day_instance() daily_ret[trial] = (cumu_ret[trial]/1000) - 1 # Here the program builds the histogram and the results figure_saver(position, daily_ret) result = results_builder(daily_ret, curr_inv) results.extend(result) results_writer(results)
def trial_investments(positions, num_trials): """This function takes in a list of positions and a number of trials to run and creates a pdf with a histogram for each position and a results text file with the mean and standard deviation of each position""" results = [] # Here the program runs the trials for each positions for position in positions: curr_inv = investment(position) cumu_ret = [0] * num_trials daily_ret = [0] * num_trials for trial in range(num_trials): cumu_ret[trial] = curr_inv.day_instance() daily_ret[trial] = (cumu_ret[trial] / 1000) - 1 # Here the program builds the histogram and the results figure_saver(position, daily_ret) result = results_builder(daily_ret, curr_inv) results.extend(result) results_writer(results)
def testInvestment1(self): with self.assertRaises(ValueError) as cm: investment("999") thisexception = cm.exception self.assertEquals(str(thisexception), 'Invalid position value, must be either of 1, 10, 100, 1000.') with self.assertRaises(ValueError) as cm: investment("hahaha") thisexception = cm.exception self.assertEquals(str(thisexception), 'Invalid position value, must be either of 1, 10, 100, 1000.') with self.assertRaises(ValueError) as cm: investment(-100) thisexception = cm.exception self.assertEquals(str(thisexception), 'Invalid position value, must be either of 1, 10, 100, 1000.')
def test_stimulate(self): """ unit tests for function stimulate We test the each value of the return dictionary 'result' has the same length as num_trials, and every element in the value of dictionary 'result' bigger than or equal to -1, and less than or equal to 1 """ t = investment([1, 10, 100], 1000) result = t.stimulate([1000.0, 100.0, 10.0], 1000) self.assertEqual(len(result[1]), 1000) self.assertEqual(len(result[10]), 1000) self.assertEqual(len(result[100]), 1000) self.assertTrue(result[1].all() <= 1) self.assertTrue(result[1].all() >= -1) self.assertTrue(result[10].all() <= 1) self.assertTrue(result[10].all() >= -1) self.assertTrue(result[100].all() <= 1) self.assertTrue(result[100].all() >= -1)
def main(): while 1: ''' input the positions from users ''' try: positions = input("input the position: ") if (positions == 'quit' or positions == 'Quit'): print("Quit the Game") sys.exit() check_positions(positions) break except ValueError as error: print(error) except EOFError: sys.exit() while 1: ''' input the num_trails from users ''' try: num_trails = input("input the trails: ") if (positions == 'quit' or positions == 'Quit'): print("Quit the Game") sys.exit() check_trails(num_trails) break except ValueError as error: print(error) except EOFError: sys.exit() i = investment(positions,num_trails) position_num, trails = i.str2num(positions, num_trails) #convert positions and trails from string format to list of integer and integer daily_ret = i.simulation(position_num, trails) #calculation i.plot_result(daily_ret) #write to result.txt and pdf return
def main(): #take user input for a list of the number of shares to buy and put them into a list positions_input = raw_input('a list of the number of shares to buy in parallel: e.g. [1, 10, 100, 1000]? ') positions = [int(x) for x in positions_input.strip('[]').split(',')] #take user input for the number of times to repeat the test num_trials = int(raw_input('how many times to repeat the test?')) #open a file to write results = open('results.txt', 'w') for position in positions: daily_ret = investment.investment(position, num_trials) mean = np.mean(daily_ret) std = np.std(daily_ret) results.write('Position is: '+ str(position) + '\n') results.write('Mean: '+ str(mean) + ' Std: '+ str(std) + '\n') #plot a histogram with x = [-1, 1] and y as the number of trials p = plt.figure() plt.hist(daily_ret, 100, range = [-1, 1]) plt.title('Histogram of Daily Return with Position {}'.format(position)) plt.xlabel('Daily Return') p.savefig('histogram_{}_pos.pdf'.format(str(position).zfill(4))) results.close()
def test_constructor(self): self.assertEqual(investment(10,10).num_positions,10) self.assertEqual(investment(10,10).num_trials,10) self.assertEqual(investment(10,10).position_value,100)
def investment_test5(self): """This function tests to see if the proper value error is raised when something besides a integer or a float is passed""" with self.assertRaise(ValueError) as error: investment("a") self.assertTrue("Improper type for this class" in str(error.exception))
def test_simulation(self): invest = investment(10,10000) daily_ret = invest.simulation() self.assertEqual(len(daily_ret), 10000) self.assertTrue(daily_ret.all() <= 1) self.assertTrue(daily_ret.all() >= -1)
def accuracy(): """ Vary the confusion probability on real data. """ N, M, Psi_t, GT = load_dataset() # inject confusions conf_probs = [0.0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4] mcmc_params = {'N_iter': 10, 'burnin': 1, 'thin': 2, 'FV': 3} res = {'sums': [], 'mv': [], 'em': [], 'mcmc': [], 'sums_f': [], 'mv_f': [], 'em_f': [], 'mcmc_f': [], 'conf_probs': conf_probs, 'avlog': [], 'avlog_f': [], 'inv': [], 'inv_f': [], 'pinv': [], 'pinv_f': [], 'mv_p': [], 'em_p': [], 'mcmc_p': [], 'mv_f_p': [], 'em_f_p': [], 'mcmc_f_p': []} for conf_prob in conf_probs: mv_accu, em_accu, mcmc_accu, sums_accu, avlog_accu, inv_accu, \ pinv_accu, mv_accu_p, em_accu_p, mcmc_accu_p = [], [], [], [], [], [], [], [], [], [] mv_accu_f, em_accu_f, mcmc_accu_f, sums_accu_f, avlog_accu_f, \ inv_accu_f, pinv_accu_f, mv_accu_f_p, em_accu_f_p, mcmc_accu_f_p = [], [], [], [], [], [], [], [], [], [] accu_G_list = [] for run in range(n_runs): GT_G, Cl, Psi = confuse(Psi_t, 1-conf_prob, GT) # MV mv_p = majority_voting(Psi) mv_b = prob_binary_convert(mv_p) # EM em_A, em_p = expectation_maximization(N, M, Psi) em_b = prob_binary_convert(em_p) # MCMC mcmc_A, mcmc_p = mcmc(N, M, Psi, mcmc_params) mcmc_b = prob_binary_convert(mcmc_p) data = adapter_input(Psi) # SUMS sums_belief = sums(N, data) sums_b = adapter_output(sums_belief, data) # AVG LOG avlog_belief = average_log(N, data) avlog_b = adapter_output(avlog_belief, data) # INVESTMENT inv_belief = investment(N, data) inv_b = adapter_output(inv_belief, data) # POOLED INVESTMENT pinv_belief = pooled_investment(N, data) pinv_b = adapter_output(pinv_belief, data) # FUZZY FUSION Psi # From now Psi is the same as Psi_fussy due to Python f_mcmc_G, Psi_fussy = f_mcmc(N, M, deepcopy(Psi), Cl, mcmc_params) g_accu = accu_G(f_mcmc_G, GT_G) print g_accu accu_G_list.append(g_accu) data_f = adapter_input(Psi_fussy) mv_pf = majority_voting(Psi_fussy) mv_bf = prob_binary_convert(mv_pf) em_Af, em_pf = expectation_maximization(N, M, Psi_fussy) em_bf = prob_binary_convert(em_pf) mcmc_Af, mcmc_pf = mcmc(N, M, Psi_fussy, mcmc_params) mcmc_bf = prob_binary_convert(mcmc_pf) sums_belief_f = sums(N, data_f) sums_bf = adapter_output(sums_belief_f, data_f) avlog_belief_f = average_log(N, data_f) avlog_bf = adapter_output(avlog_belief_f, data_f) inv_belief_f = investment(N, data_f) inv_bf = adapter_output(inv_belief_f, data_f) pinv_belief_f = pooled_investment(N, data_f) pinv_bf = adapter_output(pinv_belief_f, data_f) # exclude objects on which no conflicts obj_with_conflicts = [] for obj_id, obj in enumerate(mv_b): if len(obj) > 1 and obj_id in GT.keys(): obj_with_conflicts.append(obj_id) # Binary output mv_accu.append(np.average([mv_b[obj][GT[obj]] for obj in obj_with_conflicts])) mv_accu_f.append(np.average([mv_bf[obj][GT[obj]] for obj in obj_with_conflicts])) em_accu.append(np.average([em_b[obj][GT[obj]] for obj in obj_with_conflicts])) em_accu_f.append(np.average([em_bf[obj][GT[obj]] for obj in obj_with_conflicts])) mcmc_accu.append(np.average([mcmc_b[obj][GT[obj]] for obj in obj_with_conflicts])) mcmc_accu_f.append(np.average([mcmc_bf[obj][GT[obj]] for obj in obj_with_conflicts])) sums_accu.append(np.average([sums_b[obj][GT[obj]] for obj in obj_with_conflicts])) sums_accu_f.append(np.average([sums_bf[obj][GT[obj]] for obj in obj_with_conflicts])) avlog_accu.append(np.average([avlog_b[obj][GT[obj]] for obj in obj_with_conflicts])) avlog_accu_f.append(np.average([avlog_bf[obj][GT[obj]] for obj in obj_with_conflicts])) inv_accu.append(np.average([inv_b[obj][GT[obj]] for obj in obj_with_conflicts])) inv_accu_f.append(np.average([inv_bf[obj][GT[obj]] for obj in obj_with_conflicts])) pinv_accu.append(np.average([pinv_b[obj][GT[obj]] for obj in obj_with_conflicts])) pinv_accu_f.append(np.average([pinv_bf[obj][GT[obj]] for obj in obj_with_conflicts])) # Probability as output mv_accu_p.append(np.average([mv_p[obj][GT[obj]] for obj in obj_with_conflicts])) mv_accu_f_p.append(np.average([mv_pf[obj][GT[obj]] for obj in obj_with_conflicts])) em_accu_p.append(np.average([em_p[obj][GT[obj]] for obj in obj_with_conflicts])) em_accu_f_p.append(np.average([em_pf[obj][GT[obj]] for obj in obj_with_conflicts])) mcmc_accu_p.append(np.average([mcmc_p[obj][GT[obj]] for obj in obj_with_conflicts])) mcmc_accu_f_p.append(np.average([mcmc_pf[obj][GT[obj]] for obj in obj_with_conflicts])) res['mv'].append(np.average(mv_accu)) res['mv_f'].append(np.average(mv_accu_f)) res['em'].append(np.average(em_accu)) res['em_f'].append(np.average(em_accu_f)) res['mcmc'].append(np.average(mcmc_accu)) res['mcmc_f'].append(np.average(mcmc_accu_f)) res['sums'].append(np.average(sums_accu)) res['sums_f'].append(np.average(sums_accu_f)) res['avlog'].append(np.average(avlog_accu)) res['avlog_f'].append(np.average(avlog_accu_f)) res['inv'].append(np.average(inv_accu)) res['inv_f'].append(np.average(inv_accu_f)) res['pinv'].append(np.average(pinv_accu)) res['pinv_f'].append(np.average(pinv_accu_f)) res['mv_p'].append(np.average(mv_accu_p)) res['mv_f_p'].append(np.average(mv_accu_f_p)) res['em_p'].append(np.average(em_accu_p)) res['em_f_p'].append(np.average(em_accu_f_p)) res['mcmc_p'].append(np.average(mcmc_accu_p)) res['mcmc_f_p'].append(np.average(mcmc_accu_f_p)) print('G Accu: {:1.4f}+-{:1.4f}'.format(np.average(accu_G_list), np.std(accu_G_list))) print 'BINARY:' print('confusion probability: {}, mv: {:1.4f}, em: {:1.4f}, mcmc: {:1.4f}, ' 'sums: {:1.4f}, avlog: {:1.4f}, inv: {:1.4f}, pinv: {:1.4f}' .format(conf_prob, np.average(mv_accu), np.average(em_accu), np.average(mcmc_accu), np.average(sums_accu), np.average(avlog_accu), np.average(inv_accu), np.average(pinv_accu) )) print('confusion probability: {}, mv_f: {:1.4f}, em:_f {:1.4f}, mcmc_f: {:1.4f}, ' 'sums_f: {:1.4f}, avlog_f: {:1.4f}, inv_f: {:1.4f}, pinv_f: {:1.4f}' .format(conf_prob, np.average(mv_accu_f), np.average(em_accu_f), np.average(mcmc_accu_f), np.average(sums_accu_f), np.average(avlog_accu_f), np.average(inv_accu_f), np.average(pinv_accu_f) )) print 'PROBABILISTIC:' print('confusion probability: {}, mv_p: {:1.4f}, em_p: {:1.4f}, mcmc_p: {:1.4f}' .format(conf_prob, np.average(mv_accu_p), np.average(em_accu_p), np.average(mcmc_accu_p) )) print('confusion probability: {}, mv_f_p: {:1.4f}, em:_f_p {:1.4f}, mcmc_f_p: {:1.4f}' .format(conf_prob, np.average(mv_accu_f_p), np.average(em_accu_f_p), np.average(mcmc_accu_f_p) )) print 'BINARY:' print('confusion probability: {}, mv: {:1.4f}, em: {:1.4f}, mcmc: {:1.4f}, ' 'sums: {:1.4f}, avlog: {:1.4f}, inv: {:1.4f}, pinv: {:1.4f}' .format(conf_prob, np.std(mv_accu), np.std(em_accu), np.std(mcmc_accu), np.std(sums_accu), np.std(avlog_accu), np.std(inv_accu), np.std(pinv_accu) )) print('confusion probability: {}, mv_f: {:1.4f}, em:_f {:1.4f}, mcmc_f: {:1.4f}, ' 'sums_f: {:1.4f}, avlog_f: {:1.4f}, inv_f: {:1.4f}, pinv_f: {:1.4f}' .format(conf_prob, np.std(mv_accu_f), np.std(em_accu_f), np.std(mcmc_accu_f), np.std(sums_accu_f), np.std(avlog_accu_f), np.std(inv_accu_f), np.std(pinv_accu_f) )) print 'PROBABILISTIC:' print('confusion probability: {}, mv_p: {:1.4f}, em_p: {:1.4f}, mcmc_p: {:1.4f}' .format(conf_prob, np.std(mv_accu_p), np.std(em_accu_p), np.std(mcmc_accu_p) )) print('confusion probability: {}, mv_f_p: {:1.4f}, em:_f_p {:1.4f}, mcmc_f_p: {:1.4f}' .format(conf_prob, np.std(mv_accu_f_p), np.std(em_accu_f_p), np.std(mcmc_accu_f_p) )) pd.DataFrame(res).to_csv('flight_accuracy.csv', index=False)
def investment_test5(self) : """This function tests to see if the proper value error is raised when something besides a integer or a float is passed""" with self.assertRaise(ValueError) as error: investment("a") self.assertTrue("Improper type for this class" in str(error.exception))
def investment_test1(self) : """This function tests to see if the class investment will output the right values for 1""" inv1 = investment(1) self.assertEqual(inv1.position, 1) self.assertEqual(inv1.position_value, 1000) self.assertIn(inv1.day_instance(), range(2001))
def testInvestment3(self): self.assertEqual(investment(100).position_value, 10) self.assertEqual(investment(1).position_value, 1000)
while terminate == False: num_trials = get_trials() position_list = get_position() if num_trials == "quit": terminate = True elif position_list == "quit": terminate = True else: for position in position_list: daily_ret = [0] * num_trials cumu_ret = [0] * num_trials curr_investment = investment(position) for trial in range(num_trials): cumu_ret[trial] = curr_investment.one_day_outcome() daily_ret[trial] = (cumu_ret[trial] / 1000) - 1 mean = np.mean(daily_ret) std = np.std(daily_ret) result = "For Position " + str(position) + ", the mean is " + str( mean) + ", the standard deviation is " + str(std) + "\n" f = open('result.txt', 'a') f.writelines(result) save_plot(position, daily_ret)
break else: positions = PositionInput(position_input).positions break except KeyboardInterrupt: sys.exit(0) except EOFError: sys.exit(0) while True: try: trial_input = input("How many times to randomly repeat this test?") trial_input = int(trial_input) if trial_input == 'quit': break else: num_trials = TrialInput(trial_input).num_trials break except KeyboardInterrupt: sys.exit(0) except EOFError: sys.exit(0) #Create a text file and write mean and standard deviation for each position in this text file f = open('results.txt','w') for position in positions: invest = investment(position,num_trials) invest.histogram() f.write('Position:' + str(position) + ',' + 'mean:' + str(invest.stats()[0]) + ',' + 'std:' + str(invest.stats()[1]) + '\r\n') f.close()
def testInvestment4(self): self.assertTrue(investment(10).cumu_ret[5] in [200,0]) self.assertTrue(investment(1).cumu_ret[0] in [2000,0])
def test_constructor(self): self.assertEqual(investment(10, 10).num_positions, 10) self.assertEqual(investment(10, 10).num_trials, 10) self.assertEqual(investment(10, 10).position_value, 100)
def test_simulate(self): self.assertEqual(len(investment.simulate(investment(10, 10))), 10) self.assertTrue(all(investment.simulate(investment(10, 10)) >= -1)) self.assertTrue(all(investment.simulate(investment(10, 10)) <= 1))
def test_simulation(self): invest = investment(10, 10000) daily_ret = invest.simulation() self.assertEqual(len(daily_ret), 10000) self.assertTrue(daily_ret.all() <= 1) self.assertTrue(daily_ret.all() >= -1)
def investment_test6(self): """"This function tests to see if the proper value error is raised if a number besides 1, 10, 100, or 1000 is used to build an investment class""" with self.assertRaise(ValueError) as error: investment(4) self.assertTrue( "Improper values passed for this class" in str(error.exception))
def testInvestment2(self): self.assertEqual(investment(1).position, 1 ) self.assertEqual(investment(1000).position, 1000 )
def investment_test1(self): """This function tests to see if the class investment will output the right values for 1""" inv1 = investment(1) self.assertEqual(inv1.position, 1) self.assertEqual(inv1.position_value, 1000) self.assertIn(inv1.day_instance(), range(2001))
def test_trails(self): trails = 10000 x = investment.investment(1000) self.assertTrue(len(x.daily_investment(10,trails))==trails)
def test_init(self): self.assertEqual(investment(1, 10000).num_positions, 1) self.assertEqual(investment(1, 10000).num_trials, 10000) self.assertEqual(investment(1, 10000).position_value, 1000 / 1)
while terminate == False: num_trials = get_trials() position_list = get_position() if num_trials == "quit": terminate = True elif position_list =="quit": terminate = True else: for position in position_list: daily_ret = [0]*num_trials cumu_ret = [0]*num_trials curr_investment = investment(position) for trial in range(num_trials): cumu_ret[trial] = curr_investment.one_day_outcome() daily_ret[trial] = (cumu_ret[trial]/1000) - 1 mean = np.mean(daily_ret) std = np.std(daily_ret) result = "For Position " + str(position) + ", the mean is " + str(mean) + ", the standard deviation is " + str(std) + "\n" f = open('result.txt', 'a') f.writelines(result) save_plot(position,daily_ret)
def investment_test6(self) : """"This function tests to see if the proper value error is raised if a number besides 1, 10, 100, or 1000 is used to build an investment class""" with self.assertRaise(ValueError) as error: investment(4) self.assertTrue("Improper values passed for this class" in str(error.exception))
def test_get_cumu_ret(self): self.assertEqual(type(investment(10, 10000).get_cumu_ret()), np.float64) self.assertTrue(investment(10, 10000).get_cumu_ret() >= 0)
def test_init(self): self.assertEqual(investment(10,10000).position, 10) self.assertEqual(investment(10,10000).num_trials, 10000) self.assertEqual(investment(10,10000).position_value, 100)
def test_simulate(self): self.assertEqual(len(investment.simulate(investment(10,10))),10) self.assertTrue(all(investment.simulate(investment(10,10))>=-1)) self.assertTrue(all(investment.simulate(investment(10,10))<=1))
def test0(self): with self.assertRaise(ValueError) as error: investment("a") self.assertTrue( "Please select from one of the four options: 1, 10, 100, 1000!" in str(error.exception))
def test2(self): test2 = investment(10) self.assertEqual(test2.position, 10) self.assertEqual(test2.position_value, 100) self.assertIn(test2.one_day_outcome(), range(2001))
def investment_test4(self): test4 = investment(1000) self.assertEqual(test4.position, 1000) self.assertEqual(test4.position_value, 1) self.assertIn(test4.one_day_outcome(), range(2001))
def test_constructor(self): i = investment("[1, 10]", "10000") self.assertEqual(i.positions, "[1, 10]") self.assertEqual(i.num_trails, "10000")
def efficiency(): """ Efficiency as the number of clusters growing. """ N, M, Psi, GT = load_dataset() # inject confusions # Ncs = [10, 100, 1000, 10000] Ncs = [10000] mcmc_params = {'N_iter': 10, 'burnin': 1, 'thin': 2, 'FV': 3} # mcmc_params = {'N_iter': 30, 'burnin': 5, 'thin': 3, 'FV': 4} res = {'mv': [], 'mv std': [], 'em': [], 'em std': [], 'mcmc': [], 'mcmc std': [], 'f_mcmc': [], 'f_mcmc std': [], 'sums': [], 'sums std': [], 'avlog': [], 'avlog std': [], 'inv': [], 'inv std': [], 'pinv': [], 'pinv std': [], 'number of objects with confusions': Ncs} for Nc in Ncs: print 'Nc: {}'.format(Nc) times = [[], [], [], [], [], [], [], []] for run in range(n_runs): print 'run: {}'.format(run) GT_G, Cl, cPsi = confuse(Psi, 0.8, GT, Nc) start = time.time() majority_voting(cPsi) times[0].append(time.time() - start) start = time.time() expectation_maximization(N, M, cPsi) times[1].append(time.time() - start) start = time.time() mcmc(N, M, cPsi, mcmc_params) times[2].append(time.time() - start) start = time.time() f_mcmc(N, M, cPsi, Cl, mcmc_params) times[3].append(time.time() - start) data = adapter_input(cPsi) start = time.time() sums(N, data) times[4].append(time.time() - start) start = time.time() average_log(N, data) times[5].append(time.time() - start) start = time.time() investment(N, data) times[6].append(time.time() - start) start = time.time() pooled_investment(N, data) times[7].append(time.time() - start) res['mv'].append(np.average(times[0])) res['em'].append(np.average(times[1])) res['mcmc'].append(np.average(times[2])) res['f_mcmc'].append(np.average(times[3])) res['sums'].append(np.average(times[4])) res['avlog'].append(np.average(times[5])) res['inv'].append(np.average(times[6])) res['pinv'].append(np.average(times[7])) res['mv std'].append(np.std(times[0])) res['em std'].append(np.std(times[1])) res['mcmc std'].append(np.std(times[2])) res['f_mcmc std'].append(np.std(times[3])) res['sums std'].append(np.std(times[4])) res['avlog std'].append(np.std(times[5])) res['inv std'].append(np.std(times[6])) res['pinv std'].append(np.std(times[7])) print( '{}\tmv: {:1.4f}\tem: {:1.4f}\tmcmc: {:1.4f}\tf_mcmc: {:1.4f}\tsums: {:1.4f}\tavlog: {:1.4f}\tinv: {:1.4f}\tpinv: {:1.4f}'.format( Nc, np.average(times[0]), np.average(times[1]), np.average(times[2]), np.average(times[3]), np.average(times[4]), np.average(times[5]), np.average(times[6]), np.average(times[7]) )) print('{}\tmv: {:1.4f}\tem: {:1.4f}\tmcmc: {:1.4f}\tf_mcmc: {:1.4f}\tsums: {:1.4f}\tavlog: {:1.4f}\tinv: {:1.4f}\tpinv: {:1.4f}'.format(Nc, np.average(times[0]), np.average(times[1]), np.average(times[2]), np.average(times[3]), np.average(times[4]), np.average(times[5]), np.average(times[6]), np.average(times[7]) )) pd.DataFrame(res).to_csv(work_dir + 'experiments_results/flight_efficiency.csv', index=False)
def test_investment(self): truncated_tester = lambda amt, int: round(investment(amt, int), 9) self.assertEqual(truncated_tester(100, 0.05), 608.811017706) self.assertEqual(truncated_tester(200, 0.03), 1210.54385954)