-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmcs_pure_python.py
85 lines (64 loc) · 2.05 KB
/
mcs_pure_python.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#dSt = rSt dt + sigma * St dZt
#St = S(t-Delta t) exp ((r - sigma^2 * .5) * Delta t + sigma sqrt(Deltat) * Zt)
# hT = max(ST(i) - K, 0)
#mc C0 = exp(-rT) 1/I sum(hT(ST(i))) - avg of all mc simulations
S0 = 100.
K = 105.
T = 1.
r = 0.05
sigma = 0.2
M = 50 #time steps
dt = T / M
I = 250000 # number of paths
def simple_MC():
from time import time
from math import exp,sqrt
from random import gauss,seed
seed(2000)
t0 = time()
S = []
for i in range(I):
path = []
for t in range(M+1):
if t == 0:
path.append(S0)
else:
z = gauss(0.0,1.0)
St = path[t - 1] * exp((r - 0.5 * sigma ** 2) * dt
+ sigma * sqrt(dt) * z)
path.append(St)
S.append(path)
C0 = exp(-r * T ) * sum([max(path[-1] - K,0) for path in S]) / I
tpy = time() - t0
print "european otion value %7.3f" % C0
print "duration is sec %7.3f" % tpy
def vector_MC():
import numpy as np
import math
from time import time
np.random.seed(2000)
t0 = time()
S = np.zeros((M+1,I))
S[0] = S0
for t in range(1,M+1):
z = np.random.standard_normal(I) #pseduo numbers
S[t] = S[t - 1] * np.exp((r - 0.5 * sigma ** 2) * dt
+ sigma * math.sqrt(dt) * z)
C0 = math.exp(-r*T)*np.sum(np.maximum(S[-1] - K,0)) / I
tmp = time() - t0
print "option price using vector_MC = %7.3f" %C0
print "in time %7.3f" %tmp
def full_vector_MC():
import math
from numpy import *
from time import time
random.seed(2000)
t0 = time()
S = S0 * exp(cumsum((r - 0.5 * sigma ** 2) * dt
+ sigma * math.sqrt(dt)
* random.standard_normal((M+1,I)),axis=0))
S[0] = S0
C0 = math.exp(-r * T) * sum(maximum(S[-1] - K,0)) / I
tmp2 = time() - t0
print "european otion value %7.3f" % C0
print "duration is sec %7.3f" % tmp2