I'm trying to find the expected number of time steps for an share price to fall from $25 to $18 by generating simulations. Analytically, it can be shown that the expected number of time steps required is 129.
The following 3 codes appear fundamentally the same to me, but gives different results.
The first returns an average of 100 time steps:
target = 18
support_level = 20
I = []
n = 50000
for i in range(n):
stock_price = 25
t = 0
while stock_price != target:
t += 1
if stock_price < support_level:
stock_price += random.choices([-1, 1], weights=[1/3, 2/3])[0]
if stock_price == support_level:
stock_price += random.choices([-1, 1], weights=[0.1, 0.9])[0]
if stock_price > support_level:
stock_price += random.choices([-1, 1], weights=[2/3, 1/3])[0]
I.append(t)
sum(I) / len(I)
The second returns an average of 114:
for i in range(n):
stock_price = 25
t = 0
while stock_price != target:
x = random.uniform(0, 1)
t += 1
if stock_price < support_level:
if x <= 2/3:
stock_price += 1
else:
stock_price -= 1
if stock_price == support_level:
if x <= 0.9:
stock_price += 1
else:
stock_price -= 1
if stock_price > support_level:
if x <= 1/3:
stock_price += 1
else:
stock_price -= 1
I.append(t)
The third returns an average of 129:
for i in range(n):
p=25
t=0
while p != 18:
if(p==20):
dist = [0.1, 0.9]
elif(p>20):
dist = [2/3, 1/3]
else:
dist = [1/3, 2/3]
p=p+random.choices(direction, dist)[0]
t+=1
I.append(t)
sum(I)/len(I)
What is the difference?
Aucun commentaire:
Enregistrer un commentaire