📄 qmcversusmc_1.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*//* * QMCversusMC_1.java * * Created on May 2, 2002, 10:30 AM */package Examples.Pricing;import com.skylit.io.EasyReader;import Statistics.*;import jas.hist.*;import QuasiRandom.*;import Market.*;import Options.*;import Graphics.Frame;/** <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 relative * error of the following methods compared:</p> * *<ul> * <li>Monte Carlo simulation (Meresenne twister)</li> * <li>Quasi Monte Carlo, Sobol sequence.</li> * <li>Quasi Monte Carlo, Halton sequence.</li> *</ul> * *<p>In each case uniform vectors are converted to normal ones using * the inverse normal CDF on each coordinate. The relative error of the Monte * Carlo price is reported as a histogram over a user defined number of Monte * Carlo runs. Quasi Monte Carlo prices are deterministic, so only the * value of the relative error is reported.</p> * *<p>The user chooses the number of simulated paths for each Monte Carlo or * Quasi Monte Carlo computation. * </p> * * @author Michael J. Meyer */public class QMCversusMC_1 { public static void main(String[] args) throws java.io.FileNotFoundException, java.io.IOException { int T, nPaths, // number of paths per run nRuns, // number of Monte Carlo runs nSignChange=5, // irrelevant, but needed for asset constructor nBins=100; // number of bins for histogram of MC price errors double S_0=50, mu=0.0, sigma=0.4, q=0.0, r=0.0, dt, 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 QMC relative error (Halton and Sobol) and histogram of Monte Carlo"+ "\nrelative errors (user chooses number of runs) are computed.\n"+ "S(0)="+S_0+".\n\n"); // main loop while(true){ System.out.print("Enter call strike: "); K=io.readDouble(); System.out.print("Enter dimension: "); T=io.readInt(); dt=1.0/T; // size of time step System.out.print("Enter number of paths per simulation: "); nPaths=io.readInt(); System.out.print("Enter number of Monte Carlo runs: "); nRuns=io.readInt(); // ordinary Monte Carlo dynamics // the underlying ConstantVolatilityAsset asset=new ConstantVolatilityAsset (T,dt,nSignChange,S_0,r,q,mu,sigma); // the call as path independent option to ensure // full paths are computed when option price is evaluated. final CallAsPathDependent call=new CallAsPathDependent(K,asset); // analytic call price final double price=call.discountedAnalyticPrice(0); // QMC dynamics driven by the Sobol sequence // the underlying ConstantVolatilityAssetQMC assetSOBOL=new ConstantVolatilityAssetQMC (T,dt,nSignChange,S_0,r,q,mu,sigma,ConstantVolatilityAssetQMC.SOBOL); // the call CallAsPathDependent callSOBOL=new CallAsPathDependent(K,assetSOBOL); // call Sobol price double priceSOBOL=callSOBOL.discountedMonteCarloPrice(0,nPaths); // QMC dynamics driven by the Sobol sequence // the underlying ConstantVolatilityAssetQMC assetHALTON=new ConstantVolatilityAssetQMC (T,dt,nSignChange,S_0,r,q,mu,sigma,ConstantVolatilityAssetQMC.HALTON); // the call CallAsPathDependent callHALTON=new CallAsPathDependent(K,assetHALTON); // call Halton price double priceHALTON=callHALTON.discountedMonteCarloPrice(0,nPaths); // the relative errors // make final so can be accessed by inner class final int N=nPaths; // Monte Carlo relative error as a random variable RandomVariable errorMC=new RandomVariable(){ public double getValue(int t) { double priceMC=call.discountedMonteCarloPrice(0,N), relErrorMC=(price-priceMC)/price; return 100*relErrorMC; } }; // end errorMC // histogram of Monte Carlo price relative errors JASHist histMC=errorMC.histogram (nRuns,nBins,true,"MC","price relative error (%)"); histMC.setTitle("Call strike="+K+", dim="+T); // Halton price relative error double relErrorHALTON=100*(price-priceHALTON)/price; // Sobol price and relative error double relErrorSOBOL=100*(price-priceSOBOL)/price; // add the Sobol and Halton price to the histogram as one point // data sources PointDataSource haltonBar=new PointDataSource ("Halton",relErrorHALTON,0.05,0.2,java.awt.Color.red); histMC.addData(haltonBar).show(true); PointDataSource sobolBar=new PointDataSource ("Sobol",relErrorSOBOL,0.05,0.2,java.awt.Color.green); histMC.addData(sobolBar).show(true); // add it to a JFrame and display it Graphics.Frame window=new Graphics.Frame("Histogram"); window.setContentPane(histMC); window.setBounds(50,120,600,400); window.setVisible(true); // save it String filename="Pictures/Histograms/QMCversusMC/HistDim"+T+".eps"; window.saveAsEPS(filename); System.out.println("\n\nContinue (1/0)"); int cnt=io.readInt(); if(cnt==0)break; } // end main loop } // end main } // end QMCversusMC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -