Changeset 500

Show
Ignore:
Timestamp:
08/11/09 11:27:10 (15 years ago)
Author:
vbarta
Message:

moved emix_test to testsuite

Location:
library/tests
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • library/tests/CMakeLists.txt

    r499 r500  
    2020LINK_EXEC(square_mat_prep) 
    2121 
    22 EXEC(emix_test) 
    2322EXEC(testResample) 
    2423 
     
    4039 
    4140# using UnitTest++ 
    42 add_executable(testsuite datalink_test.cpp egiw_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) 
     41add_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) 
    4342target_link_libraries(testsuite testutil unittest) 
    4443LINK_EXEC(testsuite) 
  • library/tests/emix_test.cpp

    r477 r500  
    22#include "stat/exp_family.h" 
    33#include "stat/emix.h" 
     4#include "mat_checks.h" 
     5#include "UnitTest++.h" 
     6#include "test_util.h" 
     7 
     8const double epsilon = 0.00001; 
    49 
    510using namespace bdm; 
     
    914using std::endl; 
    1015 
    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  
     16TEST ( test_emix_old ) { 
    3317        RV x ( "{x }" ); 
    3418        RV y ( "{y }" ); 
     
    4933        M1.set_rv ( xy ); 
    5034        M1.set_parameters ( vec ( "1" ), A1, false ); 
    51         cout << "======== test if ARX and emix with one ARX are the same ===" << endl; 
    5235 
     36        // test if ARX and emix with one ARX are the same 
    5337        epdf* Mm = M1.marginal ( y ); 
    5438        epdf* Am = E1.marginal ( y ); 
     
    5640        mpdf* Ac = E1.condition ( y ); 
    5741 
    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        } 
    5949 
    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 ); 
    6455 
    65         cout << "======== Mixture with two components ===" << endl; 
     56        // mixture with two components 
    6657        Array<epdf*> A2 ( 2 ); 
    6758        A2 ( 0 ) = &E1; 
     
    7364 
    7465 
    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 ); 
    7668 
    7769        int N = 3; 
    78         vec ll ( N ); 
    7970        vec ll2 ( N ); 
    8071        mat Smp = M2.sample_m ( N ); 
    81         ll = M2.evallog_m ( Smp ); 
     72        vec ll = M2.evallog_m ( Smp ); 
    8273 
    8374        vec Emu = sum ( Smp, 2 ) / N; 
     75        CHECK_CLOSE ( vec ( "1.00054 1.0455" ), Emu, 1.0 ); 
     76 
    8477        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 ); 
    8579 
    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 ); 
    8885 
    89         shared_ptr<epdf> Mg ( dynamic_cast<epdf *> ( M2.marginal ( y ) ) ); 
    90         mpdf* Cn = ( mpdf* ) M2.condition ( x ); 
    91  
     86#if false 
    9287        it_file it ( "emix_test.it" ); 
    9388        it << Name ( "Smp" ) << Smp; 
     89#endif 
    9490 
    95         cout << "marginal mean: " << Mg->mean() << endl; 
     91        // marginal mean 
     92        CHECK_CLOSE ( vec ( "1.0" ), Mg->mean(), 0.1 ); 
    9693 
    97         cout << "========== putting them back together ======= " << endl; 
     94#if false 
     95        // putting them back together 
    9896        mepdf mMg ( Mg ); 
    9997        Array<mpdf*> AA ( 2 ); 
     
    107105        it << Name ( "ll" ) << ll; 
    108106        it << Name ( "ll2" ) << ll2; 
    109  
    110  
     107#endif 
    111108}