我正在编写一个程序,在必要条件下使用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”,它指向我为我定义的函数返回值的行。谢谢!
答案 0 :(得分:3)
log(s/k)
正在抛出错误。即使20/25 = 0.8,因为它们都是整数,20/25的计算结果为0,log(0)
抛出错误。将s或k或两者转换为浮点数,这应该可以解决您的问题