Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 类中的函数需要来自类中另一个函数的输入_Python 3.x_Function_Class_Matplotlib - Fatal编程技术网

Python 3.x 类中的函数需要来自类中另一个函数的输入

Python 3.x 类中的函数需要来自类中另一个函数的输入,python-3.x,function,class,matplotlib,Python 3.x,Function,Class,Matplotlib,我是Python新手,正在尝试创建一个包含三个函数的期权定价类:call、put和graph。调用和放置函数工作正常,但我无法理解图形函数。我希望p.append从调用函数中获取值,保持所有变量不变,除了S0等于I import numpy as np from scipy.stats import norm import matplotlib.pyplot as plt class Option(): def __init__(self, S0, K, T, r, sigma, s

我是Python新手,正在尝试创建一个包含三个函数的期权定价类:call、put和graph。调用和放置函数工作正常,但我无法理解图形函数。我希望p.append从调用函数中获取值,保持所有变量不变,除了S0等于I

import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt

class Option():

    def __init__(self, S0, K, T, r, sigma, start, stop, N):
        self.S0 = S0
        self.K = K
        self.T = T
        self.r = r
        self.sigma = sigma
        self.start = start
        self.stop = stop
        self.N = N

    def call(self):
        d1 = (np.log(self.S0/self.K) + \
        (self.r + 0.5*self.sigma**2)*self.T)/(self.sigma*np.sqrt(self.T))

        d2 = d1 - self.sigma*np.sqrt(self.T)

        price = (self.S0 * norm.cdf(d1, 0.0, 1.0) - \
        self.K * np.exp(-self.r * self.T) * norm.cdf(d2, 0.0, 1.0))

        return price

    def put(self):
        d1 = (np.log(self.S0/self.K) + \
        (self.r + 0.5*self.sigma**2)*self.T)/(self.sigma*np.sqrt(self.T))

        d2 = d1 - self.sigma*np.sqrt(self.T)

        price = (self.K * np.exp(-self.r * self.T) * norm.cdf(-d2, 0.0, 1.0) - \
        self.S0 * norm.cdf(-d1, 0.0, 1.0))

        return price

    def graphCall(self):
        S = np.linspace(self.start, self.stop, self.N)
        p = []
        for i in S:
            p.append()
        plt.plot(S, p)

x = Option(100, 50, 3, 0.05, 0.40, 100, 200, 500)
print(x.call())
x.graphCall()

您可以决定使用
self.S0
作为调用
call
put
的默认值,但也允许使用其他参数

import numpy as np
from scipy.stats import norm
import matplotlib.pyplot as plt

class Option():

    def __init__(self, S0, K, T, r, sigma, start, stop, N):
        self.S0 = S0
        self.K = K
        self.T = T
        self.r = r
        self.sigma = sigma
        self.start = start
        self.stop = stop
        self.N = N

    def call(self, s=None):
        if s is None:
            s=self.S0
        d1 = (np.log(s/self.K) + \
              (self.r + 0.5*self.sigma**2)*self.T)/(self.sigma*np.sqrt(self.T))

        d2 = d1 - self.sigma*np.sqrt(self.T)

        price = (s * norm.cdf(d1, 0.0, 1.0) - \
                 self.K * np.exp(-self.r * self.T) * norm.cdf(d2, 0.0, 1.0))

        return price

    def put(self, s=None):
        if s is None:
            s=self.S0
        d1 = (np.log(s/self.K) + \
        (self.r + 0.5*self.sigma**2)*self.T)/(self.sigma*np.sqrt(self.T))

        d2 = d1 - self.sigma*np.sqrt(self.T)

        price = (self.K * np.exp(-self.r * self.T) * norm.cdf(-d2, 0.0, 1.0) - \
                 s * norm.cdf(-d1, 0.0, 1.0))

        return price

    def graphCall(self):
        S = np.linspace(self.start, self.stop, self.N)

        plt.plot(S, self.call(S))
        plt.show()

x = Option(100, 50, 3, 0.05, 0.40, 100, 200, 500)
print(x.call())
x.graphCall()

谢谢很好用。看看你能做些什么,而不是写“谢谢”。