Changeset 520 for library

Show
Ignore:
Timestamp:
08/13/09 10:16:25 (15 years ago)
Author:
vbarta
Message:

repeating emix mean & covariance tests

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/tests/emix_test.cpp

    r504 r520  
    1010using namespace bdm; 
    1111 
     12static void check_mean ( emix &distrib_obj, int nsamples, const vec &mean, double tolerance ); 
     13 
     14static void check_covariance ( emix &distrib_obj, int nsamples, const mat &R, double tolerance); 
     15 
    1216TEST ( test_emix_old ) { 
    1317        RV x ( "{x }" ); 
    1418        RV y ( "{y }" ); 
    1519        RV xy = concat ( x, y ); 
     20        vec mu0 ( "1.00054 1.0455" ); 
    1621 
    1722        shared_ptr<enorm<ldmat> > E1 = new enorm<ldmat>(); 
    1823        E1->set_rv ( xy ); 
    19         E1->set_parameters ( "1.00054 1.0455" , mat ( "0.740142 -0.259015; -0.259015 1.0302" ) ); 
     24        E1->set_parameters ( mu0 , mat ( "0.740142 -0.259015; -0.259015 1.0302" ) ); 
    2025 
    2126        shared_ptr<enorm<ldmat> > E2 = new enorm<ldmat>(); 
     
    6469 
    6570        int N = 3; 
    66         vec ll2 ( N ); 
    6771        mat Smp = M2.sample_m ( N ); 
     72 
     73        vec exp_ll ( "-5.0 -2.53563 -2.62171" ); 
    6874        vec ll = M2.evallog_m ( Smp ); 
     75        CHECK_CLOSE ( exp_ll, ll, 5.0 ); 
    6976 
    70         vec Emu = sum ( Smp, 2 ) / N; 
    71         CHECK_CLOSE ( vec ( "1.00054 1.0455" ), Emu, 1.0 ); 
     77        check_mean ( M2, N, mu0, 1.0 ); 
    7278 
    73         mat Er = ( Smp * Smp.transpose() ) / N - outer_product ( Emu, Emu ); 
    74         CHECK_CLOSE ( mat ( "0.740142 -0.259015; -0.259015 1.0302" ), Er, 2.0 ); 
     79        mat observedR ( "0.740142 -0.259015; -0.259015 1.0302" ); 
     80        check_covariance ( M2, N, observedR, 2.0); 
    7581 
    7682        shared_ptr<epdf> Mg = M2.marginal ( y ); 
     
    8288        CHECK_CLOSE ( vec ( "1.0" ), Mg->mean(), 0.1 ); 
    8389} 
     90 
     91static void check_mean ( emix &distrib_obj, int nsamples, const vec &mean, double tolerance ) { 
     92        int tc = 0; 
     93        Array<vec> actual(CurrentContext::max_trial_count); 
     94        do { 
     95                mat smp = distrib_obj.sample_m ( nsamples ); 
     96                vec emu = sum ( smp, 2 ) / nsamples; 
     97                actual( tc ) = emu; 
     98                ++tc; 
     99        } while ( ( tc < CurrentContext::max_trial_count ) && 
     100                  !UnitTest::AreClose ( mean, actual( tc - 1 ), tolerance ) ); 
     101        if ( ( tc == CurrentContext::max_trial_count ) && 
     102             ( !UnitTest::AreClose ( mean, actual( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) { 
     103                UnitTest::MemoryOutStream stream; 
     104                UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), __LINE__); 
     105                stream << "Expected " << mean << " +/- " << tolerance << " but was " << actual; 
     106 
     107                UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); 
     108        } 
     109} 
     110 
     111static void check_covariance ( emix &distrib_obj, int nsamples, const mat &R, double tolerance) { 
     112        int tc = 0; 
     113        Array<mat> actual(CurrentContext::max_trial_count); 
     114        do { 
     115                mat smp = distrib_obj.sample_m ( nsamples ); 
     116                vec emu = sum ( smp, 2 ) / nsamples; 
     117                mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu ); 
     118                actual( tc ) = er; 
     119                ++tc; 
     120        } while ( ( tc < CurrentContext::max_trial_count ) && 
     121                  !UnitTest::AreClose ( R, actual( tc - 1 ), tolerance ) ); 
     122        if ( ( tc == CurrentContext::max_trial_count ) && 
     123             ( !UnitTest::AreClose ( R, actual( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) { 
     124                UnitTest::MemoryOutStream stream; 
     125                UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), __LINE__); 
     126                stream << "Expected " << R << " +/- " << tolerance << " but was " << actual; 
     127 
     128                UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); 
     129       } 
     130}