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.1 $ $Date: 2008/08/18 22:22:21 $
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 private float[] pathValue;
61 * The number of accepted values in the rate path.
63 private int nPathValue;
65 * Integer flag for indicating how the return was calculated.
67 private int returnDefinition;
69 * Value for the expected return rate.
71 private float expectedReturnRate;
73 * Value for the volatility, calculated from the return data.
75 private float volatility;
77 * Value for the volatility-squared, a more natural quantity
78 * to use for many of the calculations.
80 private float volatility2;
82 * Value for the mean of this return.
86 * Value for the variance of this return.
88 private 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, int nPathValue, int returnDefinition) {
120 this.pathValue = pathValue;
121 this.nPathValue = nPathValue;
122 this.returnDefinition = returnDefinition;
125 this.NONCOMPOUNDED = 2;
126 this.expectedReturnRate = (float)0.0;
127 this.volatility = (float)0.0;
128 this.volatility2 = (float)0.0;
129 this.mean = (float)0.0;
130 this.variance = (float)0.0;
133 //------------------------------------------------------------------------
135 //------------------------------------------------------------------------
136 //------------------------------------------------------------------------
137 // Accessor methods for class ReturnPath.
138 // Generated by 'makeJavaAccessor.pl' script. HWY. 20th January 1999.
139 //------------------------------------------------------------------------
141 * Accessor method for private instance variable <code>pathValue</code>.
143 * @return Value of instance variable <code>pathValue</code>.
144 * @exception DemoException thrown if instance variable <code>pathValue</code> is undefined.
146 public float[] get_pathValue(){
147 return(this.pathValue);
150 * Set method for private instance variable <code>pathValue</code>.
152 * @param pathValue the value to set for the instance variable <code>pathValue</code>.
154 public void set_pathValue(float[] pathValue) {
155 this.pathValue = pathValue;
158 * Accessor method for private instance variable <code>nPathValue</code>.
160 * @return Value of instance variable <code>nPathValue</code>.
161 * @exception DemoException thrown if instance variable <code>nPathValue</code> is undefined.
163 public int get_nPathValue() {
164 return(this.nPathValue);
167 * Set method for private instance variable <code>nPathValue</code>.
169 * @param nPathValue the value to set for the instance variable <code>nPathValue</code>.
171 public void set_nPathValue(int nPathValue) {
172 this.nPathValue = nPathValue;
175 * Accessor method for private instance variable <code>returnDefinition</code>.
177 * @return Value of instance variable <code>returnDefinition</code>.
178 * @exception DemoException thrown if instance variable <code>returnDefinition</code> is undefined.
180 public int get_returnDefinition() {
181 return(this.returnDefinition);
184 * Set method for private instance variable <code>returnDefinition</code>.
186 * @param returnDefinition the value to set for the instance variable <code>returnDefinition</code>.
188 public void set_returnDefinition(int returnDefinition) {
189 this.returnDefinition = returnDefinition;
192 * Accessor method for private instance variable <code>expectedReturnRate</code>.
194 * @return Value of instance variable <code>expectedReturnRate</code>.
195 * @exception DemoException thrown if instance variable <code>expectedReturnRate</code> is undefined.
197 public float get_expectedReturnRate() {
198 return(this.expectedReturnRate);
201 * Set method for private instance variable <code>expectedReturnRate</code>.
203 * @param expectedReturnRate the value to set for the instance variable <code>expectedReturnRate</code>.
205 public void set_expectedReturnRate(float expectedReturnRate) {
206 this.expectedReturnRate = expectedReturnRate;
209 * Accessor method for private instance variable <code>volatility</code>.
211 * @return Value of instance variable <code>volatility</code>.
212 * @exception DemoException thrown if instance variable <code>volatility</code> is undefined.
214 public float get_volatility() {
215 return(this.volatility);
218 * Set method for private instance variable <code>volatility</code>.
220 * @param volatility the value to set for the instance variable <code>volatility</code>.
222 public void set_volatility(float volatility) {
223 this.volatility = volatility;
226 * Accessor method for private instance variable <code>volatility2</code>.
228 * @return Value of instance variable <code>volatility2</code>.
229 * @exception DemoException thrown if instance variable <code>volatility2</code> is undefined.
231 public float get_volatility2() {
232 return(this.volatility2);
235 * Set method for private instance variable <code>volatility2</code>.
237 * @param volatility2 the value to set for the instance variable <code>volatility2</code>.
239 public void set_volatility2(float volatility2) {
240 this.volatility2 = volatility2;
243 * Accessor method for private instance variable <code>mean</code>.
245 * @return Value of instance variable <code>mean</code>.
246 * @exception DemoException thrown if instance variable <code>mean</code> is undefined.
248 public float get_mean() {
252 * Set method for private instance variable <code>mean</code>.
254 * @param mean the value to set for the instance variable <code>mean</code>.
256 public void set_mean(float mean) {
260 * Accessor method for private instance variable <code>variance</code>.
262 * @return Value of instance variable <code>variance</code>.
263 * @exception DemoException thrown if instance variable <code>variance</code> is undefined.
265 public float get_variance() {
266 return(this.variance);
269 * Set method for private instance variable <code>variance</code>.
271 * @param variance the value to set for the instance variable <code>variance</code>.
273 public void set_variance(float variance) {
274 this.variance = variance;
276 //------------------------------------------------------------------------
278 * Method to calculate the expected return rate from the return data,
279 * using the relationship:
280 * \mu = \frac{\bar{u}}{\Delta t} + \frac{\sigma^2}{2}
282 * @exception DemoException thrown one tries to obtain an undefined variable.
284 public void computeExpectedReturnRate() {
285 this.expectedReturnRate = mean/(float)get_dTime() + (float)0.5*volatility2;
288 * Method to calculate <code>volatility</code> and <code>volatility2</code>
289 * from the return path data, using the relationship, based on the
290 * precomputed <code>variance</code>.
291 * \sigma^2 = s^2\Delta t
293 * @exception DemoException thrown if one of the quantites in the
294 * computation are undefined.
296 public void computeVolatility() {
297 this.volatility2 = this.variance / (float)get_dTime();
298 this.volatility = Math.sqrtf(volatility2);
301 * Method to calculate the mean of the return, for use by other
304 * @exception DemoException thrown if <code>nPathValue</code> is
307 public void computeMean() {
308 this.mean = (float)0.0;
309 for( int i=1; i < nPathValue; i++ ) {
310 mean += pathValue[i];
312 this.mean /= ((float)(nPathValue - (float)1.0));
315 * Method to calculate the variance of the retrun, for use by other
318 * @exception DemoException thrown if the <code>mean</code> or
319 * <code>nPathValue</code> values are undefined.
321 public void computeVariance() {
322 this.variance = (float)0.0;
323 for( int i=1; i < nPathValue; i++ ) {
324 variance += (pathValue[i] - mean)*(pathValue[i] - mean);
326 this.variance /= ((float)(nPathValue - (float)1.0));
329 * A single method for invoking all the necessary methods which
330 * estimate the parameters.
332 * @exception DemoException thrown if there is a problem reading any
335 public void estimatePath() {
337 //System.printI(0xb0);
339 //System.printI(0xb1);
340 computeExpectedReturnRate();
341 //System.printI(0xb2);
343 //System.printI(0xb3);
346 * Dumps the contents of the fields, to standard-out, for debugging.
348 public void dbgDumpFields() {
349 super.dbgDumpFields();
350 // dbgPrintln("nPathValue=" +this.nPathValue);
351 // dbgPrintln("expectedReturnRate="+this.expectedReturnRate);
352 // dbgPrintln("volatility=" +this.volatility);
353 // dbgPrintln("volatility2=" +this.volatility2);
354 // dbgPrintln("mean=" +this.mean);
355 // dbgPrintln("variance=" +this.variance);