📄 cs_factorloadingtest.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*//* * CS_FactorLoadingTestSuite.java * * Created on August 26, 2002, 7:36 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>CS_FactorLoading</code> * in the <code>jUnit</code> testing framework. * * @author Michal J. Meyer */public class CS_FactorLoadingTest extends TestCase { // see class CS_FactorLoading int n; private double delta,A,D,alpha,beta,rho00; private double[] c,Tc; CS_FactorLoading factorLoading; /******************************************************************************* * * INTIALIZE TEST FIXTURE * * ****************************************************************************/ /** Set up the test fixture. */ protected void setUp() { n=40; delta=0.25; A=3; D=2; alpha=1.8; beta=0.1; rho00=0.4; c=new double[n]; Tc=new double[n]; for(int i=0;i<n;i++){ c[i]=Math.max(1.0+0.1*Random.STN(),1.0); Tc[i]=i*delta; } factorLoading=new CS_FactorLoading(n,A,D,alpha,beta,rho00,c,Tc); } // end setup /** Do nothing on <code>tearDown</code> since none of the tests * alters the basic data. */ protected void tearDown(){ } /******************************************************************************* * * CONSTRUCTOR, TEST SUITE * * ****************************************************************************/ /** Constructor. */ public CS_FactorLoadingTest(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 factorLoadingTestSuite() { return new TestSuite(CS_FactorLoadingTest.class); }/******************************************************************************* * * THE TESTS * ******************************************************************************/ /** Test if the correlation matrix is symmetric and positive definite */ public void testCorrelationMatrix() { ColtMatrix correlationMatrix=factorLoading.correlationMatrix(); CholeskyDecomposition cd=correlationMatrix.choleskyDecomposition(); //System.out.println("\n\nTHE CORRElATION MATRIX:\n"); //System.out.print(correlationMatrix.toString()); System.out.println("\nTESTING THE CORRELATION MATRIX:"); assertTrue(cd.isSymmetricPositiveDefinite()); assertEquals(correlationMatrix.getQuick(0,n-2),rho00,0.001); System.out.println("Correlation matrix is OK."); } // end testCorrelationMatrix /** Test the analytic covariation integrals against QMC numerical values. */ public void testCovariationIntegrals() { int N=100000, // number of Sobol points j0=n/3; // lower bound for Libor indices j // integration interval, must be contained in [0,T_j0]. double a=Tc[j0/3], b=Tc[j0], d=b-a; Sobol S=new Sobol(1); System.out.println("\n\nTESTING LOG-COVARIATION INTEGRALS:\n"); for(int i=j0;i<n/2;i++) for(int j=j0;j<=i;j++){ double qmcint, // quasi monte carlo integral aint; // analytic integral aint=factorLoading.integral_sgi_sgj_rhoij(i,j,a,b); double sum=0; for(int k=0;k<N;k++){ double t=a+d*S.nextPoint()[0], ft; // function value f(t) ft=factorLoading.sigma(i,t)* factorLoading.sigma(j,t)* factorLoading.rho(i,j); sum+=ft; } qmcint=d*sum/N; //System.out.println("\n\n\nana-int="+aint+"\n"+ // "qmc-int="+qmcint); String message="covariation integral("+i+","+j+")"; assertEquals(message,aint,qmcint,0.01); System.out.println(message+" IS OK."); } // end for ij } // end testCovariationIntegrals /** Tests if the * <a href=FactorLoading.java#log-covariation-matrix> * log-covariation-matrix</a> is symmetric and positive definite. */ public void testLogCovariationMatrices() { System.out.println("\n\nTESTING LOG-COVARIATION MATRICES:\n"); for(int t=0;t<n-1;t++){ ColtMatrix logcvm=factorLoading.logCovariationMatrix(t); CholeskyDecomposition cd=logcvm.choleskyDecomposition(); //System.out.println("\n\nTHE LOG-COVARIATION MATRIX:\n"); //System.out.print(logcvm.toString()); String message="log-covariation-matrix("+t+")"; assertTrue(message,cd.isSymmetricPositiveDefinite()); System.out.println(message+" is OK."); } // end for p } // end testLogCovariationMatrices /** <p>Sets up the array <code>CV[t]</code> of log-covariation-matrices. * Checks the matrix <code>CV[t]</code> for <code>t=n/2</code>.</p> * * <p>Note: this test takes about 25 seconds (n=40). Setting up * the covariation matrix array is the culprit.</p> */ public void testCovariationMatrixSequence() { System.out.println ("\n\nTESTING ARRAY OF LOG-COVARIATION MATRICES:\n"); for(int t=n/4; t<2*n/3; t++){ System.out.print("matrix["+t+"]: "); // covariation matrix(t), two versions, // compare only the upper triangular half double[][] cv_t=factorLoading.getCovariationMatrixArray().getMatrices()[t]; ColtMatrix CV_t=factorLoading.logCovariationMatrix(t); for(int i=t+1;i<n;i++) for(int j=i;j<n;j++) { String message="Covariation matrix sequence test, \n"+ "matrix["+t+"]("+i+","+j+")\n"; assertEquals(message, cv_t[i-t-1][j-i], CV_t.getQuick(i-t-1,j-t-1),0.000001); } System.out.print("is OK\n"); } // end for t } // end testCovariationMatrixSequence /** <p>Sets up the array <code>L[t]</code> of Cholesky roots of the * log-covariation-matrices. * Checks the matrix <code>L[t]</code> for <code>t=n/2</code>.</p> * * <p>Note: this test takes about 30 seconds (n=40). Setting up * the Cholesky root matrix array is the culprit.</p> */ public void testCholeskyRootMatrixSequence() { System.out.println ("\n\nTESTING ARRAY OF CHOLESKY ROOTS:\n"); for(int t=n/3; t<n/2; t++){ System.out.print("matrix["+t+"]: "); // Cholesky root matrix(t), two versions, // compare only the lower triangular half double[][] cr_t=factorLoading.getCholeskyRootArray().getMatrices()[t]; ColtMatrix CR_t=factorLoading.logCovariationCholeskyRoot(t); for(int i=t+1;i<n;i++) for(int j=t+1;j<=i;j++) { String message="Cholesky root sequence test, \n"+ "matrix["+t+"]("+i+","+j+")\n"; assertEquals(message, cr_t[i-t-1][j-t-1], CR_t.getQuick(i-t-1,j-t-1),0.000001); } System.out.print("is OK\n"); } // end for t } // end testCholeskyRootMatrixSequence /******************************************************************************* * * 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(factorLoadingTestSuite()); }} // end CS_FactorLoadingTest// junit.swingui.TestRunner.run() dos not work like this.// no documentation included.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -