0

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

user3654852
  • 695
  • 1
  • 5
  • 10
  • 1
    Do you get an error when you run the code? – Thomas Kerby Apr 02 '20 at 14:23
  • Thanks @PNX. Even before running the code, I get pylint warnings ("No value for argument "X" in unbound method call", for all the arguments. When I run the code, I get the following error on calling mcPaths: "TypeError: __init__() missing 7 required positional arguments: 'pc', 'type', 'So', 'K', 'T', 'Vol', and 'r'" – user3654852 Apr 02 '20 at 15:00
  • 1
    You're trying to create a new `OptParam` object, but not passing it all of the parameters that it needs. Could you explain what you're trying to achieve with this line: `OptParam.__init__(self)`? – D Malan Apr 02 '20 at 15:17
  • Thanks @DelenaMalan. I'm trying to find a way to pass those arguments...and I'm struggling with the right way to do it. I thought using the line you mentioned, would be a way to pass variables from OptParam into mcPaths....but I was wrong.... – user3654852 Apr 02 '20 at 15:27

1 Answers1

1

If I understand you correctly, in the code below, you want to be able to the values you used to create myopt from within the __init__ function of mcPaths when you create mySims:

myopt = OptParam('c','eur',100,100,1,0.75,0.02)
mySims = mcPaths(10,50)

You could add a parameter to the __init__ function mcPaths for the OptParam you want to be able to access:

class mcPaths(OptParam):
    def __init__(self, numPaths=None, numSteps=None, opt_param_value=None):

When you create mySims, you can then pass myopt as a parameter to mcPaths:

mySims = mcPaths(10, 50, myopt)

Then you would be able to get those values from opt_param_value from within __init__:

class mcPaths(OptParam):
    def __init__(self, numPaths=None, numSteps=None, opt_param_value=None):
        self.r = opt_param_value.r
D Malan
  • 8,902
  • 3
  • 22
  • 41
  • Thanks @Delena. I modified as you suggested, now I'm getting "non-default argument follows default argument (, line 30)".... – user3654852 Apr 02 '20 at 16:02
  • Ah, you can change `opt_param_value` to `opt_param_value=None`. – D Malan Apr 02 '20 at 16:10
  • it's one error after another...now it's "__init__() takes from 1 to 3 positional arguments but 4 were given"....I'll research a little more.... – user3654852 Apr 02 '20 at 16:55
  • How did you call it? Sounds like you passed an extra argument? – D Malan Apr 02 '20 at 18:49
  • Thanks for following up - I updated my original Q above so you can see the entire code (you'll see the detail if you scroll down in Q above). I think it's somehow counting "self" as an additional argument? – user3654852 Apr 02 '20 at 19:07
  • I ran your code and it didn't give me any errors. Are you running this in a REPL? If so, you either need to exit and restart the REPL or you need to [reload](https://stackoverflow.com/questions/437589/how-do-i-unload-reload-a-module) the module that you're importing. – D Malan Apr 02 '20 at 20:09
  • Hmm...I'm using VS Code......let me try again....and thanks for checking! – user3654852 Apr 02 '20 at 20:21
  • This seems to be working! I'm getting some other errors....but those are related to "index being out of bound etc" that I know how to fix. I'll test and get back. Rather weird that VSCode needs to be restarted to "forget" previous definitions/variables etc. Thanks again! – user3654852 Apr 02 '20 at 20:39
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/210874/discussion-between-delena-malan-and-user3654852). – D Malan Apr 03 '20 at 13:33