-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcs_vector_numpy.py
More file actions
33 lines (26 loc) · 821 Bytes
/
Copy pathmcs_vector_numpy.py
File metadata and controls
33 lines (26 loc) · 821 Bytes
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
#
# Monte Carlo valuation of European call options with NumPy
# mcs_vector_numpy.py
#
import math
import numpy as np
from time import time
np.random.seed(20000)
t0 = time()
# Parameters
S0 = 100.; K = 105.; T = 1.0; r = 0.05; sigma = 0.2
M = 50; dt = T / M; I = 250000
# Simulating I paths with M time steps
S = np.zeros((M+1, I))
S[0] = S0
for t in range(1, M + 1):
z = np.random.standard_normal(I) # pseudorandom numbers
S[t] = S[t - 1] * np.exp((r - 0.5 * sigma ** 2) * dt
+ sigma * math.sqrt(dt) * z)
# vectorized operation per time step over all paths
# Calculating the Monte Carlo estimator
C0 = math.exp(-r * T) * np.sum(np.maximum(S[-1] - K, 0)) / I
# Results output
tnp1 = time() - t0
print "European Option Value %7.3f" % C0
print "Duration in Seconds %7.3f" % tnp1