3 /**************************************************************************
5 * Java Grande Forum Benchmark Suite - Thread Version 1.0 *
9 * Java Grande Benchmarking Project *
13 * Edinburgh Parallel Computing Centre *
15 * email: epcc-javagrande@epcc.ed.ac.uk *
17 * Original version of this code by Hon Yau (hwyau@epcc.ed.ac.uk) *
19 * This version copyright (c) The University of Edinburgh, 2001. *
20 * All rights reserved. *
22 **************************************************************************/
25 * Class for recording the values in the time-dependent path of a security.
33 * @version $Revision: 1.2 $ $Date: 2011/07/14 21:28:29 $
35 public class RatePath extends PathId {
37 //------------------------------------------------------------------------
39 //------------------------------------------------------------------------
41 * Class variable to represent the minimal date, whence the stock prices
42 * appear. Used to trap any potential problems with the data.
44 public int MINIMUMDATE;
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
52 //------------------------------------------------------------------------
53 // Instance variables.
54 //------------------------------------------------------------------------
56 * An instance variable, for storing the rate's path values itself.
58 public float[] pathValue;
60 * An instance variable, for storing the corresponding date of the datum,
61 * in 'YYYYMMDD' format.
63 public int[] pathDate;
65 * The number of accepted values in the rate path.
67 public int nAcceptedPathValue;
69 //------------------------------------------------------------------------
71 //------------------------------------------------------------------------
73 * Constructor, where the user specifies the directory and filename in
74 * from which the data should be read.
76 * @param String dirName
77 * @param String filename
78 * @exception DemoException thrown if there is a problem reading in
82 this.MINIMUMDATE = 19000101;
83 this.EPSILON= (float)10.0 * (float)(4.9E-324);
84 this.nAcceptedPathValue = 0;
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.
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
96 * @param startDate date from which the path is supposed to end, in
98 * @param dTime the time interval between successive path values, in
99 * fractions of a year.
101 public RatePath(float[] pathValue,
106 this.MINIMUMDATE = 19000101;
107 this.EPSILON= (float)10.0 * (float)(4.9E-324);
110 this.startDate = startDate;
111 this.endDate = endDate;
113 this.pathValue = pathValue;
114 this.nAcceptedPathValue = pathValue.length;
117 * Constructor, for use by the Monte Carlo generator, when it wishes
118 * to represent its findings as a RatePath object.
120 * @param mc the Monte Carlo generator object, whose data are to
122 * @exception DemoException thrown if there is an attempt to access
123 * an undefined variable.
125 public RatePath(MonteCarloPath mc) {
126 this.MINIMUMDATE = 19000101;
127 this.EPSILON= (float)10.0 * (float)(4.9E-324);
130 // Fields pertaining to the parent PathId object:
132 this.startDate = mc.startDate;
133 this.endDate = mc.endDate;
134 this.dTime = mc.dTime;
136 // Fields pertaining to RatePath object itself.
137 pathValue=mc.pathValue;
138 nAcceptedPathValue=mc.nTimeSteps;
140 // Note that currently the pathDate is neither declared, defined,
141 // nor used in the MonteCarloPath object.
142 pathDate=new int[nAcceptedPathValue];
145 * Constructor, for when there is no actual pathValue with which to
148 * @param pathValueLegth the length of the array containing the values
150 * @param name the name to attach to the path.
151 * @param startDate date from which the path is supposed to start, in
153 * @param startDate date from which the path is supposed to end, in
155 * @param dTime the time interval between successive path values, in
156 * fractions of a year.
158 public RatePath(int pathValueLength,
163 this.MINIMUMDATE = 19000101;
164 this.EPSILON= (float)10.0 * (float)(4.9E-324);
167 this.startDate = startDate;
168 this.endDate = endDate;
170 this.pathValue = new float[pathValueLength];
171 this.nAcceptedPathValue = pathValue.length;
173 //------------------------------------------------------------------------
175 //------------------------------------------------------------------------
177 * Routine to update this rate path with the values from another rate
178 * path, via its pathValue array.
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.
184 public boolean inc_pathValue(float[] operandPath) {
185 int length = this.pathValue.length;
186 if( length != operandPath.length ) {
189 float[] pathvalue = this.pathValue;
190 for(int i=0; i<length; i++ ) {
191 pathvalue[i] += operandPath[i];
196 * Routine to scale this rate path by a constant.
198 * @param scale the constant with which to multiply to all the path
200 * @exception DemoException thrown if there is a mismatch between the
201 * lengths of the operand and target arrays.
203 public boolean inc_pathValue(float scale) {
204 float[] pathvalue = this.pathValue;
205 if( pathvalue==null ) {
208 int length = this.pathValue.length;
209 for(int i=0; i<length; i++ ) {
210 pathvalue[i] *= scale;
214 //------------------------------------------------------------------------
215 // Accessor methods for class RatePath.
216 // Generated by 'makeJavaAccessor.pl' script. HWY. 20th January 1999.
217 //------------------------------------------------------------------------
219 * Accessor method for private instance variable <code>pathValue</code>.
221 * @return Value of instance variable <code>pathValue</code>.
222 * @exception DemoException thrown if instance variable <code>pathValue</code> is undefined.
224 /*public float[] get_pathValue() {
225 return(this.pathValue);
228 * Set method for private instance variable <code>pathValue</code>.
230 * @param pathValue the value to set for the instance variable <code>pathValue</code>.
232 public void set_pathValue(float[] pathValue) {
233 this.pathValue = pathValue;
236 * Accessor method for private instance variable <code>pathDate</code>.
238 * @return Value of instance variable <code>pathDate</code>.
239 * @exception DemoException thrown if instance variable <code>pathDate</code> is undefined.
241 /*public int[] get_pathDate() {
242 return(this.pathDate);
245 * Set method for private instance variable <code>pathDate</code>.
247 * @param pathDate the value to set for the instance variable <code>pathDate</code>.
249 public void set_pathDate(int[] pathDate) {
250 this.pathDate = pathDate;
252 //------------------------------------------------------------------------
254 * Method to return the terminal value for a given rate path, as used
255 * in derivative calculations.
257 * @return The last value in the rate path.
259 public float getEndPathValue() {
260 return( getPathValue(pathValue.length-1) );
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>
266 * @param index the index on which to return the path value.
267 * @return The value of the path at the designated index.
269 public float getPathValue(int index) {
270 return(pathValue[index]);
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}}}
277 * @return the return, as defined.
278 * @exception DemoException thrown if there is a problem with the
281 public ReturnPath getReturnCompounded() {
282 int length = this.nAcceptedPathValue;
283 float[] pathvalue = this.pathValue;
284 if( pathvalue == null || length == 0 ) {
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]);
293 ReturnPath rPath = new ReturnPath(returnPathValue, length, 1);
295 // Copy the PathId information to the ReturnPath object.
296 rPath.copyInstanceVariables(this);
297 rPath.estimatePath();
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}
305 * @return the return, as defined.
306 * @exception DemoException thrown if there is a problem with the
309 public ReturnPath getReturnNonCompounded() {
310 int length = this.nAcceptedPathValue;
311 float[] pathvalue = this.pathValue;
312 if( pathvalue == null || length == 0 ) {
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];
321 ReturnPath rPath = new ReturnPath(returnPathValue, length, 2);
323 // Copy the PathId information to the ReturnPath object.
324 rPath.copyInstanceVariables(this);
325 rPath.estimatePath();
328 //------------------------------------------------------------------------
330 //------------------------------------------------------------------------
332 * Method for reading in data file, in a given format.
335 881003,0.0000,14.1944,13.9444,14.0832,2200050,0
336 881004,0.0000,14.1668,14.0556,14.1668,1490850,0
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
342 * <p>Where the fields represent, one believes, the following:
344 * <li>The date in 'YYMMDD' format</li>
350 * <li>Open Interest</li>
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.
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
363 private void readRatesFile(){
365 // Now create an array to store the rates data.
366 int minimumdate = MINIMUMDATE;
367 float epsilon = EPSILON;
368 int nLines = 1000; //200;
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;
378 /*char[] date = new char[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');
388 int aDate = 19881003;
389 /*for(int di = 0; di < 9; di++) {
390 aDate = aDate * 10 + (int)date[di];
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);
398 date += String.valueOf(month);
402 date += String.valueOf(day);*/
403 //int aDate = Integer.parseInt(date);
406 /*if(date[7] == '9') {
408 date[6] = (char)(date[6] + 1);
410 date[7] = (char)(date[7] + 1);
419 aDate += 72;// - day(29) + 101;
438 date[0] = (char)(date[0] + 1);
440 date[1] = (char)(date[1] + 1);
444 date[2] = (char)(date[2] + 1);
448 date[3] = (char)(date[3] + 1);
455 date[5] = (char)(date[5] + 1);
461 // static float float.parsefloat() method is a feature of JDK1.2!
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+".");
467 pathdate[iLine] = aDate;
468 pathvalue[iLine] = aPathValue;
474 // Record the actual number of accepted data points.
475 nAcceptedPathValue = iLine;
477 // Now to fill in the structures from the 'PathId' class.
479 this.startDate = pathdate[0];
480 this.endDate = pathdate[iLine-1];
481 this.dTime = (float)(1.0/365.0);