📄 calibratortest.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/* * CalibratorTest.java * * Created on September 18, 2002, 4:32 PM */package Libor.LiborProcess;import Libor.LiborDerivatives.*;import junit.framework.*;import Exceptions.*;/** <p>A <code>jUnit</code> test suite for the class <code>Calibrator</code>. * Sets up a Libor process of dimension 15 and then computes caplet implied * volatilities from both analytic prices and Monte Carlo prices and checks * if they are equal to the known Libor volatilities. This is done for every * caplet.<p> * * @author Michael J. Meyer */public class CalibratorTest extends TestCase { // See classes CS_FactorLoding and Calibrator // The test fixture is static final since none of the tests changes // anything. Consequently setUp() and tearDown() can be empty. static final int n=15; static final double delta=0.25, A=1.8, D=2, alpha=1.8, beta=0.1, rho00=0.4; static final double[] c=new double[n], Tc=new double[n+1], l=new double[n]; CS_FactorLoading factorLoading; LiborProcess LP; Calibrator CLBR; /******************************************************************************* * * 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 CalibratorTest(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); CLBR=new Calibrator(n,l,c,Tc); CLBR.setFactorLoading(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 calibratorTestSuite() { return new TestSuite(CalibratorTest.class); }/******************************************************************************* * * THE TESTS * ******************************************************************************/ /** Test the caplet implied volatilities computed from analytic * caplet prices. */ public void testCapletImpliedSigma() throws NoSolutionException { System.out.println ("\nTESTING CAPLET IMPLIED VOLATILITY FROM ANALYTIC PRICES:\n"); for(int i=1;i<n;i++){ double kappa,c,cImpliedSigma; kappa=0.05; c=CLBR.capletPrice(i,kappa); cImpliedSigma=CLBR.capletImpliedSigma(i,kappa,c); System.out.print("caplet "+i+": "); assertEquals(CLBR.capletSigma(i),cImpliedSigma,0.001); System.out.print("OK.\n"); } } // end testCapletImpliedSigma /** Test the caplet implied volatilities computed from Monte Carlo * caplet prices. Warning, this can fail due to imprecision in the Monte * Carlo price stemming from insufficient sample size. Required accuracy * set at +-5%. */ public void testMonteCarloCapletImpliedSigma() throws NoSolutionException { System.out.println ("\nTESTING CAPLET IMPLIED VOLATILITY FROM MONTE CARLO PRICES:\n"); for(int i=1;i<n;i++){ double kappa=0.04,c,c0,c1,cImpliedSigma; int nPath=20000; Caplet cplt_i=new Caplet(LP,i,kappa); c=LP.B0(n)*cplt_i.controlledMonteCarloForwardPrice(0,nPath); cImpliedSigma=CLBR.capletImpliedSigma(i,kappa,c); System.out.print("caplet "+i+": "); assertEquals(CLBR.capletSigma(i),cImpliedSigma,0.05); System.out.print("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(calibratorTestSuite()); }} // end CalibratorTest// junit.swingui.TestRunner.run() dos not work like this.// no documentation included.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -