next up previous
Next: Stage 2 - Use Up: Monte-Carlo Class How To Previous: Monte-Carlo Class How To

Stage 1 - Make a container

Check the base code compiles and run without any problems.

Base Code

The first stage here is creating an object, or we can think of it as a container, into which we can put all the variables we need for the Monte Carlo method. For a simple vanilla European option, we shall require the following bits of data

and a function to return the value of the option.

Set up a container empty container called MonteCarlo:

class MonteCarlo
{
   public:
   // declare your stuff in here
};


and declare all of the variables listed above, using the same name for each as that stated as arguments for the function monte_carlo (this will save time).

Now move the function monte_carlo inside the MonteCarlo class, and delete all of its arguments, so that is appears as:

double monte_carlo()
{
   // calculate option price in here
};


Make sure everything is placed underneath the public access identifier for the time being. We do not need to pass the data in as an argument any longer as the data is stored inside the object, and all functions inside that object have access to that data.

Next, we can delete the declarations in the main program and replace them by a single instance of the MonteCarlo class, which we will call option. Next assign values to all of the data stored inside option.

int _tmain( int argc, _TCHAR* argv[])
{
   // declare variables
   MonteCarlo option;
   // assign values
   option.N = 1000;
   option.r = 0.05;
   // and so on...

All that is left now is to change how we access the function monte_carlo, since it is now a member of option. Also as N is now a member of option, we will need to be a little more carefull when change its value. In the main program, change the loop so that it looks like:

   for( int N=1000;N<100001;N+=1000)
   {
      // note here that the locally stored `N' is different than the `N' stored inside the container `option'
     option.N = N;
     cout « " The value of the option with " « N « " paths is " « option.monte_carlo() « endl;
   }

Now compile and run the program. Hopefully the output is identical to the output from the base code! Once you have a working program, save it as `monte-carlo-2' and move to the next stage.


next up previous
Next: Stage 2 - Use Up: Monte-Carlo Class How To Previous: Monte-Carlo Class How To
Paul Johnson 2009-04-06