我把Blacks-Scholes方程转化为热方程。本文尝试用显式有限差分法求解偏微分方程,得到看涨期权的价格。我也用布莱克-斯科尔斯方程“解析地”来解决这个问题。在
问题是我不能在数值结果中得到更精确的结果。这是我的Python代码。在
以下是我的算法注释: https://drive.google.com/file/d/0B5h3oewtgjFgdVFpNFJRNTB5LXM/view?usp=sharing
import math
import numpy as np
from scipy.stats import norm
s0 = 15
sigma = 0.2
r = 0.01
t = 1
Xmax = 10
'''B-S price'''
def C(s,k,t):
d1 = (math.log(s/k)+(r+sigma*sigma/2)*t)/(sigma*math.sqrt(t))
d2 = (math.log(s/k)+(r-sigma*sigma/2)*t)/(sigma*math.sqrt(t))
return s*norm.cdf(d1)-math.exp(-r*t)*k*norm.cdf(d2)
print('B-S', C(s0,10,t))
'''Explicit_finite_difference'''
EFD_n_x = 500
EFD_n_t = 100
EFD_k = Xmax/EFD_n_x
EFD_h = t/EFD_n_t
EFD_xx = np.linspace(Xmax,-Xmax, 2 * EFD_n_x + 1)
EFD_xx = EFD_xx[1:2 * EFD_n_x]
def EFD_T0_Bound(x):
return max(math.exp(x)-10*math.exp(-r*t),0)
def EFD_U_Bound(tao):
return math.exp(Xmax)-10*math.exp(-r*(t-tao))
def EFD_L_Bound(tao):
return 0
EFD_T0bound = np.vectorize(EFD_T0_Bound)
EFD_lambda = EFD_h*sigma*sigma/2/EFD_k/EFD_k
EFD_A = (np.eye(2 * EFD_n_x - 1) * (1-2*EFD_lambda)
+ np.eye(2 * EFD_n_x - 1, k=1)*EFD_lambda
+ np.eye(2 * EFD_n_x - 1, k=-1)*EFD_lambda)
EFD_Y = np.zeros(2 * EFD_n_x - 1)
EFD_U = EFD_T0bound(EFD_xx)
for i in range(EFD_n_t):
EFD_Y[0] = EFD_lambda*EFD_U_Bound(EFD_h*i)
EFD_Y[2 * EFD_n_x - 2] = EFD_lambda*EFD_L_Bound(EFD_h*i)
EFD_U = np.dot(EFD_A,EFD_U) + EFD_Y #U_t_i+1 = A * U_t_i + Y
print('Explicit_finite_difference',EFD_U[EFD_n_x - 1 - round(math.log(s0)/EFD_k)])
在我看来,由于你使用的是一个显式的方案(不是无条件稳定的),你不能独立地设置你的资产步数和时间步数。通常人们将它们与波动性联系起来,以保持方案的稳定性
相关问题 更多 >
编程相关推荐