【问题标题】:Function in a class requires inputs from another function in the class类中的函数需要类中另一个函数的输入
【发布时间】:2018-08-02 23:42:56
【问题描述】:

我对 Python 很陌生,正在尝试创建一个具有三个功能的期权定价类:看涨期权、看跌期权和图表。调用和放置函数工作正常,但我无法弄清楚图形函数。我希望 p.append 从调用函数中获取值,保持所有变量不变,除了等于 i 的 S0。

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()

【问题讨论】:

    标签: python-3.x function class matplotlib


    【解决方案1】:

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

    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()
    

    【讨论】:

    • 谢谢!完美运行。
    • 请参阅someone answers,了解您可以做什么而不是写“谢谢”。
    猜你喜欢
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多