繁体   English   中英

我如何在python中调用这个函数

[英]How do I call this function in python

我在 python 中相对较新。 我正在尝试复制我在书中找到的一些代码。
我如何调用def plot_values(function) 当我尝试调用plot_values()时,我应该作为函数插入什么? 我收到错误

TypeError: plot_values() missing 1 required positional argument: 'function'

这是我尝试运行的代码:

import math
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
from scipy.integrate import quad

mpl.rcParams['font.family'] = 'serif'

#
# Helper Functions
#


def dN(x):
    ''' Probability density function of standard normal random variable x. '''
    return math.exp(-0.5 * x ** 2) / math.sqrt(2 * math.pi)


def N(d):
    ''' Cumulative density function of standard normal random variable x. '''
    return quad(lambda x: dN(x), -20, d, limit=50)[0]


def d1f(St, K, t, T, r, sigma):
    ''' Black-Scholes-Merton d1 function.
        Parameters see e.g. BSM_Call function. '''
    d1 = (math.log(St / K) + (r + (0.5 * sigma ** 2)) * (T - t)) / (sigma * math.sqrt(T - t))
    return d1

#
# Valuation Functions
#


def BSM_call_value(St, K, t, T, r, sigma):
    ''' Calculates Black-Scholes-Merton European call option value.
    Parameters
    ==========
    St : float
        stock/index level at time t
    K : float
        strike price
    t : float
        valuation date
    T : float
        date of maturity/time-to-maturity if t = 0; T > t
    r : float
        constant, risk-less short rate
    sigma : float
        volatility
    Returns
    =======
    call : float
        European call present value at t
    '''
    d1 = d1f(St, K, t, T, r, sigma)
    d2 = d1 - sigma * math.sqrt(T - t)
    call = St * N(d1) - math.exp(-r * (T - t)) * K * N(d2)
    print(call)
    return call


def BSM_put_value(St, K, t, T, r, sigma):
    ''' Calculates Black-Scholes-Merton European put option value.
    Parameters
    ==========
    St : float
        stock/index level at time t
    K : float
        strike price
    t : float
        valuation date
    T : float
        date of maturity/time-to-maturity if t = 0; T > t
    r : float
        constant, risk-less short rate
    sigma : float
        volatility
    Returns
    =======
    put : float
        European put present value at t
    '''
    put = (BSM_call_value(St, K, t, T, r, sigma) -
           St + math.exp(-r * (T - t)) * K)
    return put


#
# Plotting European Option Values
#


def plot_values(function):
    ''' Plots European option values for different parameters c.p. '''
    plt.figure(figsize=(10, 8.3))
    points = 100
    #
    # Model Parameters
    #
    St = 100.0  # index level
    K = 100.0  # option strike
    t = 0.0  # valuation date
    T = 1.0  # maturity date
    r = 0.05  # risk-less short rate
    sigma = 0.2  # volatility

    # C(K) plot
    plt.subplot(221)
    klist = np.linspace(80, 120, points)
    vlist = [function(St, K, t, T, r, sigma) for K in klist]
    plt.plot(klist, vlist)
    plt.xlabel('strike $K$')
    plt.ylabel('present value')

    # C(T) plot
    plt.subplot(222)
    tlist = np.linspace(0.0001, 1, points)
    vlist = [function(St, K, t, T, r, sigma) for T in tlist]
    plt.plot(tlist, vlist)
    plt.xlabel('maturity $T$')

    # C(r) plot
    plt.subplot(223)
    rlist = np.linspace(0, 0.1, points)
    vlist = [function(St, K, t, T, r, sigma) for r in rlist]
    plt.plot(tlist, vlist)
    plt.xlabel('short rate $r$')
    plt.ylabel('present value')
    plt.axis('tight')

    # C(sigma) plot
    plt.subplot(224)
    slist = np.linspace(0.01, 0.5, points)
    vlist = [function(St, K, t, T, r, sigma) for sigma in slist]
    plt.plot(slist, vlist)
    plt.xlabel('volatility $\sigma$')
    plt.tight_layout()

St = 200.00
K = 210.00
t = 1/252
T = 1.00
r = 0.05
sigma = 0.10
BSM_call_value(St, K, t, T, r, sigma)
plot_values()

您的错误TypeError: plot_values() missing 1 required positional argument: 'function'为您提供所需的所有信息。

plot_values() missing 1 required positional argument意味着您必须向函数 plot_values 发送另一个参数。 缺少的参数是一个名为function的位置参数(位置参数,因为您不必通过关键字传递它。例如fonction= )。

你必须明白,在 Python 中,一切都是对象。 你可以做:

>>> print('a')
a
>>> my_print = print
>>> my_print('a')
a

这项工作是因为您的变量my_print现在是具有另一个名称的函数 print 。 您可以像发送任何其他变量一样将函数发送给函数。

print是一个函数,但您可以像操作任何其他变量一样操作此函数。 看:

>>> print(print)
<built-in function print>

在这里,我给函数打印了与位置参数相同的函数,没有括号。 因此它的行为与任何其他类型一样,它打印函数 str(print) 的返回值,该函数尝试将函数转换为字符串。 由于这是不可能的,它只返回变量的类型和名称。

为了解决您的问题,您应该尝试:

plot_values(d1f)  # 8.057171421323943

或其他尊重此定义的函数:

def any_fun(St, K, t, T, r, sigma):

其中包括d1fBSM_call_valueBSM_put_value

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM