Fixed issues with compilation.
[IRC.git] / Robust / src / Benchmarks / Scheduling / GC / NON_BAMBOO / JGFMonteCarlo / RatePath.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 for recording the values in the time-dependent path of a security.
26  *
27  * <p>To Do list:
28  * <ol>
29  *   <li><i>None!</i>
30  * </ol>
31  *
32  * @author H W Yau
33  * @version $Revision: 1.2 $ $Date: 2011/07/14 21:28:29 $
34  */
35 public class RatePath extends PathId {
36
37   //------------------------------------------------------------------------
38   // Class variables.
39   //------------------------------------------------------------------------
40   /**
41    * Class variable to represent the minimal date, whence the stock prices
42    * appear. Used to trap any potential problems with the data.
43    */
44   public int MINIMUMDATE;
45   /**
46    * Class variable for defining what is meant by a small number, small enough
47    * to cause an arithmetic overflow when dividing.  According to the
48    * Java Nutshell book, the actual range is +/-4.9406564841246544E-324
49    */
50   public float EPSILON;
51
52   //------------------------------------------------------------------------
53   // Instance variables.
54   //------------------------------------------------------------------------
55   /**
56    * An instance variable, for storing the rate's path values itself.
57    */
58   public float[] pathValue;
59   /**
60    * An instance variable, for storing the corresponding date of the datum,
61    * in 'YYYYMMDD' format.
62    */
63   public int[] pathDate;
64   /**
65    * The number of accepted values in the rate path.
66    */
67   public int nAcceptedPathValue;
68
69   //------------------------------------------------------------------------
70   // Constructors.
71   //------------------------------------------------------------------------
72   /**
73    * Constructor, where the user specifies the directory and filename in
74    * from which the data should be read.
75    *
76    * @param String dirName
77    * @param String filename
78    * @exception DemoException thrown if there is a problem reading in
79    *                          the data file.
80    */
81   public RatePath() {
82     this.MINIMUMDATE = 19000101;
83     this.EPSILON= (float)10.0 * (float)(4.9E-324);
84     this.nAcceptedPathValue = 0;
85     readRatesFile();
86   }
87   /**
88    * Constructor, for when the user specifies simply an array of values
89    * for the path.  User must also include information for specifying
90    * the other characteristics of the path.
91    *
92    * @param pathValue the array containing the values for the path.
93    * @param name the name to attach to the path.
94    * @param startDate date from which the path is supposed to start, in
95    *        'YYYYMMDD' format.
96    * @param startDate date from which the path is supposed to end, in
97    *        'YYYYMMDD' format.
98    * @param dTime the time interval between successive path values, in
99    *        fractions of a year.
100    */
101   public RatePath(float[] pathValue, 
102       String name, 
103       int startDate, 
104       int endDate, 
105       float dTime) {
106     this.MINIMUMDATE = 19000101;
107     this.EPSILON= (float)10.0 * (float)(4.9E-324);
108
109     this.name = name;
110     this.startDate = startDate;
111     this.endDate = endDate;
112     this.dTime = dTime;
113     this.pathValue = pathValue;
114     this.nAcceptedPathValue = pathValue.length;
115   }
116   /**
117    * Constructor, for use by the Monte Carlo generator, when it wishes
118    * to represent its findings as a RatePath object.
119    *
120    * @param mc the Monte Carlo generator object, whose data are to
121    *           be copied over.
122    * @exception DemoException thrown if there is an attempt to access
123    *            an undefined variable.
124    */
125   public RatePath(MonteCarloPath mc) {
126     this.MINIMUMDATE = 19000101;
127     this.EPSILON= (float)10.0 * (float)(4.9E-324);
128
129     //
130     // Fields pertaining to the parent PathId object:
131     this.name = mc.name;
132     this.startDate = mc.startDate;
133     this.endDate = mc.endDate;
134     this.dTime = mc.dTime;
135     //
136     // Fields pertaining to RatePath object itself.
137     pathValue=mc.pathValue;
138     nAcceptedPathValue=mc.nTimeSteps;
139     //
140     // Note that currently the pathDate is neither declared, defined,
141     // nor used in the MonteCarloPath object.
142     pathDate=new int[nAcceptedPathValue];
143   }
144   /**
145    * Constructor, for when there is no actual pathValue with which to
146    * initialise.
147    *
148    * @param pathValueLegth the length of the array containing the values
149    *        for the path.
150    * @param name the name to attach to the path.
151    * @param startDate date from which the path is supposed to start, in
152    *        'YYYYMMDD' format.
153    * @param startDate date from which the path is supposed to end, in
154    *        'YYYYMMDD' format.
155    * @param dTime the time interval between successive path values, in
156    *        fractions of a year.
157    */
158   public RatePath(int pathValueLength, 
159       String name, 
160       int startDate, 
161       int endDate, 
162       float dTime) {
163     this.MINIMUMDATE = 19000101;
164     this.EPSILON= (float)10.0 * (float)(4.9E-324);
165
166     this.name = name;
167     this.startDate = startDate;
168     this.endDate = endDate;
169     this.dTime = dTime;
170     this.pathValue = new float[pathValueLength];
171     this.nAcceptedPathValue = pathValue.length;
172   }
173   //------------------------------------------------------------------------
174   // Methods.
175   //------------------------------------------------------------------------
176   /**
177    * Routine to update this rate path with the values from another rate
178    * path, via its pathValue array.
179    *
180    * @param operandPath the path value array to use for the update.
181    * @exception DemoException thrown if there is a mismatch between the
182    *            lengths of the operand and target arrays.
183    */
184   public boolean inc_pathValue(float[] operandPath) {
185     int length = this.pathValue.length;
186     if( length != operandPath.length ) {
187       return false;
188     }
189     float[] pathvalue = this.pathValue;
190     for(int i=0; i<length; i++ ) {
191       pathvalue[i] += operandPath[i];
192     }
193     return true;
194   }
195   /**
196    * Routine to scale this rate path by a constant.
197    *
198    * @param scale the constant with which to multiply to all the path
199    *        values.
200    * @exception DemoException thrown if there is a mismatch between the
201    *            lengths of the operand and target arrays.
202    */
203   public boolean inc_pathValue(float scale) {
204     float[] pathvalue = this.pathValue;
205     if( pathvalue==null ) {
206       return false;
207     }
208     int length = this.pathValue.length;
209     for(int i=0; i<length; i++ ) {
210       pathvalue[i] *= scale;
211     }
212     return true;
213   }
214   //------------------------------------------------------------------------
215   // Accessor methods for class RatePath.
216   // Generated by 'makeJavaAccessor.pl' script.  HWY.  20th January 1999.
217   //------------------------------------------------------------------------
218   /**
219    * Accessor method for private instance variable <code>pathValue</code>.
220    *
221    * @return Value of instance variable <code>pathValue</code>.
222    * @exception DemoException thrown if instance variable <code>pathValue</code> 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>pathDate</code>.
237    *
238    * @return Value of instance variable <code>pathDate</code>.
239    * @exception DemoException thrown if instance variable <code>pathDate</code> is undefined.
240    */
241   /*public int[] get_pathDate() {
242         return(this.pathDate);
243     }*/
244   /**
245    * Set method for private instance variable <code>pathDate</code>.
246    *
247    * @param pathDate the value to set for the instance variable <code>pathDate</code>.
248    */
249   public void set_pathDate(int[] pathDate) {
250     this.pathDate = pathDate;
251   }
252   //------------------------------------------------------------------------
253   /**
254    * Method to return the terminal value for a given rate path, as used
255    * in derivative calculations.
256    * 
257    * @return The last value in the rate path.
258    */
259   public float getEndPathValue() {
260     return( getPathValue(pathValue.length-1) );
261   }
262   /**
263    * Method to return the value for a given rate path, at a given index.
264    * <i>One may want to index this in a more user friendly manner!</i>
265    * 
266    * @param index the index on which to return the path value.
267    * @return The value of the path at the designated index.
268    */
269   public float getPathValue(int index) {
270     return(pathValue[index]);
271   }
272   /**
273    * Method for calculating the returns on a given rate path, via the
274    * definition for the instantaneous compounded return.
275    *       u_i = \ln{\frac{S_i}{S_{i-1}}}
276    * 
277    * @return the return, as defined.
278    * @exception DemoException thrown if there is a problem with the
279    *                          calculation.
280    */
281   public ReturnPath getReturnCompounded() {
282     int length = this.nAcceptedPathValue;
283     float[] pathvalue = this.pathValue;
284     if( pathvalue == null || length == 0 ) {
285       return null;
286     }
287     float[] returnPathValue = new float[length];
288     returnPathValue[0] = (float)0.0;
289     for(int i=1; i< length; i++ ) {
290       returnPathValue[i] = Math.logf(pathvalue[i] / pathvalue[i-1]);
291     }
292
293     ReturnPath rPath = new ReturnPath(returnPathValue, length, 1);
294     //
295     // Copy the PathId information to the ReturnPath object.
296     rPath.copyInstanceVariables(this);
297     rPath.estimatePath();
298     return(rPath);
299   }
300   /**
301    * Method for calculating the returns on a given rate path, via the
302    * definition for the instantaneous non-compounded return.
303    *       u_i = \frac{S_i - S_{i-1}}{S_i}
304    * 
305    * @return the return, as defined.
306    * @exception DemoException thrown if there is a problem with the
307    *                          calculation.
308    */
309   public ReturnPath getReturnNonCompounded() {
310     int length = this.nAcceptedPathValue;
311     float[] pathvalue = this.pathValue;
312     if( pathvalue == null || length == 0 ) {
313       return null;
314     }
315     float[] returnPathValue = new float[length];
316     returnPathValue[0] = (float)0.0;
317     for(int i=1; i< length; i++ ) {
318       returnPathValue[i] = (pathvalue[i] - pathvalue[i-1])/pathvalue[i];
319     }
320
321     ReturnPath rPath = new ReturnPath(returnPathValue, length, 2);
322     //
323     // Copy the PathId information to the ReturnPath object.
324     rPath.copyInstanceVariables(this);
325     rPath.estimatePath();
326     return(rPath);
327   }
328   //------------------------------------------------------------------------
329   // Private methods.
330   //------------------------------------------------------------------------
331   /**
332    * Method for reading in data file, in a given format.
333    * Namely:
334       <pre>
335       881003,0.0000,14.1944,13.9444,14.0832,2200050,0
336       881004,0.0000,14.1668,14.0556,14.1668,1490850,0
337       ...
338       990108,35.8125,36.7500,35.5625,35.8125,4381200,0
339       990111,35.8125,35.8750,34.8750,35.1250,3920800,0
340       990112,34.8750,34.8750,34.0000,34.0625,3577500,0
341       </pre>
342    * <p>Where the fields represent, one believes, the following:
343    * <ol>
344    *   <li>The date in 'YYMMDD' format</li>
345    *   <li>Open</li>
346    *   <li>High</li>
347    *   <li>Low</li>
348    *   <li>Last</li>
349    *   <li>Volume</li>
350    *   <li>Open Interest</li>
351    * </ol>
352    * One will probably make use of the closing price, but this can be
353    * redefined via the class variable <code>DATUMFIELD</code>.  Note that
354    * since the read in data are then used to compute the return, this would
355    * be a good place to trap for zero values in the data, which will cause
356    * all sorts of problems.
357    *
358    * @param dirName the directory in which to search for the data file.
359    * @param filename the data filename itself.
360    * @exception DemoException thrown if there was a problem with the data
361    *                          file.
362    */
363   private void readRatesFile(){
364     //
365     // Now create an array to store the rates data.
366     int minimumdate = MINIMUMDATE;
367     float epsilon = EPSILON;
368     int nLines = 1000; //200;
369     int year = 88;
370     int month = 10;
371     int day = 3;
372     this.pathValue = new float[nLines];
373     this.pathDate  = new int[nLines];
374     float[] pathvalue = this.pathValue;
375     int[] pathdate = this.pathDate;
376     nAcceptedPathValue=0;
377     int iLine=0;
378     /*char[] date = new char[9];
379         date[0] = '1';
380         date[1] = '9';
381         date[2] = (char)(year/10 + '0');
382         date[3] = (char)(year%10 + '0');
383         date[4] = (char)(month/10 + '0');
384         date[5] = (char)(month%10 + '0');
385         date[6] = (char)(day/10 + '0');
386         date[7] = (char)(day%10 + '0');
387         date[8] = '\0';*/
388     int aDate = 19881003;
389     /*for(int di = 0; di < 9; di++) {
390             aDate = aDate * 10 + (int)date[di];
391         }*/
392     for(int k = 0; k < 20; /*40;*/ k++ ) {
393       for(int j = 0; j < 50; /*5;*/ j++) {
394         /*String date = "19"+String.valueOf(year);
395                 if(month < 10) {
396                     date += "0";
397                 } 
398                 date += String.valueOf(month);
399                 if(day < 10) {
400                     date += "0";
401                 }
402                 date +=  String.valueOf(day);*/
403         //int aDate = Integer.parseInt(date);           
404         day++;
405         aDate++;
406         /*if(date[7] == '9') {
407                     date[7] = '0';
408                     date[6] = (char)(date[6] + 1);
409                 } else {
410                     date[7] = (char)(date[7] + 1);
411                 }*/
412         if(month == 2) {
413           if(day == 29) {
414             day = 1;
415             month++;
416             /*date[6] = '0';
417                         date[7] = '1';
418                         date[5] = '3';*/
419             aDate += 72;// - day(29) + 101;
420           }
421         } else {
422           if(day == 31) {
423             day = 1;
424             month++;
425             aDate += 70;
426             /*date[6] = '0';
427                         date[7] = '1';*/
428             if(month == 13) {
429               month = 1;
430               year++;
431               aDate += 8800;
432               /*date[4] = '0';
433                             date[5] = '1';
434                             if(date[3] == '9') {
435                                 if(date[2] == '9') {
436                                     if(date[1] == '9') {
437                                         date[1] = '0';
438                                         date[0] = (char)(date[0] + 1);
439                                     } else {
440                                         date[1] = (char)(date[1] + 1);
441                                     }
442                                     date[2] = '0';
443                                 } else {
444                                     date[2] = (char)(date[2] + 1);
445                                 }
446                                 date[3] = '0';
447                             } else {
448                                 date[3] = (char)(date[3] + 1);
449                             }*/
450             } /*else {
451                             if(date[5] == '9') {
452                                 date[4] = '1';
453                                 date[5] = '0';
454                             } else {
455                                 date[5] = (char)(date[5] + 1);
456                             }
457                         }*/
458           }
459         }
460         //
461         // static float float.parsefloat() method is a feature of JDK1.2!
462         int tmp = k + j;
463         float aPathValue = (float)(121.7500 - tmp);
464         if( (aDate <= minimumdate) /*| (Math.abs(aPathValue) < epsilon)*/ ) {
465           //System.printString("Skipped erroneous data indexed by date="+date+".");
466         } else {
467           pathdate[iLine] = aDate;
468           pathvalue[iLine] = aPathValue;
469           iLine++;
470         }
471       }
472     }
473     //
474     // Record the actual number of accepted data points.
475     nAcceptedPathValue = iLine;
476     //
477     // Now to fill in the structures from the 'PathId' class.
478     this.name = "rate";
479     this.startDate = pathdate[0];
480     this.endDate = pathdate[iLine-1];
481     this.dTime = (float)(1.0/365.0);
482   }
483 }