Fixed issues with compilation.
[IRC.git] / Robust / src / Benchmarks / Scheduling / GC / NON_BAMBOO / JGFMonteCarlo / MonteCarloPath.java
1 package JGFMonteCarlo;
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.2 $ $Date: 2011/07/14 21:28:29 $
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   public float[] fluctuations;
45   /**
46    * The path values from which the random fluctuations are used to update.
47    */
48   public 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   public int returnDefinition;
54   /**
55    * Value for the mean drift, for use in the generation of the random path.
56    */
57   public float expectedReturnRate;
58   /**
59    * Value for the volatility, for use in the generation of the random path.
60    */
61   public float volatility;
62   /**
63    * Number of time steps for which the simulation should act over.
64    */
65   public int nTimeSteps;
66   /**
67    * The starting value for of the security.
68    */
69   public 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, 
99       int nTimeSteps) {
100     /**
101      * These instance variables are members of PathId class.
102      */
103     this.expectedReturnRate = (float)0.0;
104     this.volatility = (float)0.0;
105     this.pathStartValue = (float)0.0;
106     this.returnDefinition=1;
107
108     copyInstanceVariables(returnPath);
109     this.nTimeSteps = nTimeSteps;
110     this.pathValue = new float[nTimeSteps];
111     this.fluctuations = new float[nTimeSteps];
112   }
113   /**
114    * Constructor, where the <code>PathId</code> objects is used to ease
115    * the number of instance variables to pass in.
116    *
117    * @param pathId Object used to define the identity of this Path.
118    * @param returnDefinition How the statistic variables were defined,
119    *                         according to the definitions in
120    *                         <code>ReturnPath</code>'s two class variables
121    *                         <code>COMPOUNDED</code> and
122    *                         <code>NONCOMPOUNDED</code>.
123    * @param expectedReturnRate The measured expected return rate for which to generate.
124    * @param volatility The measured volatility for which to generate.
125    * @param nTimeSteps The number of time steps for which to generate.
126    * @exception DemoException Thrown if there is a problem initialising the
127    *                          object's instance variables.
128    */
129   public MonteCarloPath(PathId pathId, 
130       int returnDefinition, 
131       float expectedReturnRate, 
132       float volatility, 
133       int nTimeSteps) {
134     /**
135      * These instance variables are members of PathId class.
136      * Invoking with this particular signature should point to the
137      * definition in the PathId class.
138      */
139     this.pathStartValue = (float)0.0;
140
141     copyInstanceVariables(pathId);
142     this.returnDefinition   = returnDefinition;
143     this.expectedReturnRate = expectedReturnRate;
144     this.volatility         = volatility;
145     this.nTimeSteps         = nTimeSteps;
146     this.pathValue          = new float[nTimeSteps];
147     this.fluctuations       = new float[nTimeSteps];
148   }
149   /**
150    * Constructor, for when the user wishes to define each of the instance
151    * variables individually.
152    *
153    * @param name The name of the security which this Monte Carlo path
154    *             should represent.
155    * @param startDate The date when the path starts, in 'YYYYMMDD' format.
156    * @param endDate The date when the path ends, in 'YYYYMMDD' format.
157    * @param dTime The interval in the data between successive data points
158    *              in the generated path.
159    * @param returnDefinition How the statistic variables were defined,
160    *                         according to the definitions in
161    *                         <code>ReturnPath</code>'s two class variables
162    *                         <code>COMPOUNDED</code> and
163    *                         <code>NONCOMPOUNDED</code>.
164    * @param expectedReturnRate The measured mean drift for which to generate.
165    * @param volatility The measured volatility for which to generate.
166    * @param nTimeSteps The number of time steps for which to generate.
167    */
168   public MonteCarloPath(String name, 
169       int startDate, 
170       int endDate, 
171       float dTime, 
172       int returnDefinition, 
173       float expectedReturnRate, 
174       float volatility, 
175       int nTimeSteps) {
176     /**
177      * These instance variables are members of PathId class.
178      */
179     this.name = name;
180     this.startDate = startDate;
181     this.endDate = endDate;
182     this.dTime = dTime;
183     this.returnDefinition   = returnDefinition;
184     this.expectedReturnRate = expectedReturnRate;
185     this.volatility         = volatility;
186     this.nTimeSteps         = nTimeSteps;
187     this.pathValue          = new float[nTimeSteps];
188     this.fluctuations       = new float[nTimeSteps];
189   }
190
191   //------------------------------------------------------------------------
192   // Methods.
193   //------------------------------------------------------------------------
194   //------------------------------------------------------------------------
195   // Accessor methods for class MonteCarloPath.
196   // Generated by 'makeJavaAccessor.pl' script.  HWY.  20th January 1999.
197   //------------------------------------------------------------------------
198   /**
199    * Accessor method for private instance variable <code>fluctuations</code>.
200    *
201    * @return Value of instance variable <code>fluctuations</code>.
202    * @exception DemoException thrown if instance variable <code>fluctuations</code> 
203    * is undefined.
204    */
205   /*public float[] get_fluctuations() {
206         return(this.fluctuations);
207     }*/
208   /**
209    * Set method for private instance variable <code>fluctuations</code>.
210    *
211    * @param fluctuations the value to set for the instance variable 
212    * <code>fluctuations</code>.
213    */
214   public void set_fluctuations(float[] fluctuations) {
215     this.fluctuations = fluctuations;
216   }
217   /**
218    * Accessor method for private instance variable <code>pathValue</code>.
219    *
220    * @return Value of instance variable <code>pathValue</code>.
221    * @exception DemoException thrown if instance variable <code>pathValue</code> 
222    * is undefined.
223    */
224   /*public float[] get_pathValue() {
225         return(this.pathValue);
226     }*/
227   /**
228    * Set method for private instance variable <code>pathValue</code>.
229    *
230    * @param pathValue the value to set for the instance variable <code>pathValue</code>.
231    */
232   public void set_pathValue(float[] pathValue) {
233     this.pathValue = pathValue;
234   }
235   /**
236    * Accessor method for private instance variable <code>returnDefinition</code>.
237    *
238    * @return Value of instance variable <code>returnDefinition</code>.
239    * @exception DemoException thrown if instance variable <code>returnDefinition</code> 
240    * is undefined.
241    */
242   /*public int get_returnDefinition() {
243         return(this.returnDefinition);
244     }*/
245   /**
246    * Set method for private instance variable <code>returnDefinition</code>.
247    *
248    * @param returnDefinition the value to set for the instance variable 
249    * <code>returnDefinition</code>.
250    */
251   public void set_returnDefinition(int returnDefinition) {
252     this.returnDefinition = returnDefinition;
253   }
254   /**
255    * Accessor method for private instance variable <code>expectedReturnRate</code>.
256    *
257    * @return Value of instance variable <code>expectedReturnRate</code>.
258    * @exception DemoException thrown if instance variable <code>expectedReturnRate</code> 
259    * is undefined.
260    */
261   /*public float get_expectedReturnRate() {
262         return(this.expectedReturnRate);
263     }*/
264   /**
265    * Set method for private instance variable <code>expectedReturnRate</code>.
266    *
267    * @param expectedReturnRate the value to set for the instance variable 
268    * <code>expectedReturnRate</code>.
269    */
270   public void set_expectedReturnRate(float expectedReturnRate) {
271     this.expectedReturnRate = expectedReturnRate;
272   }
273   /**
274    * Accessor method for private instance variable <code>volatility</code>.
275    *
276    * @return Value of instance variable <code>volatility</code>.
277    * @exception DemoException thrown if instance variable <code>volatility</code> 
278    * is undefined.
279    */
280   /*public float get_volatility() {
281         return(this.volatility);
282     }*/
283   /**
284    * Set method for private instance variable <code>volatility</code>.
285    *
286    * @param volatility the value to set for the instance variable 
287    * <code>volatility</code>.
288    */
289   public void set_volatility(float volatility) {
290     this.volatility = volatility;
291   }
292   /**
293    * Accessor method for private instance variable <code>nTimeSteps</code>.
294    *
295    * @return Value of instance variable <code>nTimeSteps</code>.
296    * @exception DemoException thrown if instance variable <code>nTimeSteps</code> 
297    * is undefined.
298    */
299   /*public int get_nTimeSteps() {
300         return(this.nTimeSteps);
301     }*/
302   /**
303    * Set method for private instance variable <code>nTimeSteps</code>.
304    *
305    * @param nTimeSteps the value to set for the instance variable 
306    * <code>nTimeSteps</code>.
307    */
308   public void set_nTimeSteps(int nTimeSteps) {
309     this.nTimeSteps = nTimeSteps;
310   }
311   /**
312    * Accessor method for private instance variable <code>pathStartValue</code>.
313    *
314    * @return Value of instance variable <code>pathStartValue</code>.
315    * @exception DemoException thrown if instance variable <code>pathStartValue</code> 
316    * is undefined.
317    */
318   /*public float get_pathStartValue() {
319         return(this.pathStartValue);
320     }*/
321   /**
322    * Set method for private instance variable <code>pathStartValue</code>.
323    *
324    * @param pathStartValue the value to set for the instance variable 
325    * <code>pathStartValue</code>.
326    */
327   public void set_pathStartValue(float pathStartValue) {
328     this.pathStartValue = pathStartValue;
329   }
330   //------------------------------------------------------------------------
331   /**
332    * Method for copying the suitable instance variable from a
333    * <code>ReturnPath</code> object.
334    *
335    * @param obj Object used to define the instance variables which
336    *            should be carried over to this object.
337    * @exception DemoException thrown if there is a problem accessing the
338    *                          instance variables from the target objetct.
339    */
340   private void copyInstanceVariables(ReturnPath obj) {
341     //
342     // Instance variables defined in the PathId object.
343     this.name = obj.name;
344     this.startDate = obj.startDate;
345     this.endDate = obj.endDate;
346     this.dTime = obj.dTime;
347     //
348     // Instance variables defined in this object.
349     this.returnDefinition   = obj.returnDefinition;
350     this.expectedReturnRate = obj.expectedReturnRate;
351     this.volatility         = obj.volatility;
352   }
353
354   /**
355    * Method for returning a RatePath object from the Monte Carlo data
356    * generated.
357    *
358    * @return a <code>RatePath</code> object representing the generated
359    *         data.
360    * @exception DemoException thrown if there was a problem creating
361    *            the RatePath object.
362    */
363   public RatePath getRatePath() {
364     return(new RatePath(this));
365   }
366   /**
367    * Method for calculating the sequence of fluctuations, based around
368    * a Gaussian distribution of given mean and variance, as defined
369    * in this class' instance variables.  Mapping from Gaussian
370    * distribution of (0,1) to (mean-drift,volatility) is done via
371    * Ito's lemma on the log of the stock price.
372    * 
373    * @param randomSeed The psuedo-random number seed value, to start off a
374    *                   given sequence of Gaussian fluctuations.
375    * @exception DemoException thrown if there are any problems with
376    *                          the computation.
377    */
378   public boolean computeFluctuationsGaussian(long randomSeed) {
379     int ntimesteps = this.nTimeSteps;
380     int length = this.fluctuations.length;
381     if( ntimesteps > length ) {
382       return false;
383     }
384     float[] flucts = this.fluctuations;
385     float expectedreturnrate = this.expectedReturnRate;
386     float vol = this.volatility;
387     float dtime = this.dTime;
388
389     //
390     // First, make use of the passed in seed value.
391     MyRandom rnd;
392     float v1,v2, r;
393     v1 = (float)0.0;
394     v2 = (float)0.0;
395     if( randomSeed == -1 ) {
396       rnd = new MyRandom(0, v1, v2);
397     } else {
398       rnd = new MyRandom((int)randomSeed, v1, v2);
399     }
400
401     //
402     // Determine the mean and standard-deviation, from the mean-drift and volatility.
403     float mean = (expectedreturnrate-(float)0.5*vol*vol)*dtime;
404     float sd   = vol*Math.sqrtf(dtime);
405     float gauss, meanGauss=(float)0.0, variance=(float)0.0;
406     for( int i=0; i < ntimesteps; i += 2 ) {
407       r  = rnd.seed();
408       gauss = r*rnd.v1;
409       meanGauss+= gauss;
410       variance+= gauss*gauss;
411       //
412       // Now map this onto a general Gaussian of given mean and variance.
413       flucts[i] = mean + sd*gauss;
414       //      dbgPrintln("gauss="+gauss+" fluctuations="+fluctuations[i]);
415
416       gauss  = r*rnd.v2;
417       meanGauss+= gauss;
418       variance+= gauss*gauss;
419       //
420       // Now map this onto a general Gaussian of given mean and variance.
421       flucts[i+1] = mean + sd*gauss;
422     }
423     meanGauss/=(float)ntimesteps;
424     variance /=(float)ntimesteps;
425     //    dbgPrintln("meanGauss="+meanGauss+" variance="+variance);
426   }
427   /**
428    * Method for calculating the sequence of fluctuations, based around
429    * a Gaussian distribution of given mean and variance, as defined
430    * in this class' instance variables.  Mapping from Gaussian
431    * distribution of (0,1) to (mean-drift,volatility) is done via
432    * Ito's lemma on the log of the stock price.  This overloaded method
433    * is for when the random seed should be decided by the system.
434    * 
435    * @exception DemoException thrown if there are any problems with
436    *                          the computation.
437    */
438   public void computeFluctuationsGaussian() {
439     computeFluctuationsGaussian((long)-1);
440   }
441   /**
442    * Method for calculating the corresponding rate path, given the
443    * fluctuations and starting rate value.
444    * 
445    * @param startValue the starting value of the rate path, to be
446    *                   updated with the precomputed fluctuations.
447    * @exception DemoException thrown if there are any problems with
448    *                          the computation.
449    */
450   public void computePathValue(float startValue) {
451     float[] pathvalue = this.pathValue;
452     float[] flucts = this.fluctuations;
453     int length = this.nTimeSteps;
454     pathvalue[0] = startValue;
455     if( returnDefinition == 1 | 
456         returnDefinition == 2) {
457       for(int i=1; i < length; i++ ) {
458         //System.printI((int)(flucts[i] * 10000));
459         pathvalue[i] = pathvalue[i-1] * Math.expf(flucts[i]);
460       }
461     }
462   }
463 }