Matlab Parallel Toolbox: How can you run ThetaML Monte Carlo models in parallel?

· Programming
Authors

Often, it is required to run multiple similar simulation models, e.g. for computation of Greeks like Delta, Gamma, Vega, etc. Another reason for multiple calls is that one wants to estimate the accuracy of a pricing. Using the Matlab Parallel Toolbox, this can be accelerated substantially.

Pricing an American put option in Parallel

Using the Matlab Parallel toolbox, it is easy to create an M-file which is executed in parallel. The following code shows, how to execute a ThetaML file of an American Put option valuation in parallel (tested on Matlab 2011b):

% initialize Java Paths required for Theta Compiler
thetaclasspath
% set workspace for each Matlab Worker
spmd
  theta_set_workspace('C:\Users\master\ThetaSuite\workspace');
end

% create compiler object with ThetaML configuration data
thc = theta_compiler('EquityOptions.thetaml');

% initialize result data
result = zeros(10,1);

% compute in parallel for each seed
parfor iterSeeds = 1:80
  % get configuration data and set "seed" of random number generator
  conf_base = thc.getConf();
  conf = conf_base;
  conf.eval.MC.random_seed.value = iterSeeds;
  thc.setConf(conf);

  % run ThetaML and save result
  res = thc.run();
  result(iterSeeds)= mean(res.output.P.value);
end

Use “SPMD” and “PARFOR” for distribution of tasks

The command “spmd” (single program multiple data) makes sure that the following code is executed on each worker. I.e. the workspace is set on each worker. Then, “parfor” is the same as “for” except that each loop iteration is computed in parallel. Note that each iteration should require about the same CPU time for a good distribution among the workers since this scheduling is conducted a priori (not knowing what the actual required time is).

Benchmarking the Code

Starting this in the usual command line without parallel executing we get

>> tic; runner; disp(toc)
 40.2520

which means the execution of the defined 80 Monte Carlo pricings with 10,000 paths each took 40 seconds. Now, starting the matlabcode in parallel with 4 workers on 4 CPU kernels is done by “matlabpool 4”, e.g.

 >> matlabpool 4
 Starting matlabpool using the 'local' configuration ... connected to 4 labs.
 >> tic; runner; disp(toc)
15.2858

the time required for execution drops to 15 seconds. In total, we got a speed-up factor of about 3x. The reason that the speed-up is not linear (4x for 4 workers) is that the initialization of the java code is done several time if executed in parallel. Also, the Matlab optimizes the m-code which again takes time. Consequently, a second evaluation is quicker:

>> tic; runner; disp(toc)
 9.1811

Now, the speed-up factor compared with a serial execution is more than 4x.

Conclusion

We went through the parallization of ThetaML pricings. Using the Matlab Parallel toolbox, significant seed-ups can be achieved.


		

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s