📄 montecarlopath.java
字号:
*/ public int get_returnDefinition() throws DemoException { if( this.returnDefinition == 0 ) throw new DemoException("Variable returnDefinition is undefined!"); return(this.returnDefinition); } /** * Set method for private instance variable <code>returnDefinition</code>. * * @param returnDefinition the value to set for the instance variable * <code>returnDefinition</code>. */ public void set_returnDefinition(int returnDefinition) { this.returnDefinition = returnDefinition; } /** * Accessor method for private instance variable <code>expectedReturnRate</code>. * * @return Value of instance variable <code>expectedReturnRate</code>. * @exception DemoException thrown if instance variable <code>expectedReturnRate</code> * is undefined. */ public double get_expectedReturnRate() throws DemoException { if( this.expectedReturnRate == Double.NaN ) throw new DemoException("Variable expectedReturnRate is undefined!"); return(this.expectedReturnRate); } /** * Set method for private instance variable <code>expectedReturnRate</code>. * * @param expectedReturnRate the value to set for the instance variable * <code>expectedReturnRate</code>. */ public void set_expectedReturnRate(double expectedReturnRate) { this.expectedReturnRate = expectedReturnRate; } /** * Accessor method for private instance variable <code>volatility</code>. * * @return Value of instance variable <code>volatility</code>. * @exception DemoException thrown if instance variable <code>volatility</code> * is undefined. */ public double get_volatility() throws DemoException { if( this.volatility == Double.NaN ) throw new DemoException("Variable volatility is undefined!"); return(this.volatility); } /** * Set method for private instance variable <code>volatility</code>. * * @param volatility the value to set for the instance variable * <code>volatility</code>. */ public void set_volatility(double volatility) { this.volatility = volatility; } /** * Accessor method for private instance variable <code>nTimeSteps</code>. * * @return Value of instance variable <code>nTimeSteps</code>. * @exception DemoException thrown if instance variable <code>nTimeSteps</code> * is undefined. */ public int get_nTimeSteps() throws DemoException { if( this.nTimeSteps == 0 ) throw new DemoException("Variable nTimeSteps is undefined!"); return(this.nTimeSteps); } /** * Set method for private instance variable <code>nTimeSteps</code>. * * @param nTimeSteps the value to set for the instance variable * <code>nTimeSteps</code>. */ public void set_nTimeSteps(int nTimeSteps) { this.nTimeSteps = nTimeSteps; } /** * Accessor method for private instance variable <code>pathStartValue</code>. * * @return Value of instance variable <code>pathStartValue</code>. * @exception DemoException thrown if instance variable <code>pathStartValue</code> * is undefined. */ public double get_pathStartValue() throws DemoException { if( this.pathStartValue == Double.NaN ) throw new DemoException("Variable pathStartValue is undefined!"); return(this.pathStartValue); } /** * Set method for private instance variable <code>pathStartValue</code>. * * @param pathStartValue the value to set for the instance variable * <code>pathStartValue</code>. */ public void set_pathStartValue(double pathStartValue) { this.pathStartValue = pathStartValue; } //------------------------------------------------------------------------ /** * Method for copying the suitable instance variable from a * <code>ReturnPath</code> 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) throws DemoException { // // 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()); // // Instance variables defined in this object. this.returnDefinition = obj.get_returnDefinition(); this.expectedReturnRate = obj.get_expectedReturnRate(); this.volatility = obj.get_volatility(); } /** * Method for writing out the values from a Monte Carlo path into a * data file. This can then be fed back in as a test. * The data are written in the following format: <pre> 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 </pre> * <p>Where the fields represent, one believes, the following: * <ol> * <li>The date in 'YYMMDD' format</li> * <li>Open</li> * <li>High</li> * <li>Low</li> * <li>Last</li> * <li>Volume</li> * <li>Open Interest</li> * </ol> * One will probably make use of the closing price, but this can be * redefined via the class variable <code>DATUMFIELD</code>. 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 write the data file. * @param filename the data filename itself. * @exception DemoException thrown if there was a problem with the data * file. */ public void writeFile(String dirName, String filename) throws DemoException { try{ java.io.File ratesFile = new File(dirName, filename); if( ratesFile.exists() && ! ratesFile.canWrite() ) throw new DemoException("Cannot write to specified filename!"); java.io.PrintWriter out = new PrintWriter(new BufferedWriter( new FileWriter(ratesFile))); for(int i=0; i < nTimeSteps; i++) { out.print("19990101,"); for(int j=1; j<DATUMFIELD; j++ ) { out.print("0.0000,"); } out.print(pathValue[i]+","); out.println("0.0000,0.0000"); } out.close(); } catch(java.io.IOException ioex) { throw new DemoException(ioex.toString()); } } /** * Method for returning a RatePath object from the Monte Carlo data * generated. * * @return a <code>RatePath</code> object representing the generated * data. * @exception DemoException thrown if there was a problem creating * the RatePath object. */ public RatePath getRatePath() throws DemoException{ 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 void computeFluctuationsGaussian(long randomSeed) throws DemoException { if( nTimeSteps > fluctuations.length ) throw new DemoException("Number of timesteps requested is greater than the allocated array!"); // // First, make use of the passed in seed value. Random rnd; if( randomSeed == -1 ) { rnd = new Random(); } else { rnd = new Random(randomSeed); } // // Determine the mean and standard-deviation, from the mean-drift and volatility. double mean = (expectedReturnRate-0.5*volatility*volatility)*get_dTime(); double sd = volatility*Math.sqrt(get_dTime()); double gauss, meanGauss=0.0, variance=0.0; for( int i=0; i < nTimeSteps; i++ ) { gauss = rnd.nextGaussian(); meanGauss+= gauss; variance+= (gauss*gauss); // // Now map this onto a general Gaussian of given mean and variance. fluctuations[i] = mean + sd*gauss; // dbgPrintln("gauss="+gauss+" fluctuations="+fluctuations[i]); } meanGauss/=(double)nTimeSteps; variance /=(double)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() throws DemoException { 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(double startValue) throws DemoException { pathValue[0] = startValue; if( returnDefinition == ReturnPath.COMPOUNDED || returnDefinition == ReturnPath.NONCOMPOUNDED) { for(int i=1; i < nTimeSteps; i++ ) { pathValue[i] = pathValue[i-1] * Math.exp(fluctuations[i]); } } else { throw new DemoException("Unknown or undefined update method."); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -