* </ol>
*
* @author H W Yau
- * @version $Revision: 1.1 $ $Date: 2008/08/18 22:22:20 $
+ * @version $Revision: 1.2 $ $Date: 2009/02/13 21:37:19 $
*/
public class AppDemo {
flag merge;
public float JGFavgExpectedReturnRateMC;
- public int Serial;
+ //public int Serial;
//------------------------------------------------------------------------
// Instance variables.
//------------------------------------------------------------------------
/**
* The default duration between time-steps, in units of a year.
*/
- private float dTime;
+ public float dTime;
/**
* Flag to determine whether initialisation has already taken place.
*/
- private boolean initialised;
+ public boolean initialised;
/**
* Variable to determine which deployment scenario to run.
*/
- private int runMode;
+ public int runMode;
- public Vector results;
+ //public Vector results;
- PriceStock psMC;
+ //public PriceStock psMC;
public float pathStartValue;
- float avgExpectedReturnRateMC;
- float avgVolatilityMC;
+ public float avgExpectedReturnRateMC;
+ public float avgVolatilityMC;
- int counter;
+ public int counter;
- RatePath avgMCrate;
+ public RatePath avgMCrate;
- public ToInitAllTasks initAllTasks;
+ //public ToInitAllTasks initAllTasks;
+ public String name;
+ public int startDate;
+ public int endDate;
+ public int returnDefinition;
+ public float expectedReturnRate;
+ public float volatility;
public AppDemo(int nTimeStepsMC, int nRunsMC, int group) {
this.JGFavgExpectedReturnRateMC = (float)0.0;
- this.Serial = 1;
+ //this.Serial = 1;
this.nTimeStepsMC = nTimeStepsMC;
this.nRunsMC = nRunsMC;
this.counter = 0;
- this.avgMCrate = new RatePath(this.nTimeStepsMC, "MC", 19990109, 19991231, this.dTime);
+ this.avgMCrate = new RatePath(this.nTimeStepsMC,
+ "MC",
+ 19990109,
+ 19991231,
+ this.dTime);
- this.initAllTasks = null;
+ //this.initAllTasks = null;
}
/**
* Single point of contact for running this increasingly bloated
public void initSerial() {
//
// Measure the requested path rate.
- //System.printI(0xf0);
RatePath rateP = new RatePath();
- //System.printI(0xf1);
//rateP.dbgDumpFields();
ReturnPath returnP = rateP.getReturnCompounded();
- //System.printI(0xf2);
returnP.estimatePath();
- //System.printI(0xf3);
//returnP.dbgDumpFields();
- float expectedReturnRate = returnP.get_expectedReturnRate();
- float volatility = returnP.get_volatility();
- initAllTasks = new ToInitAllTasks(returnP, nTimeStepsMC, pathStartValue);
+ /*initAllTasks = new ToInitAllTasks(returnP,
+ nTimeStepsMC,
+ pathStartValue);*/
+ this.name = returnP.name;
+ this.startDate = returnP.startDate;
+ this.endDate = returnP.endDate;
+ //
+ // Instance variables defined in ReturnPath object.
+ this.returnDefinition = returnP.returnDefinition;
+ this.expectedReturnRate = returnP.expectedReturnRate;
+ this.volatility = returnP.volatility;
+
this.counter = 0;
- //System.printI(0xf4);
return;
}
* any values.
*/
boolean processResults(Vector returnMCs) {
+ float sumReturnRateMC = (float) 0.0;
+ float sumVolatilityMC = (float) 0.0;
+ RatePath avgmcrate = this.avgMCrate;
for(int i = 0; i < returnMCs.size(); i++) {
ToResult returnMC = (ToResult)returnMCs.elementAt(i);
- avgMCrate.inc_pathValue(returnMC.get_pathValue());
- avgExpectedReturnRateMC += returnMC.get_expectedReturnRate();
- avgVolatilityMC += returnMC.get_volatility();
+ avgmcrate.inc_pathValue(returnMC.pathValue);
+ sumReturnRateMC += returnMC.expectedReturnRate;
+ sumVolatilityMC += returnMC.volatility;
}
+ avgExpectedReturnRateMC = sumReturnRateMC;
+ avgVolatilityMC = sumVolatilityMC;
this.counter++;
- if(this.counter == this.group) {
- avgMCrate.inc_pathValue((float)1.0/((float)nRunsMC));
+ boolean isfinish = (this.counter == this.group);
+ if(isfinish) {
+ avgmcrate.inc_pathValue((float)1.0/((float)nRunsMC));
avgExpectedReturnRateMC /= nRunsMC;
avgVolatilityMC /= nRunsMC;
JGFavgExpectedReturnRateMC = avgExpectedReturnRateMC;
}
- return (this.counter == this.group);
+ return isfinish;
}
//
//------------------------------------------------------------------------
*
* @return Value of instance variable <code>nTimeStepsMC</code>.
*/
- public int get_nTimeStepsMC() {
+ /*public int get_nTimeStepsMC() {
return(this.nTimeStepsMC);
- }
+ }*/
/**
* Set method for private instance variable <code>nTimeStepsMC</code>.
*
*
* @return Value of instance variable <code>nRunsMC</code>.
*/
- public int get_nRunsMC() {
+ /*public int get_nRunsMC() {
return(this.nRunsMC);
- }
+ }*/
/**
* Set method for private instance variable <code>nRunsMC</code>.
*
*
* @return Value of instance variable <code>results</code>.
*/
- public Vector get_results() {
+ /*public Vector get_results() {
return(this.results);
- }
+ }*/
/**
* Set method for private instance variable <code>results</code>.
*
* @param results the value to set for the instance variable <code>results</code>.
*/
- public void set_results(Vector results) {
+ /*public void set_results(Vector results) {
this.results = results;
- }
+ }*/
//------------------------------------------------------------------------
}
public float pathStartValue;
int id, nRunsMC, group;
- ToInitAllTasks toinitalltasks;
+ //ToInitAllTasks toinitalltasks;
public Vector results;
- public AppDemoRunner(int id,int nRunsMC, int group, ToInitAllTasks initalltask) {
+ public AppDemoRunner(int id,
+ int nRunsMC,
+ int group,
+ AppDemo ad
+ /*ToInitAllTasks initalltask*/) {
this.id = id;
this.nRunsMC=nRunsMC;
this.group = group;
this.results = new Vector();
- this.header = initalltask.get_header();
- this.name = initalltask.get_name();
- this.startDate = initalltask.get_startDate();
- this.endDate = initalltask.get_endDate();
- this.dTime = initalltask.get_dTime();
- this.returnDefinition = initalltask.get_returnDefinition();
- this.expectedReturnRate = initalltask.get_expectedReturnRate();
- this.volatility = initalltask.get_volatility();
- this.nTimeSteps = initalltask.get_nTimeSteps();
- this.pathStartValue = initalltask.get_pathStartValue();
+ //this.header = initalltask.header;
+ this.name = ad.name;
+ this.startDate = ad.startDate;
+ this.endDate = ad.endDate;
+ this.dTime = ad.dTime;
+ this.returnDefinition = ad.returnDefinition;
+ this.expectedReturnRate = ad.expectedReturnRate;
+ this.volatility = ad.volatility;
+ this.nTimeSteps = ad.nTimeStepsMC;
+ this.pathStartValue = ad.pathStartValue;
}
public void run() {
// Now do the computation.
int ilow, iupper, slice;
+ int gp = this.group;
+ int index = this.id;
+ int nruns = this.nRunsMC;
- slice = (nRunsMC + this.group-1)/this.group;
+ slice = (nruns + gp-1)/gp;
- ilow = id*slice;
- iupper = (id+1)*slice;
- if (id==this.group-1) {
- iupper=nRunsMC;
+ ilow = index*slice;
+ iupper = (index+1)*slice;
+ if (index==gp-1) {
+ iupper=nruns;
}
- //System.printI(0xba0);
for(int iRun=ilow; iRun < iupper; iRun++ ) {
- //System.printI(0xba1);
- String header="MC run "+String.valueOf(iRun);
+ //String header="MC run "+String.valueOf(iRun);
PriceStock ps = new PriceStock();
- //System.printI(0xba2);
ps.setInitAllTasks(this);
- ps.setTask(header, (long)iRun*11);
- //System.printI(0xba3);
+ ps.setTask(/*header, */(long)iRun*11);
ps.run();
- //System.printI(0xba4);
results.addElement(ps.getResult());
- //System.printI(0xba5);
}
}
}
\ No newline at end of file
ad.initSerial();
for(int i = 0; i < group; i++) {
- AppDemoRunner adr = new AppDemoRunner(i, nruns, group, ad.initAllTasks){run};
+ AppDemoRunner adr = new AppDemoRunner(i,
+ nruns,
+ group,
+ ad){run};
}
taskexit(s{!initialstate});
* </ol>
*
* @author H W Yau
- * @version $Revision: 1.1 $ $Date: 2008/08/18 22:22:20 $
+ * @version $Revision: 1.2 $ $Date: 2009/02/13 21:37:19 $
*/
public class MonteCarloPath extends PathId {
* Random fluctuations generated as a series of random numbers with
* given distribution.
*/
- private float[] fluctuations;
+ public float[] fluctuations;
/**
* The path values from which the random fluctuations are used to update.
*/
- private float[] pathValue;
+ public float[] pathValue;
/**
* Integer flag for determining how the return was calculated, when
* used to calculate the mean drift and volatility parameters.
*/
- private int returnDefinition;
+ public int returnDefinition;
/**
* Value for the mean drift, for use in the generation of the random path.
*/
- private float expectedReturnRate;
+ public float expectedReturnRate;
/**
* Value for the volatility, for use in the generation of the random path.
*/
- private float volatility;
+ public float volatility;
/**
* Number of time steps for which the simulation should act over.
*/
- private int nTimeSteps;
+ public int nTimeSteps;
/**
* The starting value for of the security.
*/
- private float pathStartValue;
+ public float pathStartValue;
//------------------------------------------------------------------------
// Constructors.
//------------------------------------------------------------------------
* @exception DemoException Thrown if there is a problem initialising the
* object's instance variables.
*/
- public MonteCarloPath(ReturnPath returnPath, int nTimeSteps) {
+ public MonteCarloPath(ReturnPath returnPath,
+ int nTimeSteps) {
/**
* These instance variables are members of PathId class.
*/
* @exception DemoException Thrown if there is a problem initialising the
* object's instance variables.
*/
- public MonteCarloPath(PathId pathId, int returnDefinition, float expectedReturnRate,
- float volatility, int nTimeSteps) {
+ public MonteCarloPath(PathId pathId,
+ int returnDefinition,
+ float expectedReturnRate,
+ float volatility,
+ int nTimeSteps) {
/**
* These instance variables are members of PathId class.
* Invoking with this particular signature should point to the
* @param volatility The measured volatility for which to generate.
* @param nTimeSteps The number of time steps for which to generate.
*/
- public MonteCarloPath(String name, int startDate, int endDate, float dTime,
- int returnDefinition, float expectedReturnRate, float volatility,
- int nTimeSteps) {
+ public MonteCarloPath(String name,
+ int startDate,
+ int endDate,
+ float dTime,
+ int returnDefinition,
+ float expectedReturnRate,
+ float volatility,
+ int nTimeSteps) {
/**
* These instance variables are members of PathId class.
*/
- set_name(name);
- set_startDate(startDate);
- set_endDate(endDate);
- set_dTime(dTime);
+ this.name = name;
+ this.startDate = startDate;
+ this.endDate = endDate;
+ this.dTime = dTime;
this.returnDefinition = returnDefinition;
this.expectedReturnRate = expectedReturnRate;
this.volatility = volatility;
* @exception DemoException thrown if instance variable <code>fluctuations</code>
* is undefined.
*/
- public float[] get_fluctuations() {
+ /*public float[] get_fluctuations() {
return(this.fluctuations);
- }
+ }*/
/**
* Set method for private instance variable <code>fluctuations</code>.
*
* @exception DemoException thrown if instance variable <code>pathValue</code>
* is undefined.
*/
- public float[] get_pathValue() {
+ /*public float[] get_pathValue() {
return(this.pathValue);
- }
+ }*/
/**
* Set method for private instance variable <code>pathValue</code>.
*
* @exception DemoException thrown if instance variable <code>returnDefinition</code>
* is undefined.
*/
- public int get_returnDefinition() {
+ /*public int get_returnDefinition() {
return(this.returnDefinition);
- }
+ }*/
/**
* Set method for private instance variable <code>returnDefinition</code>.
*
* @exception DemoException thrown if instance variable <code>expectedReturnRate</code>
* is undefined.
*/
- public float get_expectedReturnRate() {
+ /*public float get_expectedReturnRate() {
return(this.expectedReturnRate);
- }
+ }*/
/**
* Set method for private instance variable <code>expectedReturnRate</code>.
*
* @exception DemoException thrown if instance variable <code>volatility</code>
* is undefined.
*/
- public float get_volatility() {
+ /*public float get_volatility() {
return(this.volatility);
- }
+ }*/
/**
* Set method for private instance variable <code>volatility</code>.
*
* @exception DemoException thrown if instance variable <code>nTimeSteps</code>
* is undefined.
*/
- public int get_nTimeSteps() {
+ /*public int get_nTimeSteps() {
return(this.nTimeSteps);
- }
+ }*/
/**
* Set method for private instance variable <code>nTimeSteps</code>.
*
* @exception DemoException thrown if instance variable <code>pathStartValue</code>
* is undefined.
*/
- public float get_pathStartValue() {
+ /*public float get_pathStartValue() {
return(this.pathStartValue);
- }
+ }*/
/**
* Set method for private instance variable <code>pathStartValue</code>.
*
private void copyInstanceVariables(ReturnPath obj) {
//
// Instance variables defined in the PathId object.
- set_name(obj.get_name());
- set_startDate(obj.get_startDate());
- set_endDate(obj.get_endDate());
- set_dTime(obj.get_dTime());
+ this.name = obj.name;
+ this.startDate = obj.startDate;
+ this.endDate = obj.endDate;
+ this.dTime = obj.dTime;
//
// Instance variables defined in this object.
- this.returnDefinition = obj.get_returnDefinition();
- this.expectedReturnRate = obj.get_expectedReturnRate();
- this.volatility = obj.get_volatility();
+ this.returnDefinition = obj.returnDefinition;
+ this.expectedReturnRate = obj.expectedReturnRate;
+ this.volatility = obj.volatility;
}
/**
* the computation.
*/
public boolean computeFluctuationsGaussian(long randomSeed) {
- if( nTimeSteps > fluctuations.length ) {
+ int ntimesteps = this.nTimeSteps;
+ int length = this.fluctuations.length;
+ if( ntimesteps > length ) {
return false;
}
- //System.printI(0xc0);
+ float[] flucts = this.fluctuations;
+ float expectedreturnrate = this.expectedReturnRate;
+ float vol = this.volatility;
+ float dtime = this.dTime;
+
//
// First, make use of the passed in seed value.
MyRandom rnd;
} else {
rnd = new MyRandom((int)randomSeed, v1, v2);
}
- //System.printI(0xc1);
+
//
// Determine the mean and standard-deviation, from the mean-drift and volatility.
- float mean = (expectedReturnRate-(float)0.5*volatility*volatility)*get_dTime();
- //System.printI(0xc2);
- float sd = volatility*Math.sqrtf(get_dTime());
- //System.printI(0xc3);
+ float mean = (expectedreturnrate-(float)0.5*vol*vol)*dtime;
+ float sd = vol*Math.sqrtf(dtime);
float gauss, meanGauss=(float)0.0, variance=(float)0.0;
- //System.printI(0xc4);
- for( int i=0; i < nTimeSteps; i += 2 ) {
- //System.printI(0xc5);
+ for( int i=0; i < ntimesteps; i += 2 ) {
r = rnd.seed();
gauss = r*rnd.v1;
- //System.printI(0xc6);
meanGauss+= gauss;
- variance+= (gauss*gauss);
+ variance+= gauss*gauss;
//
// Now map this onto a general Gaussian of given mean and variance.
- fluctuations[i] = mean + sd*gauss;
+ flucts[i] = mean + sd*gauss;
// dbgPrintln("gauss="+gauss+" fluctuations="+fluctuations[i]);
gauss = r*rnd.v2;
meanGauss+= gauss;
- variance+= (gauss*gauss);
+ variance+= gauss*gauss;
//
// Now map this onto a general Gaussian of given mean and variance.
- fluctuations[i+1] = mean + sd*gauss;
+ flucts[i+1] = mean + sd*gauss;
}
- //System.printI(0xc7);
- meanGauss/=(float)nTimeSteps;
- //System.printI(0xc8);
- variance /=(float)nTimeSteps;
- //System.printI(0xc9);
+ meanGauss/=(float)ntimesteps;
+ variance /=(float)ntimesteps;
// dbgPrintln("meanGauss="+meanGauss+" variance="+variance);
}
/**
* the computation.
*/
public void computePathValue(float startValue) {
- pathValue[0] = startValue;
- //System.printI(0xca0);
- if( returnDefinition == 1 ||
+ float[] pathvalue = this.pathValue;
+ float[] flucts = this.fluctuations;
+ int length = this.nTimeSteps;
+ pathvalue[0] = startValue;
+ if( returnDefinition == 1 |
returnDefinition == 2) {
- //System.printI(0xca1);
- for(int i=1; i < nTimeSteps; i++ ) {
- //System.printI(0xca2);
- pathValue[i] = pathValue[i-1] * Math.expf(fluctuations[i]);
+ for(int i=1; i < length; i++ ) {
+ //System.printI((int)(flucts[i] * 10000));
+ pathvalue[i] = pathvalue[i-1] * Math.expf(flucts[i]);
}
}
}
}
public float update() {
-
float rand;
float scale= (float)4.656612875e-10;
int is1,is2,iss2;
int imult= 16807;
int imod = 2147483647;
+ int seed = this.iseed;
- if (iseed<=0) {
- iseed = 1;
- }
+ if (seed<=0) {
+ iseed = seed = 1;
+ }
- is2 = iseed % 32768;
- is1 = (iseed-is2)/32768;
+ is2 = seed % 32768;
+ is1 = seed / 32768;
iss2 = is2 * imult;
is2 = iss2 % 32768;
- is1 = (is1 * imult+(iss2-is2) / 32768) % (65536);
+ is1 = (is1 * imult + iss2 / 32768) % (65536);
- iseed = (is1 * 32768 + is2) % imod;
+ iseed = seed = (is1 * 32768 + is2) % imod;
- rand = scale * iseed;
+ rand = scale * seed;
return rand;
float s,u1,u2,r;
s = (float)1.0;
//do {
- //System.printI(0xb1);
u1 = update();
u2 = update();
- //System.printI(0xb2);
v1 = (float)2.0 * u1 - (float)1.0;
v2 = (float)2.0 * u2 - (float)1.0;
s = v1*v1 + v2*v2;
- //System.printI(0xb3);
//} while (s >= (float)1.0);
s = s - (int)s;
//System.printI(0xb4);
* Also provides some methods for writing out debug messages.
*
* @author H W Yau
- * @version $Revision: 1.1 $ $Date: 2008/08/18 22:22:21 $
+ * @version $Revision: 1.2 $ $Date: 2009/02/13 21:37:19 $
*/
public class PathId {
/**
* Simple string name.
*/
- private String name;
+ public String name;
/**
* The start date for the path, in YYYYMMDD format.
*/
- private int startDate;
+ public int startDate;
/**
* The end date for the path, in YYYYMMDD format.
*/
- private int endDate;
+ public int endDate;
/**
* The change in time between two successive data values.
*/
- private float dTime;
+ public float dTime;
//------------------------------------------------------------------------
// Constructors.
* @return Value of instance variable <code>name</code>.
* @exception DemoException thrown if instance variable <code>name</code> is undefined.
*/
- public String get_name() {
+ /*public String get_name() {
return(this.name);
- }
+ }*/
/**
* Set method for private instance variable <code>name</code>.
*
* @return Value of instance variable <code>startDate</code>.
* @exception DemoException thrown if instance variable <code>startDate</code> is undefined.
*/
- public int get_startDate() {
+ /*public int get_startDate() {
return(this.startDate);
- }
+ }*/
/**
* Set method for private instance variable <code>startDate</code>.
*
* @return Value of instance variable <code>endDate</code>.
* @exception DemoException thrown if instance variable <code>endDate</code> is undefined.
*/
- public int get_endDate() {
+ /*public int get_endDate() {
return(this.endDate);
- }
+ }*/
/**
* Set method for private instance variable <code>endDate</code>.
*
* @return Value of instance variable <code>dTime</code>.
* @exception DemoException thrown if instance variable <code>dTime</code> is undefined.
*/
- public float get_dTime() {
+ /*public float get_dTime() {
return(this.dTime);
- }
+ }*/
/**
* Set method for private instance variable <code>dTime</code>.
*
* any undefined objects.
*/
public void copyInstanceVariables(PathId obj) {
- this.name = obj.get_name();
- this.startDate = obj.get_startDate();
- this.endDate = obj.get_endDate();
- this.dTime = obj.get_dTime();
+ this.name = obj.name;
+ this.startDate = obj.startDate;
+ this.endDate = obj.endDate;
+ this.dTime = obj.dTime;
}
/**
* Dumps the contents of the fields, to standard-out, for debugging.
* value.
*
* @author H W Yau
- * @version $Revision: 1.1 $ $Date: 2008/08/18 22:22:21 $
+ * @version $Revision: 1.2 $ $Date: 2009/02/13 21:37:19 $
*/
public class PriceStock{
/**
* The Monte Carlo path to be generated.
*/
- private MonteCarloPath mcPath;
+ public MonteCarloPath mcPath;
/**
* String identifier for a given task.
*/
- private String taskHeader;
+ //private String taskHeader;
/**
* Random seed from which the Monte Carlo sequence is started.
*/
- private long randomSeed;
+ public long randomSeed;
/**
* Initial stock price value.
*/
- private float pathStartValue;
+ public float pathStartValue;
/**
* Object which represents the results from a given computation task.
*/
- private ToResult result;
- private float expectedReturnRate;
- private float volatility;
- private float volatility2;
- private float finalStockPrice;
- private float[] pathValue;
+ public ToResult result;
+ public float expectedReturnRate;
+ public float volatility;
+ public float volatility2;
+ public float finalStockPrice;
+ public float[] pathValue;
//------------------------------------------------------------------------
// Constructors.
* Default constructor.
*/
public PriceStock() {
- this.taskHeader = "";
+ //this.taskHeader = "";
this.randomSeed=-1;
this.pathStartValue=(float)0.0;
this.expectedReturnRate=(float)0.0;
* @param obj Object representing data which are common to all tasks.
*/
public void setInitAllTasks(AppDemoRunner obj) {
- mcPath.set_name(obj.name);
- mcPath.set_startDate(obj.startDate);
- mcPath.set_endDate(obj.endDate);
- mcPath.set_dTime(obj.dTime);
- mcPath.set_returnDefinition(obj.returnDefinition);
- mcPath.set_expectedReturnRate(obj.expectedReturnRate);
- mcPath.set_volatility(obj.volatility);
+ mcPath.name = obj.name;
+ mcPath.startDate = obj.startDate;
+ mcPath.endDate = obj.endDate;
+ mcPath.dTime = obj.dTime;
+ mcPath.returnDefinition = obj.returnDefinition;
+ mcPath.expectedReturnRate = obj.expectedReturnRate;
+ mcPath.volatility = obj.volatility;
int nTimeSteps = obj.nTimeSteps;
- mcPath.set_nTimeSteps(nTimeSteps);
+ mcPath.nTimeSteps = nTimeSteps;
this.pathStartValue = obj.pathStartValue;
- mcPath.set_pathStartValue(pathStartValue);
- mcPath.set_pathValue(new float[nTimeSteps]);
- mcPath.set_fluctuations(new float[nTimeSteps]);
+ mcPath.pathStartValue = pathStartValue;
+ mcPath.pathValue = new float[nTimeSteps];
+ mcPath.fluctuations = new float[nTimeSteps];
}
/**
* Method which is passed in the data representing each task, which then
*
* @param obj Object representing the data which defines a given task.
*/
- public void setTask(String header, long randomSeed) {
- this.taskHeader = header;
+ public void setTask(/*String header, */long randomSeed) {
+ //this.taskHeader = header;
this.randomSeed = randomSeed;
}
/**
*/
public void run() {
mcPath.computeFluctuationsGaussian(randomSeed);
- //System.printI(0xa0);
mcPath.computePathValue(pathStartValue);
- //System.printI(0xa1);
RatePath rateP = new RatePath(mcPath);
- //System.printI(0xa2);
ReturnPath returnP = rateP.getReturnCompounded();
- //System.printI(0xa3);
returnP.estimatePath();
- //System.printI(0xa4);
- expectedReturnRate = returnP.get_expectedReturnRate();
- volatility = returnP.get_volatility();
- volatility2 = returnP.get_volatility2();
- finalStockPrice = rateP.getEndPathValue();
- pathValue = mcPath.get_pathValue();
- //System.printI(0xa5);
+ expectedReturnRate = returnP.expectedReturnRate;
+ volatility = returnP.volatility;
+ volatility2 = returnP.volatility2;
+ finalStockPrice = rateP.getEndPathValue();//pathValue[rateP.pathValue.length-1];
+ pathValue = mcPath.pathValue;
}
/*
* Method which returns the results of a computation back to the caller.
* @return An object representing the computed results.
*/
public ToResult getResult() {
- String resultHeader = "Result of task with Header="+taskHeader+": randomSeed="+randomSeed+": pathStartValue="+(int)pathStartValue;
- ToResult res = new ToResult(resultHeader,expectedReturnRate,volatility,
- volatility2,finalStockPrice,pathValue);
+ //String resultHeader = "Result of task with Header="+taskHeader+": randomSeed="+randomSeed+": pathStartValue="+(int)pathStartValue;
+ ToResult res = new ToResult(/*resultHeader,*/
+ expectedReturnRate,
+ volatility,
+ volatility2,
+ finalStockPrice,
+ pathValue);
return res;
}
}
* </ol>
*
* @author H W Yau
- * @version $Revision: 1.1 $ $Date: 2008/08/18 22:22:21 $
+ * @version $Revision: 1.2 $ $Date: 2009/02/13 21:37:19 $
*/
public class RatePath extends PathId {
/**
* An instance variable, for storing the rate's path values itself.
*/
- private float[] pathValue;
+ public float[] pathValue;
/**
* An instance variable, for storing the corresponding date of the datum,
* in 'YYYYMMDD' format.
*/
- private int[] pathDate;
+ public int[] pathDate;
/**
* The number of accepted values in the rate path.
*/
- private int nAcceptedPathValue;
+ public int nAcceptedPathValue;
//------------------------------------------------------------------------
// Constructors.
this.MINIMUMDATE = 19000101;
this.EPSILON= (float)10.0 * (float)(4.9E-324);
this.nAcceptedPathValue = 0;
- //System.printI(0xaa0);
readRatesFile();
- //System.printI(0xaa1);
}
/**
* Constructor, for when the user specifies simply an array of values
* @param dTime the time interval between successive path values, in
* fractions of a year.
*/
- public RatePath(float[] pathValue, String name, int startDate, int endDate, float dTime) {
+ public RatePath(float[] pathValue,
+ String name,
+ int startDate,
+ int endDate,
+ float dTime) {
this.MINIMUMDATE = 19000101;
this.EPSILON= (float)10.0 * (float)(4.9E-324);
- set_name(name);
- set_startDate(startDate);
- set_endDate(endDate);
- set_dTime(dTime);
+ this.name = name;
+ this.startDate = startDate;
+ this.endDate = endDate;
+ this.dTime = dTime;
this.pathValue = pathValue;
this.nAcceptedPathValue = pathValue.length;
}
//
// Fields pertaining to the parent PathId object:
- set_name(mc.get_name());
- set_startDate(mc.get_startDate());
- set_endDate(mc.get_endDate());
- set_dTime(mc.get_dTime());
+ this.name = mc.name;
+ this.startDate = mc.startDate;
+ this.endDate = mc.endDate;
+ this.dTime = mc.dTime;
//
// Fields pertaining to RatePath object itself.
- pathValue=mc.get_pathValue();
- nAcceptedPathValue=mc.get_nTimeSteps();
+ pathValue=mc.pathValue;
+ nAcceptedPathValue=mc.nTimeSteps;
//
// Note that currently the pathDate is neither declared, defined,
// nor used in the MonteCarloPath object.
* @param dTime the time interval between successive path values, in
* fractions of a year.
*/
- public RatePath(int pathValueLength, String name, int startDate, int endDate, float dTime) {
+ public RatePath(int pathValueLength,
+ String name,
+ int startDate,
+ int endDate,
+ float dTime) {
this.MINIMUMDATE = 19000101;
this.EPSILON= (float)10.0 * (float)(4.9E-324);
- set_name(name);
- set_startDate(startDate);
- set_endDate(endDate);
- set_dTime(dTime);
+ this.name = name;
+ this.startDate = startDate;
+ this.endDate = endDate;
+ this.dTime = dTime;
this.pathValue = new float[pathValueLength];
this.nAcceptedPathValue = pathValue.length;
}
* lengths of the operand and target arrays.
*/
public boolean inc_pathValue(float[] operandPath) {
- if( pathValue.length != operandPath.length ) {
+ int length = this.pathValue.length;
+ if( length != operandPath.length ) {
return false;
}
- for(int i=0; i<pathValue.length; i++ ) {
- pathValue[i] += operandPath[i];
+ float[] pathvalue = this.pathValue;
+ for(int i=0; i<length; i++ ) {
+ pathvalue[i] += operandPath[i];
}
return true;
}
* lengths of the operand and target arrays.
*/
public boolean inc_pathValue(float scale) {
- if( pathValue==null ) {
+ float[] pathvalue = this.pathValue;
+ if( pathvalue==null ) {
return false;
}
- for(int i=0; i<pathValue.length; i++ ) {
- pathValue[i] *= scale;
+ int length = this.pathValue.length;
+ for(int i=0; i<length; i++ ) {
+ pathvalue[i] *= scale;
}
return true;
}
* @return Value of instance variable <code>pathValue</code>.
* @exception DemoException thrown if instance variable <code>pathValue</code> is undefined.
*/
- public float[] get_pathValue() {
+ /*public float[] get_pathValue() {
return(this.pathValue);
- }
+ }*/
/**
* Set method for private instance variable <code>pathValue</code>.
*
* @return Value of instance variable <code>pathDate</code>.
* @exception DemoException thrown if instance variable <code>pathDate</code> is undefined.
*/
- public int[] get_pathDate() {
+ /*public int[] get_pathDate() {
return(this.pathDate);
- }
+ }*/
/**
* Set method for private instance variable <code>pathDate</code>.
*
* calculation.
*/
public ReturnPath getReturnCompounded() {
- if( pathValue == null || nAcceptedPathValue == 0 ) {
+ int length = this.nAcceptedPathValue;
+ float[] pathvalue = this.pathValue;
+ if( pathvalue == null || length == 0 ) {
return null;
}
- float[] returnPathValue = new float[nAcceptedPathValue];
+ float[] returnPathValue = new float[length];
returnPathValue[0] = (float)0.0;
- for(int i=1; i< nAcceptedPathValue; i++ ) {
- returnPathValue[i] = Math.logf(pathValue[i] / pathValue[i-1]);
+ for(int i=1; i< length; i++ ) {
+ returnPathValue[i] = Math.logf(pathvalue[i] / pathvalue[i-1]);
}
- ReturnPath rPath = new ReturnPath(returnPathValue, nAcceptedPathValue, 1);
+ ReturnPath rPath = new ReturnPath(returnPathValue, length, 1);
//
// Copy the PathId information to the ReturnPath object.
rPath.copyInstanceVariables(this);
* calculation.
*/
public ReturnPath getReturnNonCompounded() {
- if( pathValue == null || nAcceptedPathValue == 0 ) {
+ int length = this.nAcceptedPathValue;
+ float[] pathvalue = this.pathValue;
+ if( pathvalue == null || length == 0 ) {
return null;
}
- float[] returnPathValue = new float[nAcceptedPathValue];
+ float[] returnPathValue = new float[length];
returnPathValue[0] = (float)0.0;
- for(int i=1; i< nAcceptedPathValue; i++ ) {
- returnPathValue[i] = (pathValue[i] - pathValue[i-1])/pathValue[i];
+ for(int i=1; i< length; i++ ) {
+ returnPathValue[i] = (pathvalue[i] - pathvalue[i-1])/pathvalue[i];
}
- ReturnPath rPath = new ReturnPath(returnPathValue, nAcceptedPathValue, 2);
+ ReturnPath rPath = new ReturnPath(returnPathValue, length, 2);
//
// Copy the PathId information to the ReturnPath object.
rPath.copyInstanceVariables(this);
private void readRatesFile(){
//
// Now create an array to store the rates data.
+ int minimumdate = MINIMUMDATE;
+ float epsilon = EPSILON;
int nLines = 200;
int year = 88;
int month = 10;
int day = 3;
- //System.printI(0xab0);
this.pathValue = new float[nLines];
this.pathDate = new int[nLines];
+ float[] pathvalue = this.pathValue;
+ int[] pathdate = this.pathDate;
nAcceptedPathValue=0;
int iLine=0;
+ /*char[] date = new char[9];
+ date[0] = '1';
+ date[1] = '9';
+ date[2] = (char)(year/10 + '0');
+ date[3] = (char)(year%10 + '0');
+ date[4] = (char)(month/10 + '0');
+ date[5] = (char)(month%10 + '0');
+ date[6] = (char)(day/10 + '0');
+ date[7] = (char)(day%10 + '0');
+ date[8] = '\0';*/
+ int aDate = 19881003;
+ /*for(int di = 0; di < 9; di++) {
+ aDate = aDate * 10 + (int)date[di];
+ }*/
for(int k = 0; k < 40; k++ ) {
- //System.printI(0xab1);
for(int j = 0; j < 5; j++) {
- //System.printI(0xab2);
- String date = "19"+String.valueOf(year);
+ /*String date = "19"+String.valueOf(year);
if(month < 10) {
date += "0";
}
if(day < 10) {
date += "0";
}
- date += String.valueOf(day);
- int aDate = Integer.parseInt(date);
+ date += String.valueOf(day);*/
+ //int aDate = Integer.parseInt(date);
day++;
+ aDate++;
+ /*if(date[7] == '9') {
+ date[7] = '0';
+ date[6] = (char)(date[6] + 1);
+ } else {
+ date[7] = (char)(date[7] + 1);
+ }*/
if(month == 2) {
if(day == 29) {
day = 1;
month++;
+ /*date[6] = '0';
+ date[7] = '1';
+ date[5] = '3';*/
+ aDate += 72;// - day(29) + 101;
}
} else {
if(day == 31) {
day = 1;
month++;
+ aDate += 70;
+ /*date[6] = '0';
+ date[7] = '1';*/
if(month == 13) {
month = 1;
year++;
- }
+ aDate += 8800;
+ /*date[4] = '0';
+ date[5] = '1';
+ if(date[3] == '9') {
+ if(date[2] == '9') {
+ if(date[1] == '9') {
+ date[1] = '0';
+ date[0] = (char)(date[0] + 1);
+ } else {
+ date[1] = (char)(date[1] + 1);
+ }
+ date[2] = '0';
+ } else {
+ date[2] = (char)(date[2] + 1);
+ }
+ date[3] = '0';
+ } else {
+ date[3] = (char)(date[3] + 1);
+ }*/
+ } /*else {
+ if(date[5] == '9') {
+ date[4] = '1';
+ date[5] = '0';
+ } else {
+ date[5] = (char)(date[5] + 1);
+ }
+ }*/
}
}
//
// static float float.parsefloat() method is a feature of JDK1.2!
- float aPathValue = (float)(121.7500 - k - j);
- //System.printI(0xab3);
- if( (aDate <= MINIMUMDATE) || (Math.abs(aPathValue) < EPSILON) ) {
+ int tmp = k + j;
+ float aPathValue = (float)(121.7500 - tmp);
+ if( (aDate <= minimumdate) /*| (Math.abs(aPathValue) < epsilon)*/ ) {
//System.printString("Skipped erroneous data indexed by date="+date+".");
} else {
- pathDate[iLine] = aDate;
- pathValue[iLine] = aPathValue;
+ pathdate[iLine] = aDate;
+ pathvalue[iLine] = aPathValue;
iLine++;
}
- //System.printI(0xab4);
}
}
- //System.printI(0xab5);
//
// Record the actual number of accepted data points.
nAcceptedPathValue = iLine;
//
// Now to fill in the structures from the 'PathId' class.
- set_name("rate");
- set_startDate(pathDate[0]);
- set_endDate(pathDate[nAcceptedPathValue-1]);
- set_dTime((float)(1.0/365.0));
- //System.printI(0xab6);
+ this.name = "rate";
+ this.startDate = pathdate[0];
+ this.endDate = pathdate[iLine-1];
+ this.dTime = (float)(1.0/365.0);
}
}
* </ol>
*
* @author H W Yau
- * @version $Revision: 1.1 $ $Date: 2008/08/18 22:22:21 $
+ * @version $Revision: 1.2 $ $Date: 2009/02/13 21:37:19 $
*/
public class ReturnPath extends PathId {
/**
/**
* An instance variable, for storing the return values.
*/
- private float[] pathValue;
+ public float[] pathValue;
/**
* The number of accepted values in the rate path.
*/
- private int nPathValue;
+ public int nPathValue;
/**
* Integer flag for indicating how the return was calculated.
*/
- private int returnDefinition;
+ public int returnDefinition;
/**
* Value for the expected return rate.
*/
- private float expectedReturnRate;
+ public float expectedReturnRate;
/**
* Value for the volatility, calculated from the return data.
*/
- private float volatility;
+ public float volatility;
/**
* Value for the volatility-squared, a more natural quantity
* to use for many of the calculations.
*/
- private float volatility2;
+ public float volatility2;
/**
* Value for the mean of this return.
*/
- private float mean;
+ public float mean;
/**
* Value for the variance of this return.
*/
- private float variance;
+ public float variance;
//------------------------------------------------------------------------
// Constructors.
* @param returnDefinition to tell this class how the return path values
* were computed.
*/
- public ReturnPath(float[] pathValue, int nPathValue, int returnDefinition) {
+ public ReturnPath(float[] pathValue,
+ int nPathValue,
+ int returnDefinition) {
this.pathValue = pathValue;
this.nPathValue = nPathValue;
this.returnDefinition = returnDefinition;
* @return Value of instance variable <code>pathValue</code>.
* @exception DemoException thrown if instance variable <code>pathValue</code> is undefined.
*/
- public float[] get_pathValue(){
+ /*public float[] get_pathValue(){
return(this.pathValue);
- }
+ }*/
/**
* Set method for private instance variable <code>pathValue</code>.
*
* @return Value of instance variable <code>nPathValue</code>.
* @exception DemoException thrown if instance variable <code>nPathValue</code> is undefined.
*/
- public int get_nPathValue() {
+ /*public int get_nPathValue() {
return(this.nPathValue);
- }
+ }*/
/**
* Set method for private instance variable <code>nPathValue</code>.
*
* @return Value of instance variable <code>returnDefinition</code>.
* @exception DemoException thrown if instance variable <code>returnDefinition</code> is undefined.
*/
- public int get_returnDefinition() {
+ /*public int get_returnDefinition() {
return(this.returnDefinition);
- }
+ }*/
/**
* Set method for private instance variable <code>returnDefinition</code>.
*
* @return Value of instance variable <code>expectedReturnRate</code>.
* @exception DemoException thrown if instance variable <code>expectedReturnRate</code> is undefined.
*/
- public float get_expectedReturnRate() {
+ /*public float get_expectedReturnRate() {
return(this.expectedReturnRate);
- }
+ }*/
/**
* Set method for private instance variable <code>expectedReturnRate</code>.
*
* @return Value of instance variable <code>volatility</code>.
* @exception DemoException thrown if instance variable <code>volatility</code> is undefined.
*/
- public float get_volatility() {
+ /*public float get_volatility() {
return(this.volatility);
- }
+ }*/
/**
* Set method for private instance variable <code>volatility</code>.
*
* @return Value of instance variable <code>volatility2</code>.
* @exception DemoException thrown if instance variable <code>volatility2</code> is undefined.
*/
- public float get_volatility2() {
+ /*public float get_volatility2() {
return(this.volatility2);
- }
+ }*/
/**
* Set method for private instance variable <code>volatility2</code>.
*
* @return Value of instance variable <code>mean</code>.
* @exception DemoException thrown if instance variable <code>mean</code> is undefined.
*/
- public float get_mean() {
+ /*public float get_mean() {
return(this.mean);
- }
+ }*/
/**
* Set method for private instance variable <code>mean</code>.
*
* @return Value of instance variable <code>variance</code>.
* @exception DemoException thrown if instance variable <code>variance</code> is undefined.
*/
- public float get_variance() {
+ /*public float get_variance() {
return(this.variance);
- }
+ }*/
/**
* Set method for private instance variable <code>variance</code>.
*
* @exception DemoException thrown one tries to obtain an undefined variable.
*/
public void computeExpectedReturnRate() {
- this.expectedReturnRate = mean/(float)get_dTime() + (float)0.5*volatility2;
+ this.expectedReturnRate = mean/(float)this.dTime + (float)0.5*volatility2;
}
/**
* Method to calculate <code>volatility</code> and <code>volatility2</code>
* computation are undefined.
*/
public void computeVolatility() {
- this.volatility2 = this.variance / (float)get_dTime();
+ this.volatility2 = this.variance / (float)this.dTime;
this.volatility = Math.sqrtf(volatility2);
}
/**
* undefined.
*/
public void computeMean() {
- this.mean = (float)0.0;
- for( int i=1; i < nPathValue; i++ ) {
- mean += pathValue[i];
- }
- this.mean /= ((float)(nPathValue - (float)1.0));
+ float sum = (float) 0.0;
+ float[] tmpvalue = this.pathValue;
+ int length = this.nPathValue;
+ for( int i=1; i < length; i++ ) {
+ sum += tmpvalue[i];
+ }
+ this.mean = sum / ((float)(length - (float)1.0));
}
/**
* Method to calculate the variance of the retrun, for use by other
* <code>nPathValue</code> values are undefined.
*/
public void computeVariance() {
- this.variance = (float)0.0;
- for( int i=1; i < nPathValue; i++ ) {
- variance += (pathValue[i] - mean)*(pathValue[i] - mean);
+ float sum = (float) 0.0;
+ int length = this.nPathValue;
+ float[] tmpvalue = this.pathValue;
+ float tmpmean = this.mean;
+ for( int i=1; i < length; i++ ) {
+ sum += (tmpvalue[i] - tmpmean)*(tmpvalue[i] - tmpmean);
}
- this.variance /= ((float)(nPathValue - (float)1.0));
+ this.variance = sum / ((float)(length - (float)1.0));
}
/**
* A single method for invoking all the necessary methods which
*/
public void estimatePath() {
computeMean();
- //System.printI(0xb0);
computeVariance();
- //System.printI(0xb1);
computeExpectedReturnRate();
- //System.printI(0xb2);
computeVolatility();
- //System.printI(0xb3);
}
/**
* Dumps the contents of the fields, to standard-out, for debugging.
* the Monte Carlo generate rate path.
*
* @author H W Yau
- * @version $Revision: 1.1 $ $Date: 2008/08/18 22:22:21 $
+ * @version $Revision: 1.2 $ $Date: 2009/02/13 21:37:19 $
*/
public class ToResult {
- private String header;
- private float expectedReturnRate;
- private float volatility;
- private float volatility2;
- private float finalStockPrice;
- private float[] pathValue;
+ //private String header;
+ public float expectedReturnRate;
+ public float volatility;
+ public float volatility2;
+ public float finalStockPrice;
+ public float[] pathValue;
/**
* Constructor, for the results from a computation.
* @param header Simple header string.
* @param pathValue Data computed by the Monte Carlo generator.
*/
- public ToResult(String header, float expectedReturnRate, float volatility,
- float volatility2, float finalStockPrice, float[] pathValue) {
- this.header=header;
+ public ToResult(/*String header, */
+ float expectedReturnRate,
+ float volatility,
+ float volatility2,
+ float finalStockPrice,
+ float[] pathValue) {
+ //this.header=header;
this.expectedReturnRate = expectedReturnRate;
this.volatility = volatility;
this.volatility2 = volatility2;
*
* @return String representation of this object.
*/
- public String toString(){
+ /*public String toString(){
return(header);
- }
+ }*/
//------------------------------------------------------------------------
// Accessor methods for class ToResult.
// Generated by 'makeJavaAccessor.pl' script. HWY. 20th January 1999.
*
* @return Value of instance variable <code>header</code>.
*/
- public String get_header() {
+ /*public String get_header() {
return(this.header);
- }
+ }*/
/**
* Set method for private instance variable <code>header</code>.
*
* @param header the value to set for the instance variable <code>header</code>.
*/
- public void set_header(String header) {
+ /*public void set_header(String header) {
this.header = header;
- }
+ }*/
/**
* Accessor method for private instance variable <code>expectedReturnRate</code>.
*
* @return Value of instance variable <code>expectedReturnRate</code>.
*/
- public float get_expectedReturnRate() {
+ /*public float get_expectedReturnRate() {
return(this.expectedReturnRate);
- }
+ }*/
/**
* Set method for private instance variable <code>expectedReturnRate</code>.
*
*
* @return Value of instance variable <code>volatility</code>.
*/
- public float get_volatility() {
+ /*public float get_volatility() {
return(this.volatility);
- }
+ }*/
/**
* Set method for private instance variable <code>volatility</code>.
*
*
* @return Value of instance variable <code>volatility2</code>.
*/
- public float get_volatility2() {
+ /*public float get_volatility2() {
return(this.volatility2);
- }
+ }*/
/**
* Set method for private instance variable <code>volatility2</code>.
*
*
* @return Value of instance variable <code>finalStockPrice</code>.
*/
- public float get_finalStockPrice() {
+ /*public float get_finalStockPrice() {
return(this.finalStockPrice);
- }
+ }*/
/**
* Set method for private instance variable <code>finalStockPrice</code>.
*
*
* @return Value of instance variable <code>pathValue</code>.
*/
- public float[] get_pathValue() {
+ /*public float[] get_pathValue() {
return(this.pathValue);
- }
+ }*/
/**
* Set method for private instance variable <code>pathValue</code>.
*
--- /dev/null
+/** Single thread C Version **/
+
+/**************************************************************************
+* *
+* Java Grande Forum Benchmark Suite - Thread Version 1.0 *
+* *
+* produced by *
+* *
+* Java Grande Benchmarking Project *
+* *
+* at *
+* *
+* Edinburgh Parallel Computing Centre *
+* *
+* email: epcc-javagrande@epcc.ed.ac.uk *
+* *
+* *
+* This version copyright (c) The University of Edinburgh, 2001. *
+* All rights reserved. *
+* *
+**************************************************************************/
+
+#ifdef RAW
+#include <raw.h>
+#else
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#endif
+#include <math.h>
+
+int MINIMUMDATE = 19000101;
+float EPSILON = (float)10.0 * (float)(4.9E-324);
+
+struct PathId {
+ char * name;
+ int startDate;
+ int endDate;
+ float dTime;
+};
+
+struct RatePath {
+ float * pathValue;
+ int pathLength;
+ int * pathDate;
+ int nAcceptedPathValue;
+ struct PathId pathId;
+};
+
+struct ReturnPath {
+ int COMPOUNDED; // 1: compount; 0: noncompound
+ float * pathValue;
+ int pathLength;
+ int nPathValue;
+ int returnDefinition;
+ float expectedReturnRate;
+ float volatility;
+ float volatility2;
+ float mean;
+ float variance;
+ struct PathId pathId;
+};
+
+struct MonteCarloPath {
+ float * fluctuations;
+ int fluctuationsLen;
+ float * pathValue;
+ int pathValueLen;
+ int returnDefinition;
+ float expectedReturnRate;
+ float volatility;
+ int nTimeSteps;
+ float pathStartValue;
+ struct PathId pathId;
+};
+
+struct PriceStock{
+ struct MonteCarloPath mcPath;
+ long randomSeed;
+ float pathStartValue;
+ float expectedReturnRate;
+ float volatility;
+ float volatility2;
+ float finalStockPrice;
+ float * pathValue;
+};
+
+struct MyRandom {
+ int iseed;
+ float v1;
+ float v2;
+};
+
+#define nLines 200
+
+int nTimeStepsMC = 1000;
+int nruns = 32 * 16;
+float dTime = (float)1.0/(float)365.0;
+float pathStartValue = (float)100.0;
+float* pathValue;
+int* pathDate;
+int nAcceptedPathValue = 0;
+float* returnPathValue;
+struct RatePath avgMCrate;
+float avgExpectedReturnRateMC = (float)0.0;
+float avgVolatilityMC = (float)0.0;
+float JGFavgExpectedReturnRateMC = (float)0.0;
+struct RatePath ratePath;
+struct ReturnPath returnPath;
+
+void begin(void);
+void init(void);
+void estimatePath(struct ReturnPath* returnPath);
+void computeMean(struct ReturnPath* returnPath);
+void computeVariance(struct ReturnPath* returnPath);
+void computeExpectedReturnRate(struct ReturnPath* returnPath);
+void computeVolatility(struct ReturnPath* returnPath);
+int computeFluctuationsGaussian(long randomSeed,
+ struct MonteCarloPath* mcPath);
+void computePathValue(float startValue,
+ struct MonteCarloPath* mcPath);
+int inc_pathValue(float * operandPath,
+ int len,
+ struct RatePath* ratePath);
+int inc_pathValue_int(float scale,
+ struct RatePath* ratePath);
+float seed(struct MyRandom* rnd);
+float update(struct MyRandom* rnd);
+void run(int id);
+
+#ifndef RAW
+int main(int argc, char **argv) {
+ begin();
+ return 0;
+}
+#endif
+
+void begin(void) {
+ int i = 0;
+
+ pathValue = (float *)(malloc(sizeof(float) * nLines));
+ pathDate = (int *)(malloc(sizeof(int) * nLines));
+
+ avgMCrate.pathId.name = "MC";
+ avgMCrate.pathId.startDate = 19990109;
+ avgMCrate.pathId.endDate = 19991231;
+ avgMCrate.pathId.dTime = dTime;
+ avgMCrate.pathValue = (float *)malloc(sizeof(float) * nTimeStepsMC);
+ avgMCrate.nAcceptedPathValue = nTimeStepsMC;
+
+ init();
+
+ /* Main loop: */
+ for(i = 0; i < nruns; ++i) {
+ run(i);
+ }
+
+ inc_pathValue_int((float)1.0/((float)nruns), &avgMCrate);
+ avgExpectedReturnRateMC /= nruns;
+ avgVolatilityMC /= nruns;
+ JGFavgExpectedReturnRateMC = avgExpectedReturnRateMC;
+
+#ifdef RAW
+ raw_test_pass(raw_get_cycle());
+ raw_test_done(1);
+#endif
+}
+
+void run(int id) {
+ int iRun = id;
+ int k;
+
+ struct PriceStock ps;
+ ps.randomSeed = (long)iRun*11;
+ ps.pathStartValue = pathStartValue;
+ ps.expectedReturnRate=(float)0.0;
+ ps.volatility=(float)0.0;
+ ps.volatility2=(float)0.0;
+ ps.finalStockPrice=(float)0.0;
+ ps.mcPath.pathId.name = returnPath.pathId.name;
+ ps.mcPath.pathId.startDate = returnPath.pathId.startDate;
+ ps.mcPath.pathId.endDate = returnPath.pathId.endDate;
+ ps.mcPath.pathId.dTime = returnPath.pathId.dTime;
+ ps.mcPath.expectedReturnRate = returnPath.expectedReturnRate;
+ ps.mcPath.volatility = returnPath.volatility;
+ ps.mcPath.pathStartValue = pathStartValue;
+ ps.mcPath.returnDefinition = returnPath.returnDefinition;
+ ps.mcPath.nTimeSteps = nTimeStepsMC;
+ ps.mcPath.fluctuationsLen = nTimeStepsMC;
+ ps.mcPath.fluctuations = (float *)malloc(sizeof(float) * nTimeStepsMC);
+ ps.mcPath.pathValue = (float *)malloc(sizeof(float) * nTimeStepsMC);
+ ps.mcPath.pathValueLen = nTimeStepsMC;
+
+ computeFluctuationsGaussian(ps.randomSeed, &ps.mcPath);
+ computePathValue(ps.pathStartValue, &ps.mcPath);
+
+ struct RatePath rateP;
+ rateP.pathId.name = ps.mcPath.pathId.name;
+ rateP.pathId.startDate = ps.mcPath.pathId.startDate;
+ rateP.pathId.endDate = ps.mcPath.pathId.endDate;
+ rateP.pathId.dTime = ps.mcPath.pathId.dTime;
+ //
+ // Fields pertaining to RatePath object itself.
+ rateP.pathValue = ps.mcPath.pathValue;
+ rateP.nAcceptedPathValue = ps.mcPath.nTimeSteps;
+ //
+ // Note that currently the pathDate is neither declared, defined,
+ // nor used in the MonteCarloPath object.
+ rateP.pathDate = (int *)malloc(sizeof(int) * nAcceptedPathValue);
+
+ struct ReturnPath returnP;
+ // create corresponding COMPOUND returnPath()
+ float * returnPathValue = (float *)malloc(sizeof(float) *
+ rateP.nAcceptedPathValue);
+ returnPathValue[0] = (float)0.0;
+ for(k=1; k< rateP.nAcceptedPathValue; k++ ) {
+ returnPathValue[k] = logf(rateP.pathValue[k] /
+ rateP.pathValue[k-1]);
+ }
+ returnP.pathValue = returnPathValue;
+ returnP.nPathValue = rateP.nAcceptedPathValue;
+ returnP.returnDefinition = 1;
+ returnP.COMPOUNDED = 1;
+ returnP.expectedReturnRate = (float)0.0;
+ returnP.volatility = (float)0.0;
+ returnP.volatility2 = (float)0.0;
+ returnP.mean = (float)0.0;
+ returnP.variance = (float)0.0;
+ //
+ // Copy the PathId information to the ReturnPath object.
+ returnP.pathId.name = rateP.pathId.name;
+ returnP.pathId.startDate = rateP.pathDate[0];
+ returnP.pathId.endDate = rateP.pathDate[rateP.nAcceptedPathValue-1];
+ returnP.pathId.dTime = (float)(1.0/365.0);
+ estimatePath(&returnP);
+ estimatePath(&returnP);
+
+ ps.expectedReturnRate = returnP.expectedReturnRate;
+ ps.volatility = returnP.volatility;
+ ps.volatility2 = returnP.volatility2;
+ ps.finalStockPrice = rateP.pathValue[rateP.pathLength-1];
+ ps.pathValue = ps.mcPath.pathValue;
+
+ inc_pathValue(ps.mcPath.pathValue, ps.mcPath.pathValueLen, &avgMCrate);
+ avgExpectedReturnRateMC += ps.mcPath.expectedReturnRate;
+ avgVolatilityMC += ps.mcPath.volatility;
+}
+
+void init(void) {
+ // create a RatePath()
+ int year = 88;
+ int month = 10;
+ int day = 3;
+
+ int iLine=0;
+ int k = 0;
+ int j = 0;
+ int aDate = 19881003;
+ for(k = 0; k < 40; k++) {
+ for(j = 0; j < 5; j++) {
+ day++;
+ aDate++;
+ if(month == 2) {
+ if(day == 29) {
+ day = 1;
+ month++;
+ aDate += 72;
+ }
+ } else {
+ if(day == 31) {
+ day = 1;
+ month++;
+ aDate += 70;
+ if(month == 13) {
+ month = 1;
+ year++;
+ aDate += 8800;
+ }
+ }
+ }
+ //
+ // static float float.parsefloat() method is a feature of JDK1.2!
+ int tmp = k+j;
+ float aPathValue = (float)(121.7500 - tmp);
+ if( (aDate <= MINIMUMDATE)) {
+ //System.printString("Skipped erroneous data indexed by date="+
+ //date+".");
+ } else {
+ pathDate[iLine] = aDate;
+ pathValue[iLine] = aPathValue;
+ iLine++;
+ }
+ }
+ }
+ //
+ // Record the actual number of accepted data points.
+ nAcceptedPathValue = iLine;
+ //
+ // Now to fill in the structures from the 'PathId' class.
+ ratePath.pathValue = pathValue;
+ ratePath.pathLength = nLines;
+ ratePath.pathDate = pathDate;
+ ratePath.nAcceptedPathValue = nAcceptedPathValue;
+ ratePath.pathId.name = "rate";
+ ratePath.pathId.startDate = pathDate[0];
+ ratePath.pathId.endDate = pathDate[nAcceptedPathValue-1];
+ ratePath.pathId.dTime = (float)(1.0/365.0);
+
+ // create corresponding COMPOUND returnPath()
+ returnPathValue = (float *)malloc(sizeof(float) * nAcceptedPathValue);
+ returnPathValue[0] = (float)0.0;
+ for(k=1; k< nAcceptedPathValue; k++ ) {
+ returnPathValue[k] = logf(pathValue[k] / pathValue[k-1]);
+ }
+ returnPath.pathValue = returnPathValue;
+ returnPath.nPathValue = nAcceptedPathValue;
+ returnPath.returnDefinition = 1;
+ returnPath.COMPOUNDED = 1;
+ returnPath.expectedReturnRate = (float)0.0;
+ returnPath.volatility = (float)0.0;
+ returnPath.volatility2 = (float)0.0;
+ returnPath.mean = (float)0.0;
+ returnPath.variance = (float)0.0;
+ //
+ // Copy the PathId information to the ReturnPath object.
+ returnPath.pathId.name = ratePath.pathId.name;
+ returnPath.pathId.startDate = pathDate[0];
+ returnPath.pathId.endDate = pathDate[nAcceptedPathValue-1];
+ returnPath.pathId.dTime = (float)(1.0/365.0);
+ estimatePath(&returnPath);
+ estimatePath(&returnPath);
+}
+
+void estimatePath(struct ReturnPath* returnPath) {
+ computeMean(returnPath);
+ computeVariance(returnPath);
+ computeExpectedReturnRate(returnPath);
+ computeVolatility(returnPath);
+}
+
+void computeMean(struct ReturnPath* returnPath) {
+ int i = 0;
+ returnPath->mean = (float)0.0;
+ for(i=1; i < returnPath->nPathValue; i++ ) {
+ returnPath->mean += returnPath->pathValue[i];
+ }
+ returnPath->mean /= ((float)(returnPath->nPathValue - (float)1.0));
+}
+
+void computeVariance(struct ReturnPath* returnPath) {
+ int i = 0;
+ returnPath->variance = (float)0.0;
+ for(i=1; i < returnPath->nPathValue; i++ ) {
+ returnPath->variance += (returnPath->pathValue[i] - returnPath->mean)*
+ (returnPath->pathValue[i] - returnPath->mean);
+ }
+ returnPath->variance /= ((float)(returnPath->nPathValue - (float)1.0));
+}
+
+void computeExpectedReturnRate(struct ReturnPath * returnPath) {
+ returnPath->expectedReturnRate = returnPath->mean/(float)returnPath->pathId.dTime +
+ (float)0.5*returnPath->volatility2;
+}
+
+void computeVolatility(struct ReturnPath* returnPath) {
+ returnPath->volatility2 = returnPath->variance /
+ (float)returnPath->pathId.dTime;
+ returnPath->volatility = sqrtf(returnPath->volatility2);
+}
+
+int computeFluctuationsGaussian(long randomSeed,
+ struct MonteCarloPath* mcPath) {
+ float mean, sd, gauss,meanGauss, variance;
+ int i;
+
+ if( mcPath->nTimeSteps > mcPath->fluctuationsLen ) {
+ return 0;
+ }
+ //
+ // First, make use of the passed in seed value.
+ struct MyRandom rnd;
+ float v1,v2, r;
+ v1 = (float)0.0;
+ v2 = (float)0.0;
+ if( randomSeed == -1 ) {
+ rnd.iseed = 0;
+ } else {
+ rnd.iseed = (int)randomSeed;
+ }
+ rnd.v1 = v1;
+ rnd.v2 = v2;
+ //
+ // Determine the mean and standard-deviation, from the mean-drift and volatility.
+ mean = (mcPath->expectedReturnRate-
+ (float)0.5*mcPath->volatility*mcPath->volatility)*
+ mcPath->pathId.dTime;
+ sd = mcPath->volatility*sqrtf(mcPath->pathId.dTime);
+ gauss = (float)0.0;
+ meanGauss=(float)0.0;
+ variance=(float)0.0;
+ for( i=0; i < mcPath->nTimeSteps; i += 2 ) {
+ r = seed(&rnd);
+ gauss = r*rnd.v1;
+ meanGauss+= gauss;
+ variance+= (gauss*gauss);
+ //
+ // Now map this onto a general Gaussian of given mean and variance.
+ mcPath->fluctuations[i] = mean + sd*gauss;
+ // dbgPrintln("gauss="+gauss+" fluctuations="+fluctuations[i]);
+
+ gauss = r*rnd.v2;
+ meanGauss+= gauss;
+ variance+= (gauss*gauss);
+ //
+ // Now map this onto a general Gaussian of given mean and variance.
+ mcPath->fluctuations[i+1] = mean + sd*gauss;
+ }
+ meanGauss/=(float)mcPath->nTimeSteps;
+ variance /=(float)mcPath->nTimeSteps;
+ // dbgPrintln("meanGauss="+meanGauss+" variance="+variance);
+ return 1;
+}
+
+void computePathValue(float startValue,
+ struct MonteCarloPath* mcPath) {
+ int i;
+ mcPath->pathValue[0] = startValue;
+ if( mcPath->returnDefinition == 1 |
+ mcPath->returnDefinition == 2) {
+ for(i=1; i < mcPath->nTimeSteps; i++ ) {
+ mcPath->pathValue[i] = mcPath->pathValue[i-1] *
+ expf(mcPath->fluctuations[i]);
+ }
+ }
+}
+
+float seed(struct MyRandom* rnd) {
+ float s,u1,u2,r;
+ s = (float)1.0;
+ u1 = update(rnd);
+ u2 = update(rnd);
+
+ rnd->v1 = (float)2.0 * u1 - (float)1.0;
+ rnd->v2 = (float)2.0 * u2 - (float)1.0;
+ s = rnd->v1*rnd->v1 + rnd->v2*rnd->v2;
+ s = s - (int)s;
+ r = sqrtf((float)(-2.0*logf(s))/(float)s);
+ return r;
+}
+
+float update(struct MyRandom* rnd) {
+ float rand;
+ float scale= (float)4.656612875e-10;
+ int is1,is2,iss2;
+ int imult= 16807;
+ int imod = 2147483647;
+
+ if (rnd->iseed<=0) {
+ rnd->iseed = 1;
+ }
+
+ is2 = rnd->iseed % 32768;
+ is1 = (rnd->iseed-is2)/32768;
+ iss2 = is2 * imult;
+ is2 = iss2 % 32768;
+ is1 = (is1 * imult+(iss2-is2) / 32768) % (65536);
+
+ rnd->iseed = (is1 * 32768 + is2) % imod;
+
+ rand = scale * rnd->iseed;
+
+ return rand;
+}
+
+int inc_pathValue(float* operandPath,
+ int len,
+ struct RatePath* ratePath) {
+ int i;
+ if( ratePath->pathLength != len ) {
+ return 0;
+ }
+ for(i=0; i<len; i++ ) {
+ ratePath->pathValue[i] += operandPath[i];
+ }
+ return 1;
+}
+
+int inc_pathValue_int(float scale,
+ struct RatePath* ratePath) {
+ int i;
+ if( ratePath->pathValue==NULL ) {
+ return 0;
+ }
+ for(i=0; i<ratePath->pathLength; i++ ) {
+ ratePath->pathValue[i] *= scale;
+ }
+ return 1;
+}
--- /dev/null
+TOPDIR=/home/jzhou/starsearch
+include $(TOPDIR)/Makefile.include
+
+RGCCFLAGS += -O2
+RGCCFLAGS += -DRAW
+
+USE_SLGCC=1
+
+SIM-CYCLES = 10000
+
+ATTRIBUTES += HWIC
+
+TILES = 00
+
+OBJECT_FILES_00 = JGFMonteCarloBench.o
+
+# this is for a multi-tile test
+include $(COMMONDIR)/Makefile.all
+#include $(COMMONDIR)/Makefile.single