您的位置:首页 > 编程语言 > Python开发

区块链金融中的python应用--LSM定价

2019-02-18 11:56 761 查看

区块链金融中的BSM定价是最基本的,网络上有很多,就不一一阐述了。这里给了一个用最小二乘法进行的美式定价。

from math import log,sqrt,exp,ceil
import numpy as np
import numpy.random as npr

S0= 100.
r = 0.05
sigma = 0.25
T = 1.0
I=50000
M=50
#给出假设的基本的条件

def gen_sn(M,I,anti_paths=True,mo_match=True):
# 生成随机值(运用方差缩减-对偶方法或者矩匹配的方法,改善正态分布随机值的匹配

if anti_paths is True:
#对偶方法
sn = npr.standard_normal((M+1,int(I / 2)))
sn = np.concatenate((sn,-sn), axis=1)
else:
sn = npr.standard_normal((M+1,I))
if mo_match is True:
#矩匹配
sn = (sn - sn.mean())/sn.std()
return sn

def gbm_mcs_amer(K):#定义LSM
dt = T/M
df = np.exp(-r*dt)
S = np.zeros((M+1,I))
S[0]=S0
sn = gen_sn(M,I)
#生成随机值
for t in range(1,M+1):
S[t]=S[t-1]*np.exp((r-0.5*sigma**2)*dt+sigma*np.sqrt(dt)*sn[t])
h=np.maximum(S-K,0)
#收益函数
V=np.copy(h)
for t in range(M-1,0,-1):
#最小二乘法
reg = np.polyfit(S[t],V[t+1]*df,7)
C = np.polyval(reg,S[t])
V[t] = np.where(C>h[t],V[t+1]*df,h[t])
C0= df*1/I *np.sum(V[1])
return C0
gbm_mcs_amer(110.)
## 7.8241662128626945
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: