📄 caplettest.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/* * CapletTest.java * * Created on September 18, 2002, 4:32 PM */package Libor.LiborDerivatives;import Libor.LiborProcess.*;import junit.framework.*;import Statistics.FinMath;/** <p>A <code>jUnit</code> test suite for the class <code>Caplet</code>. * No <code>jUnit</code> assertions used, instead results printed for * inspection.</p> * * </p>Allocates a Libor process of dimension 16 and then calculates the prices * of all caplets (strike 0.05, Libor L_j(0)=0.04) from samples of 20,000 * paths. Analytic and Monte Carlo prices with and without control variates * are printed and compared.</p> * * <p>Results indicate that the necessary forward transporting from very early * dates adds much variability. Performance of the control variate is * unconvincing despite high corrleation.</p> * * @author Michael J. Meyer */public class CapletTest extends TestCase { // See classes CS_FactorLoding and Liborprocess // The test fixture is static final since none of the tests changes // anything. Consequently setUp() and tearDown() can be empty. static final int n=16; static final double delta=0.25, A=1.8, D=2, alpha=1.8, beta=0.1, rho00=0.4, kappa=0.05; static final double[] c=new double[n], Tc=new double[n+1], l=new double[n]; CS_FactorLoading factorLoading; LiborProcess LP; /******************************************************************************* * * INTIALIZE TEST FIXTURE * * ****************************************************************************/ /** Do nothing on <code>setUp</code> since the test fixture is static final. */ protected void setUp(){ } /** Do nothing on <code>tearDown</code> since none of the tests * alters the basic data. */ protected void tearDown(){ } /******************************************************************************* * * CONSTRUCTOR, TEST SUITE * * ****************************************************************************/ /** Constructor. */ public CapletTest(String testName) { super(testName); // tenor structure and initial Libors for(int i=0;i<n+1;i++)Tc[i]=i*delta; // T_i for(int i=0;i<n;i++){ l[i]=0.04; // L_i(0) c[i]=0.2*(1+((double)i)/n); } // c_i // factor loading factorLoading=new CS_FactorLoading(n,A,D,alpha,beta,rho00,c,Tc); LP=new LiborProcess(l,factorLoading); } // end constructor /** Returns the test suite object which is then <code>run</code> * in one of the test suite runners juint.textui.TestRunner or * junit.swingui.TestRunner. */ public static Test capletTestSuite() { return new TestSuite(CapletTest.class); }/******************************************************************************* * * THE TESTS * ******************************************************************************/ /** Test the computation of caplet implied vols. */ public void testCapletForwardPrice() { int nPath=20000; System.out.println ("\nTESTING CAPLET FORWARD PRICES:\n" +(n-1)+" caplets, "+nPath+" paths.\n"); for(int i=1;i<n;i++){ double vol, cvcorr, aprice, mcprice, cvmcprice, mcerror, cvmcerror; Caplet cplt_i=new Caplet(LP,i,kappa); //Libor volatility vol=LP.vol(i); // prices cvmcprice=cplt_i.controlledMonteCarloForwardPrice(0,nPath); aprice=cplt_i.analyticForwardPrice(0); mcprice=cplt_i.monteCarloForwardPrice(0,nPath); // control variate correlation cvcorr=cplt_i.controlledForwardPayoff(). correlationWithControlVariate(0,nPath/10); //errors in percent of true price mcerror=(aprice-mcprice)/aprice; cvmcerror=(aprice-cvmcprice)/aprice; // error in %, rounded to 4 decimals mcerror=100*FinMath.round(mcerror,4); cvmcerror=100*FinMath.round(cvmcerror,4); // more rounding cvcorr=FinMath.round(cvcorr,4); vol=FinMath.round(vol,3); aprice=FinMath.round(aprice,8); System.out.println("caplet "+i+": "); System.out.println("Libor volatility: "+vol); System.out.println("Control variate correlation: "+cvcorr); System.out.println("analytic price: "+aprice); System.out.println ("Monte Carlo price, error: "+mcerror+"%"); System.out.println ("Controlled Monte Carlo price, error: "+cvmcerror+"%"); //assertEquals(CLBR.capletSigma(i),cImpliedSigma,0.001); System.out.println("OK.\n"); } } // end testCapletImpliedSigma /******************************************************************************* * * THE TEST RUN * ******************************************************************************/ /** run the tests in a Swing GUI */ public static void main(String[] args) { // run tests in text user interface junit.textui.TestRunner.run(capletTestSuite()); }} // end CapletTest// junit.swingui.TestRunner.run() dos not work like this.// no documentation included.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -