-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmcs_full_vector_numpy
More file actions
31 lines (30 loc) · 916 Bytes
/
mcs_full_vector_numpy
File metadata and controls
31 lines (30 loc) · 916 Bytes
1
###Monte Carlo valuation of European call options with NumPy (log version)# mcs_full_vector_numpy.py# import mathfrom numpy import *from time import time# star import for shorter coderandom.seed(20000)t0 = time()# ParametersS0 = 100.; K = 105.; T = 1.0; r = 0.05; sigma = 0.2M = 50; dt = T / M; I = 250000# Simulating I paths with M time stepsS = S0 * exp(cumsum((r - 0.5 * sigma ** 2) * dt + sigma * math.sqrt(dt) * random.standard_normal((M + 1, I)), axis=0))# sum instead of cumsum would also do# if only the final values are of interestS[0] = S0# Calculating the Monte Carlo estimatorC0 = math.exp(-r * T) * sum(maximum(S[-1] - K, 0)) / I# Results outputtnp2 = time() - t0print ('European Option Value %7.3f' % C0)print ('Duration in Seconds %7.3f' % tnp2)import matplotlib.pyplot as pltplt.grid(True)plt.xlabel('time step')plt.ylabel('index level')plt.plot(S[:, :10])