Changeset 428
- Timestamp:
- 07/24/09 13:57:16 (16 years ago)
- Location:
- library/tests
- Files:
-
- 1 removed
- 5 modified
Legend:
- Unmodified
- Added
- Removed
-
library/tests/CMakeLists.txt
r426 r428 17 17 18 18 EXEC(fsqmat_test) 19 EXEC(enorm_test)20 19 EXEC(egiw_test) 21 20 EXEC(emix_test) … … 34 33 EXEC(test_particle) 35 34 EXEC(testSmp) 36 EXEC(testEpdf)37 35 EXEC(test_kalman_QR) 38 36 EXEC(test_kalman_QRexh) … … 41 39 42 40 # using UnitTest++ 43 add_executable(testsuite datalink_test.cpp loggers_test.cpp rv_test.cpp square_mat_test.cpp testsuite.cpp test_user_info.cpp test_shared_ptr.cpp)41 add_executable(testsuite datalink_test.cpp enorm_test.cpp loggers_test.cpp rv_test.cpp square_mat_test.cpp testsuite.cpp test_user_info.cpp test_shared_ptr.cpp) 44 42 target_link_libraries(testsuite bdm itpp testutil unittest) 45 43 -
library/tests/enorm_test.cpp
r394 r428 1 #define BDMLIB // not an ideal way to prevent double registration of UI factories... 1 2 #include "stat/exp_family.h" 2 3 #include "stat/emix.h" 4 #include "mat_checks.h" 5 #include "UnitTest++.h" 6 3 7 using namespace bdm; 4 8 5 //These lines are needed for use of cout and endl 6 using std::cout; 7 using std::endl; 9 const double epsilon = 0.00001; 10 11 namespace UnitTest 12 { 13 14 inline void CheckClose(TestResults &results, const itpp::vec &expected, 15 const itpp::vec &actual, double tolerance, 16 TestDetails const& details) { 17 if (!AreClose(expected, actual, tolerance)) { 18 MemoryOutStream stream; 19 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; 20 21 results.OnTestFailure(details, stream.GetText()); 22 } 23 } 24 25 inline void CheckClose(TestResults &results, const itpp::mat &expected, 26 const itpp::mat &actual, double tolerance, 27 TestDetails const& details) { 28 if (!AreClose(expected, actual, tolerance)) { 29 MemoryOutStream stream; 30 stream << "Expected " << expected << " +/- " << tolerance << " but was " << actual; 31 32 results.OnTestFailure(details, stream.GetText()); 33 } 34 } 35 36 } 8 37 9 38 double normcoef ( const epdf* ep,const vec &xb, const vec &yb, int Ngr=100 ) { … … 25 54 } 26 55 27 int main() {28 56 TEST(test_enorm) { 57 RNG_randomize(); 29 58 30 cout << "Testing enorm(2,2)"<<endl; 31 // Setup model 32 vec mu ( "1.1 -1" ); 33 ldmat R ( mat ( "1 -0.5; -0.5 2" ) ); 59 // Setup model 60 vec mu("1.1 -1"); 61 ldmat R(mat("1 -0.5; -0.5 2")); 34 62 35 RV x ( "{x }");36 RV y ( "{y }");63 RV x("{x }"); 64 RV y("{y }"); 37 65 38 enorm<ldmat> E; 39 E.set_rv(concat(x,y)); 40 E.set_parameters ( mu,R ); 41 cout << "enorm mean value:" << E.mean() <<endl; 42 cout << "enorm mean variance:" << R.to_mat() <<endl; 43 cout << "enorm normalizing constant:" << E.lognc() <<endl; 44 cout << " Numerically integrates to [1.0]: " << normcoef ( &E, vec ( "-5 5" ), vec ( "-5 5" ) ) << endl; 66 enorm<ldmat> E; 67 E.set_rv(concat(x,y)); 68 E.set_parameters(mu, R); 69 CHECK_EQUAL(mu, E.mean()); 70 CHECK_CLOSE(2.11768, E.lognc(), epsilon); 71 CHECK_CLOSE(1.0, normcoef(&E, vec("-5 5"), vec("-5 5")), 0.01); 45 72 46 int N=1000;47 vec ll ( N);48 mat Smp=E.sample ( 1000);49 vec Emu = sum ( Smp,2 ) /N;50 mat Er = ( Smp*Smp.transpose() ) /N - outer_product ( Emu,Emu);73 int N = 1000; 74 vec ll(N); 75 mat Smp = E.sample(1000); 76 vec Emu = sum(Smp, 2) / N; 77 CHECK_CLOSE(mu, Emu, 0.3); 51 78 52 cout << "original empirical mean: " <<Emu <<endl;53 cout << "original empirical variance: " <<Er <<endl;79 mat Er = (Smp * Smp.T()) / N - outer_product(Emu, Emu); 80 CHECK_CLOSE(R.to_mat(), Er, 0.3); 54 81 55 epdf* Mg = E.marginal ( y);56 mpdf* Cn = E.condition ( x);82 epdf *Mg = E.marginal(y); 83 CHECK_CLOSE(vec("-1"), Mg->mean(), epsilon); 57 84 85 // putting them back together 86 mpdf *Cn = E.condition(x); 87 mepdf mMg(Mg); 88 Array<mpdf *> A(2); 89 A(0) = Cn; 90 A(1) = &mMg; 91 mprod mEp(A); 92 Smp = mEp.samplecond(vec(0), 1000); 93 Emu = sum(Smp, 2) / N; 94 CHECK_CLOSE(mu, Emu, 0.3); 58 95 59 cout<< "marginal mean: " << Mg->mean() <<endl; 96 Er = (Smp * Smp.T()) / N - outer_product(Emu, Emu); 97 CHECK_CLOSE(R.to_mat(), Er, 0.3); 60 98 61 cout << "========== putting them back together ======= "<<endl; 62 mepdf mMg ( Mg ); 63 Array<mpdf*> A ( 2 ); 64 A ( 0 ) = Cn; 65 A ( 1 ) = &mMg; 66 mprod mEp ( A ); 99 // test of pdflog at zero 100 vec zero(0); 101 vec zero2("0 0"); 102 CHECK_CLOSE(E.evallog(zero2), mEp.evallogcond(zero2, zero), epsilon); 103 } 67 104 68 Smp=mEp.samplecond ( vec ( 0 ),1000 ); 69 Emu = sum ( Smp,2 ) /N; 70 Er = ( Smp*Smp.transpose() ) /N - outer_product ( Emu,Emu ); 105 // from testEpdf 106 TEST(test_enorm_sum) { 107 vec x = "-10:0.1:10"; 108 vec y = "-10:0.1:10"; 71 109 72 cout << "composite mean: " <<Emu <<endl; 73 cout << "composite variance: " <<Er <<endl; 74 75 cout << endl << " test of pdflog at zero "<<endl; 76 cout << "original: " << E.evallog(vec("0 0")) <<endl; 77 cout << "composite: " << mEp.evallogcond(vec("0 0"),vec(0)) << endl; 110 RV rv("{x2 }", "2"); 111 vec mu0 = "0.0 0.0"; 112 mat V0 = "5 -0.05; -0.05 5.20"; 113 fsqmat R(V0); 114 115 enorm<fsqmat> eN; 116 eN.set_rv(rv); 117 eN.set_parameters(mu0, R); 118 119 vec pom(2); 120 double suma = 0.0; 121 for (int i = 0; i < x.length(); i++) { 122 for (int j=0; j<y.length(); j++) { 123 pom(0) = x(i); 124 pom(1) = y(j); 125 suma += exp(eN.evallog(pom)); 126 } 127 } 128 129 CHECK_CLOSE(100, suma, 0.1); 78 130 } -
library/tests/mat_checks.cpp
r426 r428 3 3 namespace UnitTest 4 4 { 5 6 bool AreClose(const itpp::vec &expected, const itpp::vec &actual, 7 double tolerance) { 8 if (expected.length() != actual.length()) { 9 return false; 10 } 11 12 for (int i = 0; i < expected.length(); ++i) { 13 if (!AreClose(expected(i), actual(i), tolerance)) { 14 return false; 15 } 16 } 17 18 return true; 19 } 5 20 6 21 bool AreClose(const itpp::mat &expected, const itpp::mat &actual, double tolerance) { -
library/tests/mat_checks.h
r426 r428 19 19 namespace UnitTest 20 20 { 21 bool AreClose(const itpp::vec &expected, const itpp::vec &actual, 22 double tolerance); 23 21 24 bool AreClose(const itpp::mat &expected, const itpp::mat &actual, 22 25 double tolerance); -
library/tests/square_mat_test.cpp
r427 r428 12 12 { 13 13 14 void CheckClose(TestResults &results, const itpp::mat &expected,15 const itpp::mat &actual, double tolerance,16 TestDetails const& details) {14 inline void CheckClose(TestResults &results, const itpp::mat &expected, 15 const itpp::mat &actual, double tolerance, 16 TestDetails const& details) { 17 17 if (!AreClose(expected, actual, tolerance)) { 18 18 MemoryOutStream stream;