latest bug fixing and new benchmarks
[IRC.git] / Robust / src / Benchmarks / Scheduling / JGFMonteCarlo / RatePath.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 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.1 $ $Date: 2008/08/18 22:22:21 $
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     private float[] pathValue;
59     /**
60      * An instance variable, for storing the corresponding date of the datum,
61      * in 'YYYYMMDD' format.
62      */
63     private int[] pathDate;
64     /**
65      * The number of accepted values in the rate path.
66      */
67     private 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         //System.printI(0xaa0);
86         readRatesFile();
87         //System.printI(0xaa1);
88     }
89     /**
90      * Constructor, for when the user specifies simply an array of values
91      * for the path.  User must also include information for specifying
92      * the other characteristics of the path.
93      *
94      * @param pathValue the array containing the values for the path.
95      * @param name the name to attach to the path.
96      * @param startDate date from which the path is supposed to start, in
97      *        'YYYYMMDD' format.
98      * @param startDate date from which the path is supposed to end, in
99      *        'YYYYMMDD' format.
100      * @param dTime the time interval between successive path values, in
101      *        fractions of a year.
102      */
103     public RatePath(float[] pathValue, String name, int startDate, int endDate, float dTime) {
104         this.MINIMUMDATE = 19000101;
105         this.EPSILON= (float)10.0 * (float)(4.9E-324);
106         
107         set_name(name);
108         set_startDate(startDate);
109         set_endDate(endDate);
110         set_dTime(dTime);
111         this.pathValue = pathValue;
112         this.nAcceptedPathValue = pathValue.length;
113     }
114     /**
115      * Constructor, for use by the Monte Carlo generator, when it wishes
116      * to represent its findings as a RatePath object.
117      *
118      * @param mc the Monte Carlo generator object, whose data are to
119      *           be copied over.
120      * @exception DemoException thrown if there is an attempt to access
121      *            an undefined variable.
122      */
123     public RatePath(MonteCarloPath mc) {
124         this.MINIMUMDATE = 19000101;
125         this.EPSILON= (float)10.0 * (float)(4.9E-324);
126
127         //
128         // Fields pertaining to the parent PathId object:
129         set_name(mc.get_name());
130         set_startDate(mc.get_startDate());
131         set_endDate(mc.get_endDate());
132         set_dTime(mc.get_dTime());
133         //
134         // Fields pertaining to RatePath object itself.
135         pathValue=mc.get_pathValue();
136         nAcceptedPathValue=mc.get_nTimeSteps();
137         //
138         // Note that currently the pathDate is neither declared, defined,
139         // nor used in the MonteCarloPath object.
140         pathDate=new int[nAcceptedPathValue];
141     }
142     /**
143      * Constructor, for when there is no actual pathValue with which to
144      * initialise.
145      *
146      * @param pathValueLegth the length of the array containing the values
147      *        for the path.
148      * @param name the name to attach to the path.
149      * @param startDate date from which the path is supposed to start, in
150      *        'YYYYMMDD' format.
151      * @param startDate date from which the path is supposed to end, in
152      *        'YYYYMMDD' format.
153      * @param dTime the time interval between successive path values, in
154      *        fractions of a year.
155      */
156     public RatePath(int pathValueLength, String name, int startDate, int endDate, float dTime) {
157         this.MINIMUMDATE = 19000101;
158         this.EPSILON= (float)10.0 * (float)(4.9E-324);
159         
160         set_name(name);
161         set_startDate(startDate);
162         set_endDate(endDate);
163         set_dTime(dTime);
164         this.pathValue = new float[pathValueLength];
165         this.nAcceptedPathValue = pathValue.length;
166     }
167     //------------------------------------------------------------------------
168     // Methods.
169     //------------------------------------------------------------------------
170     /**
171      * Routine to update this rate path with the values from another rate
172      * path, via its pathValue array.
173      *
174      * @param operandPath the path value array to use for the update.
175      * @exception DemoException thrown if there is a mismatch between the
176      *            lengths of the operand and target arrays.
177      */
178     public boolean inc_pathValue(float[] operandPath) {
179         if( pathValue.length != operandPath.length ) {
180             return false;
181         }
182         for(int i=0; i<pathValue.length; i++ ) {
183             pathValue[i] += operandPath[i];
184         }
185         return true;
186     }
187     /**
188      * Routine to scale this rate path by a constant.
189      *
190      * @param scale the constant with which to multiply to all the path
191      *        values.
192      * @exception DemoException thrown if there is a mismatch between the
193      *            lengths of the operand and target arrays.
194      */
195     public boolean inc_pathValue(float scale) {
196         if( pathValue==null ) {
197             return false;
198         }
199         for(int i=0; i<pathValue.length; i++ ) {
200             pathValue[i] *= scale;
201         }
202         return true;
203     }
204     //------------------------------------------------------------------------
205     // Accessor methods for class RatePath.
206     // Generated by 'makeJavaAccessor.pl' script.  HWY.  20th January 1999.
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> is undefined.
213      */
214     public float[] get_pathValue() {
215         return(this.pathValue);
216     }
217     /**
218      * Set method for private instance variable <code>pathValue</code>.
219      *
220      * @param pathValue the value to set for the instance variable <code>pathValue</code>.
221      */
222     public void set_pathValue(float[] pathValue) {
223         this.pathValue = pathValue;
224     }
225     /**
226      * Accessor method for private instance variable <code>pathDate</code>.
227      *
228      * @return Value of instance variable <code>pathDate</code>.
229      * @exception DemoException thrown if instance variable <code>pathDate</code> is undefined.
230      */
231     public int[] get_pathDate() {
232         return(this.pathDate);
233     }
234     /**
235      * Set method for private instance variable <code>pathDate</code>.
236      *
237      * @param pathDate the value to set for the instance variable <code>pathDate</code>.
238      */
239     public void set_pathDate(int[] pathDate) {
240         this.pathDate = pathDate;
241     }
242     //------------------------------------------------------------------------
243     /**
244      * Method to return the terminal value for a given rate path, as used
245      * in derivative calculations.
246      * 
247      * @return The last value in the rate path.
248      */
249     public float getEndPathValue() {
250         return( getPathValue(pathValue.length-1) );
251     }
252     /**
253      * Method to return the value for a given rate path, at a given index.
254      * <i>One may want to index this in a more user friendly manner!</i>
255      * 
256      * @param index the index on which to return the path value.
257      * @return The value of the path at the designated index.
258      */
259     public float getPathValue(int index) {
260         return(pathValue[index]);
261     }
262     /**
263      * Method for calculating the returns on a given rate path, via the
264      * definition for the instantaneous compounded return.
265      *       u_i = \ln{\frac{S_i}{S_{i-1}}}
266      * 
267      * @return the return, as defined.
268      * @exception DemoException thrown if there is a problem with the
269      *                          calculation.
270      */
271     public ReturnPath getReturnCompounded() {
272         if( pathValue == null || nAcceptedPathValue == 0 ) {
273             return null;
274         }
275         float[] returnPathValue = new float[nAcceptedPathValue];
276         returnPathValue[0] = (float)0.0;
277         for(int i=1; i< nAcceptedPathValue; i++ ) {
278             returnPathValue[i] = Math.logf(pathValue[i] / pathValue[i-1]);
279         }
280
281         ReturnPath rPath = new ReturnPath(returnPathValue, nAcceptedPathValue, 1);
282         //
283         // Copy the PathId information to the ReturnPath object.
284         rPath.copyInstanceVariables(this);
285         rPath.estimatePath();
286         return(rPath);
287     }
288     /**
289      * Method for calculating the returns on a given rate path, via the
290      * definition for the instantaneous non-compounded return.
291      *       u_i = \frac{S_i - S_{i-1}}{S_i}
292      * 
293      * @return the return, as defined.
294      * @exception DemoException thrown if there is a problem with the
295      *                          calculation.
296      */
297     public ReturnPath getReturnNonCompounded() {
298         if( pathValue == null || nAcceptedPathValue == 0 ) {
299             return null;
300         }
301         float[] returnPathValue = new float[nAcceptedPathValue];
302         returnPathValue[0] = (float)0.0;
303         for(int i=1; i< nAcceptedPathValue; i++ ) {
304             returnPathValue[i] = (pathValue[i] - pathValue[i-1])/pathValue[i];
305         }
306         
307         ReturnPath rPath = new ReturnPath(returnPathValue, nAcceptedPathValue, 2);
308         //
309         // Copy the PathId information to the ReturnPath object.
310         rPath.copyInstanceVariables(this);
311         rPath.estimatePath();
312         return(rPath);
313     }
314     //------------------------------------------------------------------------
315     // Private methods.
316     //------------------------------------------------------------------------
317     /**
318      * Method for reading in data file, in a given format.
319      * Namely:
320       <pre>
321       881003,0.0000,14.1944,13.9444,14.0832,2200050,0
322       881004,0.0000,14.1668,14.0556,14.1668,1490850,0
323       ...
324       990108,35.8125,36.7500,35.5625,35.8125,4381200,0
325       990111,35.8125,35.8750,34.8750,35.1250,3920800,0
326       990112,34.8750,34.8750,34.0000,34.0625,3577500,0
327       </pre>
328      * <p>Where the fields represent, one believes, the following:
329      * <ol>
330      *   <li>The date in 'YYMMDD' format</li>
331      *   <li>Open</li>
332      *   <li>High</li>
333      *   <li>Low</li>
334      *   <li>Last</li>
335      *   <li>Volume</li>
336      *   <li>Open Interest</li>
337      * </ol>
338      * One will probably make use of the closing price, but this can be
339      * redefined via the class variable <code>DATUMFIELD</code>.  Note that
340      * since the read in data are then used to compute the return, this would
341      * be a good place to trap for zero values in the data, which will cause
342      * all sorts of problems.
343      *
344      * @param dirName the directory in which to search for the data file.
345      * @param filename the data filename itself.
346      * @exception DemoException thrown if there was a problem with the data
347      *                          file.
348      */
349     private void readRatesFile(){
350         //
351         // Now create an array to store the rates data.
352         int nLines = 200;
353         int year = 88;
354         int month = 10;
355         int day = 3;
356         //System.printI(0xab0);
357         this.pathValue = new float[nLines];
358         this.pathDate  = new int[nLines];
359         nAcceptedPathValue=0;
360         int iLine=0;
361         for(int k = 0; k < 40; k++ ) {
362             //System.printI(0xab1);
363             for(int j = 0; j < 5; j++) {
364                 //System.printI(0xab2);
365                 String date = "19"+String.valueOf(year);
366                 if(month < 10) {
367                     date += "0";
368                 } 
369                 date += String.valueOf(month);
370                 if(day < 10) {
371                     date += "0";
372                 }
373                 date +=  String.valueOf(day);
374                 int aDate = Integer.parseInt(date);
375                 day++;
376                 if(month == 2) {
377                     if(day == 29) {
378                         day = 1;
379                         month++;
380                     }
381                 } else {
382                     if(day == 31) {
383                         day = 1;
384                         month++;
385                         if(month == 13) {
386                             month = 1;
387                             year++;
388                         }
389                     }
390                 }
391                 //
392                 // static float float.parsefloat() method is a feature of JDK1.2!
393                 float aPathValue = (float)(121.7500 - k - j);
394                 //System.printI(0xab3);
395                 if( (aDate <= MINIMUMDATE) || (Math.abs(aPathValue) < EPSILON) ) {
396                     //System.printString("Skipped erroneous data indexed by date="+date+".");
397                 } else {
398                     pathDate[iLine] = aDate;
399                     pathValue[iLine] = aPathValue;
400                     iLine++;
401                 }
402                 //System.printI(0xab4);
403             }
404         }
405         //System.printI(0xab5);
406         //
407         // Record the actual number of accepted data points.
408         nAcceptedPathValue = iLine;
409         //
410         // Now to fill in the structures from the 'PathId' class.
411         set_name("rate");
412         set_startDate(pathDate[0]);
413         set_endDate(pathDate[nAcceptedPathValue-1]);
414         set_dTime((float)(1.0/365.0));
415         //System.printI(0xab6);
416     }
417 }