Monte-Carlo API

46.1. Overview

Monte Carlo methods are a class of computational algorithms that rely on repeated random sampling to compute their results. Monte Carlo methods are often used when simulating physical and mathematical systems. Because of their reliance on repeated computation and random or pseudo-random numbers, Monte Carlo methods are most suited to calculation by a computer. Monte Carlo methods tend to be used when it is infeasible or impossible to compute an exact result with a deterministic algorithm.

The goal of the ProActive Monte-Carlo API is to provide and easy to use API for running Monte-Carlo simulations in a distributed environment.

The main features are:

  • Monte-Carlo simulations or other tasks can be run on remote workers. Tasks are defined by implementing an interface.

  • It integratesSSJ, a distributed random generator written by Pierre l'Ecuyer. Each worker has its own independant stream of random numbers and there is a guaranty that two different workers, will generate sequences that won't overlap until a certain number of experiences (2¹²⁷).

  • It is based on ProActive Master-Worker API as the underlying framework (see ProActive documentation, Master/Worker for more info).

  • Deployment of the workers infrastructure is done through GCM descriptors (see ProActive documentation, GCMDeployment for more info).

  • A small set of processes, taken from the financial field, are included as example.

46.2. API

The Monte-Carlo APi is located in the following package:

org.objectweb.proactive.extra.montecarlo.example

46.2.1. Main Class

The entry point of the Monte-Carlo API is the PAMonteCarlo class. Its main constructor allows you to deploy the monte-carlo framework, using ProActive GCM deployment framework. The first constructor is the URL of the GCMApplication file which will be used. The next argument is the virtual node name corresponding to the Workers infrastructure of machines. The third argument (optional) is a virtual node name that will correspond to a remote deployment of the master. The final argument is to allow changing the default Random Number Generator from the SSJ package.

/**
* Initialize the Monte-Carlo toolkit by giving a descriptor and two virtual node names.
* Workers will be instanciated on resources created by the first one.
* The second one should create one single resource. The master will be deployed on that resource
* The last parameter is a Class object holding the definition of the RandomStream to use.
* By default, the generator in use is the MRG32k3a one.
*
* @param descriptorURL url of a descriptor
* @param masterVNName virtual node name corresponding to the master
* @param workersVNName virtual node name corresponding to workers
* @param randomStreamClass Random Number Generator class that workers will be using
* @throws ProActiveException
*/
public PAMonteCarlo(URL descriptorURL, String workersVNName, String masterVNName, Class
 randomStreamClass)
       throws ProActiveException

The run method from the PAMonteCarlo class, is the starting point of the computation. It runs a single task, called a top-level engine task. This task, while running, will most likely generate new children subtasks. The different tasks existing in the framework are described inSection 46.2.2, “Tasks”.

/**
* Runs the simulation by giving the definition of a top-level task.
* This task is not directly a Monte Carlo simulation, but should rather contain the code of your
 general algorithm.
* The top-level task can submit nested general tasks or nested Monte-Carlo simulations (Experience
 Set).
* @param toplevelTask top-level task
* @return the result of the top-level task
* @throws TaskException if an exception occured during the execution of the top-level task.
*/
public T run(EngineTask<T> toplevelTask) throws TaskException

Finally, the terminate method will shut down the framework and every remote JVM created during the initialization phase.

/**
* Terminates the Monte-Carlo toolkit and free created resources.
*/
public void terminate()

46.2.2. Tasks

This section describes the different types of tasks and their use

46.2.2.1. Engine Task

An Engine Task is a general-purpose task, it is used when any computation which doesn't include runnig Monte-Carlo simulation needs to be done. The top-level task, submitted to the PAMonteCarlo class is an engine task. An engine task can, through the access to two interfaces (the Executor and the Simulator), spawn children engine tasks or simulation sets.

public interface EngineTask<T extends Serializable> extends Serializable {

   /**
    * Defines a general purpose task which can be run by the Monte-Carlo framework
    *
    * @param simulator gives the possibility to schedule children simulation sets
    * @param executor  gives the possibility to schedule children engine tasks
    * @return the result of this task
    */
   public T run(Simulator simulator, Executor executor);

}

46.2.2.2. Simuation Set

A Simulation Set is a specific task for running Monte-Carlo simulations. It is given a random number generator and can use it to generate random double numbers which are by default in the uniform distribution. Classes from the SSJ package can be used to convert this distribution to any wanted one. Unlike the Engine Task, the Simulation Set cannot spawn other tasks and is therefore a "terminal task".

public interface SimulationSet<T extends Serializable> extends Serializable {

   /**
    * Defines a Monte-Carlo set of successive experiences, a Random generator is given as a
 parameter
    *
    * A list of double values is expected as output, result of the successive experiences.
    * These experiences can be independant or correlated, this choice is left to the user inside
 the implementation of this method.
    *
    * @param rng random number generator
    * @return a list of double values
    */
   T simulate(final RandomStream rng);
}

46.2.2.3. Simuation Set Post Process

A simulation set task will return as output a huge number of values (e.g. arrays of size 10000 or more). In general, these results are not the result of the general algorithm which uses the Monte-Carlo method. Therefore, a post-treatment needs to be done on this big array. Of course, this post-treatment could be done by an engine task that spawned the simulation set task, but in that case, the big array would be tranferred by the network from the Worker which handles the simulation set to the worker which handles the parent engine task. The Simulation Set Post Process avoids this tranfer, and allows to do some computations on the same machine which generated the Monte-Carlo simulations.

public interface SimulationSetPostProcess<T extends Serializable, R extends Serializable> {

   /**
    * Defines a post-processing of results received from a simulation set
    * @param experiencesResults results receive from a Simulation Set task
    * @return the result of the post processing
    */
   R postprocess(T experiencesResults);
}

46.2.3. Examples

46.2.3.1. Basic Simulations Processes

Some basic Simulation Sets are provided as an example. These basic processes are widely used in the financial risk management theory. An example of such a process is the Geometric Brownian Motion:

/**
* Simulating geometric Brownian motion. This equation is the exact solution of the geometrix
 brownian motion SDE.
* @param s0 Initial value at t=0 of geometric Brownian
* @param mu Drift term
* @param sigma Volatility
* @param t time
* @param N number of experiences
*/
public GeometricBrownianMotion(double s0, double mu, double sigma, double t, int N)

46.2.3.2. Basic Example Applications

Two basic example applications are provided. The first one computes PI using the Monte-Carlo method (it's only a theoretic approach as the method is very unefficient to compute PI at a good precision). The second example is an European Option Pricing from the financial risk analysis world. Both examples can be found in the subpackage example of the monte-carlo package. A script exists to launch the European Option and is located at PROACTIVE/examples/montecarlo