From: jzhou Date: Sat, 24 Jul 2010 23:25:15 +0000 (+0000) Subject: add the MonteCarlo benchmark for multicore gc X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=44a9b43e3162cdedfda62cbf9e97fa10cb2e7982;p=IRC.git add the MonteCarlo benchmark for multicore gc --- diff --git a/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/AppDemo.java b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/AppDemo.java new file mode 100644 index 00000000..e6edd3d4 --- /dev/null +++ b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/AppDemo.java @@ -0,0 +1,243 @@ +/** Banboo 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 * + * * + * Original version of this code by Hon Yau (hwyau@epcc.ed.ac.uk) * + * * + * This version copyright (c) The University of Edinburgh, 2001. * + * All rights reserved. * + * * + **************************************************************************/ + + +/** + * Code, a test-harness for invoking and driving the Applications + * Demonstrator classes. + * + *

To do: + *

    + *
  1. Very long delay prior to connecting to the server.
  2. + *
  3. Some text output seem to struggle to get out, without + * the user tapping ENTER on the keyboard!
  4. + *
+ * + * @author H W Yau + * @version $Revision: 1.1 $ $Date: 2010/07/24 23:25:15 $ + */ +public class AppDemo { + flag merge; + flag validate; + + //------------------------------------------------------------------------ + // Class variables. + //------------------------------------------------------------------------ + + public float JGFavgExpectedReturnRateMC; + + //public int Serial; + //------------------------------------------------------------------------ + // Instance variables. + //------------------------------------------------------------------------ + /** + * The number of time-steps which the Monte Carlo simulation should + * run for. + */ + public int nTimeStepsMC; + /** + * The number of Monte Carlo simulations to run. + */ + public int nRunsMC; + + public int group; + /** + * The default duration between time-steps, in units of a year. + */ + public float dTime; + /** + * Flag to determine whether initialisation has already taken place. + */ + public boolean initialised; + /** + * Variable to determine which deployment scenario to run. + */ + public int runMode; + + //public Vector results; + + //public PriceStock psMC; + public float pathStartValue; + public float avgExpectedReturnRateMC; + public float avgVolatilityMC; + + public int counter; + + public RatePath avgMCrate; + + //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.nTimeStepsMC = nTimeStepsMC; + this.nRunsMC = nRunsMC; + this.group = group; + this.initialised = false; + + this.dTime = (float)1.0/(float)365.0; + this.pathStartValue = (float)100.0; + this.avgExpectedReturnRateMC = (float)0.0; + this.avgVolatilityMC = (float)0.0; + + this.counter = 0; + + this.avgMCrate = new RatePath(this.nTimeStepsMC, + "MC", + 19990109, + 19991231, + this.dTime); + + //this.initAllTasks = null; + } + /** + * Single point of contact for running this increasingly bloated + * class. Other run modes can later be defined for whether a new rate + * should be loaded in, etc. + * Note that if the hostname is set to the string "none", + * then the demonstrator runs in purely serial mode. + */ + + /** + * Initialisation and Run methods. + */ + public void initSerial() { + // + // Measure the requested path rate. + RatePath rateP = new RatePath(); + //rateP.dbgDumpFields(); + ReturnPath returnP = rateP.getReturnCompounded(); + returnP.estimatePath(); + //returnP.dbgDumpFields(); + /*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; + return; + } + + //------------------------------------------------------------------------ + /** + * Method for doing something with the Monte Carlo simulations. + * It's probably not mathematically correct, but shall take an average over + * all the simulated rate paths. + * + * @exception DemoException thrown if there is a problem with reading in + * 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.pathValue); + sumReturnRateMC += returnMC.expectedReturnRate; + sumVolatilityMC += returnMC.volatility; + } + avgExpectedReturnRateMC = sumReturnRateMC; + avgVolatilityMC = sumVolatilityMC; + + this.counter++; + boolean isfinish = (this.counter == this.group); + if(isfinish) { + avgmcrate.inc_pathValue((float)1.0/((float)nRunsMC)); + avgExpectedReturnRateMC /= nRunsMC; + avgVolatilityMC /= nRunsMC; + JGFavgExpectedReturnRateMC = avgExpectedReturnRateMC; + } + + return isfinish; + } + // + //------------------------------------------------------------------------ + // Accessor methods for class AppDemo. + // Generated by 'makeJavaAccessor.pl' script. HWY. 20th January 1999. + //------------------------------------------------------------------------ + /** + * Accessor method for private instance variable nTimeStepsMC. + * + * @return Value of instance variable nTimeStepsMC. + */ + /*public int get_nTimeStepsMC() { + return(this.nTimeStepsMC); + }*/ + /** + * Set method for private instance variable nTimeStepsMC. + * + * @param nTimeStepsMC the value to set for the instance variable nTimeStepsMC. + */ + public void set_nTimeStepsMC(int nTimeStepsMC) { + this.nTimeStepsMC = nTimeStepsMC; + } + /** + * Accessor method for private instance variable nRunsMC. + * + * @return Value of instance variable nRunsMC. + */ + /*public int get_nRunsMC() { + return(this.nRunsMC); + }*/ + /** + * Set method for private instance variable nRunsMC. + * + * @param nRunsMC the value to set for the instance variable nRunsMC. + */ + public void set_nRunsMC(int nRunsMC) { + this.nRunsMC = nRunsMC; + } + /** + * Accessor method for private instance variable results. + * + * @return Value of instance variable results. + */ + /*public Vector get_results() { + return(this.results); + }*/ + /** + * Set method for private instance variable results. + * + * @param results the value to set for the instance variable results. + */ + /*public void set_results(Vector results) { + this.results = results; + }*/ + //------------------------------------------------------------------------ +} + diff --git a/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/AppDemoRunner.java b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/AppDemoRunner.java new file mode 100644 index 00000000..588873e6 --- /dev/null +++ b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/AppDemoRunner.java @@ -0,0 +1,66 @@ +class AppDemoRunner { + flag run; + flag turnin; + + public String header; + public String name; + public int startDate; + public int endDate; + public float dTime; + public int returnDefinition; + public float expectedReturnRate; + public float volatility; + public int nTimeSteps; + public float pathStartValue; + + int id, nRunsMC, group; + //ToInitAllTasks toinitalltasks; + public Vector results; + + 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.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 = (nruns + gp-1)/gp; + + ilow = index*slice; + iupper = (index+1)*slice; + if (index==gp-1) { + iupper=nruns; + } + + for(int iRun=ilow; iRun < iupper; iRun++ ) { + //String header="MC run "+String.valueOf(iRun); + PriceStock ps = new PriceStock(); + ps.setInitAllTasks(this); + ps.setTask(/*header, */(long)iRun*11); + ps.run(); + results.addElement(ps.getResult()); + } + } +} \ No newline at end of file diff --git a/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/JGFMonteCarloBench.java b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/JGFMonteCarloBench.java new file mode 100644 index 00000000..7b6cf0ff --- /dev/null +++ b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/JGFMonteCarloBench.java @@ -0,0 +1,73 @@ +/** Banboo 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. * +* * +**************************************************************************/ + +task t1(StartupObject s{initialstate}) { + //System.printString("task t1\n"); + + int datasize = 10000; //should be times of 2 + int nruns = 62 * 62; //16 * 16; + int group = 62; // 16; + + AppDemo ad = new AppDemo(datasize, nruns, group){merge}; + + ad.initSerial(); + + for(int i = 0; i < group; i++) { + AppDemoRunner adr = new AppDemoRunner(i, + nruns, + group, + ad){run}; + } + + taskexit(s{!initialstate}); +} + +task t2(AppDemoRunner adr{run}) { + //System.printString("task t2\n"); + + // Now do the computation. + adr.run(); + + taskexit(adr{!run, turnin}); +} + +task t3(AppDemo ad{merge}, AppDemoRunner adr{turnin}) { + //System.printString("task t3\n"); + boolean isFinish = ad.processResults(adr.results); + + if(isFinish) { + taskexit(ad{!merge, validate}, adr{!turnin}); + } + taskexit(adr{!turnin}); +} + +task t4(AppDemo ad{validate}) { + //System.printString("task t4\n"); + float refval = (float)(-0.0333976656762814); + float dev = Math.abs(ad.JGFavgExpectedReturnRateMC - refval); + if (dev > 1.0e-12 ){ + //System.printString("Validation failed"); + //System.printString(" expectedReturnRate= " + (int)(ad.JGFavgExpectedReturnRateMC*10000) + " " + (int)(dev*10000) + "\n"); + } + taskexit(ad{!validate}); +} + diff --git a/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/MonteCarloPath.java b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/MonteCarloPath.java new file mode 100644 index 00000000..11f91d25 --- /dev/null +++ b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/MonteCarloPath.java @@ -0,0 +1,463 @@ +/** Banboo 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 * + * * + * Original version of this code by Hon Yau (hwyau@epcc.ed.ac.uk) * + * * + * This version copyright (c) The University of Edinburgh, 2001. * + * All rights reserved. * + * * + **************************************************************************/ + +/** + * Class representing the paths generated by the Monte Carlo engine. + * + *

To do list: + *

    + *
  1. double[] pathDate is not simulated.
  2. + *
+ * + * @author H W Yau + * @version $Revision: 1.1 $ $Date: 2010/07/24 23:25:15 $ + */ +public class MonteCarloPath extends PathId { + + //------------------------------------------------------------------------ + // Instance variables. + //------------------------------------------------------------------------ + /** + * Random fluctuations generated as a series of random numbers with + * given distribution. + */ + public float[] fluctuations; + /** + * The path values from which the random fluctuations are used to update. + */ + public float[] pathValue; + /** + * Integer flag for determining how the return was calculated, when + * used to calculate the mean drift and volatility parameters. + */ + public int returnDefinition; + /** + * Value for the mean drift, for use in the generation of the random path. + */ + public float expectedReturnRate; + /** + * Value for the volatility, for use in the generation of the random path. + */ + public float volatility; + /** + * Number of time steps for which the simulation should act over. + */ + public int nTimeSteps; + /** + * The starting value for of the security. + */ + public float pathStartValue; + //------------------------------------------------------------------------ + // Constructors. + //------------------------------------------------------------------------ + /** + * Default constructor. Needed by the HPT library to start create + * new instances of this class. The instance variables for this should + * then be initialised with the setInitAllTasks() method. + */ + public MonteCarloPath() { + super(); + this.expectedReturnRate = (float)0.0; + this.volatility = (float)0.0; + this.pathStartValue = (float)0.0; + this.returnDefinition=1; + this.nTimeSteps=0; + } + + /** + * Constructor, using the ReturnPath object to initialise + * the necessary instance variables. + * + * @param returnPath Object used to define the instance variables in + * this object. + * @param nTimeSteps The number of time steps for which to generate the + * random path. + * @exception DemoException Thrown if there is a problem initialising the + * object's instance variables. + */ + public MonteCarloPath(ReturnPath returnPath, + int nTimeSteps) { + /** + * These instance variables are members of PathId class. + */ + this.expectedReturnRate = (float)0.0; + this.volatility = (float)0.0; + this.pathStartValue = (float)0.0; + this.returnDefinition=1; + + copyInstanceVariables(returnPath); + this.nTimeSteps = nTimeSteps; + this.pathValue = new float[nTimeSteps]; + this.fluctuations = new float[nTimeSteps]; + } + /** + * Constructor, where the PathId objects is used to ease + * the number of instance variables to pass in. + * + * @param pathId Object used to define the identity of this Path. + * @param returnDefinition How the statistic variables were defined, + * according to the definitions in + * ReturnPath's two class variables + * COMPOUNDED and + * NONCOMPOUNDED. + * @param expectedReturnRate The measured expected return rate for which to generate. + * @param volatility The measured volatility for which to generate. + * @param nTimeSteps The number of time steps for which to generate. + * @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) { + /** + * These instance variables are members of PathId class. + * Invoking with this particular signature should point to the + * definition in the PathId class. + */ + this.pathStartValue = (float)0.0; + + copyInstanceVariables(pathId); + this.returnDefinition = returnDefinition; + this.expectedReturnRate = expectedReturnRate; + this.volatility = volatility; + this.nTimeSteps = nTimeSteps; + this.pathValue = new float[nTimeSteps]; + this.fluctuations = new float[nTimeSteps]; + } + /** + * Constructor, for when the user wishes to define each of the instance + * variables individually. + * + * @param name The name of the security which this Monte Carlo path + * should represent. + * @param startDate The date when the path starts, in 'YYYYMMDD' format. + * @param endDate The date when the path ends, in 'YYYYMMDD' format. + * @param dTime The interval in the data between successive data points + * in the generated path. + * @param returnDefinition How the statistic variables were defined, + * according to the definitions in + * ReturnPath's two class variables + * COMPOUNDED and + * NONCOMPOUNDED. + * @param expectedReturnRate The measured mean drift for which to generate. + * @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) { + /** + * These instance variables are members of PathId class. + */ + this.name = name; + this.startDate = startDate; + this.endDate = endDate; + this.dTime = dTime; + this.returnDefinition = returnDefinition; + this.expectedReturnRate = expectedReturnRate; + this.volatility = volatility; + this.nTimeSteps = nTimeSteps; + this.pathValue = new float[nTimeSteps]; + this.fluctuations = new float[nTimeSteps]; + } + + //------------------------------------------------------------------------ + // Methods. + //------------------------------------------------------------------------ + //------------------------------------------------------------------------ + // Accessor methods for class MonteCarloPath. + // Generated by 'makeJavaAccessor.pl' script. HWY. 20th January 1999. + //------------------------------------------------------------------------ + /** + * Accessor method for private instance variable fluctuations. + * + * @return Value of instance variable fluctuations. + * @exception DemoException thrown if instance variable fluctuations + * is undefined. + */ + /*public float[] get_fluctuations() { + return(this.fluctuations); + }*/ + /** + * Set method for private instance variable fluctuations. + * + * @param fluctuations the value to set for the instance variable + * fluctuations. + */ + public void set_fluctuations(float[] fluctuations) { + this.fluctuations = fluctuations; + } + /** + * Accessor method for private instance variable pathValue. + * + * @return Value of instance variable pathValue. + * @exception DemoException thrown if instance variable pathValue + * is undefined. + */ + /*public float[] get_pathValue() { + return(this.pathValue); + }*/ + /** + * Set method for private instance variable pathValue. + * + * @param pathValue the value to set for the instance variable pathValue. + */ + public void set_pathValue(float[] pathValue) { + this.pathValue = pathValue; + } + /** + * Accessor method for private instance variable returnDefinition. + * + * @return Value of instance variable returnDefinition. + * @exception DemoException thrown if instance variable returnDefinition + * is undefined. + */ + /*public int get_returnDefinition() { + return(this.returnDefinition); + }*/ + /** + * Set method for private instance variable returnDefinition. + * + * @param returnDefinition the value to set for the instance variable + * returnDefinition. + */ + public void set_returnDefinition(int returnDefinition) { + this.returnDefinition = returnDefinition; + } + /** + * Accessor method for private instance variable expectedReturnRate. + * + * @return Value of instance variable expectedReturnRate. + * @exception DemoException thrown if instance variable expectedReturnRate + * is undefined. + */ + /*public float get_expectedReturnRate() { + return(this.expectedReturnRate); + }*/ + /** + * Set method for private instance variable expectedReturnRate. + * + * @param expectedReturnRate the value to set for the instance variable + * expectedReturnRate. + */ + public void set_expectedReturnRate(float expectedReturnRate) { + this.expectedReturnRate = expectedReturnRate; + } + /** + * Accessor method for private instance variable volatility. + * + * @return Value of instance variable volatility. + * @exception DemoException thrown if instance variable volatility + * is undefined. + */ + /*public float get_volatility() { + return(this.volatility); + }*/ + /** + * Set method for private instance variable volatility. + * + * @param volatility the value to set for the instance variable + * volatility. + */ + public void set_volatility(float volatility) { + this.volatility = volatility; + } + /** + * Accessor method for private instance variable nTimeSteps. + * + * @return Value of instance variable nTimeSteps. + * @exception DemoException thrown if instance variable nTimeSteps + * is undefined. + */ + /*public int get_nTimeSteps() { + return(this.nTimeSteps); + }*/ + /** + * Set method for private instance variable nTimeSteps. + * + * @param nTimeSteps the value to set for the instance variable + * nTimeSteps. + */ + public void set_nTimeSteps(int nTimeSteps) { + this.nTimeSteps = nTimeSteps; + } + /** + * Accessor method for private instance variable pathStartValue. + * + * @return Value of instance variable pathStartValue. + * @exception DemoException thrown if instance variable pathStartValue + * is undefined. + */ + /*public float get_pathStartValue() { + return(this.pathStartValue); + }*/ + /** + * Set method for private instance variable pathStartValue. + * + * @param pathStartValue the value to set for the instance variable + * pathStartValue. + */ + public void set_pathStartValue(float pathStartValue) { + this.pathStartValue = pathStartValue; + } + //------------------------------------------------------------------------ + /** + * Method for copying the suitable instance variable from a + * ReturnPath object. + * + * @param obj Object used to define the instance variables which + * should be carried over to this object. + * @exception DemoException thrown if there is a problem accessing the + * instance variables from the target objetct. + */ + private void copyInstanceVariables(ReturnPath obj) { + // + // Instance variables defined in the PathId object. + 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.returnDefinition; + this.expectedReturnRate = obj.expectedReturnRate; + this.volatility = obj.volatility; + } + + /** + * Method for returning a RatePath object from the Monte Carlo data + * generated. + * + * @return a RatePath object representing the generated + * data. + * @exception DemoException thrown if there was a problem creating + * the RatePath object. + */ + public RatePath getRatePath() { + return(new RatePath(this)); + } + /** + * Method for calculating the sequence of fluctuations, based around + * a Gaussian distribution of given mean and variance, as defined + * in this class' instance variables. Mapping from Gaussian + * distribution of (0,1) to (mean-drift,volatility) is done via + * Ito's lemma on the log of the stock price. + * + * @param randomSeed The psuedo-random number seed value, to start off a + * given sequence of Gaussian fluctuations. + * @exception DemoException thrown if there are any problems with + * the computation. + */ + public boolean computeFluctuationsGaussian(long randomSeed) { + int ntimesteps = this.nTimeSteps; + int length = this.fluctuations.length; + if( ntimesteps > length ) { + return false; + } + 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; + float v1,v2, r; + v1 = (float)0.0; + v2 = (float)0.0; + if( randomSeed == -1 ) { + rnd = new MyRandom(0, v1, v2); + } else { + rnd = new MyRandom((int)randomSeed, v1, v2); + } + + // + // Determine the mean and standard-deviation, from the mean-drift and volatility. + 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; + for( int i=0; i < ntimesteps; i += 2 ) { + r = rnd.seed(); + gauss = r*rnd.v1; + meanGauss+= gauss; + variance+= gauss*gauss; + // + // Now map this onto a general Gaussian of given mean and variance. + flucts[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. + flucts[i+1] = mean + sd*gauss; + } + meanGauss/=(float)ntimesteps; + variance /=(float)ntimesteps; + // dbgPrintln("meanGauss="+meanGauss+" variance="+variance); + } + /** + * Method for calculating the sequence of fluctuations, based around + * a Gaussian distribution of given mean and variance, as defined + * in this class' instance variables. Mapping from Gaussian + * distribution of (0,1) to (mean-drift,volatility) is done via + * Ito's lemma on the log of the stock price. This overloaded method + * is for when the random seed should be decided by the system. + * + * @exception DemoException thrown if there are any problems with + * the computation. + */ + public void computeFluctuationsGaussian() { + computeFluctuationsGaussian((long)-1); + } + /** + * Method for calculating the corresponding rate path, given the + * fluctuations and starting rate value. + * + * @param startValue the starting value of the rate path, to be + * updated with the precomputed fluctuations. + * @exception DemoException thrown if there are any problems with + * the computation. + */ + public void computePathValue(float startValue) { + float[] pathvalue = this.pathValue; + float[] flucts = this.fluctuations; + int length = this.nTimeSteps; + pathvalue[0] = startValue; + if( returnDefinition == 1 | + returnDefinition == 2) { + for(int i=1; i < length; i++ ) { + //System.printI((int)(flucts[i] * 10000)); + pathvalue[i] = pathvalue[i-1] * Math.expf(flucts[i]); + } + } + } +} diff --git a/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/MyRandom.java b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/MyRandom.java new file mode 100644 index 00000000..2a8f985d --- /dev/null +++ b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/MyRandom.java @@ -0,0 +1,58 @@ +public class MyRandom { + + public int iseed; + public float v1,v2; + + public MyRandom(int iseed, float v1, float v2) { + this.iseed = iseed; + this.v1 = v1; + this.v2 = v2; + } + + 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 (seed<=0) { + iseed = seed = 1; + } + + is2 = seed % 32768; + is1 = seed / 32768; + iss2 = is2 * imult; + is2 = iss2 % 32768; + is1 = (is1 * imult + iss2 / 32768) % (65536); + + iseed = seed = (is1 * 32768 + is2) % imod; + + rand = scale * seed; + + return rand; + + } + + public float seed() { + + float s,u1,u2,r; + s = (float)1.0; + //do { + u1 = update(); + u2 = update(); + + v1 = (float)2.0 * u1 - (float)1.0; + v2 = (float)2.0 * u2 - (float)1.0; + s = v1*v1 + v2*v2; + //} while (s >= (float)1.0); + s = s - (int)s; + //System.printI(0xb4); + r = Math.sqrtf((float)(-2.0*Math.logf(s))/(float)s); + //System.printI(0xb5); + return r; + + } +} diff --git a/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/PathId.java b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/PathId.java new file mode 100644 index 00000000..9e9c92ed --- /dev/null +++ b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/PathId.java @@ -0,0 +1,179 @@ +/** Banboo 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 * +* * +* Original version of this code by Hon Yau (hwyau@epcc.ed.ac.uk) * +* * +* This version copyright (c) The University of Edinburgh, 2001. * +* All rights reserved. * +* * +**************************************************************************/ + +/** + * Base class for all the security objects, namely in terms of + * providing a consistent means of identifying each such object. + * Also provides some methods for writing out debug messages. + * + * @author H W Yau + * @version $Revision: 1.1 $ $Date: 2010/07/24 23:25:15 $ + */ +public class PathId { + + //------------------------------------------------------------------------ + // Instance variables. + //------------------------------------------------------------------------ + /** + * Simple string name. + */ + public String name; + + /** + * The start date for the path, in YYYYMMDD format. + */ + public int startDate; + /** + * The end date for the path, in YYYYMMDD format. + */ + public int endDate; + /** + * The change in time between two successive data values. + */ + public float dTime; + + //------------------------------------------------------------------------ + // Constructors. + //------------------------------------------------------------------------ + /** + * Default constructor. + */ + public PathId() { + this.startDate=0; + this.endDate=0; + this.dTime=(float)0.0; + } + + /** + * Another constructor. + * + * @param name The name for the security to record. + */ + public PathId(String name) { + this.name = name; + this.startDate=0; + this.endDate=0; + this.dTime=(float)0.0; + } + + //------------------------------------------------------------------------ + // Methods. + //------------------------------------------------------------------------ + //------------------------------------------------------------------------ + // Accessor methods for class PathId. + // Generated by 'makeJavaAccessor.pl' script. HWY. 20th January 1999. + //------------------------------------------------------------------------ + /** + * Accessor method for private instance variable name. + * + * @return Value of instance variable name. + * @exception DemoException thrown if instance variable name is undefined. + */ + /*public String get_name() { + return(this.name); + }*/ + /** + * Set method for private instance variable name. + * + * @param name the value to set for the instance variable name. + */ + public void set_name(String name) { + this.name = name; + } + /** + * Accessor method for private instance variable startDate. + * + * @return Value of instance variable startDate. + * @exception DemoException thrown if instance variable startDate is undefined. + */ + /*public int get_startDate() { + return(this.startDate); + }*/ + /** + * Set method for private instance variable startDate. + * + * @param startDate the value to set for the instance variable startDate. + */ + public void set_startDate(int startDate) { + this.startDate = startDate; + } + /** + * Accessor method for private instance variable endDate. + * + * @return Value of instance variable endDate. + * @exception DemoException thrown if instance variable endDate is undefined. + */ + /*public int get_endDate() { + return(this.endDate); + }*/ + /** + * Set method for private instance variable endDate. + * + * @param endDate the value to set for the instance variable endDate. + */ + public void set_endDate(int endDate) { + this.endDate = endDate; + } + /** + * Accessor method for private instance variable dTime. + * + * @return Value of instance variable dTime. + * @exception DemoException thrown if instance variable dTime is undefined. + */ + /*public float get_dTime() { + return(this.dTime); + }*/ + /** + * Set method for private instance variable dTime. + * + * @param dTime the value to set for the instance variable dTime. + */ + public void set_dTime(float dTime) { + this.dTime = dTime; + } + //------------------------------------------------------------------------ + /** + * Clone the instance variables in this class, from another instance + * of this class. + * + * @param obj the PathId object from which to copy. + * @exception DemoException thrown if the values to be copied contain + * any undefined objects. + */ + public void copyInstanceVariables(PathId obj) { + 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. + */ + public void dbgDumpFields() { +// dbgPrintln("name=" +this.name); +// dbgPrintln("startDate="+this.startDate); +// dbgPrintln("endDate=" +this.endDate); +// dbgPrintln("dTime=" +this.dTime); + } +} + diff --git a/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/PriceStock.java b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/PriceStock.java new file mode 100644 index 00000000..fa7479e7 --- /dev/null +++ b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/PriceStock.java @@ -0,0 +1,150 @@ +/** Banboo 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 * + * * + * Original version of this code by Hon Yau (hwyau@epcc.ed.ac.uk) * + * * + * This version copyright (c) The University of Edinburgh, 2001. * + * All rights reserved. * + * * + **************************************************************************/ + +/** + * Class to do the work in the Application demonstrator, in particular + * the pricing of the stock path generated by Monte Carlo. The run + * method will generate a single sequence with the required statistics, + * estimate its volatility, expected return rate and final stock price + * value. + * + * @author H W Yau + * @version $Revision: 1.1 $ $Date: 2010/07/24 23:25:15 $ + */ +public class PriceStock{ + + //------------------------------------------------------------------------ + // Instance variables. + //------------------------------------------------------------------------ + /** + * The Monte Carlo path to be generated. + */ + public MonteCarloPath mcPath; + /** + * String identifier for a given task. + */ + //private String taskHeader; + /** + * Random seed from which the Monte Carlo sequence is started. + */ + public long randomSeed; + /** + * Initial stock price value. + */ + public float pathStartValue; + /** + * Object which represents the results from a given computation task. + */ + 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.randomSeed=-1; + this.pathStartValue=(float)0.0; + this.expectedReturnRate=(float)0.0; + this.volatility=(float)0.0; + this.volatility2=(float)0.0; + this.finalStockPrice=(float)0.0; + + mcPath = new MonteCarloPath(); + } + //------------------------------------------------------------------------ + // Methods. + //------------------------------------------------------------------------ + //------------------------------------------------------------------------ + // Methods which implement the Slaveable interface. + //------------------------------------------------------------------------ + /** + * Method which is passed in the initialisation data common to all tasks, + * and then unpacks them for use by this object. + * + * @param obj Object representing data which are common to all tasks. + */ + public void setInitAllTasks(AppDemoRunner obj) { + 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.nTimeSteps = nTimeSteps; + this.pathStartValue = obj.pathStartValue; + 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 + * unpacks it for use by this object. + * + * @param obj Object representing the data which defines a given task. + */ + public void setTask(/*String header, */long randomSeed) { + //this.taskHeader = header; + this.randomSeed = randomSeed; + } + /** + * The business end. Invokes the necessary computation routine, for a + * a given task. + */ + public void run() { + mcPath.computeFluctuationsGaussian(randomSeed); + mcPath.computePathValue(pathStartValue); + RatePath rateP = new RatePath(mcPath); + ReturnPath returnP = rateP.getReturnCompounded(); + returnP.estimatePath(); + 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); + return res; + } +} diff --git a/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/RatePath.java b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/RatePath.java new file mode 100644 index 00000000..8ca8e57e --- /dev/null +++ b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/RatePath.java @@ -0,0 +1,483 @@ +/** Banboo 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 * + * * + * Original version of this code by Hon Yau (hwyau@epcc.ed.ac.uk) * + * * + * This version copyright (c) The University of Edinburgh, 2001. * + * All rights reserved. * + * * + **************************************************************************/ + +/** + * Class for recording the values in the time-dependent path of a security. + * + *

To Do list: + *

    + *
  1. None! + *
+ * + * @author H W Yau + * @version $Revision: 1.1 $ $Date: 2010/07/24 23:25:15 $ + */ +public class RatePath extends PathId { + + //------------------------------------------------------------------------ + // Class variables. + //------------------------------------------------------------------------ + /** + * Class variable to represent the minimal date, whence the stock prices + * appear. Used to trap any potential problems with the data. + */ + public int MINIMUMDATE; + /** + * Class variable for defining what is meant by a small number, small enough + * to cause an arithmetic overflow when dividing. According to the + * Java Nutshell book, the actual range is +/-4.9406564841246544E-324 + */ + public float EPSILON; + + //------------------------------------------------------------------------ + // Instance variables. + //------------------------------------------------------------------------ + /** + * An instance variable, for storing the rate's path values itself. + */ + public float[] pathValue; + /** + * An instance variable, for storing the corresponding date of the datum, + * in 'YYYYMMDD' format. + */ + public int[] pathDate; + /** + * The number of accepted values in the rate path. + */ + public int nAcceptedPathValue; + + //------------------------------------------------------------------------ + // Constructors. + //------------------------------------------------------------------------ + /** + * Constructor, where the user specifies the directory and filename in + * from which the data should be read. + * + * @param String dirName + * @param String filename + * @exception DemoException thrown if there is a problem reading in + * the data file. + */ + public RatePath() { + this.MINIMUMDATE = 19000101; + this.EPSILON= (float)10.0 * (float)(4.9E-324); + this.nAcceptedPathValue = 0; + readRatesFile(); + } + /** + * Constructor, for when the user specifies simply an array of values + * for the path. User must also include information for specifying + * the other characteristics of the path. + * + * @param pathValue the array containing the values for the path. + * @param name the name to attach to the path. + * @param startDate date from which the path is supposed to start, in + * 'YYYYMMDD' format. + * @param startDate date from which the path is supposed to end, in + * 'YYYYMMDD' format. + * @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) { + this.MINIMUMDATE = 19000101; + this.EPSILON= (float)10.0 * (float)(4.9E-324); + + this.name = name; + this.startDate = startDate; + this.endDate = endDate; + this.dTime = dTime; + this.pathValue = pathValue; + this.nAcceptedPathValue = pathValue.length; + } + /** + * Constructor, for use by the Monte Carlo generator, when it wishes + * to represent its findings as a RatePath object. + * + * @param mc the Monte Carlo generator object, whose data are to + * be copied over. + * @exception DemoException thrown if there is an attempt to access + * an undefined variable. + */ + public RatePath(MonteCarloPath mc) { + this.MINIMUMDATE = 19000101; + this.EPSILON= (float)10.0 * (float)(4.9E-324); + + // + // Fields pertaining to the parent PathId object: + this.name = mc.name; + this.startDate = mc.startDate; + this.endDate = mc.endDate; + this.dTime = mc.dTime; + // + // Fields pertaining to RatePath object itself. + pathValue=mc.pathValue; + nAcceptedPathValue=mc.nTimeSteps; + // + // Note that currently the pathDate is neither declared, defined, + // nor used in the MonteCarloPath object. + pathDate=new int[nAcceptedPathValue]; + } + /** + * Constructor, for when there is no actual pathValue with which to + * initialise. + * + * @param pathValueLegth the length of the array containing the values + * for the path. + * @param name the name to attach to the path. + * @param startDate date from which the path is supposed to start, in + * 'YYYYMMDD' format. + * @param startDate date from which the path is supposed to end, in + * 'YYYYMMDD' format. + * @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) { + this.MINIMUMDATE = 19000101; + this.EPSILON= (float)10.0 * (float)(4.9E-324); + + this.name = name; + this.startDate = startDate; + this.endDate = endDate; + this.dTime = dTime; + this.pathValue = new float[pathValueLength]; + this.nAcceptedPathValue = pathValue.length; + } + //------------------------------------------------------------------------ + // Methods. + //------------------------------------------------------------------------ + /** + * Routine to update this rate path with the values from another rate + * path, via its pathValue array. + * + * @param operandPath the path value array to use for the update. + * @exception DemoException thrown if there is a mismatch between the + * lengths of the operand and target arrays. + */ + public boolean inc_pathValue(float[] operandPath) { + int length = this.pathValue.length; + if( length != operandPath.length ) { + return false; + } + float[] pathvalue = this.pathValue; + for(int i=0; ipathValue. + * + * @return Value of instance variable pathValue. + * @exception DemoException thrown if instance variable pathValue is undefined. + */ + /*public float[] get_pathValue() { + return(this.pathValue); + }*/ + /** + * Set method for private instance variable pathValue. + * + * @param pathValue the value to set for the instance variable pathValue. + */ + public void set_pathValue(float[] pathValue) { + this.pathValue = pathValue; + } + /** + * Accessor method for private instance variable pathDate. + * + * @return Value of instance variable pathDate. + * @exception DemoException thrown if instance variable pathDate is undefined. + */ + /*public int[] get_pathDate() { + return(this.pathDate); + }*/ + /** + * Set method for private instance variable pathDate. + * + * @param pathDate the value to set for the instance variable pathDate. + */ + public void set_pathDate(int[] pathDate) { + this.pathDate = pathDate; + } + //------------------------------------------------------------------------ + /** + * Method to return the terminal value for a given rate path, as used + * in derivative calculations. + * + * @return The last value in the rate path. + */ + public float getEndPathValue() { + return( getPathValue(pathValue.length-1) ); + } + /** + * Method to return the value for a given rate path, at a given index. + * One may want to index this in a more user friendly manner! + * + * @param index the index on which to return the path value. + * @return The value of the path at the designated index. + */ + public float getPathValue(int index) { + return(pathValue[index]); + } + /** + * Method for calculating the returns on a given rate path, via the + * definition for the instantaneous compounded return. + * u_i = \ln{\frac{S_i}{S_{i-1}}} + * + * @return the return, as defined. + * @exception DemoException thrown if there is a problem with the + * calculation. + */ + public ReturnPath getReturnCompounded() { + int length = this.nAcceptedPathValue; + float[] pathvalue = this.pathValue; + if( pathvalue == null || length == 0 ) { + return null; + } + float[] returnPathValue = new float[length]; + returnPathValue[0] = (float)0.0; + for(int i=1; i< length; i++ ) { + returnPathValue[i] = Math.logf(pathvalue[i] / pathvalue[i-1]); + } + + ReturnPath rPath = new ReturnPath(returnPathValue, length, 1); + // + // Copy the PathId information to the ReturnPath object. + rPath.copyInstanceVariables(this); + rPath.estimatePath(); + return(rPath); + } + /** + * Method for calculating the returns on a given rate path, via the + * definition for the instantaneous non-compounded return. + * u_i = \frac{S_i - S_{i-1}}{S_i} + * + * @return the return, as defined. + * @exception DemoException thrown if there is a problem with the + * calculation. + */ + public ReturnPath getReturnNonCompounded() { + int length = this.nAcceptedPathValue; + float[] pathvalue = this.pathValue; + if( pathvalue == null || length == 0 ) { + return null; + } + float[] returnPathValue = new float[length]; + returnPathValue[0] = (float)0.0; + for(int i=1; i< length; i++ ) { + returnPathValue[i] = (pathvalue[i] - pathvalue[i-1])/pathvalue[i]; + } + + ReturnPath rPath = new ReturnPath(returnPathValue, length, 2); + // + // Copy the PathId information to the ReturnPath object. + rPath.copyInstanceVariables(this); + rPath.estimatePath(); + return(rPath); + } + //------------------------------------------------------------------------ + // Private methods. + //------------------------------------------------------------------------ + /** + * Method for reading in data file, in a given format. + * Namely: +
+      881003,0.0000,14.1944,13.9444,14.0832,2200050,0
+      881004,0.0000,14.1668,14.0556,14.1668,1490850,0
+      ...
+      990108,35.8125,36.7500,35.5625,35.8125,4381200,0
+      990111,35.8125,35.8750,34.8750,35.1250,3920800,0
+      990112,34.8750,34.8750,34.0000,34.0625,3577500,0
+      
+ *

Where the fields represent, one believes, the following: + *

    + *
  1. The date in 'YYMMDD' format
  2. + *
  3. Open
  4. + *
  5. High
  6. + *
  7. Low
  8. + *
  9. Last
  10. + *
  11. Volume
  12. + *
  13. Open Interest
  14. + *
+ * One will probably make use of the closing price, but this can be + * redefined via the class variable DATUMFIELD. Note that + * since the read in data are then used to compute the return, this would + * be a good place to trap for zero values in the data, which will cause + * all sorts of problems. + * + * @param dirName the directory in which to search for the data file. + * @param filename the data filename itself. + * @exception DemoException thrown if there was a problem with the data + * file. + */ + private void readRatesFile(){ + // + // Now create an array to store the rates data. + int minimumdate = MINIMUMDATE; + float epsilon = EPSILON; + int nLines = 1000; //200; + int year = 88; + int month = 10; + int day = 3; + 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 < 20; /*40;*/ k++ ) { + for(int j = 0; j < 50; /*5;*/ j++) { + /*String date = "19"+String.valueOf(year); + if(month < 10) { + date += "0"; + } + date += String.valueOf(month); + if(day < 10) { + date += "0"; + } + 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! + 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; + iLine++; + } + } + } + // + // Record the actual number of accepted data points. + nAcceptedPathValue = iLine; + // + // Now to fill in the structures from the 'PathId' class. + this.name = "rate"; + this.startDate = pathdate[0]; + this.endDate = pathdate[iLine-1]; + this.dTime = (float)(1.0/365.0); + } +} diff --git a/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/ReturnPath.java b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/ReturnPath.java new file mode 100644 index 00000000..8bfcfe6b --- /dev/null +++ b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/ReturnPath.java @@ -0,0 +1,360 @@ +/** Banboo 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 * +* * +* Original version of this code by Hon Yau (hwyau@epcc.ed.ac.uk) * +* * +* This version copyright (c) The University of Edinburgh, 2001. * +* All rights reserved. * +* * +**************************************************************************/ + + +/** + * Class for representing the returns of a given security. + * + *

To do list: + *

    + *
  1. Define a window over which the mean drift and volatility + * are calculated.
  2. + *
  3. Hash table to reference {DATE}->{pathValue-index}.
  4. + *
+ * + * @author H W Yau + * @version $Revision: 1.1 $ $Date: 2010/07/24 23:25:15 $ + */ +public class ReturnPath extends PathId { + /** + * Flag for indicating one of the return definitions, via: + * u_i = \ln{\frac{S_i}{S_{i-1}}} + * corresponding to the instantaneous compounded return. + */ + public int COMPOUNDED; + + /** + * Flag for indicating one of the return definitions, via: + * u_i = \frac{S_i - S_{i-1}}{S_i} + * corresponding to the instantaneous non-compounded return. + */ + public int NONCOMPOUNDED; + + //------------------------------------------------------------------------ + // Instance variables. + //------------------------------------------------------------------------ + /** + * An instance variable, for storing the return values. + */ + public float[] pathValue; + /** + * The number of accepted values in the rate path. + */ + public int nPathValue; + /** + * Integer flag for indicating how the return was calculated. + */ + public int returnDefinition; + /** + * Value for the expected return rate. + */ + public float expectedReturnRate; + /** + * Value for the volatility, calculated from the return data. + */ + public float volatility; + /** + * Value for the volatility-squared, a more natural quantity + * to use for many of the calculations. + */ + public float volatility2; + /** + * Value for the mean of this return. + */ + public float mean; + /** + * Value for the variance of this return. + */ + public float variance; + + //------------------------------------------------------------------------ + // Constructors. + //------------------------------------------------------------------------ + /** + * Default constructor. + */ + public ReturnPath() { + super(); + + this.COMPOUNDED = 1; + this.NONCOMPOUNDED = 2; + this.nPathValue=-1; + this.returnDefinition = -1; + this.expectedReturnRate = (float)0.0; + this.volatility = (float)0.0; + this.volatility2 = (float)0.0; + this.mean = (float)0.0; + this.variance = (float)0.0; + } + + /** + * Another constructor. + * + * @param pathValue for creating a return path with a precomputed path + * value. Indexed from 1 to nPathArray-1. + * @param nPathValue the number of accepted data points in the array. + * @param returnDefinition to tell this class how the return path values + * were computed. + */ + public ReturnPath(float[] pathValue, + int nPathValue, + int returnDefinition) { + this.pathValue = pathValue; + this.nPathValue = nPathValue; + this.returnDefinition = returnDefinition; + + this.COMPOUNDED = 1; + this.NONCOMPOUNDED = 2; + this.expectedReturnRate = (float)0.0; + this.volatility = (float)0.0; + this.volatility2 = (float)0.0; + this.mean = (float)0.0; + this.variance = (float)0.0; + } + + //------------------------------------------------------------------------ + // Methods. + //------------------------------------------------------------------------ + //------------------------------------------------------------------------ + // Accessor methods for class ReturnPath. + // Generated by 'makeJavaAccessor.pl' script. HWY. 20th January 1999. + //------------------------------------------------------------------------ + /** + * Accessor method for private instance variable pathValue. + * + * @return Value of instance variable pathValue. + * @exception DemoException thrown if instance variable pathValue is undefined. + */ + /*public float[] get_pathValue(){ + return(this.pathValue); + }*/ + /** + * Set method for private instance variable pathValue. + * + * @param pathValue the value to set for the instance variable pathValue. + */ + public void set_pathValue(float[] pathValue) { + this.pathValue = pathValue; + } + /** + * Accessor method for private instance variable nPathValue. + * + * @return Value of instance variable nPathValue. + * @exception DemoException thrown if instance variable nPathValue is undefined. + */ + /*public int get_nPathValue() { + return(this.nPathValue); + }*/ + /** + * Set method for private instance variable nPathValue. + * + * @param nPathValue the value to set for the instance variable nPathValue. + */ + public void set_nPathValue(int nPathValue) { + this.nPathValue = nPathValue; + } + /** + * Accessor method for private instance variable returnDefinition. + * + * @return Value of instance variable returnDefinition. + * @exception DemoException thrown if instance variable returnDefinition is undefined. + */ + /*public int get_returnDefinition() { + return(this.returnDefinition); + }*/ + /** + * Set method for private instance variable returnDefinition. + * + * @param returnDefinition the value to set for the instance variable returnDefinition. + */ + public void set_returnDefinition(int returnDefinition) { + this.returnDefinition = returnDefinition; + } + /** + * Accessor method for private instance variable expectedReturnRate. + * + * @return Value of instance variable expectedReturnRate. + * @exception DemoException thrown if instance variable expectedReturnRate is undefined. + */ + /*public float get_expectedReturnRate() { + return(this.expectedReturnRate); + }*/ + /** + * Set method for private instance variable expectedReturnRate. + * + * @param expectedReturnRate the value to set for the instance variable expectedReturnRate. + */ + public void set_expectedReturnRate(float expectedReturnRate) { + this.expectedReturnRate = expectedReturnRate; + } + /** + * Accessor method for private instance variable volatility. + * + * @return Value of instance variable volatility. + * @exception DemoException thrown if instance variable volatility is undefined. + */ + /*public float get_volatility() { + return(this.volatility); + }*/ + /** + * Set method for private instance variable volatility. + * + * @param volatility the value to set for the instance variable volatility. + */ + public void set_volatility(float volatility) { + this.volatility = volatility; + } + /** + * Accessor method for private instance variable volatility2. + * + * @return Value of instance variable volatility2. + * @exception DemoException thrown if instance variable volatility2 is undefined. + */ + /*public float get_volatility2() { + return(this.volatility2); + }*/ + /** + * Set method for private instance variable volatility2. + * + * @param volatility2 the value to set for the instance variable volatility2. + */ + public void set_volatility2(float volatility2) { + this.volatility2 = volatility2; + } + /** + * Accessor method for private instance variable mean. + * + * @return Value of instance variable mean. + * @exception DemoException thrown if instance variable mean is undefined. + */ + /*public float get_mean() { + return(this.mean); + }*/ + /** + * Set method for private instance variable mean. + * + * @param mean the value to set for the instance variable mean. + */ + public void set_mean(float mean) { + this.mean = mean; + } + /** + * Accessor method for private instance variable variance. + * + * @return Value of instance variable variance. + * @exception DemoException thrown if instance variable variance is undefined. + */ + /*public float get_variance() { + return(this.variance); + }*/ + /** + * Set method for private instance variable variance. + * + * @param variance the value to set for the instance variable variance. + */ + public void set_variance(float variance) { + this.variance = variance; + } + //------------------------------------------------------------------------ + /** + * Method to calculate the expected return rate from the return data, + * using the relationship: + * \mu = \frac{\bar{u}}{\Delta t} + \frac{\sigma^2}{2} + * + * @exception DemoException thrown one tries to obtain an undefined variable. + */ + public void computeExpectedReturnRate() { + this.expectedReturnRate = mean/(float)this.dTime + (float)0.5*volatility2; + } + /** + * Method to calculate volatility and volatility2 + * from the return path data, using the relationship, based on the + * precomputed variance. + * \sigma^2 = s^2\Delta t + * + * @exception DemoException thrown if one of the quantites in the + * computation are undefined. + */ + public void computeVolatility() { + this.volatility2 = this.variance / (float)this.dTime; + this.volatility = Math.sqrtf(volatility2); + } + /** + * Method to calculate the mean of the return, for use by other + * calculations. + * + * @exception DemoException thrown if nPathValue is + * undefined. + */ + public void computeMean() { + 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 + * calculations. + * + * @exception DemoException thrown if the mean or + * nPathValue values are undefined. + */ + public void computeVariance() { + 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 = sum / ((float)(length - (float)1.0)); + } + /** + * A single method for invoking all the necessary methods which + * estimate the parameters. + * + * @exception DemoException thrown if there is a problem reading any + * variables. + */ + public void estimatePath() { + computeMean(); + computeVariance(); + computeExpectedReturnRate(); + computeVolatility(); + } + /** + * Dumps the contents of the fields, to standard-out, for debugging. + */ + public void dbgDumpFields() { + super.dbgDumpFields(); +// dbgPrintln("nPathValue=" +this.nPathValue); +// dbgPrintln("expectedReturnRate="+this.expectedReturnRate); +// dbgPrintln("volatility=" +this.volatility); +// dbgPrintln("volatility2=" +this.volatility2); +// dbgPrintln("mean=" +this.mean); +// dbgPrintln("variance=" +this.variance); + } +} diff --git a/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/ToResult.java b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/ToResult.java new file mode 100644 index 00000000..ecc10ca8 --- /dev/null +++ b/Robust/src/Benchmarks/Scheduling/GC/JGFMonteCarlo/ToResult.java @@ -0,0 +1,171 @@ +/** Banboo 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 * +* * +* Original version of this code by Hon Yau (hwyau@epcc.ed.ac.uk) * +* * +* This version copyright (c) The University of Edinburgh, 2001. * +* All rights reserved. * +* * +**************************************************************************/ + +/** + * Class for defining the results of a task. Currently, this is simply + * the Monte Carlo generate rate path. + * + * @author H W Yau + * @version $Revision: 1.1 $ $Date: 2010/07/24 23:25:15 $ + */ +public class ToResult { + //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; + this.expectedReturnRate = expectedReturnRate; + this.volatility = volatility; + this.volatility2 = volatility2; + this.finalStockPrice = finalStockPrice; + this.pathValue = pathValue; + } + /** + * Gives a simple string representation of this object. + * + * @return String representation of this object. + */ + /*public String toString(){ + return(header); + }*/ + //------------------------------------------------------------------------ + // Accessor methods for class ToResult. + // Generated by 'makeJavaAccessor.pl' script. HWY. 20th January 1999. + //------------------------------------------------------------------------ + /** + * Accessor method for private instance variable header. + * + * @return Value of instance variable header. + */ + /*public String get_header() { + return(this.header); + }*/ + /** + * Set method for private instance variable header. + * + * @param header the value to set for the instance variable header. + */ + /*public void set_header(String header) { + this.header = header; + }*/ + /** + * Accessor method for private instance variable expectedReturnRate. + * + * @return Value of instance variable expectedReturnRate. + */ + /*public float get_expectedReturnRate() { + return(this.expectedReturnRate); + }*/ + /** + * Set method for private instance variable expectedReturnRate. + * + * @param expectedReturnRate the value to set for the instance variable + * expectedReturnRate. + */ + public void set_expectedReturnRate(float expectedReturnRate) { + this.expectedReturnRate = expectedReturnRate; + } + /** + * Accessor method for private instance variable volatility. + * + * @return Value of instance variable volatility. + */ + /*public float get_volatility() { + return(this.volatility); + }*/ + /** + * Set method for private instance variable volatility. + * + * @param volatility the value to set for the instance variable volatility. + */ + public void set_volatility(float volatility) { + this.volatility = volatility; + } + /** + * Accessor method for private instance variable volatility2. + * + * @return Value of instance variable volatility2. + */ + /*public float get_volatility2() { + return(this.volatility2); + }*/ + /** + * Set method for private instance variable volatility2. + * + * @param volatility2 the value to set for the instance variable volatility2. + */ + public void set_volatility2(float volatility2) { + this.volatility2 = volatility2; + } + /** + * Accessor method for private instance variable finalStockPrice. + * + * @return Value of instance variable finalStockPrice. + */ + /*public float get_finalStockPrice() { + return(this.finalStockPrice); + }*/ + /** + * Set method for private instance variable finalStockPrice. + * + * @param finalStockPrice the value to set for the instance variable + * finalStockPrice. + */ + public void set_finalStockPrice(float finalStockPrice) { + this.finalStockPrice = finalStockPrice; + } + /** + * Accessor method for private instance variable pathValue. + * + * @return Value of instance variable pathValue. + */ + /*public float[] get_pathValue() { + return(this.pathValue); + }*/ + /** + * Set method for private instance variable pathValue. + * + * @param pathValue the value to set for the instance variable pathValue. + */ + public void set_pathValue(float[] pathValue) { + this.pathValue = pathValue; + } + //------------------------------------------------------------------------ +} + +