示例#1
0
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.")
示例#2
0
 def test_position(self): 
     self.assertEqual(investment(10,100).position,10)
     self.assertEqual(investment(100,100).position,100)
示例#3
0
 def test_simulation(self):
     self.assertEqual(len(investment(10,100).simulation()), 100)
     self.assertEqual(len(investment(10,1000).simulation()), 1000)
示例#4
0
 def test_daily_ret(self):
     self.assertTrue(investment(10,100).daily_ret() <= 1)
     self.assertTrue(investment(10,100).daily_ret() >= -1)
示例#5
0
 def test_cumu_ret(self):
     self.assertTrue(investment(10,10).cumu_ret() >= 0)
     self.assertTrue(investment(10,100).cumu_ret() >= 0)
示例#6
0
 def test_position_value(self):
     self.assertEqual(investment(10,100).position_value(),100)
     self.assertEqual(investment(100,100).position_value(),10)
示例#7
0
 def test_num_trials(self):
     self.assertEqual(investment(10,10).num_trials,10)
     self.assertEqual(investment(10,100).num_trials,100)