1f1eaee9c91ce4a95fef6db0e6a4b79bfceaa2d4
[IRC.git] / Robust / src / Benchmarks / Scheduling / GC / NON_BAMBOO / JGFMonteCarlo / ReturnPath.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 /**
26   * Class for representing the returns of a given security.
27   *
28   * <p>To do list:
29   * <ol>
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>
33   * </ol>
34   *
35   * @author H W Yau
36   * @version $Revision: 1.1 $ $Date: 2011/07/13 23:49:52 $
37   */
38 public class ReturnPath extends PathId {
39   /**
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.
43     */
44   public int COMPOUNDED;
45
46   /**
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.
50     */
51   public int NONCOMPOUNDED;
52
53   //------------------------------------------------------------------------
54   // Instance variables.
55   //------------------------------------------------------------------------
56   /**
57     * An instance variable, for storing the return values.
58     */
59   public float[] pathValue;
60   /**
61     * The number of accepted values in the rate path.
62     */
63   public int nPathValue;
64   /**
65     * Integer flag for indicating how the return was calculated.
66     */
67   public int returnDefinition;
68   /**
69     * Value for the expected return rate.
70     */
71   public float expectedReturnRate;
72   /**
73     * Value for the volatility, calculated from the return data.
74     */
75   public float volatility;
76   /**
77     * Value for the volatility-squared, a more natural quantity
78     * to use for many of the calculations.
79     */
80   public float volatility2;
81   /**
82     * Value for the mean of this return.
83     */
84   public float mean;
85   /**
86     * Value for the variance of this return.
87     */
88   public float variance;
89
90   //------------------------------------------------------------------------
91   // Constructors.
92   //------------------------------------------------------------------------
93   /**
94     * Default constructor.
95     */
96   public ReturnPath() {
97     super();
98     
99     this.COMPOUNDED = 1;
100     this.NONCOMPOUNDED = 2;
101     this.nPathValue=-1;
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;
108   }
109
110   /**
111     * Another constructor.
112     *
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
117     *                         were computed.
118     */
119   public ReturnPath(float[] pathValue, 
120                     int nPathValue, 
121                     int returnDefinition) {
122     this.pathValue = pathValue;
123     this.nPathValue = nPathValue;
124     this.returnDefinition = returnDefinition;
125     
126     this.COMPOUNDED = 1;
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;
133   }
134
135   //------------------------------------------------------------------------
136   // Methods.
137   //------------------------------------------------------------------------
138   //------------------------------------------------------------------------
139   // Accessor methods for class ReturnPath.
140   // Generated by 'makeJavaAccessor.pl' script.  HWY.  20th January 1999.
141   //------------------------------------------------------------------------
142   /**
143     * Accessor method for private instance variable <code>pathValue</code>.
144     *
145     * @return Value of instance variable <code>pathValue</code>.
146     * @exception DemoException thrown if instance variable <code>pathValue</code> is undefined.
147     */
148   /*public float[] get_pathValue(){
149     return(this.pathValue);
150   }*/
151   /**
152     * Set method for private instance variable <code>pathValue</code>.
153     *
154     * @param pathValue the value to set for the instance variable <code>pathValue</code>.
155     */
156   public void set_pathValue(float[] pathValue) {
157     this.pathValue = pathValue;
158   }
159   /**
160     * Accessor method for private instance variable <code>nPathValue</code>.
161     *
162     * @return Value of instance variable <code>nPathValue</code>.
163     * @exception DemoException thrown if instance variable <code>nPathValue</code> is undefined.
164     */
165   /*public int get_nPathValue() {
166     return(this.nPathValue);
167   }*/
168   /**
169     * Set method for private instance variable <code>nPathValue</code>.
170     *
171     * @param nPathValue the value to set for the instance variable <code>nPathValue</code>.
172     */
173   public void set_nPathValue(int nPathValue) {
174     this.nPathValue = nPathValue;
175   }
176   /**
177     * Accessor method for private instance variable <code>returnDefinition</code>.
178     *
179     * @return Value of instance variable <code>returnDefinition</code>.
180     * @exception DemoException thrown if instance variable <code>returnDefinition</code> is undefined.
181     */
182   /*public int get_returnDefinition() {
183     return(this.returnDefinition);
184   }*/
185   /**
186     * Set method for private instance variable <code>returnDefinition</code>.
187     *
188     * @param returnDefinition the value to set for the instance variable <code>returnDefinition</code>.
189     */
190   public void set_returnDefinition(int returnDefinition) {
191     this.returnDefinition = returnDefinition;
192   }
193   /**
194     * Accessor method for private instance variable <code>expectedReturnRate</code>.
195     *
196     * @return Value of instance variable <code>expectedReturnRate</code>.
197     * @exception DemoException thrown if instance variable <code>expectedReturnRate</code> is undefined.
198     */
199   /*public float get_expectedReturnRate() {
200     return(this.expectedReturnRate);
201   }*/
202   /**
203     * Set method for private instance variable <code>expectedReturnRate</code>.
204     *
205     * @param expectedReturnRate the value to set for the instance variable <code>expectedReturnRate</code>.
206     */
207   public void set_expectedReturnRate(float expectedReturnRate) {
208     this.expectedReturnRate = expectedReturnRate;
209   }
210   /**
211     * Accessor method for private instance variable <code>volatility</code>.
212     *
213     * @return Value of instance variable <code>volatility</code>.
214     * @exception DemoException thrown if instance variable <code>volatility</code> is undefined.
215     */
216   /*public float get_volatility() {
217     return(this.volatility);
218   }*/
219   /**
220     * Set method for private instance variable <code>volatility</code>.
221     *
222     * @param volatility the value to set for the instance variable <code>volatility</code>.
223     */
224   public void set_volatility(float volatility) {
225     this.volatility = volatility;
226   }
227   /**
228     * Accessor method for private instance variable <code>volatility2</code>.
229     *
230     * @return Value of instance variable <code>volatility2</code>.
231     * @exception DemoException thrown if instance variable <code>volatility2</code> is undefined.
232     */
233   /*public float get_volatility2() {
234     return(this.volatility2);
235   }*/
236   /**
237     * Set method for private instance variable <code>volatility2</code>.
238     *
239     * @param volatility2 the value to set for the instance variable <code>volatility2</code>.
240     */
241   public void set_volatility2(float volatility2) {
242     this.volatility2 = volatility2;
243   }
244   /**
245     * Accessor method for private instance variable <code>mean</code>.
246     *
247     * @return Value of instance variable <code>mean</code>.
248     * @exception DemoException thrown if instance variable <code>mean</code> is undefined.
249     */
250   /*public float get_mean() {
251     return(this.mean);
252   }*/
253   /**
254     * Set method for private instance variable <code>mean</code>.
255     *
256     * @param mean the value to set for the instance variable <code>mean</code>.
257     */
258   public void set_mean(float mean) {
259     this.mean = mean;
260   }
261   /**
262     * Accessor method for private instance variable <code>variance</code>.
263     *
264     * @return Value of instance variable <code>variance</code>.
265     * @exception DemoException thrown if instance variable <code>variance</code> is undefined.
266     */
267   /*public float get_variance() {
268     return(this.variance);
269   }*/
270   /**
271     * Set method for private instance variable <code>variance</code>.
272     *
273     * @param variance the value to set for the instance variable <code>variance</code>.
274     */
275   public void set_variance(float variance) {
276     this.variance = variance;
277   }
278   //------------------------------------------------------------------------
279   /**
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}
283     *
284     * @exception DemoException thrown one tries to obtain an undefined variable.
285     */
286   public void computeExpectedReturnRate() {
287     this.expectedReturnRate = mean/(float)this.dTime + (float)0.5*volatility2;
288   }
289   /**
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
294     * 
295     * @exception DemoException thrown if one of the quantites in the
296     *                          computation are undefined.
297     */
298   public void computeVolatility() {
299     this.volatility2 = this.variance / (float)this.dTime;
300     this.volatility  = Math.sqrtf(volatility2);
301   }
302   /**
303     * Method to calculate the mean of the return, for use by other
304     * calculations.
305     *
306     * @exception DemoException thrown if <code>nPathValue</code> is
307     *            undefined.
308     */
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++ ) {
314           sum += tmpvalue[i];
315       }
316       this.mean = sum / ((float)(length - (float)1.0));
317   }
318   /**
319     * Method to calculate the variance of the retrun, for use by other
320     * calculations.
321     *
322     * @exception DemoException thrown if the <code>mean</code> or
323     *            <code>nPathValue</code> values are undefined.
324     */
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);
332     }
333     this.variance = sum / ((float)(length - (float)1.0));
334   }
335   /**
336     * A single method for invoking all the necessary methods which
337     * estimate the parameters.
338     *
339     * @exception DemoException thrown if there is a problem reading any
340     *            variables.
341     */
342   public void estimatePath() {
343     computeMean();
344     computeVariance();
345     computeExpectedReturnRate();
346     computeVolatility();
347   }
348   /**
349     * Dumps the contents of the fields, to standard-out, for debugging.
350     */
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);
359   }
360 }