在python中利蒙特卡洛公式实现估值计算,具体方法如下:
from time import time
from math import exp, sqrt, log
from random import gauss, seed
seed(2000)
# 计算的初始值
S_0 = 100.0
K = 105
T = 1.0
r = 0.05
sigma = 0.2
M = 50
dt = T/M
I = 20000
start = time()
S = []
for i in range(I):
path = []
for t in range(M+1):
if t==0:
path.append(S_0)
else:
z = gauss(0.0, 1.0)
S_t = path[t-1] * exp((r-0.5*sigma**2) * dt + sigma * sqrt(dt) * z)
path.append(S_t)
S.append(path)
# 实现估值
C_0 = exp(-r * T) *sum([max(path[-1] -K, 0) for path in S])/I
total_time = time() - start
print 'European Option value %.6f'% C_0
print 'total time is %.6f seconds'% total_time
输出结果为:
European Option value 8.159995
total time is 2.384639 seconds