【发布时间】:2016-11-16 14:24:23
【问题描述】:
我正在编写一个程序,在必要条件下使用 Black-Scholes 公式显式计算看涨期权的价格。运行代码时出现错误。
我不确定是什么原因造成的。请,非常感谢任何和所有的帮助。到目前为止,这是我的代码:
## This program is to perform an explicit Black-Scholes hedge using the formula:
##
## If a stock has a constant volatility of 18% and constant drift of 8%, with
## continuously compounded interest rates constant at 6%, what is the value of
## an option to buy the stock for $25 in two years time, given a current stock
## price of $20?
##
## The description fits the Black-Scholes conditions. Thus, using s = 20, k = $25,
## sigma = 0.18, r = 0.06, and t= 2, we can calculate V_0 = $1.221. We will verify
## that this result is correct:
##
import numpy as np
from math import exp, log
from scipy.stats import norm
import matplotlib.pyplot as plt
# Parameters
s = 20 # current stock price
k = 25 #strike price of the option in dollars
sigma = 0.18 # constant volatility
r = 0.06 # constant interest rate
T = 2 # expiry date of contract, 2 years time
def V(s,T):
return s * norm.cdf( (log(s / k) + (r + 0.5 * pow(sigma,2)) * T) / (sigma * np.sqrt(T)) )
- k * exp(-r * T) * norm.cdf( (log(s / k) + (r - 0.5 * pow(sigma,2.0)) * T) / (sigma * np.sqrt(T)) )
V_0 = V(s,T) # the value of our option at time t=0 is the same at expiry T
print V_0
这是我在运行代码时得到的:“ValueError: math domain error”,它指向我为我定义的函数返回值的行。谢谢!
【问题讨论】:
-
通常在记录负数时会出现该错误;检查所有值以确保您为每个函数使用正确的域
-
log( s / k) = log( 0.8 ),所以我认为还不够。不过还是谢谢 -
可能是其他函数之一正在接收超出其域的输入。负数的平方根(虽然看起来你没有)也会抛出这个错误。我不确定
scipy包中的norm.cdf,但这可能也有一些域限制,您可以查看一下吗? -
好的,我会注意的。非常感谢!