我试图创建两个类来为期权定价。第一个类捕获选项的参数,第二个类生成路径。我需要将一些变量(基本波动率的起点)从第一类传递到第二类。然而,我所指的头等舱有些不对劲。我希望得到一些指导
下面是代码(我使用的是VSCode(1.43.2)和Python(3.7.3)):
class bsEuroParam(object):
# model parameters of a European option
# pc = put/call
#type = vanila, lookback, asian
def __init__(self,pc,type,So,K,T,Vol,r):
self.pc = pc
self.type = type
self.So = So
self.K = K
self.T = T
self.Vol = Vol
self.r = r
class mcPaths(OptParam):
def __init__(self,numPaths=None,numSteps=None):
***OptParam.__init__(self)***
self.numPaths = numPaths
self.numSteps = numSteps
def paths(self):
dt=self.So/self.numSteps
S=np.zeros((self.numSteps,self.numPaths+1))
S[0]=self.So
for t in range(1,self.numSteps+1):
S[t]=S[t-1]*np.exp((self.r - 0.5 * self.Vol **2) * dt + self.Vol * math.sqrt(dt)*npr.standard_normal(self.numPaths+1))
self.results = np.array(S)
我在班上第一行的第三行做错了什么mcPaths
。我不太确定引用第一个类的正确方式是什么
在运行代码之前,我得到以下pylint
警告:
No value for argument "X" in unbound method call" for each of the arguments defined in class OptParam.
Pylint突出显示mcPaths
类下行中的optParam
,表明这是错误的来源
当我按如下方式运行代码时:
myopt = OptParam('c','eur',100,100,1,0.75,0.02)
mySims = mcPaths(10,50)
第一行运行良好。第二行mySims=mcPaths(10,50)
返回:
TypeError Traceback (most recent call last)
filepath\pytest12.py in
----> 9 mySims = mcPaths(10,50)
filepath\pytest12.py in __init__(self, numPaths, numSteps)
30 class mcPaths(OptParam):
31 def __init__(self,numPaths=None,numSteps=None):
---> 32 OptParam.__init__(self)
33 self.numPaths = numPaths
34 self.numSteps = numSteps
TypeError: __init__() missing 7 required positional arguments: 'pc', 'type', 'So', 'K', 'T', 'Vol', and 'r'
------根据与Delena的讨论编辑代码
Class OptParam(object):
# model parameters of a European option
# pc = put/call
#type = vanila, lookback, asian
def __init__(self,pc,type,So,K,T,Vol,r):
self.pc = pc
self.type = type
self.So = So
self.K = K
self.T = T
self.Vol = Vol
self.r = r
#myopt=bsEuroParam('c','eur',100,100,1,0.75,0.02)
class mcPaths(OptParam):
def __init__(self,numPaths=None,numSteps=None,optPrm_val=None):
#OptParam.__init__(self)
self.numPaths = numPaths
self.numSteps = numSteps
self.So = optPrm_val.So
self.Vol = optPrm_val.Vol
self.r = optPrm_val.r
def paths(self):
dt=self.So/self.numSteps
S=np.zeros((self.numSteps,self.numPaths+1))
S[0]=self.So
for t in range(1,self.numSteps+1):
S[t]=S[t-1]*np.exp((self.r - 0.5 * self.Vol **2) * dt + self.Vol * math.sqrt(dt)*npr.standard_normal(self.numPaths+1))
self.results = np.array(S)
致电:
myopt=OptParam('c','eur',100,100,1,0.75,0.02)
mysim = mcPaths(10,50,myopt)
错误: 11 mysim=mcPaths(10,50,myopt) init()接受1到3个位置参数,但给出了4个
如果我理解正确,在下面的代码中,您希望能够在创建
mySims
时从mcPaths
的__init__
函数中创建myopt
时使用的值:您可以为要访问的
OptParam
向__init__
函数mcPaths
添加一个参数:创建
mySims
时,可以将myopt
作为参数传递给mcPaths
:然后您将能够从
opt_param_value
中从__init__
中获取这些值:相关问题 更多 >
编程相关推荐