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 representing the paths generated by the Monte Carlo engine.
29 * <li><code>double[] pathDate</code> is not simulated.</li>
33 * @version $Revision: 1.2 $ $Date: 2011/07/14 21:28:29 $
35 public class MonteCarloPath extends PathId {
37 //------------------------------------------------------------------------
38 // Instance variables.
39 //------------------------------------------------------------------------
41 * Random fluctuations generated as a series of random numbers with
44 public float[] fluctuations;
46 * The path values from which the random fluctuations are used to update.
48 public float[] pathValue;
50 * Integer flag for determining how the return was calculated, when
51 * used to calculate the mean drift and volatility parameters.
53 public int returnDefinition;
55 * Value for the mean drift, for use in the generation of the random path.
57 public float expectedReturnRate;
59 * Value for the volatility, for use in the generation of the random path.
61 public float volatility;
63 * Number of time steps for which the simulation should act over.
65 public int nTimeSteps;
67 * The starting value for of the security.
69 public float pathStartValue;
70 //------------------------------------------------------------------------
72 //------------------------------------------------------------------------
74 * Default constructor. Needed by the HPT library to start create
75 * new instances of this class. The instance variables for this should
76 * then be initialised with the <code>setInitAllTasks()</code> method.
78 public MonteCarloPath() {
80 this.expectedReturnRate = (float)0.0;
81 this.volatility = (float)0.0;
82 this.pathStartValue = (float)0.0;
83 this.returnDefinition=1;
88 * Constructor, using the <code>ReturnPath</code> object to initialise
89 * the necessary instance variables.
91 * @param returnPath Object used to define the instance variables in
93 * @param nTimeSteps The number of time steps for which to generate the
95 * @exception DemoException Thrown if there is a problem initialising the
96 * object's instance variables.
98 public MonteCarloPath(ReturnPath returnPath,
101 * These instance variables are members of PathId class.
103 this.expectedReturnRate = (float)0.0;
104 this.volatility = (float)0.0;
105 this.pathStartValue = (float)0.0;
106 this.returnDefinition=1;
108 copyInstanceVariables(returnPath);
109 this.nTimeSteps = nTimeSteps;
110 this.pathValue = new float[nTimeSteps];
111 this.fluctuations = new float[nTimeSteps];
114 * Constructor, where the <code>PathId</code> objects is used to ease
115 * the number of instance variables to pass in.
117 * @param pathId Object used to define the identity of this Path.
118 * @param returnDefinition How the statistic variables were defined,
119 * according to the definitions in
120 * <code>ReturnPath</code>'s two class variables
121 * <code>COMPOUNDED</code> and
122 * <code>NONCOMPOUNDED</code>.
123 * @param expectedReturnRate The measured expected return rate for which to generate.
124 * @param volatility The measured volatility for which to generate.
125 * @param nTimeSteps The number of time steps for which to generate.
126 * @exception DemoException Thrown if there is a problem initialising the
127 * object's instance variables.
129 public MonteCarloPath(PathId pathId,
130 int returnDefinition,
131 float expectedReturnRate,
135 * These instance variables are members of PathId class.
136 * Invoking with this particular signature should point to the
137 * definition in the PathId class.
139 this.pathStartValue = (float)0.0;
141 copyInstanceVariables(pathId);
142 this.returnDefinition = returnDefinition;
143 this.expectedReturnRate = expectedReturnRate;
144 this.volatility = volatility;
145 this.nTimeSteps = nTimeSteps;
146 this.pathValue = new float[nTimeSteps];
147 this.fluctuations = new float[nTimeSteps];
150 * Constructor, for when the user wishes to define each of the instance
151 * variables individually.
153 * @param name The name of the security which this Monte Carlo path
155 * @param startDate The date when the path starts, in 'YYYYMMDD' format.
156 * @param endDate The date when the path ends, in 'YYYYMMDD' format.
157 * @param dTime The interval in the data between successive data points
158 * in the generated path.
159 * @param returnDefinition How the statistic variables were defined,
160 * according to the definitions in
161 * <code>ReturnPath</code>'s two class variables
162 * <code>COMPOUNDED</code> and
163 * <code>NONCOMPOUNDED</code>.
164 * @param expectedReturnRate The measured mean drift for which to generate.
165 * @param volatility The measured volatility for which to generate.
166 * @param nTimeSteps The number of time steps for which to generate.
168 public MonteCarloPath(String name,
172 int returnDefinition,
173 float expectedReturnRate,
177 * These instance variables are members of PathId class.
180 this.startDate = startDate;
181 this.endDate = endDate;
183 this.returnDefinition = returnDefinition;
184 this.expectedReturnRate = expectedReturnRate;
185 this.volatility = volatility;
186 this.nTimeSteps = nTimeSteps;
187 this.pathValue = new float[nTimeSteps];
188 this.fluctuations = new float[nTimeSteps];
191 //------------------------------------------------------------------------
193 //------------------------------------------------------------------------
194 //------------------------------------------------------------------------
195 // Accessor methods for class MonteCarloPath.
196 // Generated by 'makeJavaAccessor.pl' script. HWY. 20th January 1999.
197 //------------------------------------------------------------------------
199 * Accessor method for private instance variable <code>fluctuations</code>.
201 * @return Value of instance variable <code>fluctuations</code>.
202 * @exception DemoException thrown if instance variable <code>fluctuations</code>
205 /*public float[] get_fluctuations() {
206 return(this.fluctuations);
209 * Set method for private instance variable <code>fluctuations</code>.
211 * @param fluctuations the value to set for the instance variable
212 * <code>fluctuations</code>.
214 public void set_fluctuations(float[] fluctuations) {
215 this.fluctuations = fluctuations;
218 * Accessor method for private instance variable <code>pathValue</code>.
220 * @return Value of instance variable <code>pathValue</code>.
221 * @exception DemoException thrown if instance variable <code>pathValue</code>
224 /*public float[] get_pathValue() {
225 return(this.pathValue);
228 * Set method for private instance variable <code>pathValue</code>.
230 * @param pathValue the value to set for the instance variable <code>pathValue</code>.
232 public void set_pathValue(float[] pathValue) {
233 this.pathValue = pathValue;
236 * Accessor method for private instance variable <code>returnDefinition</code>.
238 * @return Value of instance variable <code>returnDefinition</code>.
239 * @exception DemoException thrown if instance variable <code>returnDefinition</code>
242 /*public int get_returnDefinition() {
243 return(this.returnDefinition);
246 * Set method for private instance variable <code>returnDefinition</code>.
248 * @param returnDefinition the value to set for the instance variable
249 * <code>returnDefinition</code>.
251 public void set_returnDefinition(int returnDefinition) {
252 this.returnDefinition = returnDefinition;
255 * Accessor method for private instance variable <code>expectedReturnRate</code>.
257 * @return Value of instance variable <code>expectedReturnRate</code>.
258 * @exception DemoException thrown if instance variable <code>expectedReturnRate</code>
261 /*public float get_expectedReturnRate() {
262 return(this.expectedReturnRate);
265 * Set method for private instance variable <code>expectedReturnRate</code>.
267 * @param expectedReturnRate the value to set for the instance variable
268 * <code>expectedReturnRate</code>.
270 public void set_expectedReturnRate(float expectedReturnRate) {
271 this.expectedReturnRate = expectedReturnRate;
274 * Accessor method for private instance variable <code>volatility</code>.
276 * @return Value of instance variable <code>volatility</code>.
277 * @exception DemoException thrown if instance variable <code>volatility</code>
280 /*public float get_volatility() {
281 return(this.volatility);
284 * Set method for private instance variable <code>volatility</code>.
286 * @param volatility the value to set for the instance variable
287 * <code>volatility</code>.
289 public void set_volatility(float volatility) {
290 this.volatility = volatility;
293 * Accessor method for private instance variable <code>nTimeSteps</code>.
295 * @return Value of instance variable <code>nTimeSteps</code>.
296 * @exception DemoException thrown if instance variable <code>nTimeSteps</code>
299 /*public int get_nTimeSteps() {
300 return(this.nTimeSteps);
303 * Set method for private instance variable <code>nTimeSteps</code>.
305 * @param nTimeSteps the value to set for the instance variable
306 * <code>nTimeSteps</code>.
308 public void set_nTimeSteps(int nTimeSteps) {
309 this.nTimeSteps = nTimeSteps;
312 * Accessor method for private instance variable <code>pathStartValue</code>.
314 * @return Value of instance variable <code>pathStartValue</code>.
315 * @exception DemoException thrown if instance variable <code>pathStartValue</code>
318 /*public float get_pathStartValue() {
319 return(this.pathStartValue);
322 * Set method for private instance variable <code>pathStartValue</code>.
324 * @param pathStartValue the value to set for the instance variable
325 * <code>pathStartValue</code>.
327 public void set_pathStartValue(float pathStartValue) {
328 this.pathStartValue = pathStartValue;
330 //------------------------------------------------------------------------
332 * Method for copying the suitable instance variable from a
333 * <code>ReturnPath</code> object.
335 * @param obj Object used to define the instance variables which
336 * should be carried over to this object.
337 * @exception DemoException thrown if there is a problem accessing the
338 * instance variables from the target objetct.
340 private void copyInstanceVariables(ReturnPath obj) {
342 // Instance variables defined in the PathId object.
343 this.name = obj.name;
344 this.startDate = obj.startDate;
345 this.endDate = obj.endDate;
346 this.dTime = obj.dTime;
348 // Instance variables defined in this object.
349 this.returnDefinition = obj.returnDefinition;
350 this.expectedReturnRate = obj.expectedReturnRate;
351 this.volatility = obj.volatility;
355 * Method for returning a RatePath object from the Monte Carlo data
358 * @return a <code>RatePath</code> object representing the generated
360 * @exception DemoException thrown if there was a problem creating
361 * the RatePath object.
363 public RatePath getRatePath() {
364 return(new RatePath(this));
367 * Method for calculating the sequence of fluctuations, based around
368 * a Gaussian distribution of given mean and variance, as defined
369 * in this class' instance variables. Mapping from Gaussian
370 * distribution of (0,1) to (mean-drift,volatility) is done via
371 * Ito's lemma on the log of the stock price.
373 * @param randomSeed The psuedo-random number seed value, to start off a
374 * given sequence of Gaussian fluctuations.
375 * @exception DemoException thrown if there are any problems with
378 public boolean computeFluctuationsGaussian(long randomSeed) {
379 int ntimesteps = this.nTimeSteps;
380 int length = this.fluctuations.length;
381 if( ntimesteps > length ) {
384 float[] flucts = this.fluctuations;
385 float expectedreturnrate = this.expectedReturnRate;
386 float vol = this.volatility;
387 float dtime = this.dTime;
390 // First, make use of the passed in seed value.
395 if( randomSeed == -1 ) {
396 rnd = new MyRandom(0, v1, v2);
398 rnd = new MyRandom((int)randomSeed, v1, v2);
402 // Determine the mean and standard-deviation, from the mean-drift and volatility.
403 float mean = (expectedreturnrate-(float)0.5*vol*vol)*dtime;
404 float sd = vol*Math.sqrtf(dtime);
405 float gauss, meanGauss=(float)0.0, variance=(float)0.0;
406 for( int i=0; i < ntimesteps; i += 2 ) {
410 variance+= gauss*gauss;
412 // Now map this onto a general Gaussian of given mean and variance.
413 flucts[i] = mean + sd*gauss;
414 // dbgPrintln("gauss="+gauss+" fluctuations="+fluctuations[i]);
418 variance+= gauss*gauss;
420 // Now map this onto a general Gaussian of given mean and variance.
421 flucts[i+1] = mean + sd*gauss;
423 meanGauss/=(float)ntimesteps;
424 variance /=(float)ntimesteps;
425 // dbgPrintln("meanGauss="+meanGauss+" variance="+variance);
428 * Method for calculating the sequence of fluctuations, based around
429 * a Gaussian distribution of given mean and variance, as defined
430 * in this class' instance variables. Mapping from Gaussian
431 * distribution of (0,1) to (mean-drift,volatility) is done via
432 * Ito's lemma on the log of the stock price. This overloaded method
433 * is for when the random seed should be decided by the system.
435 * @exception DemoException thrown if there are any problems with
438 public void computeFluctuationsGaussian() {
439 computeFluctuationsGaussian((long)-1);
442 * Method for calculating the corresponding rate path, given the
443 * fluctuations and starting rate value.
445 * @param startValue the starting value of the rate path, to be
446 * updated with the precomputed fluctuations.
447 * @exception DemoException thrown if there are any problems with
450 public void computePathValue(float startValue) {
451 float[] pathvalue = this.pathValue;
452 float[] flucts = this.fluctuations;
453 int length = this.nTimeSteps;
454 pathvalue[0] = startValue;
455 if( returnDefinition == 1 |
456 returnDefinition == 2) {
457 for(int i=1; i < length; i++ ) {
458 //System.printI((int)(flucts[i] * 10000));
459 pathvalue[i] = pathvalue[i-1] * Math.expf(flucts[i]);