I'm trying to create two classes for pricing an option. The first class captures the parameters of the option and the second one generates path. I need to pass some variables (starting point of the underlying volatility) from the first class to the second. There's something wrong in the way I'm referring to the first class however. I would appreciate some guidance.
Here's the code (I'm using VSCode (1.43.2), and 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)
I'm doing something wrong in the third line from the top of the class mcPaths. I am not quite sure what the right way to reference the first class would be.
Before running the code, I get the following pylint warning:
No value for argument "X" in unbound method call" for each of the arguments defined in class OptParam.
Pylint highlights the optParam from the line under the mcPaths class, indicating that's the source of the error.
When I run the code as follows:
myopt = OptParam('c','eur',100,100,1,0.75,0.02)
mySims = mcPaths(10,50)
The first line runs fine. The second line, mySims=mcPaths(10,50), returns:
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'
---------------code edits based on discussions with 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)
To call:
myopt=OptParam('c','eur',100,100,1,0.75,0.02)
mysim = mcPaths(10,50,myopt)
error: 11 mysim = mcPaths(10,50,myopt) init() takes from 1 to 3 positional arguments but 4 were given