📄 liborprocesstest.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*//* * LiborProcessTest.java * * Created on September 1, 2002, 9:22 AM */package Libor.LiborProcess;import junit.framework.*;import Statistics.*;import LinAlg.*;import ArrayClasses.*;import QuasiRandom.*;import cern.colt.matrix.linalg.CholeskyDecomposition;/** Class of unit tests for the class <code>LiborProcess</code> * in the <code>jUnit</code> testing framework. * * @author Michal J. Meyer */public class LiborProcessTest extends TestCase { // text fixture, not altered by any test, so static final // no setup, tear down. static final int n=15, // dimension of Libor process Npath=100000, // number of paths p=n/4,q=2*n/3; // Libors L_j tested for j=p,...,n-1. static final double precision=0.003; static final LMM_Parameters lmmParams=new LMM_Parameters(n,LMM_Parameters.CS); static final LiborProcess LP=new LiborProcess(lmmParams); static final double[] Tc=LP.tenorStructure(), l=LP.initialTermStructure(), vec=new double[n-p]; // work space // the vector of X^0_j(T_p), j=p,...,n-1, simulated directly static final LiborVector X0=LP.X0LiborVector(p); // the same vetor from path simulation static final RandomVector Xo=new RandomVector(n-p){ public double[] getValue(int t) { LP.newPath(false,true,false); for(int i=p;i<n;i++)vec[i-p]=LP.X0(i,p); return vec; } }; // end Lo // log-Libors Y^0_j(T_j)=log(X^0_j(T_j)), coordinatewise logarithms of // the above random vectors: // log-Libor Y^0_j=log(X^0_j), j=p,...,q-1, at reset times T_j, // simulated directly static final RandomVector logX0=new RandomVector(q-p){ public double[] getValue(int t) { double[] x=X0.getValue(t); for(int i=p;i<q;i++)x[i-p]=Math.log(x[i-p]); return x; } }; // end logL0 // same as the previous random vector but path computed static final RandomVector logXo=new RandomVector(q-p){ public double[] getValue(int t) { double[] x=Xo.getValue(t); for(int i=p;i<q;i++)x[i-p]=Math.log(x[i-p]); return x; } }; // end logLo /******************************************************************************* * * INTIALIZE TEST FIXTURE * * ****************************************************************************/ /** Do nothing on <code>setUp</code> (test fixture is * <code>static final</code> since none of the tests * alters the basic data). */ protected void setUp(){ } /** Do nothing on <code>tearDown</code> since none of the tests * alters the basic data. */ protected void tearDown(){ } /******************************************************************************* * * SOME ADDITIONAL ASSERTIONS * * ****************************************************************************/ /** Entry by entry equality of one dimensional arrays. * * @param u first array. * @param v second array (compared to first). * @param precision maxiumum tolerated entry by entry deviation. * @param message string attached to failure message. */ public void assertEquals (double[] u, double[] v, double precision, String message) { int n=u.length, m=v.length; if(n!=m)Assert.fail(message+", array dimensions not equal\n"); for(int i=0;i<n;i++) assertEquals(message+", entry "+i+"\n",u[i],v[i],precision); } // end assertEquals /** Entry by entry equality of two dimensional (ragged) arrays. * * @param A first array. * @param B second array (compared to first). * @param precision maxiumum tolerated entry by entry deviation. * @param message string attached to failure message. */ public void assertEquals (double[][] A, double[][] B, double precision, String message) { int n=A.length, m=B.length; if(n!=m)Assert.fail(message+", row number not equal\n"); for(int i=0;i<n;i++) { int n_i=A[i].length, m_i=B[i].length; if(n_i!=m_i)Assert.fail(message+", row "+i+" length not equal\n"); for(int j=0;j<n_i;j++)assertEquals (message+", entry("+i+","+j+")\n",A[i][j],B[i][j],precision); } } // end assertEquals /******************************************************************************* * * CONSTRUCTOR, TEST SUITE * * ****************************************************************************/ /** Constructor */ public LiborProcessTest(String testName) { super(testName); } /** 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 liborProcessTestSuite() { return new TestSuite(LiborProcessTest.class); }/******************************************************************************* * * THE TESTS * ******************************************************************************/ /** Tests the mean of the Libor vector <code>X0LiborVector(p)</code> * <code>(X^0_p(T_p),X^0_{p+1}(T_p),....,X^0_{n-1}(T_p))</code> * (both simulated directly and path simulated) against the known analytic * mean vector. */ public void testX0LiborMeans() { System.out.println ("\nTESTING X0-LIBOR VECTOR - MEANS.\n"+ "Warning: tests sample means for equality with analytic means.\n"+ "Test may fail due to insufficient sample size.\n"+ "Sample size: "+Npath+"\n"); // recall we have the Libors L_j for j=p,...,n-1. double[] ameans=new double[n-p]; // analytic means // X^0_i(T_p)=exp(Y^0_i(T_p)) mean (log normal mean formula) for(int i=p;i<n;i++) { double x_i=LP.X(i,0), F=x_i/(1+x_i), sum=0; for(int j=i+1;j<n;j++) sum+=LP.factorLoading.integral_sgi_sgj_rhoij(i,j,0,Tc[p]); sum*=F; ameans[i-p]=x_i*Math.exp(-sum); } // directly simulated Libor means double[] X0means=X0.expectation(Npath); //System.out.println(ArrayUtils.toString(L0means)); assertEquals(ameans,X0means,precision, "\nX0-means, direct simulation"); // path computed driftless Libor means double[] Xomeans=Xo.expectation(Npath); //System.out.println(ArrayUtils.toString(Lomeans)); assertEquals(ameans,Xomeans,precision, "\nX0-means, path computed"); } // end testX0Means /** Tests the covariation matrix of the vector of * <code>Y^0_j(T_p), j=p,...,q-1</code> * (both simulated directly and path simulated) against the known * analytic covariation matrix. */ public void testY0Covariances() { System.out.println ("\nTESTING Y0=log(X0)-LIBOR VECTOR - COVARIANCE MATRIX.\n"+ "Warning: tests sample covariances for equality.\n"+ "Test may fail due to insufficient sample size.\n"+ "Sample size: "+Npath+"\n"); // analytic covariance matrix of L0, Lo: double[][] acvm=new double[q-p][q-p]; for(int i=p;i<q;i++) for(int j=i;j<q;j++){ double cv_ij=LP.factorLoading.integral_sgi_sgj_rhoij(i,j,0,Tc[p]); acvm[i-p][j-p]=cv_ij; acvm[j-p][i-p]=cv_ij; } // sample covariance matrix L0, directly simulated driftless Libor double[][] cvmlogX0=logX0.covarianceMatrix(Npath); assertEquals(acvm,cvmlogX0,precision, "\nDriftless Libor covariance matrix, direct simulation"); // sample covariance matrix Lo, path computed driftless Libor double[][] cvmlogLo=logXo.covarianceMatrix(Npath); assertEquals(acvm,cvmlogLo,precision, "\nDriftless Libor covariance matrix, path computation"); } // end testY0Covariances /** Tests if the general zero coupon bond <code>B(t,T)</code> agrees * with the special bonds <code>B(i,j)=B(T_i,T_j)</code> in case * <code>t=T_i, T=T_j</code>. Test for various <code>i,j</code> along * one path of Libor. */ public void testZeroCouponBonds() { System.out.println ("\nTESTING DRIFTLESS ZERO COUPON BONDS."); double[] Tc=LP.tenorStructure(); LP.newPath(); for(int i=2;i<14;i++) for(int j=2;j<=i;j++){ double t=Tc[j], T=Tc[i]; assertEquals("\nbonds B("+i+","+j+"):", LP.B(i,j),LP.B(t,T),0.000001); } } // end testZeroCouponBonds /** Tests the swap rates and annuities by comparing the streamlined * implementations against the straightforward ones over a sample * of ten paths. */ public void testSwapRates() { System.out.println ("\nTESTING SWAP RATES AND ANNUITY NUMERAIRE"); int t=2; // must be <= n/3-1 for(int paths=0;paths<10;paths++){ LP.newPath(); for(int q=n/3;q<n;q++) for(int p=n/3-1;p<q-1;p++){ assertEquals("\nSwap rate("+p+","+q+"):", LP.swRate(p,q,t),LP.swapRate(p,q,t),0.0000001); assertEquals("\nAnnuity("+p+","+q+"):", LP.b_pq(p,q,t),LP.B_pq(p,q,t),0.0000001); } } // end for paths } // end testSwapRates /******************************************************************************* * * THE TEST RUN * ******************************************************************************/ /** Run the tests in a text UI. */ public static void main(String[] args) { // run tests in text user interface junit.textui.TestRunner.run(liborProcessTestSuite()); }} // end LiborProcessTest// junit.swingui.TestRunner.run() dos not work like this.// no documentation included.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -