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.1 $ $Date: 2008/08/18 22:22:21 $
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 private float[] pathValue;
60 * An instance variable, for storing the corresponding date of the datum,
61 * in 'YYYYMMDD' format.
63 private int[] pathDate;
65 * The number of accepted values in the rate path.
67 private 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;
85 //System.printI(0xaa0);
87 //System.printI(0xaa1);
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.
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
98 * @param startDate date from which the path is supposed to end, in
100 * @param dTime the time interval between successive path values, in
101 * fractions of a year.
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);
108 set_startDate(startDate);
109 set_endDate(endDate);
111 this.pathValue = pathValue;
112 this.nAcceptedPathValue = pathValue.length;
115 * Constructor, for use by the Monte Carlo generator, when it wishes
116 * to represent its findings as a RatePath object.
118 * @param mc the Monte Carlo generator object, whose data are to
120 * @exception DemoException thrown if there is an attempt to access
121 * an undefined variable.
123 public RatePath(MonteCarloPath mc) {
124 this.MINIMUMDATE = 19000101;
125 this.EPSILON= (float)10.0 * (float)(4.9E-324);
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());
134 // Fields pertaining to RatePath object itself.
135 pathValue=mc.get_pathValue();
136 nAcceptedPathValue=mc.get_nTimeSteps();
138 // Note that currently the pathDate is neither declared, defined,
139 // nor used in the MonteCarloPath object.
140 pathDate=new int[nAcceptedPathValue];
143 * Constructor, for when there is no actual pathValue with which to
146 * @param pathValueLegth the length of the array containing the values
148 * @param name the name to attach to the path.
149 * @param startDate date from which the path is supposed to start, in
151 * @param startDate date from which the path is supposed to end, in
153 * @param dTime the time interval between successive path values, in
154 * fractions of a year.
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);
161 set_startDate(startDate);
162 set_endDate(endDate);
164 this.pathValue = new float[pathValueLength];
165 this.nAcceptedPathValue = pathValue.length;
167 //------------------------------------------------------------------------
169 //------------------------------------------------------------------------
171 * Routine to update this rate path with the values from another rate
172 * path, via its pathValue array.
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.
178 public boolean inc_pathValue(float[] operandPath) {
179 if( pathValue.length != operandPath.length ) {
182 for(int i=0; i<pathValue.length; i++ ) {
183 pathValue[i] += operandPath[i];
188 * Routine to scale this rate path by a constant.
190 * @param scale the constant with which to multiply to all the path
192 * @exception DemoException thrown if there is a mismatch between the
193 * lengths of the operand and target arrays.
195 public boolean inc_pathValue(float scale) {
196 if( pathValue==null ) {
199 for(int i=0; i<pathValue.length; i++ ) {
200 pathValue[i] *= scale;
204 //------------------------------------------------------------------------
205 // Accessor methods for class RatePath.
206 // Generated by 'makeJavaAccessor.pl' script. HWY. 20th January 1999.
207 //------------------------------------------------------------------------
209 * Accessor method for private instance variable <code>pathValue</code>.
211 * @return Value of instance variable <code>pathValue</code>.
212 * @exception DemoException thrown if instance variable <code>pathValue</code> is undefined.
214 public float[] get_pathValue() {
215 return(this.pathValue);
218 * Set method for private instance variable <code>pathValue</code>.
220 * @param pathValue the value to set for the instance variable <code>pathValue</code>.
222 public void set_pathValue(float[] pathValue) {
223 this.pathValue = pathValue;
226 * Accessor method for private instance variable <code>pathDate</code>.
228 * @return Value of instance variable <code>pathDate</code>.
229 * @exception DemoException thrown if instance variable <code>pathDate</code> is undefined.
231 public int[] get_pathDate() {
232 return(this.pathDate);
235 * Set method for private instance variable <code>pathDate</code>.
237 * @param pathDate the value to set for the instance variable <code>pathDate</code>.
239 public void set_pathDate(int[] pathDate) {
240 this.pathDate = pathDate;
242 //------------------------------------------------------------------------
244 * Method to return the terminal value for a given rate path, as used
245 * in derivative calculations.
247 * @return The last value in the rate path.
249 public float getEndPathValue() {
250 return( getPathValue(pathValue.length-1) );
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>
256 * @param index the index on which to return the path value.
257 * @return The value of the path at the designated index.
259 public float getPathValue(int index) {
260 return(pathValue[index]);
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}}}
267 * @return the return, as defined.
268 * @exception DemoException thrown if there is a problem with the
271 public ReturnPath getReturnCompounded() {
272 if( pathValue == null || nAcceptedPathValue == 0 ) {
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]);
281 ReturnPath rPath = new ReturnPath(returnPathValue, nAcceptedPathValue, 1);
283 // Copy the PathId information to the ReturnPath object.
284 rPath.copyInstanceVariables(this);
285 rPath.estimatePath();
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}
293 * @return the return, as defined.
294 * @exception DemoException thrown if there is a problem with the
297 public ReturnPath getReturnNonCompounded() {
298 if( pathValue == null || nAcceptedPathValue == 0 ) {
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];
307 ReturnPath rPath = new ReturnPath(returnPathValue, nAcceptedPathValue, 2);
309 // Copy the PathId information to the ReturnPath object.
310 rPath.copyInstanceVariables(this);
311 rPath.estimatePath();
314 //------------------------------------------------------------------------
316 //------------------------------------------------------------------------
318 * Method for reading in data file, in a given format.
321 881003,0.0000,14.1944,13.9444,14.0832,2200050,0
322 881004,0.0000,14.1668,14.0556,14.1668,1490850,0
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
328 * <p>Where the fields represent, one believes, the following:
330 * <li>The date in 'YYMMDD' format</li>
336 * <li>Open Interest</li>
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.
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
349 private void readRatesFile(){
351 // Now create an array to store the rates data.
356 //System.printI(0xab0);
357 this.pathValue = new float[nLines];
358 this.pathDate = new int[nLines];
359 nAcceptedPathValue=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);
369 date += String.valueOf(month);
373 date += String.valueOf(day);
374 int aDate = Integer.parseInt(date);
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+".");
398 pathDate[iLine] = aDate;
399 pathValue[iLine] = aPathValue;
402 //System.printI(0xab4);
405 //System.printI(0xab5);
407 // Record the actual number of accepted data points.
408 nAcceptedPathValue = iLine;
410 // Now to fill in the structures from the 'PathId' class.
412 set_startDate(pathDate[0]);
413 set_endDate(pathDate[nAcceptedPathValue-1]);
414 set_dTime((float)(1.0/365.0));
415 //System.printI(0xab6);