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 to do the work in the Application demonstrator, in particular
26 * the pricing of the stock path generated by Monte Carlo. The run
27 * method will generate a single sequence with the required statistics,
28 * estimate its volatility, expected return rate and final stock price
32 * @version $Revision: 1.1 $ $Date: 2008/08/18 22:22:21 $
34 public class PriceStock{
36 //------------------------------------------------------------------------
37 // Instance variables.
38 //------------------------------------------------------------------------
40 * The Monte Carlo path to be generated.
42 private MonteCarloPath mcPath;
44 * String identifier for a given task.
46 private String taskHeader;
48 * Random seed from which the Monte Carlo sequence is started.
50 private long randomSeed;
52 * Initial stock price value.
54 private float pathStartValue;
56 * Object which represents the results from a given computation task.
58 private ToResult result;
59 private float expectedReturnRate;
60 private float volatility;
61 private float volatility2;
62 private float finalStockPrice;
63 private float[] pathValue;
65 //------------------------------------------------------------------------
67 //------------------------------------------------------------------------
69 * Default constructor.
74 this.pathStartValue=(float)0.0;
75 this.expectedReturnRate=(float)0.0;
76 this.volatility=(float)0.0;
77 this.volatility2=(float)0.0;
78 this.finalStockPrice=(float)0.0;
80 mcPath = new MonteCarloPath();
82 //------------------------------------------------------------------------
84 //------------------------------------------------------------------------
85 //------------------------------------------------------------------------
86 // Methods which implement the Slaveable interface.
87 //------------------------------------------------------------------------
89 * Method which is passed in the initialisation data common to all tasks,
90 * and then unpacks them for use by this object.
92 * @param obj Object representing data which are common to all tasks.
94 public void setInitAllTasks(AppDemoRunner obj) {
95 mcPath.set_name(obj.name);
96 mcPath.set_startDate(obj.startDate);
97 mcPath.set_endDate(obj.endDate);
98 mcPath.set_dTime(obj.dTime);
99 mcPath.set_returnDefinition(obj.returnDefinition);
100 mcPath.set_expectedReturnRate(obj.expectedReturnRate);
101 mcPath.set_volatility(obj.volatility);
102 int nTimeSteps = obj.nTimeSteps;
103 mcPath.set_nTimeSteps(nTimeSteps);
104 this.pathStartValue = obj.pathStartValue;
105 mcPath.set_pathStartValue(pathStartValue);
106 mcPath.set_pathValue(new float[nTimeSteps]);
107 mcPath.set_fluctuations(new float[nTimeSteps]);
110 * Method which is passed in the data representing each task, which then
111 * unpacks it for use by this object.
113 * @param obj Object representing the data which defines a given task.
115 public void setTask(String header, long randomSeed) {
116 this.taskHeader = header;
117 this.randomSeed = randomSeed;
120 * The business end. Invokes the necessary computation routine, for a
124 mcPath.computeFluctuationsGaussian(randomSeed);
125 //System.printI(0xa0);
126 mcPath.computePathValue(pathStartValue);
127 //System.printI(0xa1);
128 RatePath rateP = new RatePath(mcPath);
129 //System.printI(0xa2);
130 ReturnPath returnP = rateP.getReturnCompounded();
131 //System.printI(0xa3);
132 returnP.estimatePath();
133 //System.printI(0xa4);
134 expectedReturnRate = returnP.get_expectedReturnRate();
135 volatility = returnP.get_volatility();
136 volatility2 = returnP.get_volatility2();
137 finalStockPrice = rateP.getEndPathValue();
138 pathValue = mcPath.get_pathValue();
139 //System.printI(0xa5);
142 * Method which returns the results of a computation back to the caller.
144 * @return An object representing the computed results.
146 public ToResult getResult() {
147 String resultHeader = "Result of task with Header="+taskHeader+": randomSeed="+randomSeed+": pathStartValue="+(int)pathStartValue;
148 ToResult res = new ToResult(resultHeader,expectedReturnRate,volatility,
149 volatility2,finalStockPrice,pathValue);