changes to build script to increase java heap memory
[IRC.git] / Robust / src / Benchmarks / Scheduling / JGFMonteCarlo / MonteCarloPath.java
1 /** Banboo Version  **/
2
3 /**************************************************************************
4  *                                                                         *
5  *         Java Grande Forum Benchmark Suite - Thread Version 1.0          *
6  *                                                                         *
7  *                            produced by                                  *
8  *                                                                         *
9  *                  Java Grande Benchmarking Project                       *
10  *                                                                         *
11  *                                at                                       *
12  *                                                                         *
13  *                Edinburgh Parallel Computing Centre                      *
14  *                                                                         *
15  *                email: epcc-javagrande@epcc.ed.ac.uk                     *
16  *                                                                         *
17  *      Original version of this code by Hon Yau (hwyau@epcc.ed.ac.uk)     *
18  *                                                                         *
19  *      This version copyright (c) The University of Edinburgh, 2001.      *
20  *                         All rights reserved.                            *
21  *                                                                         *
22  **************************************************************************/
23
24 /**
25  * Class representing the paths generated by the Monte Carlo engine.
26  *
27  * <p>To do list:
28  * <ol>
29  *   <li><code>double[] pathDate</code> is not simulated.</li>
30  * </ol>
31  *
32  * @author H W Yau
33  * @version $Revision: 1.1 $ $Date: 2008/08/18 22:22:20 $
34  */
35 public class MonteCarloPath extends PathId {
36
37     //------------------------------------------------------------------------
38     // Instance variables.
39     //------------------------------------------------------------------------
40     /**
41      * Random fluctuations generated as a series of random numbers with
42      * given distribution.
43      */
44     private float[] fluctuations;
45     /**
46      * The path values from which the random fluctuations are used to update.
47      */
48     private float[] pathValue;
49     /**
50      * Integer flag for determining how the return was calculated, when
51      * used to calculate the mean drift and volatility parameters.
52      */
53     private int returnDefinition;
54     /**
55      * Value for the mean drift, for use in the generation of the random path.
56      */
57     private float expectedReturnRate;
58     /**
59      * Value for the volatility, for use in the generation of the random path.
60      */
61     private float volatility;
62     /**
63      * Number of time steps for which the simulation should act over.
64      */
65     private int nTimeSteps;
66     /**
67      * The starting value for of the security.
68      */
69     private float pathStartValue;
70     //------------------------------------------------------------------------
71     // Constructors.
72     //------------------------------------------------------------------------
73     /**
74      * Default constructor.  Needed by the HPT library to start create
75      * new instances of this class.  The instance variables for this should
76      * then be initialised with the <code>setInitAllTasks()</code> method.
77      */
78     public MonteCarloPath() {
79         super();
80         this.expectedReturnRate = (float)0.0;
81         this.volatility = (float)0.0;
82         this.pathStartValue = (float)0.0;
83         this.returnDefinition=1;
84         this.nTimeSteps=0;
85     }
86
87     /**
88      * Constructor, using the <code>ReturnPath</code> object to initialise
89      * the necessary instance variables.
90      *
91      * @param returnPath Object used to define the instance variables in
92      *                   this object.
93      * @param nTimeSteps The number of time steps for which to generate the
94      *                   random path.
95      * @exception DemoException Thrown if there is a problem initialising the
96      *                          object's instance variables.
97      */
98     public MonteCarloPath(ReturnPath returnPath, int nTimeSteps) {
99         /**
100          * These instance variables are members of PathId class.
101          */
102         this.expectedReturnRate = (float)0.0;
103         this.volatility = (float)0.0;
104         this.pathStartValue = (float)0.0;
105         this.returnDefinition=1;
106         
107         copyInstanceVariables(returnPath);
108         this.nTimeSteps = nTimeSteps;
109         this.pathValue = new float[nTimeSteps];
110         this.fluctuations = new float[nTimeSteps];
111     }
112     /**
113      * Constructor, where the <code>PathId</code> objects is used to ease
114      * the number of instance variables to pass in.
115      *
116      * @param pathId Object used to define the identity of this Path.
117      * @param returnDefinition How the statistic variables were defined,
118      *                         according to the definitions in
119      *                         <code>ReturnPath</code>'s two class variables
120      *                         <code>COMPOUNDED</code> and
121      *                         <code>NONCOMPOUNDED</code>.
122      * @param expectedReturnRate The measured expected return rate for which to generate.
123      * @param volatility The measured volatility for which to generate.
124      * @param nTimeSteps The number of time steps for which to generate.
125      * @exception DemoException Thrown if there is a problem initialising the
126      *                          object's instance variables.
127      */
128     public MonteCarloPath(PathId pathId, int returnDefinition, float expectedReturnRate, 
129             float volatility, int nTimeSteps) {
130         /**
131          * These instance variables are members of PathId class.
132          * Invoking with this particular signature should point to the
133          * definition in the PathId class.
134          */
135         this.pathStartValue = (float)0.0;
136             
137         copyInstanceVariables(pathId);
138         this.returnDefinition   = returnDefinition;
139         this.expectedReturnRate = expectedReturnRate;
140         this.volatility         = volatility;
141         this.nTimeSteps         = nTimeSteps;
142         this.pathValue          = new float[nTimeSteps];
143         this.fluctuations       = new float[nTimeSteps];
144     }
145     /**
146      * Constructor, for when the user wishes to define each of the instance
147      * variables individually.
148      *
149      * @param name The name of the security which this Monte Carlo path
150      *             should represent.
151      * @param startDate The date when the path starts, in 'YYYYMMDD' format.
152      * @param endDate The date when the path ends, in 'YYYYMMDD' format.
153      * @param dTime The interval in the data between successive data points
154      *              in the generated path.
155      * @param returnDefinition How the statistic variables were defined,
156      *                         according to the definitions in
157      *                         <code>ReturnPath</code>'s two class variables
158      *                         <code>COMPOUNDED</code> and
159      *                         <code>NONCOMPOUNDED</code>.
160      * @param expectedReturnRate The measured mean drift for which to generate.
161      * @param volatility The measured volatility for which to generate.
162      * @param nTimeSteps The number of time steps for which to generate.
163      */
164     public MonteCarloPath(String name, int startDate, int endDate, float dTime, 
165             int returnDefinition, float expectedReturnRate, float volatility, 
166             int nTimeSteps) {
167         /**
168          * These instance variables are members of PathId class.
169          */
170         set_name(name);
171         set_startDate(startDate);
172         set_endDate(endDate);
173         set_dTime(dTime);
174         this.returnDefinition   = returnDefinition;
175         this.expectedReturnRate = expectedReturnRate;
176         this.volatility         = volatility;
177         this.nTimeSteps         = nTimeSteps;
178         this.pathValue          = new float[nTimeSteps];
179         this.fluctuations       = new float[nTimeSteps];
180     }
181
182     //------------------------------------------------------------------------
183     // Methods.
184     //------------------------------------------------------------------------
185     //------------------------------------------------------------------------
186     // Accessor methods for class MonteCarloPath.
187     // Generated by 'makeJavaAccessor.pl' script.  HWY.  20th January 1999.
188     //------------------------------------------------------------------------
189     /**
190      * Accessor method for private instance variable <code>fluctuations</code>.
191      *
192      * @return Value of instance variable <code>fluctuations</code>.
193      * @exception DemoException thrown if instance variable <code>fluctuations</code> 
194      * is undefined.
195      */
196     public float[] get_fluctuations() {
197         return(this.fluctuations);
198     }
199     /**
200      * Set method for private instance variable <code>fluctuations</code>.
201      *
202      * @param fluctuations the value to set for the instance variable 
203      * <code>fluctuations</code>.
204      */
205     public void set_fluctuations(float[] fluctuations) {
206         this.fluctuations = fluctuations;
207     }
208     /**
209      * Accessor method for private instance variable <code>pathValue</code>.
210      *
211      * @return Value of instance variable <code>pathValue</code>.
212      * @exception DemoException thrown if instance variable <code>pathValue</code> 
213      * is undefined.
214      */
215     public float[] get_pathValue() {
216         return(this.pathValue);
217     }
218     /**
219      * Set method for private instance variable <code>pathValue</code>.
220      *
221      * @param pathValue the value to set for the instance variable <code>pathValue</code>.
222      */
223     public void set_pathValue(float[] pathValue) {
224         this.pathValue = pathValue;
225     }
226     /**
227      * Accessor method for private instance variable <code>returnDefinition</code>.
228      *
229      * @return Value of instance variable <code>returnDefinition</code>.
230      * @exception DemoException thrown if instance variable <code>returnDefinition</code> 
231      * is undefined.
232      */
233     public int get_returnDefinition() {
234         return(this.returnDefinition);
235     }
236     /**
237      * Set method for private instance variable <code>returnDefinition</code>.
238      *
239      * @param returnDefinition the value to set for the instance variable 
240      * <code>returnDefinition</code>.
241      */
242     public void set_returnDefinition(int returnDefinition) {
243         this.returnDefinition = returnDefinition;
244     }
245     /**
246      * Accessor method for private instance variable <code>expectedReturnRate</code>.
247      *
248      * @return Value of instance variable <code>expectedReturnRate</code>.
249      * @exception DemoException thrown if instance variable <code>expectedReturnRate</code> 
250      * is undefined.
251      */
252     public float get_expectedReturnRate() {
253         return(this.expectedReturnRate);
254     }
255     /**
256      * Set method for private instance variable <code>expectedReturnRate</code>.
257      *
258      * @param expectedReturnRate the value to set for the instance variable 
259      * <code>expectedReturnRate</code>.
260      */
261     public void set_expectedReturnRate(float expectedReturnRate) {
262         this.expectedReturnRate = expectedReturnRate;
263     }
264     /**
265      * Accessor method for private instance variable <code>volatility</code>.
266      *
267      * @return Value of instance variable <code>volatility</code>.
268      * @exception DemoException thrown if instance variable <code>volatility</code> 
269      * is undefined.
270      */
271     public float get_volatility() {
272         return(this.volatility);
273     }
274     /**
275      * Set method for private instance variable <code>volatility</code>.
276      *
277      * @param volatility the value to set for the instance variable 
278      * <code>volatility</code>.
279      */
280     public void set_volatility(float volatility) {
281         this.volatility = volatility;
282     }
283     /**
284      * Accessor method for private instance variable <code>nTimeSteps</code>.
285      *
286      * @return Value of instance variable <code>nTimeSteps</code>.
287      * @exception DemoException thrown if instance variable <code>nTimeSteps</code> 
288      * is undefined.
289      */
290     public int get_nTimeSteps() {
291         return(this.nTimeSteps);
292     }
293     /**
294      * Set method for private instance variable <code>nTimeSteps</code>.
295      *
296      * @param nTimeSteps the value to set for the instance variable 
297      * <code>nTimeSteps</code>.
298      */
299     public void set_nTimeSteps(int nTimeSteps) {
300         this.nTimeSteps = nTimeSteps;
301     }
302     /**
303      * Accessor method for private instance variable <code>pathStartValue</code>.
304      *
305      * @return Value of instance variable <code>pathStartValue</code>.
306      * @exception DemoException thrown if instance variable <code>pathStartValue</code> 
307      * is undefined.
308      */
309     public float get_pathStartValue() {
310         return(this.pathStartValue);
311     }
312     /**
313      * Set method for private instance variable <code>pathStartValue</code>.
314      *
315      * @param pathStartValue the value to set for the instance variable 
316      * <code>pathStartValue</code>.
317      */
318     public void set_pathStartValue(float pathStartValue) {
319         this.pathStartValue = pathStartValue;
320     }
321     //------------------------------------------------------------------------
322     /**
323      * Method for copying the suitable instance variable from a
324      * <code>ReturnPath</code> object.
325      *
326      * @param obj Object used to define the instance variables which
327      *            should be carried over to this object.
328      * @exception DemoException thrown if there is a problem accessing the
329      *                          instance variables from the target objetct.
330      */
331     private void copyInstanceVariables(ReturnPath obj) {
332         //
333         // Instance variables defined in the PathId object.
334         set_name(obj.get_name());
335         set_startDate(obj.get_startDate());
336         set_endDate(obj.get_endDate());
337         set_dTime(obj.get_dTime());
338         //
339         // Instance variables defined in this object.
340         this.returnDefinition   = obj.get_returnDefinition();
341         this.expectedReturnRate = obj.get_expectedReturnRate();
342         this.volatility         = obj.get_volatility();
343     }
344
345     /**
346      * Method for returning a RatePath object from the Monte Carlo data
347      * generated.
348      *
349      * @return a <code>RatePath</code> object representing the generated
350      *         data.
351      * @exception DemoException thrown if there was a problem creating
352      *            the RatePath object.
353      */
354     public RatePath getRatePath() {
355         return(new RatePath(this));
356     }
357     /**
358      * Method for calculating the sequence of fluctuations, based around
359      * a Gaussian distribution of given mean and variance, as defined
360      * in this class' instance variables.  Mapping from Gaussian
361      * distribution of (0,1) to (mean-drift,volatility) is done via
362      * Ito's lemma on the log of the stock price.
363      * 
364      * @param randomSeed The psuedo-random number seed value, to start off a
365      *                   given sequence of Gaussian fluctuations.
366      * @exception DemoException thrown if there are any problems with
367      *                          the computation.
368      */
369     public boolean computeFluctuationsGaussian(long randomSeed) {
370         if( nTimeSteps > fluctuations.length ) {
371             return false;
372         }
373         //System.printI(0xc0);
374         //
375         // First, make use of the passed in seed value.
376         MyRandom rnd;
377         float v1,v2, r;
378         v1 = (float)0.0;
379         v2 = (float)0.0;
380         if( randomSeed == -1 ) {
381             rnd = new MyRandom(0, v1, v2);
382         } else {
383             rnd = new MyRandom((int)randomSeed, v1, v2);
384         }
385         //System.printI(0xc1);
386         //
387         // Determine the mean and standard-deviation, from the mean-drift and volatility.
388         float mean = (expectedReturnRate-(float)0.5*volatility*volatility)*get_dTime();
389         //System.printI(0xc2);
390         float sd   = volatility*Math.sqrtf(get_dTime());
391         //System.printI(0xc3);
392         float gauss, meanGauss=(float)0.0, variance=(float)0.0;
393         //System.printI(0xc4);
394         for( int i=0; i < nTimeSteps; i += 2 ) {
395             //System.printI(0xc5);
396             r  = rnd.seed();
397             gauss = r*rnd.v1;
398             //System.printI(0xc6);
399             meanGauss+= gauss;
400             variance+= (gauss*gauss);
401             //
402             // Now map this onto a general Gaussian of given mean and variance.
403             fluctuations[i] = mean + sd*gauss;
404             //      dbgPrintln("gauss="+gauss+" fluctuations="+fluctuations[i]);
405             
406             gauss  = r*rnd.v2;
407             meanGauss+= gauss;
408             variance+= (gauss*gauss);
409             //
410             // Now map this onto a general Gaussian of given mean and variance.
411             fluctuations[i+1] = mean + sd*gauss;
412         }
413         //System.printI(0xc7);
414         meanGauss/=(float)nTimeSteps;
415         //System.printI(0xc8);
416         variance /=(float)nTimeSteps;
417         //System.printI(0xc9);
418         //    dbgPrintln("meanGauss="+meanGauss+" variance="+variance);
419     }
420     /**
421      * Method for calculating the sequence of fluctuations, based around
422      * a Gaussian distribution of given mean and variance, as defined
423      * in this class' instance variables.  Mapping from Gaussian
424      * distribution of (0,1) to (mean-drift,volatility) is done via
425      * Ito's lemma on the log of the stock price.  This overloaded method
426      * is for when the random seed should be decided by the system.
427      * 
428      * @exception DemoException thrown if there are any problems with
429      *                          the computation.
430      */
431     public void computeFluctuationsGaussian() {
432         computeFluctuationsGaussian((long)-1);
433     }
434     /**
435      * Method for calculating the corresponding rate path, given the
436      * fluctuations and starting rate value.
437      * 
438      * @param startValue the starting value of the rate path, to be
439      *                   updated with the precomputed fluctuations.
440      * @exception DemoException thrown if there are any problems with
441      *                          the computation.
442      */
443     public void computePathValue(float startValue) {
444         pathValue[0] = startValue;
445         //System.printI(0xca0);
446         if( returnDefinition == 1 || 
447                 returnDefinition == 2) {
448             //System.printI(0xca1);
449             for(int i=1; i < nTimeSteps; i++ ) {
450                 //System.printI(0xca2);
451                 pathValue[i] = pathValue[i-1] * Math.expf(fluctuations[i]);
452             }
453         }
454     }
455 }