python金融入门之MonteCarle公式

# monte carlo valuation of european call options with pure python
from time import time 
from math import exp,log,sqrt
from random import gauss,seed
seed(20000)
t0=time()
#parameter
S0=100 #initial value
K=105 #strike price
T=1.0 #maturity
r=0.05 #riskless short rate
sigma=0.2 #volatility
M=50 #number of time steps
dt=T/M #length of time interval
I=250000 #number of paths
#Simulating I paths with M time steps
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)


#calculating the MonteCarlo estimator
C0=exp(-r*T)*sum([max(path[-1]-K,0) for path in S])/I
#Result output
tpy=time()-0
print('European option value %7.3f' % C0)
print('Duration in seconds %7.3f' % tpy)
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值