1

我试图在 Python 中找到S看跌期权的价值,其中是期权的执行价格,是基础执行价格。此外,在 Black-Sholes 的函数调用中,是波动率,是支付的股息,是到期时间,是无风险利率。K-SKSsigmadeltaTr

我用 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
4

1 回答 1

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
             }
于 2017-04-10T19:13:18.120 回答