使用BlackSholes,找出看跌期权等于KS的位置,抛出异常。为什么?

2024-04-19 07:42:56 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图找到S的值,在Python中,看跌期权等于K-S,其中K是期权的执行价格,S是潜在的执行价格。另外,在Black Sholes的函数调用中,sigma是波动率,delta是支付的股息,T是到期时间,r是无风险利率。你知道吗

我用K=75,r=0.05,T=1/12,sigma=0.35来测试它。你知道吗

此外,我知道我的定价为黑鞋工程,因为我已经用在我以前的项目,这是可能的,修改版本的语法错误。你知道吗

我试过使用scipy.optimize,但总是出错。你知道吗

有没有别的方法,我应该用?
如果是,怎么做?你知道吗

import numpy as np
import scipy.stats as ss
import time
import pylab as pl
import matplotlib.pyplot as plt
from prettytable import PrettyTable
import datetime
import scipy.optimize

from scipy.optimize import newton


# initial stock price
type = 'C'

def d1(S0, K, r, sigma, T, delta):
    return (np.log(float(S0) / K) + (r - delta + sigma ** 2 / 2) * T) / (sigma * np.sqrt(T))


def d2(S0, K, r, sigma, T,delta):
    return (d1(S0, K, r, sigma, T,delta)-sigma * np.sqrt(T))


def blackScholes(type, S0, K, r, sigma, T, delta):
    if type == "C":
        return S0 * np.exp(-delta * T)* ss.norm.cdf(d1(S0, K, r, sigma, T, delta)) - K * np.exp(-r * T) * ss.norm.cdf(d2(S0, K, r, sigma, T, delta))
    else:
        return K * np.exp(-r * T) * ss.norm.cdf(-d2(S0, K, r, sigma, T, delta)) - S0 * ss.norm.cdf(-d1(S0, K, r, sigma, T, delta))

# args is args = (type,K,r,sigma,T,delta)
# Modifying Black-Sholes function arguments for multivariate optimization
def d1Modified(S, args):

        type = args[0]
        K = args[1]
        r = args[2]
        sigma = args[3]
        T = args[4]
        delta = args[5]
        return (np.log(float(S) / K) + (r - delta + sigma ** 2 / 2) * T) / (sigma * np.sqrt(T))

def d2Modified(S, args):
    type = args[0]
    K = args[1]
    r = args[2]
    sigma = args[3]
    T = args[4]
    delta = args[5]
    return (d1Modified(S,args) - sigma * np.sqrt(T))

def blackScholesModified(S, args):
    type = args[0]
    print("print args")
    print(args)
    K = args[1]
    r = args[2]
    sigma = args[3]
    T = args[4]
    delta = args[5]
    if type == "C":
        return S * np.exp(-delta * T) * ss.norm.cdf(d1Modified(S, args)) - K * np.exp(
            -r * T) * ss.norm.cdf(d2Modified(S,args))
    else:
        return K * np.exp(-r * T) * ss.norm.cdf(-d2Modified(S, args)) - S * ss.norm.cdf(
               -d1Modified(S,args))


print("Pricing a European Put Option")

p = "P"

#Note: at is the tuple with the inputs into the black-sholes equation
# Where p is a string indicating a put option, K is the strike price
# r is the risk free rate of interest, sigma is volatility, T is time to
# expiration and delta is dividends
ar = (p,K,r,sigma,T,delta)


putOptionPriceList = []
for i in range(1,74):
    stockPriceList.append(i)

for x in stockPriceList:
    price = blackScholes("P",x,K,r,sigma,T,delta)
    print(price)
    putOptionPriceList.append(price)



# Constraints for multivariate optimization where price = K-S
def con(S,ar):
    k= 75
    return blackScholesModified(S, ar) -(k-S)


cons = {'type':'eq', 'fun': con(S,ar)}


sPrice = scipy.optimize.minimize(blackScholesModified, 0.1, args=ar, constraints=cons)

print("Value sought")
print(sPrice)

我不断得到以下错误:

Traceback (most recent call last):

    sPrice = scipy.optimize.minimize(blackScholesModified, 0.1, args=ar, constraints=cons)
  File "C:\Users\user\Anaconda3\lib\site-packages\scipy\optimize\_minimize.py", line 458, in minimize
    constraints, callback=callback, **options)
  File "C:\Users\user\Anaconda3\lib\site-packages\scipy\optimize\slsqp.py", line 311, in _minimize_slsqp
    meq = sum(map(len, [atleast_1d(c['fun'](x, *c['args'])) for c in cons['eq']]))
  File "C:\Users\user\Anaconda3\lib\site-packages\scipy\optimize\slsqp.py", line 311, in <listcomp>
    meq = sum(map(len, [atleast_1d(c['fun'](x, *c['args'])) for c in cons['eq']]))
TypeError: 'numpy.float64' object is not callable

Tags: importnormreturnisdeftypenpargs
1条回答
网友
1楼 · 发布于 2024-04-19 07:42:56

我的猜测是要仔细遵循0.18指定的调用接口:

   cons = { 'type': 'eq',
            'fun':   con,       # con( S, ar ) here'd yield float64, not callable
            'args':  ( S, ar )  #    ( ^__^__)_ passed to a fun == con()-callable
             }

相关问题 更多 >