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.1 $ $Date: 2008/08/18 22:22:20 $
35 public class MonteCarloPath extends PathId {
37 //------------------------------------------------------------------------
38 // Instance variables.
39 //------------------------------------------------------------------------
41 * Random fluctuations generated as a series of random numbers with
44 private float[] fluctuations;
46 * The path values from which the random fluctuations are used to update.
48 private float[] pathValue;
50 * Integer flag for determining how the return was calculated, when
51 * used to calculate the mean drift and volatility parameters.
53 private int returnDefinition;
55 * Value for the mean drift, for use in the generation of the random path.
57 private float expectedReturnRate;
59 * Value for the volatility, for use in the generation of the random path.
61 private float volatility;
63 * Number of time steps for which the simulation should act over.
65 private int nTimeSteps;
67 * The starting value for of the security.
69 private 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, int nTimeSteps) {
100 * These instance variables are members of PathId class.
102 this.expectedReturnRate = (float)0.0;
103 this.volatility = (float)0.0;
104 this.pathStartValue = (float)0.0;
105 this.returnDefinition=1;
107 copyInstanceVariables(returnPath);
108 this.nTimeSteps = nTimeSteps;
109 this.pathValue = new float[nTimeSteps];
110 this.fluctuations = new float[nTimeSteps];
113 * Constructor, where the <code>PathId</code> objects is used to ease
114 * the number of instance variables to pass in.
116 * @param pathId Object used to define the identity of this Path.
117 * @param returnDefinition How the statistic variables were defined,
118 * according to the definitions in
119 * <code>ReturnPath</code>'s two class variables
120 * <code>COMPOUNDED</code> and
121 * <code>NONCOMPOUNDED</code>.
122 * @param expectedReturnRate The measured expected return rate for which to generate.
123 * @param volatility The measured volatility for which to generate.
124 * @param nTimeSteps The number of time steps for which to generate.
125 * @exception DemoException Thrown if there is a problem initialising the
126 * object's instance variables.
128 public MonteCarloPath(PathId pathId, int returnDefinition, float expectedReturnRate,
129 float volatility, int nTimeSteps) {
131 * These instance variables are members of PathId class.
132 * Invoking with this particular signature should point to the
133 * definition in the PathId class.
135 this.pathStartValue = (float)0.0;
137 copyInstanceVariables(pathId);
138 this.returnDefinition = returnDefinition;
139 this.expectedReturnRate = expectedReturnRate;
140 this.volatility = volatility;
141 this.nTimeSteps = nTimeSteps;
142 this.pathValue = new float[nTimeSteps];
143 this.fluctuations = new float[nTimeSteps];
146 * Constructor, for when the user wishes to define each of the instance
147 * variables individually.
149 * @param name The name of the security which this Monte Carlo path
151 * @param startDate The date when the path starts, in 'YYYYMMDD' format.
152 * @param endDate The date when the path ends, in 'YYYYMMDD' format.
153 * @param dTime The interval in the data between successive data points
154 * in the generated path.
155 * @param returnDefinition How the statistic variables were defined,
156 * according to the definitions in
157 * <code>ReturnPath</code>'s two class variables
158 * <code>COMPOUNDED</code> and
159 * <code>NONCOMPOUNDED</code>.
160 * @param expectedReturnRate The measured mean drift for which to generate.
161 * @param volatility The measured volatility for which to generate.
162 * @param nTimeSteps The number of time steps for which to generate.
164 public MonteCarloPath(String name, int startDate, int endDate, float dTime,
165 int returnDefinition, float expectedReturnRate, float volatility,
168 * These instance variables are members of PathId class.
171 set_startDate(startDate);
172 set_endDate(endDate);
174 this.returnDefinition = returnDefinition;
175 this.expectedReturnRate = expectedReturnRate;
176 this.volatility = volatility;
177 this.nTimeSteps = nTimeSteps;
178 this.pathValue = new float[nTimeSteps];
179 this.fluctuations = new float[nTimeSteps];
182 //------------------------------------------------------------------------
184 //------------------------------------------------------------------------
185 //------------------------------------------------------------------------
186 // Accessor methods for class MonteCarloPath.
187 // Generated by 'makeJavaAccessor.pl' script. HWY. 20th January 1999.
188 //------------------------------------------------------------------------
190 * Accessor method for private instance variable <code>fluctuations</code>.
192 * @return Value of instance variable <code>fluctuations</code>.
193 * @exception DemoException thrown if instance variable <code>fluctuations</code>
196 public float[] get_fluctuations() {
197 return(this.fluctuations);
200 * Set method for private instance variable <code>fluctuations</code>.
202 * @param fluctuations the value to set for the instance variable
203 * <code>fluctuations</code>.
205 public void set_fluctuations(float[] fluctuations) {
206 this.fluctuations = fluctuations;
209 * Accessor method for private instance variable <code>pathValue</code>.
211 * @return Value of instance variable <code>pathValue</code>.
212 * @exception DemoException thrown if instance variable <code>pathValue</code>
215 public float[] get_pathValue() {
216 return(this.pathValue);
219 * Set method for private instance variable <code>pathValue</code>.
221 * @param pathValue the value to set for the instance variable <code>pathValue</code>.
223 public void set_pathValue(float[] pathValue) {
224 this.pathValue = pathValue;
227 * Accessor method for private instance variable <code>returnDefinition</code>.
229 * @return Value of instance variable <code>returnDefinition</code>.
230 * @exception DemoException thrown if instance variable <code>returnDefinition</code>
233 public int get_returnDefinition() {
234 return(this.returnDefinition);
237 * Set method for private instance variable <code>returnDefinition</code>.
239 * @param returnDefinition the value to set for the instance variable
240 * <code>returnDefinition</code>.
242 public void set_returnDefinition(int returnDefinition) {
243 this.returnDefinition = returnDefinition;
246 * Accessor method for private instance variable <code>expectedReturnRate</code>.
248 * @return Value of instance variable <code>expectedReturnRate</code>.
249 * @exception DemoException thrown if instance variable <code>expectedReturnRate</code>
252 public float get_expectedReturnRate() {
253 return(this.expectedReturnRate);
256 * Set method for private instance variable <code>expectedReturnRate</code>.
258 * @param expectedReturnRate the value to set for the instance variable
259 * <code>expectedReturnRate</code>.
261 public void set_expectedReturnRate(float expectedReturnRate) {
262 this.expectedReturnRate = expectedReturnRate;
265 * Accessor method for private instance variable <code>volatility</code>.
267 * @return Value of instance variable <code>volatility</code>.
268 * @exception DemoException thrown if instance variable <code>volatility</code>
271 public float get_volatility() {
272 return(this.volatility);
275 * Set method for private instance variable <code>volatility</code>.
277 * @param volatility the value to set for the instance variable
278 * <code>volatility</code>.
280 public void set_volatility(float volatility) {
281 this.volatility = volatility;
284 * Accessor method for private instance variable <code>nTimeSteps</code>.
286 * @return Value of instance variable <code>nTimeSteps</code>.
287 * @exception DemoException thrown if instance variable <code>nTimeSteps</code>
290 public int get_nTimeSteps() {
291 return(this.nTimeSteps);
294 * Set method for private instance variable <code>nTimeSteps</code>.
296 * @param nTimeSteps the value to set for the instance variable
297 * <code>nTimeSteps</code>.
299 public void set_nTimeSteps(int nTimeSteps) {
300 this.nTimeSteps = nTimeSteps;
303 * Accessor method for private instance variable <code>pathStartValue</code>.
305 * @return Value of instance variable <code>pathStartValue</code>.
306 * @exception DemoException thrown if instance variable <code>pathStartValue</code>
309 public float get_pathStartValue() {
310 return(this.pathStartValue);
313 * Set method for private instance variable <code>pathStartValue</code>.
315 * @param pathStartValue the value to set for the instance variable
316 * <code>pathStartValue</code>.
318 public void set_pathStartValue(float pathStartValue) {
319 this.pathStartValue = pathStartValue;
321 //------------------------------------------------------------------------
323 * Method for copying the suitable instance variable from a
324 * <code>ReturnPath</code> object.
326 * @param obj Object used to define the instance variables which
327 * should be carried over to this object.
328 * @exception DemoException thrown if there is a problem accessing the
329 * instance variables from the target objetct.
331 private void copyInstanceVariables(ReturnPath obj) {
333 // Instance variables defined in the PathId object.
334 set_name(obj.get_name());
335 set_startDate(obj.get_startDate());
336 set_endDate(obj.get_endDate());
337 set_dTime(obj.get_dTime());
339 // Instance variables defined in this object.
340 this.returnDefinition = obj.get_returnDefinition();
341 this.expectedReturnRate = obj.get_expectedReturnRate();
342 this.volatility = obj.get_volatility();
346 * Method for returning a RatePath object from the Monte Carlo data
349 * @return a <code>RatePath</code> object representing the generated
351 * @exception DemoException thrown if there was a problem creating
352 * the RatePath object.
354 public RatePath getRatePath() {
355 return(new RatePath(this));
358 * Method for calculating the sequence of fluctuations, based around
359 * a Gaussian distribution of given mean and variance, as defined
360 * in this class' instance variables. Mapping from Gaussian
361 * distribution of (0,1) to (mean-drift,volatility) is done via
362 * Ito's lemma on the log of the stock price.
364 * @param randomSeed The psuedo-random number seed value, to start off a
365 * given sequence of Gaussian fluctuations.
366 * @exception DemoException thrown if there are any problems with
369 public boolean computeFluctuationsGaussian(long randomSeed) {
370 if( nTimeSteps > fluctuations.length ) {
373 //System.printI(0xc0);
375 // First, make use of the passed in seed value.
380 if( randomSeed == -1 ) {
381 rnd = new MyRandom(0, v1, v2);
383 rnd = new MyRandom((int)randomSeed, v1, v2);
385 //System.printI(0xc1);
387 // Determine the mean and standard-deviation, from the mean-drift and volatility.
388 float mean = (expectedReturnRate-(float)0.5*volatility*volatility)*get_dTime();
389 //System.printI(0xc2);
390 float sd = volatility*Math.sqrtf(get_dTime());
391 //System.printI(0xc3);
392 float gauss, meanGauss=(float)0.0, variance=(float)0.0;
393 //System.printI(0xc4);
394 for( int i=0; i < nTimeSteps; i += 2 ) {
395 //System.printI(0xc5);
398 //System.printI(0xc6);
400 variance+= (gauss*gauss);
402 // Now map this onto a general Gaussian of given mean and variance.
403 fluctuations[i] = mean + sd*gauss;
404 // dbgPrintln("gauss="+gauss+" fluctuations="+fluctuations[i]);
408 variance+= (gauss*gauss);
410 // Now map this onto a general Gaussian of given mean and variance.
411 fluctuations[i+1] = mean + sd*gauss;
413 //System.printI(0xc7);
414 meanGauss/=(float)nTimeSteps;
415 //System.printI(0xc8);
416 variance /=(float)nTimeSteps;
417 //System.printI(0xc9);
418 // dbgPrintln("meanGauss="+meanGauss+" variance="+variance);
421 * Method for calculating the sequence of fluctuations, based around
422 * a Gaussian distribution of given mean and variance, as defined
423 * in this class' instance variables. Mapping from Gaussian
424 * distribution of (0,1) to (mean-drift,volatility) is done via
425 * Ito's lemma on the log of the stock price. This overloaded method
426 * is for when the random seed should be decided by the system.
428 * @exception DemoException thrown if there are any problems with
431 public void computeFluctuationsGaussian() {
432 computeFluctuationsGaussian((long)-1);
435 * Method for calculating the corresponding rate path, given the
436 * fluctuations and starting rate value.
438 * @param startValue the starting value of the rate path, to be
439 * updated with the precomputed fluctuations.
440 * @exception DemoException thrown if there are any problems with
443 public void computePathValue(float startValue) {
444 pathValue[0] = startValue;
445 //System.printI(0xca0);
446 if( returnDefinition == 1 ||
447 returnDefinition == 2) {
448 //System.printI(0xca1);
449 for(int i=1; i < nTimeSteps; i++ ) {
450 //System.printI(0xca2);
451 pathValue[i] = pathValue[i-1] * Math.expf(fluctuations[i]);