helpful progress reporting
[IRC.git] / Robust / src / Benchmarks / Scheduling / 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: 2008/08/18 22:22:21 $
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   private float[] pathValue;
60   /**
61     * The number of accepted values in the rate path.
62     */
63   private int nPathValue;
64   /**
65     * Integer flag for indicating how the return was calculated.
66     */
67   private int returnDefinition;
68   /**
69     * Value for the expected return rate.
70     */
71   private float expectedReturnRate;
72   /**
73     * Value for the volatility, calculated from the return data.
74     */
75   private float volatility;
76   /**
77     * Value for the volatility-squared, a more natural quantity
78     * to use for many of the calculations.
79     */
80   private float volatility2;
81   /**
82     * Value for the mean of this return.
83     */
84   private float mean;
85   /**
86     * Value for the variance of this return.
87     */
88   private 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, int nPathValue, int returnDefinition) {
120     this.pathValue = pathValue;
121     this.nPathValue = nPathValue;
122     this.returnDefinition = returnDefinition;
123     
124     this.COMPOUNDED = 1;
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;
131   }
132
133   //------------------------------------------------------------------------
134   // Methods.
135   //------------------------------------------------------------------------
136   //------------------------------------------------------------------------
137   // Accessor methods for class ReturnPath.
138   // Generated by 'makeJavaAccessor.pl' script.  HWY.  20th January 1999.
139   //------------------------------------------------------------------------
140   /**
141     * Accessor method for private instance variable <code>pathValue</code>.
142     *
143     * @return Value of instance variable <code>pathValue</code>.
144     * @exception DemoException thrown if instance variable <code>pathValue</code> is undefined.
145     */
146   public float[] get_pathValue(){
147     return(this.pathValue);
148   }
149   /**
150     * Set method for private instance variable <code>pathValue</code>.
151     *
152     * @param pathValue the value to set for the instance variable <code>pathValue</code>.
153     */
154   public void set_pathValue(float[] pathValue) {
155     this.pathValue = pathValue;
156   }
157   /**
158     * Accessor method for private instance variable <code>nPathValue</code>.
159     *
160     * @return Value of instance variable <code>nPathValue</code>.
161     * @exception DemoException thrown if instance variable <code>nPathValue</code> is undefined.
162     */
163   public int get_nPathValue() {
164     return(this.nPathValue);
165   }
166   /**
167     * Set method for private instance variable <code>nPathValue</code>.
168     *
169     * @param nPathValue the value to set for the instance variable <code>nPathValue</code>.
170     */
171   public void set_nPathValue(int nPathValue) {
172     this.nPathValue = nPathValue;
173   }
174   /**
175     * Accessor method for private instance variable <code>returnDefinition</code>.
176     *
177     * @return Value of instance variable <code>returnDefinition</code>.
178     * @exception DemoException thrown if instance variable <code>returnDefinition</code> is undefined.
179     */
180   public int get_returnDefinition() {
181     return(this.returnDefinition);
182   }
183   /**
184     * Set method for private instance variable <code>returnDefinition</code>.
185     *
186     * @param returnDefinition the value to set for the instance variable <code>returnDefinition</code>.
187     */
188   public void set_returnDefinition(int returnDefinition) {
189     this.returnDefinition = returnDefinition;
190   }
191   /**
192     * Accessor method for private instance variable <code>expectedReturnRate</code>.
193     *
194     * @return Value of instance variable <code>expectedReturnRate</code>.
195     * @exception DemoException thrown if instance variable <code>expectedReturnRate</code> is undefined.
196     */
197   public float get_expectedReturnRate() {
198     return(this.expectedReturnRate);
199   }
200   /**
201     * Set method for private instance variable <code>expectedReturnRate</code>.
202     *
203     * @param expectedReturnRate the value to set for the instance variable <code>expectedReturnRate</code>.
204     */
205   public void set_expectedReturnRate(float expectedReturnRate) {
206     this.expectedReturnRate = expectedReturnRate;
207   }
208   /**
209     * Accessor method for private instance variable <code>volatility</code>.
210     *
211     * @return Value of instance variable <code>volatility</code>.
212     * @exception DemoException thrown if instance variable <code>volatility</code> is undefined.
213     */
214   public float get_volatility() {
215     return(this.volatility);
216   }
217   /**
218     * Set method for private instance variable <code>volatility</code>.
219     *
220     * @param volatility the value to set for the instance variable <code>volatility</code>.
221     */
222   public void set_volatility(float volatility) {
223     this.volatility = volatility;
224   }
225   /**
226     * Accessor method for private instance variable <code>volatility2</code>.
227     *
228     * @return Value of instance variable <code>volatility2</code>.
229     * @exception DemoException thrown if instance variable <code>volatility2</code> is undefined.
230     */
231   public float get_volatility2() {
232     return(this.volatility2);
233   }
234   /**
235     * Set method for private instance variable <code>volatility2</code>.
236     *
237     * @param volatility2 the value to set for the instance variable <code>volatility2</code>.
238     */
239   public void set_volatility2(float volatility2) {
240     this.volatility2 = volatility2;
241   }
242   /**
243     * Accessor method for private instance variable <code>mean</code>.
244     *
245     * @return Value of instance variable <code>mean</code>.
246     * @exception DemoException thrown if instance variable <code>mean</code> is undefined.
247     */
248   public float get_mean() {
249     return(this.mean);
250   }
251   /**
252     * Set method for private instance variable <code>mean</code>.
253     *
254     * @param mean the value to set for the instance variable <code>mean</code>.
255     */
256   public void set_mean(float mean) {
257     this.mean = mean;
258   }
259   /**
260     * Accessor method for private instance variable <code>variance</code>.
261     *
262     * @return Value of instance variable <code>variance</code>.
263     * @exception DemoException thrown if instance variable <code>variance</code> is undefined.
264     */
265   public float get_variance() {
266     return(this.variance);
267   }
268   /**
269     * Set method for private instance variable <code>variance</code>.
270     *
271     * @param variance the value to set for the instance variable <code>variance</code>.
272     */
273   public void set_variance(float variance) {
274     this.variance = variance;
275   }
276   //------------------------------------------------------------------------
277   /**
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}
281     *
282     * @exception DemoException thrown one tries to obtain an undefined variable.
283     */
284   public void computeExpectedReturnRate() {
285     this.expectedReturnRate = mean/(float)get_dTime() + (float)0.5*volatility2;
286   }
287   /**
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
292     * 
293     * @exception DemoException thrown if one of the quantites in the
294     *                          computation are undefined.
295     */
296   public void computeVolatility() {
297     this.volatility2 = this.variance / (float)get_dTime();
298     this.volatility  = Math.sqrtf(volatility2);
299   }
300   /**
301     * Method to calculate the mean of the return, for use by other
302     * calculations.
303     *
304     * @exception DemoException thrown if <code>nPathValue</code> is
305     *            undefined.
306     */
307   public void computeMean() {
308     this.mean = (float)0.0;
309     for( int i=1; i < nPathValue; i++ ) {
310       mean += pathValue[i];
311     }
312     this.mean /= ((float)(nPathValue - (float)1.0));
313   }
314   /**
315     * Method to calculate the variance of the retrun, for use by other
316     * calculations.
317     *
318     * @exception DemoException thrown if the <code>mean</code> or
319     *            <code>nPathValue</code> values are undefined.
320     */
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);
325     }
326     this.variance /= ((float)(nPathValue - (float)1.0));
327   }
328   /**
329     * A single method for invoking all the necessary methods which
330     * estimate the parameters.
331     *
332     * @exception DemoException thrown if there is a problem reading any
333     *            variables.
334     */
335   public void estimatePath() {
336     computeMean();
337     //System.printI(0xb0);
338     computeVariance();
339     //System.printI(0xb1);
340     computeExpectedReturnRate();
341     //System.printI(0xb2);
342     computeVolatility();
343     //System.printI(0xb3);
344   }
345   /**
346     * Dumps the contents of the fields, to standard-out, for debugging.
347     */
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);
356   }
357 }