Changeset 500
- Timestamp:
- 08/11/09 11:27:10 (15 years ago)
- Location:
- library/tests
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
library/tests/CMakeLists.txt
r499 r500 20 20 LINK_EXEC(square_mat_prep) 21 21 22 EXEC(emix_test)23 22 EXEC(testResample) 24 23 … … 40 39 41 40 # using UnitTest++ 42 add_executable(testsuite datalink_test.cpp egiw_test.cpp e pdf_test.cpp loggers_test.cpp mpdf_test.cpp rv_test.cpp shared_ptr_test.cpp square_mat_test.cpp testsuite.cpp user_info_test.cpp)41 add_executable(testsuite datalink_test.cpp egiw_test.cpp emix_test.cpp epdf_test.cpp loggers_test.cpp mpdf_test.cpp rv_test.cpp shared_ptr_test.cpp square_mat_test.cpp testsuite.cpp user_info_test.cpp) 43 42 target_link_libraries(testsuite testutil unittest) 44 43 LINK_EXEC(testsuite) -
library/tests/emix_test.cpp
r477 r500 2 2 #include "stat/exp_family.h" 3 3 #include "stat/emix.h" 4 #include "mat_checks.h" 5 #include "UnitTest++.h" 6 #include "test_util.h" 7 8 const double epsilon = 0.00001; 4 9 5 10 using namespace bdm; … … 9 14 using std::endl; 10 15 11 double normcoef ( const epdf* ep, const vec &xb, const vec &yb, int Ngr = 100 ) { 12 mat PPdf ( Ngr + 1, Ngr + 1 ); 13 vec rgr ( 2 ); 14 15 int i = 0, j = 0; 16 double xstep = ( xb ( 1 ) - xb ( 0 ) ) / Ngr; 17 double ystep = ( yb ( 1 ) - yb ( 0 ) ) / Ngr; 18 19 for ( double x = xb ( 0 ); x <= xb ( 1 ); x += xstep, i++ ) { 20 rgr ( 0 ) = x; 21 j = 0; 22 for ( double y = yb ( 0 ); y <= yb ( 1 ); y += ystep, j++ ) { 23 rgr ( 1 ) = y; 24 PPdf ( i, j ) = exp ( ep->evallog ( rgr ) ); 25 } 26 } 27 return sumsum ( PPdf ) *xstep*ystep; 28 } 29 30 int main() { 31 RNG_randomize(); 32 16 TEST ( test_emix_old ) { 33 17 RV x ( "{x }" ); 34 18 RV y ( "{y }" ); … … 49 33 M1.set_rv ( xy ); 50 34 M1.set_parameters ( vec ( "1" ), A1, false ); 51 cout << "======== test if ARX and emix with one ARX are the same ===" << endl;52 35 36 // test if ARX and emix with one ARX are the same 53 37 epdf* Mm = M1.marginal ( y ); 54 38 epdf* Am = E1.marginal ( y ); … … 56 40 mpdf* Ac = E1.condition ( y ); 57 41 58 cout << * ( ( mlnorm<ldmat>* ) Ac ) << endl; 42 mlnorm<ldmat> *wacnd = dynamic_cast<mlnorm<ldmat> *>(Ac); 43 CHECK(wacnd); 44 if ( wacnd ) { 45 CHECK_CLOSE ( mat ( "-0.349953" ), wacnd->_A(), epsilon ); 46 CHECK_CLOSE ( vec ( "1.39564" ), wacnd->_mu_const(), epsilon ); 47 CHECK_CLOSE ( mat ( "0.939557" ), wacnd->_R(), epsilon ); 48 } 59 49 60 cout << "Mix marg at 0: " << Mm->evallog ( vec_1 ( 0.0 ) ) << endl; 61 cout << "ARX marg at 0: " << Am->evallog ( vec_1 ( 0.0 ) ) << endl; 62 cout << "Mix cond at 0,0: " << Mc->evallogcond ( vec_1 ( 0.0 ), vec_1 ( 0.0 ) ) << endl; 63 cout << "ARX cond at 0,0: " << Ac->evallogcond ( vec_1 ( 0.0 ), vec_1 ( 0.0 ) ) << endl; 50 double same = -1.46433; 51 CHECK_CLOSE ( same, Mm->evallog ( vec_1 ( 0.0 ) ), epsilon ); 52 CHECK_CLOSE ( same, Am->evallog ( vec_1 ( 0.0 ) ), epsilon ); 53 CHECK_CLOSE ( 0.145974, Mc->evallogcond ( vec_1 ( 0.0 ), vec_1 ( 0.0 ) ), epsilon ); 54 CHECK_CLOSE ( -1.92433, Ac->evallogcond ( vec_1 ( 0.0 ), vec_1 ( 0.0 ) ), epsilon ); 64 55 65 cout << "======== Mixture with two components ===" << endl;56 // mixture with two components 66 57 Array<epdf*> A2 ( 2 ); 67 58 A2 ( 0 ) = &E1; … … 73 64 74 65 75 cout << "Mixture normalization: " << normcoef ( &M2, vec ( "-3 3 " ), vec ( "-3 3 " ) ) << endl; 66 // mixture normalization 67 CHECK_CLOSE ( 1.0, normcoef ( &M2, vec ( "-3 3 " ), vec ( "-3 3 " ) ), 0.1 ); 76 68 77 69 int N = 3; 78 vec ll ( N );79 70 vec ll2 ( N ); 80 71 mat Smp = M2.sample_m ( N ); 81 ll = M2.evallog_m ( Smp );72 vec ll = M2.evallog_m ( Smp ); 82 73 83 74 vec Emu = sum ( Smp, 2 ) / N; 75 CHECK_CLOSE ( vec ( "1.00054 1.0455" ), Emu, 1.0 ); 76 84 77 mat Er = ( Smp * Smp.transpose() ) / N - outer_product ( Emu, Emu ); 78 CHECK_CLOSE ( mat ( "0.740142 -0.259015; -0.259015 1.0302" ), Er, 2.0 ); 85 79 86 cout << "original empirical mean: " << Emu << endl; 87 cout << "original empirical variance: " << Er << endl; 80 epdf *nm2mrg = M2.marginal ( y ); 81 CHECK ( nm2mrg ); 82 shared_ptr<epdf> Mg ( nm2mrg ); 83 mpdf *Cn = M2.condition ( x ); 84 CHECK ( Cn ); 88 85 89 shared_ptr<epdf> Mg ( dynamic_cast<epdf *> ( M2.marginal ( y ) ) ); 90 mpdf* Cn = ( mpdf* ) M2.condition ( x ); 91 86 #if false 92 87 it_file it ( "emix_test.it" ); 93 88 it << Name ( "Smp" ) << Smp; 89 #endif 94 90 95 cout << "marginal mean: " << Mg->mean() << endl; 91 // marginal mean 92 CHECK_CLOSE ( vec ( "1.0" ), Mg->mean(), 0.1 ); 96 93 97 cout << "========== putting them back together ======= " << endl; 94 #if false 95 // putting them back together 98 96 mepdf mMg ( Mg ); 99 97 Array<mpdf*> AA ( 2 ); … … 107 105 it << Name ( "ll" ) << ll; 108 106 it << Name ( "ll2" ) << ll2; 109 110 107 #endif 111 108 }