找到看跌期权等于K-S的位置,使用Black-Sholes,抛出异常。为什么?

时间:2017-04-08 23:07:36

标签: python quantitative-finance computational-finance

我试图在Python中找到其中Put选项等于S K-S 的值,其中K是该选项的执行价格而S是潜在的执行价格。此外,在Black-Sholes的函数调用中,sigma是波动率,delta是支付的股息,T是到期时间,r是风险 - 免费利率。

我正在测试它,K = 75,r = 0.05,T = 1/12,sigma = 0.35。

此外,我知道我对Black-Sholes的定价是有效的,因为我在之前的项目中使用它,可能是语法错误的修改版本。

我尝试过使用 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

1 个答案:

答案 0 :(得分:0)

我的猜测是要小心地遵循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
             }