【问题标题】:Finding Where Put Option Equals K-S, using the Black-Sholes, throws Exceptions. Why?使用 Black-Scholes 查找看跌期权等于 K-S 的位置会引发异常。为什么?
【发布时间】:2017-04-08 23:07:35
【问题描述】:

我试图找到 S 的值,其中看跌期权在 Python 中等于 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

【问题讨论】:

  • 作为 StackOverflow 的新手,您可能需要注意一个值,编辑历史正在被存储和重新读取 (forever ),所以宁可小心谨慎不要发布任何敏感数据(密码、PIN、访问令牌等)在stackoverflow.com/posts/43301278/revisions 中仍然可见无论如何,享受这个伟大的知识社区并随时成为下一个积极贡献的会员。

标签: python quantitative-finance computational-finance


【解决方案1】:

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

【讨论】:

  • 感谢您的回复!当我更改时,我收到以下错误:
  • File "C:\Users\user\Anaconda3\lib\site-packages\scipy\optimize\slsqp.py", line 311, in &lt;listcomp&gt; meq = sum(map(len, [atleast_1d(c['fun'](x, *c['args'])) for c in cons['eq']])) TypeError: con() takes 2 positional arguments but 3 were given
  • 如果你仔细阅读slsqp.py + 解码调用接口,它使用间接组装的参数来满足规范,用户提供的约束功能(@987654324 @) 应具有与上面定义的不同的调用接口。从slsqp.py 源中可以看出,scipy.optimize.minimize() 期望约束函数始终接收 x,因为它是第一个参数(从 scipy 自动传递内部性),接下来,迭代,无论多少参数,通过单个“包装”元组间接定义 cons['args'] 所以def con( x, S, ar):
猜你喜欢
  • 2022-07-05
  • 1970-01-01
  • 2022-07-21
  • 1970-01-01
  • 2016-11-16
  • 2013-01-22
  • 1970-01-01
  • 2018-12-20
  • 1970-01-01
相关资源
最近更新 更多