gpt4 book ai didi

python - 值错误 : math domain error - Black-Scholes Option Valuation

转载 作者:太空宇宙 更新时间:2023-11-04 00:48:09 24 4
gpt4 key购买 nike

我正在编写一个程序,在必要条件下使用 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) 抛出错误。即使 20/25 = 0.8,因为它们都是整数,20/25 的计算结果为 0,并且 log(0) 会抛出错误。将 s 或 k 或两者都转换为 float ,这应该可以解决您的问题

关于python - 值错误 : math domain error - Black-Scholes Option Valuation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38361148/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com