def plotResult(positions, num_trials): ''' plot the simulation results and save to 4 different pdf files ''' for p in positions: ins = instrument(p) #create an investment instrument of type p daily_ret = np.array(simulateTrials(ins, num_trials)) #get daily return fig = plt.figure() plt.hist(daily_ret, 100, range = [-1,1]) plt.title('return for investing ' + str(p) + ' shares') plt.xlabel('daily return') plt.ylabel('frequency') file_name = 'histogram_' + str(p).zfill(4) + '_pos.pdf' fig.savefig(file_name) plt.clf()
def writeResult(positions, num_trials): ''' write the simulation results into a results.txt file ''' file = open('results.txt','w') for p in positions: ins = instrument(p) #create an investment instrument of type p daily_ret = np.array(simulateTrials(ins, num_trials)) #get daily return mean = np.mean(daily_ret) sd = np.std(daily_ret) str_out = 'For position = %d, mean = %f, standard deviation = %f' %(p, mean, sd) file.write(str_out) file.write('\n') file.close()