def simulation(positions, num_trials): """ Simulate a list of positions in num_trials times. Then plot and write down the numerical results. """ num_result = open('result.txt', 'w') for i in positions: sim = investment(i, num_trials).simulation() # Plot and print the mean and sd of its daily plt.hist(sim,100,range=[-1,1]) plt.title("position = " + str(i)) plt.savefig("histogram_" + "{:04d}".format(i) + "_pos.pdf") plt.clf() num_result.write ("Numerical results when position = " + str(i) +"\n") num_result.write ("Mean value: " + str(np.mean(sim)) + "\n" ) num_result.write ("standard deviation: " + str(np.std(sim)) + "\n" +"\n") print("Position = " + str(i) + " finished.")
def test_position(self): self.assertEqual(investment(10,100).position,10) self.assertEqual(investment(100,100).position,100)
def test_simulation(self): self.assertEqual(len(investment(10,100).simulation()), 100) self.assertEqual(len(investment(10,1000).simulation()), 1000)
def test_daily_ret(self): self.assertTrue(investment(10,100).daily_ret() <= 1) self.assertTrue(investment(10,100).daily_ret() >= -1)
def test_cumu_ret(self): self.assertTrue(investment(10,10).cumu_ret() >= 0) self.assertTrue(investment(10,100).cumu_ret() >= 0)
def test_position_value(self): self.assertEqual(investment(10,100).position_value(),100) self.assertEqual(investment(100,100).position_value(),10)
def test_num_trials(self): self.assertEqual(investment(10,10).num_trials,10) self.assertEqual(investment(10,100).num_trials,100)