📄 qmcversusmc_2.java
字号:
/* WARANTY NOTICE AND COPYRIGHTThis program is free software; you can redistribute it and/ormodify it under the terms of the GNU General Public Licenseas published by the Free Software Foundation; either version 2of the License, or (at your option) any later version.This program is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with this program; if not, write to the Free SoftwareFoundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.Copyright (C) Michael J. Meyermatmjm@mindspring.comspyqqqdia@yahoo.com*//* * SobolVersusMonteCarlo.java * * Created on April 21, 2002, 2:21 PM */package Examples.Pricing;import com.skylit.io.EasyReader;import Statistics.*;import QuasiRandom.*;import Market.*;import Options.*;import Graphics.*;/** <p>A call on a constant volatility asset is valued using Monte Carlo * and Quasi Monte Carlo simulation using the Sobol sequence. * The valuation is carried out as a problem in any user defined dimension * (the number of time steps to the horizon).</p> * * <p>The results are compared to the analytic call price and the probability * that Monte Carlo beats the Sobol sequence is computed from a user defined * number of Monte Carlo runs. The number of simulated paths is user defined. * </p> * * @author java */public class QMCversusMC_2 { public static void main(String[] args) throws java.io.FileNotFoundException, java.io.IOException { int T, nPaths, nStrikes=16, nRuns, nSignChange=5, // irrelevant, but needed for asset constructor ldSequence; // flag double S_0=50, mu=0.0, sigma=0.4, q=0.0, r=0.0, dt, K_min=45, K_max=90, K; // read dimension and number of Monte Carlo runs: EasyReader io=new EasyReader(); System.out.println ("Call price will be computed by QMC and ordinary Monte Carlo\n"+ "The dimension is the number T of time steps to call expiry.\n"+ "The probability that Monte Carlo beats QMC sequence is computed\n"+ "from a number of Monte Carlo runs.\n"); // main loop while(true){ // get user input System.out.print ("Which low discrepancy sequence should be used:\n"+ "Halton ("+ConstantVolatilityAssetQMC.HALTON+ ") or Sobol ("+ConstantVolatilityAssetQMC.SOBOL+")?\n\n"+ "Sequence: "); ldSequence=io.readInt(); String sqName=" "; switch(ldSequence) { case ConstantVolatilityAssetQMC.HALTON: sqName="Halton"; break; case ConstantVolatilityAssetQMC.SOBOL: sqName="Sobol"; } System.out.print("Enter dimension: "); T=io.readInt(); dt=1.0/T; // size of time step System.out.print("\nEnter number of Monte Carlo runs: "); nRuns=io.readInt(); System.out.print("Enter number of paths per run: "); nPaths=io.readInt(); // Q[j] is the probability that Sobol beats Monte Carlo at strike K_j double[] Q=new double[nStrikes]; // e[j] error of Sobol price at strike K_j in percent of true price double[] e=new double[nStrikes]; // the underlying with ordinary random dynamics ConstantVolatilityAsset asset=new ConstantVolatilityAsset (T,dt,nSignChange,S_0,r,q,mu,sigma); // the underlying with quasi random dynamics driven by quasi // normal increments ConstantVolatilityAssetQMC assetQMC=new ConstantVolatilityAssetQMC (T,dt,nSignChange,S_0,r,q,mu,sigma,ldSequence); // loop over the strikes LoopStatus loopStatus=new LoopStatus(); long before=System.currentTimeMillis(); int loops=0; for(int j=0;j<nStrikes;j++) { int N=nStrikes-1; K=K_min+(K_max-K_min)*j/N; // the call as path independent option to ensure // full paths are computed when option price is evaluated. // the call based on QMC asset CallAsPathDependent callQMC=new CallAsPathDependent(K,assetQMC); // the call based on Monte Carlo asset CallAsPathDependent call=new CallAsPathDependent(K,asset); // analytic price double price=call.discountedAnalyticPrice(0); // QMC Monte Carlo price, absolute and relative error double priceQMC=callQMC.discountedMonteCarloPrice(nPaths), abserrQMC=Math.abs(price-priceQMC), relerrQMC=abserrQMC/price; // nRun Monte Carlo runs int mc_wins=0; // number of times MC beats Sobol for(int l=0;l<nRuns;l++) { double priceMC=call.discountedMonteCarloPrice(nPaths), abserrMC=Math.abs(price-priceMC); if(abserrMC<abserrQMC)mc_wins++; //progress report every run loops++; if(loops%100==99) loopStatus.progressReport(loops,nStrikes*nRuns,100,before); } Q[j]=1.0*mc_wins/nRuns; e[j]=relerrQMC; } // end for j double y_axis_min=-e[nStrikes-1]/10; JGraph jGraph=new JGraph(K_min,K_max); jGraph.addSeries(Q,"Probability that MC beats QMC"); jGraph.addSeries(e,"QMC price relative error"); jGraph.setTitle("QMC ("+sqName+") versus MC, dimension "+T); jGraph.setXAxisLabel("Call strike"); jGraph.setYAxisMin(y_axis_min); jGraph.setVisible(true); String filename="QMCversusMC_"+T+".dat"; //jGraph.saveAsEPS(filename); jGraph.saveAsPNG(); jGraph.saveAsASCII(filename); } // end main } // end main } // end SobolVesrusMonteCarlo
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -