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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
Empty file added bz866/.metadata/.lock
Empty file.
Binary file added bz866/.metadata/.mylyn/.taskListIndex/segments.gen
Binary file not shown.
Binary file added bz866/.metadata/.mylyn/.taskListIndex/segments_1
Binary file not shown.
Binary file added bz866/.metadata/.mylyn/.tasks.xml.zip
Binary file not shown.
Binary file added bz866/.metadata/.mylyn/repositories.xml.zip
Binary file not shown.
Binary file added bz866/.metadata/.mylyn/tasks.xml.zip
Binary file not shown.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'''
Created on 2016年12月4日

@author: EricTseng
'''
import unittest


class Test(unittest.TestCase):


def testName(self):
pass


if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'''
Created on 2016年11月日

@author: EricTseng
'''

from Investment.investment import *
from Investment.exp_handle import *

while True:
try:
positions = eval(input('Please input a list of positions.\n'))
num_trials = eval(input('Please input the number of trials.\n'))
simulationInit = trialInput(positions, num_trials)
simulationInit.simulate()
break
except InvalidListError:
print('Invalid position list.')
except PositionError:
print('Invalid positions in your list.')
except IntegerError:
print('Invalid Value. Trail number must be a integer.')

if __name__ == '__main__':
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
'''
Created on 2016年12月4日

@author: EricTseng
'''

import numpy as np
import matplotlib.pyplot as plt
from Investment.exp_handle import *

class trialInput(object):
def __init__(self, positionList, num_trials):
if not isinstance(positionList, list):
raise InvalidListError()
else:
denominations = [1000,100,10,1]
for i in range(len(positionList)):
if 1000/positionList[i] != denominations[i]:
raise PositionError()
if not isinstance(num_trials, int):
raise ValueError()

self.positionList = positionList
self.num_trials = num_trials


'''function that repeat num_trail times stimulation for each in
position_values and return their results as daily_ret.
Meanwhile, '''
def simulate(self):
f = open('results.txt', 'w')
for p in self.positionList:
cumu_ret=np.zeros(self.num_trials)
daily_ret=np.zeros(self.num_trials)
for t in range(self.num_trials):
cumu_num = 0
for i in range(int(1000/p)):
random_num = np.random.rand()
if (0 <= random_num <= 0.51):
cumu_num = cumu_num + 2 * p
elif (1 > random_num > 0.51):
cumu_num = cumu_num
cumu_ret[t]=cumu_num
daily_ret[t]=(cumu_ret[t]/1000)-1
plt.clf()
plt.hist(daily_ret,100,range=[-1,1])
plt.xlabel('Daily Returns')
plt.ylabel(str(p) + 'trails result')

if p ==1:
plt.savefig('histogram_0001_pos.pdf')
elif p == 10:
plt.savefig('histogram_0010_pos.pdf')
elif p ==100:
plt.savefig('histogram_0100_pos.pdf')
elif p ==1000:
plt.savefig('histogram_1000_pos.pdf')

ret_mean = np.mean(daily_ret)
ret_std = np.std(daily_ret)
f.write('mean of return for position ' + str(p) + ' : ' + str(ret_mean) + '\n')
f.write('std of return for position ' + str(p) + ' : ' + str(ret_std) + '\n')

f.close()
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'''
Created on 2016年12月4日

@author: EricTseng
'''

from investment.investment import *
import sys

if __name__ == '__main__':
while True:
try:
inputstr=input('Please enter a list of numbers of shares to buy in parallel, such as [1,10,100,1000]')
if (inputstr=='quit'):
print("User Directed Quit.")
sys.exit()
inputstr=inputstr.replace(' ','')#Remove extra blank
if inputstr=='':
raise ValueError('Invalid Input: Need a list of numbers of shares to buy in parallel')
if inputstr[0]!='[' or inputstr[-1]!=']':
raise ValueError('Invalid Input: Need a list of number in [ and ], such as [1,10,100,1000]')
positions=list(map(int,inputstr[1:-1].split(",")))
if any(x<=0 for x in positions):
raise ValueError('Invalid Input: all numbers of shares need to be positive')
break
except ValueError as msg:
print(msg)
except KeyboardInterrupt:
print('Keyboard Interruption')
sys.exit()
except EOFError:
print('EOFError')
sys.exit()

while True:
try:
inputstr2=input('Please enter the number of times you want to repeat the simulation')
if (inputstr2=='quit'):
print("User Directed Quit.")
sys.exit()
num_trials=int(inputstr2)
investment.investment(positions,num_trials).simulation()
break
except ValueError as msg:
print(msg)
except KeyboardInterrupt:
print('Keyboard Interruption')
sys.exit()
except EOFError:
print('EOFError')
sys.exit()
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
'''
Created on 2016年11月日

@author: EricTseng
'''

from Investment.investment import *
from Investment.exceptions import *

while True:
try:
positions = eval(input('Please input a list of positions.\n'))
num_trials = eval(input('Please input the number of trials.\n'))
simulationInit = trialInput(positions, num_trials)
simulationInit.simulate()
break
except InvalidListError:
print('The position list you input is invalid. Please try again.')
except PositionError:
print('The positions in your list is invalid. Please try again.')
except IntegerError:
print('The number of trial must be an integer. Please try again.')

if __name__ == '__main__':
pass
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'''
Created on 2016年12月4日

@author: EricTseng
'''

import numpy as np
import matplotlib.pyplot as plt
from Investment.exceptions import *

class trialInput(object):
def __init__(self, positionList, num_trials):
if not isinstance(positionList, list):
raise InvalidListError()
else:
denominations = [1000,100,10,1]
for i in range(len(positionList)):
if 1000/positionList[i] != denominations[i]:
raise PositionError()
if not isinstance(num_trials, int):
raise IntegerError()

self.positionList = positionList
self.num_trials = num_trials

def simulate(self):
f = open('results.txt', 'w')
for position in self.positionList:
cumu_ret=np.zeros(self.num_trials)
daily_ret=np.zeros(self.num_trials)
for trial in range(self.num_trials):
num_win=np.random.binomial(position,0.49)
num_lost=position-num_win
position_value=1000/position
cumu_ret[trial]=(num_win*(1+1)+num_lost*(1-1))*position_value
daily_ret[trial]=(cumu_ret[trial]/1000)-1
plt.clf()
plt.hist(daily_ret,100,range=[-1,1])
plt.xlabel('Daily Returns')
plt.title('Histogram of Daily Returns for position = ' + str(position))
plt.savefig('histogram_'+'{:04d}'.format(position)+'_pos.pdf')
mean_ret = np.mean(daily_ret)
std_ret = np.std(daily_ret)
f.write('mean of return for position ' + str(position) + ' : ' + str(mean_ret) + '\n')
f.write('std of return for position ' + str(position) + ' : ' + str(std_ret) + '\n')
f.close()
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
'''
Created on 2016年12月4日

@author: EricTseng
'''

from Investment.investment import *
import sys
import Investment

if __name__ == '__main__':
try:
inputstr=input('Please enter a list of numbers of shares to buy in parallel, such as [1,10,100,1000]')
if (inputstr=='quit'):
print("User Directed Quit.")
sys.exit()
inputstr=inputstr.replace(' ','')#Remove extra blank
if inputstr=='':
raise ValueError('Invalid Input: Need a list of numbers of shares to buy in parallel')
if inputstr[0]!='[' or inputstr[-1]!=']':
raise ValueError('Invalid Input: Need a list of number in [ and ], such as [1,10,100,1000]')
positions=list(map(int,inputstr[1:-1].split(",")))
if any(x<=0 for x in positions):
raise ValueError('Invalid Input: all numbers of shares need to be positive')
break
except ValueError as msg:
print(msg)
except KeyboardInterrupt:
print('Keyboard Interruption')
sys.exit()
except EOFError:
print('EOFError')
sys.exit()

while True:
try:
inputstr2=input('Please enter the number of times you want to repeat the simulation')
if (inputstr2=='quit'):
print("User Directed Quit.")
sys.exit()
num_trials=int(inputstr2)
Investment.investment(positions,num_trials).simulation()
break
except ValueError as msg:
print(msg)
except KeyboardInterrupt:
print('Keyboard Interruption')
sys.exit()
except EOFError:
print('EOFError')
sys.exit()
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
'''
Created on 2016年12月4日

@author: EricTseng
'''

import numpy as np
import matplotlib.pyplot as plt
from Investment.exceptions import *

class trialInput(object):
def __init__(self, positionList, num_trials):
if not isinstance(positionList, list):
raise InvalidListError()
else:
denominations = [1000,100,10,1]
for i in range(len(positionList)):
if 1000/positionList[i] != denominations[i]:
raise PositionError()
if not isinstance(num_trials, int):
raise IntegerError()

self.positionList = positionList
self.num_trials = num_trials

def simulate(self):
f = open('results.txt', 'w')
for position in self.positionList:
cumu_ret=np.zeros(self.num_trials)
daily_ret=np.zeros(self.num_trials)
for trial in range(self.num_trials):
num_win=np.random.binomial(position,0.49)
num_lost=position-num_win
position_value=1000/position
cumu_ret[trial]=(num_win*(1+1)+num_lost*(1-1))*position_value
daily_ret[trial]=(cumu_ret[trial]/1000)-1
plt.clf()
plt.hist(daily_ret,100,range=[-1,1])
plt.xlabel('Daily Returns')
plt.title('Histogram of Daily Returns for position = ' str(position))
plt.savefig('histogram_'+'{:04d}'.format(position)+'_pos.pdf')
mean_ret = np.mean(daily_ret)
std_ret = np.std(daily_ret)
f.write('mean of return for position ' str(position) ' : ' str(mean_ret) '\n')
f.write('std of return for position ' str(position) ' : ' str(std_ret) '\n')
f.close()
Loading