diff --git a/Histogram_1000_pos.pdf b/Histogram_1000_pos.pdf new file mode 100644 index 0000000..4901ffe Binary files /dev/null and b/Histogram_1000_pos.pdf differ diff --git a/Histogram_100_pos.pdf b/Histogram_100_pos.pdf new file mode 100644 index 0000000..9db804e Binary files /dev/null and b/Histogram_100_pos.pdf differ diff --git a/Histogram_10_pos.pdf b/Histogram_10_pos.pdf new file mode 100644 index 0000000..fc254d6 Binary files /dev/null and b/Histogram_10_pos.pdf differ diff --git a/Histogram_1_pos.pdf b/Histogram_1_pos.pdf new file mode 100644 index 0000000..42cad91 Binary files /dev/null and b/Histogram_1_pos.pdf differ diff --git a/assignment8.py b/assignment8.py new file mode 100644 index 0000000..386f2f0 --- /dev/null +++ b/assignment8.py @@ -0,0 +1,39 @@ +# Author: Yang Sun +import sys +from simulation import * + +'''Users should input a list of numbers of shares to buy, and a number of times to repeat to do the +simulation of the investment. The program will yield mean and standard deviation of each position, +and histogram of daily return.''' + +while True: + try: + pos_num = raw_input('A list of number of shares to buy in parallel? ' + 'Valid numbers to input are 1,10,100 and 1000\n') + positions = pos_num.split() + for i, position in enumerate(positions): + positions[i] = int(position) + break + except EOFError: + sys.exit(1) + except KeyboardInterrupt: + sys.exit(2) + +while True: + try: + num_trials = int(raw_input('How many times to randomly repeat the test?\n')) + break + except ValueError: + print('Invalid number!') + except EOFError: + sys.exit(3) + except KeyboardInterrupt: + sys.exit(4) + +f = open ('results.txt','w') +for i in positions: + s = simulation(i, num_trials) + f.write('Position: ' + str(i) + ', Mean: ' + str(s.mean)+ ', Std: ' + str(s.std) + "\r\n") + s.histogram('Histogram_' + str(i) + '_pos.pdf') + +f.close() diff --git a/results.txt b/results.txt new file mode 100644 index 0000000..27e9b59 --- /dev/null +++ b/results.txt @@ -0,0 +1,4 @@ +Position: 1, Mean: 0.0182, Std: 0.999834366283 +Position: 10, Mean: 0.01614, Std: 0.314145667486 +Position: 100, Mean: 0.020338, Std: 0.0997569333731 +Position: 1000, Mean: 0.0195904, Std: 0.0314628197694 diff --git a/simulation.py b/simulation.py new file mode 100644 index 0000000..af3efed --- /dev/null +++ b/simulation.py @@ -0,0 +1,32 @@ +import numpy as np +import matplotlib.pyplot as plt + +'''This module "simulation" have two functions. The first function takes in two parameters, number of +positions bought and number of times to simulate. Daily return, the mean and standard deviation of daily +return are all calculated. The second function takes in one parameter and draws a histogram for each + daily return''' + +class simulation: + def __init__(self, position, num_trials): + position_value = 1000 / position + self.cumu_ret = [] + ret_list = [] + for i in range(num_trials): + cumu_ret = 0 + for j in range(position): + chance = np.random.uniform(0, 1) + if chance <= 0.51: + cumu_ret += position_value * 2 + else: + cumu_ret += 0 + ret_list.append(cumu_ret/1000.0 - 1) + self.daily_ret = np.asarray(ret_list) + self.mean = self.daily_ret.mean() + self.std = self.daily_ret.std() + + def histogram(self,f_name): + plt.hist(self.daily_ret, 100, range = [-1,1]) + plt.xlabel('Daily_ret') + plt.ylabel('Number of trials') + plt.savefig(f_name) + plt.close() diff --git a/simulation.pyc b/simulation.pyc new file mode 100644 index 0000000..dc2f219 Binary files /dev/null and b/simulation.pyc differ diff --git a/test.py b/test.py new file mode 100644 index 0000000..c023dae --- /dev/null +++ b/test.py @@ -0,0 +1,22 @@ +import unittest +from simulation import * + +'''This class contains two functions. First one tests the mean generated by the module "simulation", and +the second one tests the standard deviation of the previous result. Both pass successfully''' + +class tests(unittest.TestCase): + def test_mean(self): + s1 = simulation(10, 10000) + s2 = simulation(100, 10000) + self.assertTrue(abs(s1.mean - 0.01588) < 0.01) + self.assertTrue(abs(s2.mean - 0.021166) < 0.01) + + def test_std(self): + s3 = simulation(100, 10000) + s4 = simulation(1000, 10000) + self.assertTrue(abs(s3.std - 0.099479648391) < 0.01) + self.assertTrue(abs(s4.std - 0.03175307467) < 0.01) + + +if __name__ == "__main__": + unittest.main() \ No newline at end of file