【问题标题】:ValueError: math domain error - Black-Scholes Option ValuationValueError:数学域错误 - Black-Scholes 期权估值
【发布时间】: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,但这可能也有一些域限制,您可以查看一下吗?
  • 好的,我会注意的。非常感谢!

标签: python numpy scipy


【解决方案1】:

log(s/k) 正在抛出错误。即使 20/25 = 0.8,因为它们都是整数,所以 20/25 的计算结果为 0,log(0) 会抛出错误。将 s 或 k 或两者都转换为浮点数,这应该可以解决您的问题

【讨论】:

  • 您还可以将以下内容放在脚本顶部:from __future__ import division。这会将 Python 的除法语义更改为 Python 3 中使用的语义,其中不会发生整数除法,除非您使用 // 运算符。
猜你喜欢
  • 2022-07-21
  • 2022-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-04
  • 2021-08-04
  • 2020-05-06
相关资源
最近更新 更多