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 **************************************************************************/
26 * Class for representing the returns of a given security.
30 * <li>Define a window over which the mean drift and volatility
31 * are calculated.</li>
32 * <li>Hash table to reference {DATE}->{pathValue-index}.</li>
36 * @version $Revision: 1.2 $ $Date: 2011/07/14 21:28:29 $
38 public class ReturnPath extends PathId {
40 * Flag for indicating one of the return definitions, via:
41 * u_i = \ln{\frac{S_i}{S_{i-1}}}
42 * corresponding to the instantaneous compounded return.
44 public int COMPOUNDED;
47 * Flag for indicating one of the return definitions, via:
48 * u_i = \frac{S_i - S_{i-1}}{S_i}
49 * corresponding to the instantaneous non-compounded return.
51 public int NONCOMPOUNDED;
53 //------------------------------------------------------------------------
54 // Instance variables.
55 //------------------------------------------------------------------------
57 * An instance variable, for storing the return values.
59 public float[] pathValue;
61 * The number of accepted values in the rate path.
63 public int nPathValue;
65 * Integer flag for indicating how the return was calculated.
67 public int returnDefinition;
69 * Value for the expected return rate.
71 public float expectedReturnRate;
73 * Value for the volatility, calculated from the return data.
75 public float volatility;
77 * Value for the volatility-squared, a more natural quantity
78 * to use for many of the calculations.
80 public float volatility2;
82 * Value for the mean of this return.
86 * Value for the variance of this return.
88 public float variance;
90 //------------------------------------------------------------------------
92 //------------------------------------------------------------------------
94 * Default constructor.
100 this.NONCOMPOUNDED = 2;
102 this.returnDefinition = -1;
103 this.expectedReturnRate = (float)0.0;
104 this.volatility = (float)0.0;
105 this.volatility2 = (float)0.0;
106 this.mean = (float)0.0;
107 this.variance = (float)0.0;
111 * Another constructor.
113 * @param pathValue for creating a return path with a precomputed path
114 * value. Indexed from 1 to <code>nPathArray-1</code>.
115 * @param nPathValue the number of accepted data points in the array.
116 * @param returnDefinition to tell this class how the return path values
119 public ReturnPath(float[] pathValue,
121 int returnDefinition) {
122 this.pathValue = pathValue;
123 this.nPathValue = nPathValue;
124 this.returnDefinition = returnDefinition;
127 this.NONCOMPOUNDED = 2;
128 this.expectedReturnRate = (float)0.0;
129 this.volatility = (float)0.0;
130 this.volatility2 = (float)0.0;
131 this.mean = (float)0.0;
132 this.variance = (float)0.0;
135 //------------------------------------------------------------------------
137 //------------------------------------------------------------------------
138 //------------------------------------------------------------------------
139 // Accessor methods for class ReturnPath.
140 // Generated by 'makeJavaAccessor.pl' script. HWY. 20th January 1999.
141 //------------------------------------------------------------------------
143 * Accessor method for private instance variable <code>pathValue</code>.
145 * @return Value of instance variable <code>pathValue</code>.
146 * @exception DemoException thrown if instance variable <code>pathValue</code> is undefined.
148 /*public float[] get_pathValue(){
149 return(this.pathValue);
152 * Set method for private instance variable <code>pathValue</code>.
154 * @param pathValue the value to set for the instance variable <code>pathValue</code>.
156 public void set_pathValue(float[] pathValue) {
157 this.pathValue = pathValue;
160 * Accessor method for private instance variable <code>nPathValue</code>.
162 * @return Value of instance variable <code>nPathValue</code>.
163 * @exception DemoException thrown if instance variable <code>nPathValue</code> is undefined.
165 /*public int get_nPathValue() {
166 return(this.nPathValue);
169 * Set method for private instance variable <code>nPathValue</code>.
171 * @param nPathValue the value to set for the instance variable <code>nPathValue</code>.
173 public void set_nPathValue(int nPathValue) {
174 this.nPathValue = nPathValue;
177 * Accessor method for private instance variable <code>returnDefinition</code>.
179 * @return Value of instance variable <code>returnDefinition</code>.
180 * @exception DemoException thrown if instance variable <code>returnDefinition</code> is undefined.
182 /*public int get_returnDefinition() {
183 return(this.returnDefinition);
186 * Set method for private instance variable <code>returnDefinition</code>.
188 * @param returnDefinition the value to set for the instance variable <code>returnDefinition</code>.
190 public void set_returnDefinition(int returnDefinition) {
191 this.returnDefinition = returnDefinition;
194 * Accessor method for private instance variable <code>expectedReturnRate</code>.
196 * @return Value of instance variable <code>expectedReturnRate</code>.
197 * @exception DemoException thrown if instance variable <code>expectedReturnRate</code> is undefined.
199 /*public float get_expectedReturnRate() {
200 return(this.expectedReturnRate);
203 * Set method for private instance variable <code>expectedReturnRate</code>.
205 * @param expectedReturnRate the value to set for the instance variable <code>expectedReturnRate</code>.
207 public void set_expectedReturnRate(float expectedReturnRate) {
208 this.expectedReturnRate = expectedReturnRate;
211 * Accessor method for private instance variable <code>volatility</code>.
213 * @return Value of instance variable <code>volatility</code>.
214 * @exception DemoException thrown if instance variable <code>volatility</code> is undefined.
216 /*public float get_volatility() {
217 return(this.volatility);
220 * Set method for private instance variable <code>volatility</code>.
222 * @param volatility the value to set for the instance variable <code>volatility</code>.
224 public void set_volatility(float volatility) {
225 this.volatility = volatility;
228 * Accessor method for private instance variable <code>volatility2</code>.
230 * @return Value of instance variable <code>volatility2</code>.
231 * @exception DemoException thrown if instance variable <code>volatility2</code> is undefined.
233 /*public float get_volatility2() {
234 return(this.volatility2);
237 * Set method for private instance variable <code>volatility2</code>.
239 * @param volatility2 the value to set for the instance variable <code>volatility2</code>.
241 public void set_volatility2(float volatility2) {
242 this.volatility2 = volatility2;
245 * Accessor method for private instance variable <code>mean</code>.
247 * @return Value of instance variable <code>mean</code>.
248 * @exception DemoException thrown if instance variable <code>mean</code> is undefined.
250 /*public float get_mean() {
254 * Set method for private instance variable <code>mean</code>.
256 * @param mean the value to set for the instance variable <code>mean</code>.
258 public void set_mean(float mean) {
262 * Accessor method for private instance variable <code>variance</code>.
264 * @return Value of instance variable <code>variance</code>.
265 * @exception DemoException thrown if instance variable <code>variance</code> is undefined.
267 /*public float get_variance() {
268 return(this.variance);
271 * Set method for private instance variable <code>variance</code>.
273 * @param variance the value to set for the instance variable <code>variance</code>.
275 public void set_variance(float variance) {
276 this.variance = variance;
278 //------------------------------------------------------------------------
280 * Method to calculate the expected return rate from the return data,
281 * using the relationship:
282 * \mu = \frac{\bar{u}}{\Delta t} + \frac{\sigma^2}{2}
284 * @exception DemoException thrown one tries to obtain an undefined variable.
286 public void computeExpectedReturnRate() {
287 this.expectedReturnRate = mean/(float)this.dTime + (float)0.5*volatility2;
290 * Method to calculate <code>volatility</code> and <code>volatility2</code>
291 * from the return path data, using the relationship, based on the
292 * precomputed <code>variance</code>.
293 * \sigma^2 = s^2\Delta t
295 * @exception DemoException thrown if one of the quantites in the
296 * computation are undefined.
298 public void computeVolatility() {
299 this.volatility2 = this.variance / (float)this.dTime;
300 this.volatility = Math.sqrtf(volatility2);
303 * Method to calculate the mean of the return, for use by other
306 * @exception DemoException thrown if <code>nPathValue</code> is
309 public void computeMean() {
310 float sum = (float) 0.0;
311 float[] tmpvalue = this.pathValue;
312 int length = this.nPathValue;
313 for( int i=1; i < length; i++ ) {
316 this.mean = sum / ((float)(length - (float)1.0));
319 * Method to calculate the variance of the retrun, for use by other
322 * @exception DemoException thrown if the <code>mean</code> or
323 * <code>nPathValue</code> values are undefined.
325 public void computeVariance() {
326 float sum = (float) 0.0;
327 int length = this.nPathValue;
328 float[] tmpvalue = this.pathValue;
329 float tmpmean = this.mean;
330 for( int i=1; i < length; i++ ) {
331 sum += (tmpvalue[i] - tmpmean)*(tmpvalue[i] - tmpmean);
333 this.variance = sum / ((float)(length - (float)1.0));
336 * A single method for invoking all the necessary methods which
337 * estimate the parameters.
339 * @exception DemoException thrown if there is a problem reading any
342 public void estimatePath() {
345 computeExpectedReturnRate();
349 * Dumps the contents of the fields, to standard-out, for debugging.
351 public void dbgDumpFields() {
352 super.dbgDumpFields();
353 // dbgPrintln("nPathValue=" +this.nPathValue);
354 // dbgPrintln("expectedReturnRate="+this.expectedReturnRate);
355 // dbgPrintln("volatility=" +this.volatility);
356 // dbgPrintln("volatility2=" +this.volatility2);
357 // dbgPrintln("mean=" +this.mean);
358 // dbgPrintln("variance=" +this.variance);