Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Histogram_1000_pos.pdf
Binary file not shown.
Binary file added Histogram_100_pos.pdf
Binary file not shown.
Binary file added Histogram_10_pos.pdf
Binary file not shown.
Binary file added Histogram_1_pos.pdf
Binary file not shown.
39 changes: 39 additions & 0 deletions assignment8.py
Original file line number Diff line number Diff line change
@@ -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()
4 changes: 4 additions & 0 deletions results.txt
Original file line number Diff line number Diff line change
@@ -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
32 changes: 32 additions & 0 deletions simulation.py
Original file line number Diff line number Diff line change
@@ -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()
Binary file added simulation.pyc
Binary file not shown.
22 changes: 22 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
@@ -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()