[386] | 1 | #include "stat/exp_family.h" |
---|
[394] | 2 | #include "stat/emix.h" |
---|
[717] | 3 | #include "../mat_checks.h" |
---|
[500] | 4 | #include "UnitTest++.h" |
---|
[717] | 5 | #include "../test_util.h" |
---|
[721] | 6 | #include "../pdf_harness.h" |
---|
[461] | 7 | |
---|
[721] | 8 | |
---|
[500] | 9 | const double epsilon = 0.00001; |
---|
| 10 | |
---|
[254] | 11 | using namespace bdm; |
---|
[193] | 12 | |
---|
[520] | 13 | static void check_mean ( emix &distrib_obj, int nsamples, const vec &mean, double tolerance ); |
---|
| 14 | |
---|
| 15 | static void check_covariance ( emix &distrib_obj, int nsamples, const mat &R, double tolerance); |
---|
| 16 | |
---|
[721] | 17 | TEST ( emix_test ) { |
---|
| 18 | pdf_harness::test_config ( "emix.cfg" ); |
---|
| 19 | } |
---|
| 20 | |
---|
[689] | 21 | TEST ( emix_1_test ) { |
---|
[522] | 22 | RV x ( "{emixx }" ); |
---|
| 23 | RV y ( "{emixy }" ); |
---|
[477] | 24 | RV xy = concat ( x, y ); |
---|
[520] | 25 | vec mu0 ( "1.00054 1.0455" ); |
---|
[193] | 26 | |
---|
[529] | 27 | enorm_ldmat_ptr E1; |
---|
[504] | 28 | E1->set_rv ( xy ); |
---|
[520] | 29 | E1->set_parameters ( mu0 , mat ( "0.740142 -0.259015; -0.259015 1.0302" ) ); |
---|
[477] | 30 | |
---|
[529] | 31 | enorm_ldmat_ptr E2; |
---|
[504] | 32 | E2->set_rv ( xy ); |
---|
| 33 | E2->set_parameters ( "-1.2 -0.1" , mat ( "1 0.4; 0.4 0.5" ) ); |
---|
[477] | 34 | |
---|
[529] | 35 | epdf_array A1 ( 1 ); |
---|
[504] | 36 | A1 ( 0 ) = E1; |
---|
[477] | 37 | |
---|
| 38 | emix M1; |
---|
| 39 | M1.set_rv ( xy ); |
---|
[504] | 40 | M1.set_parameters ( vec ( "1" ), A1 ); |
---|
[477] | 41 | |
---|
[500] | 42 | // test if ARX and emix with one ARX are the same |
---|
[529] | 43 | epdf_ptr Mm = M1.marginal ( y ); |
---|
| 44 | epdf_ptr Am = E1->marginal ( y ); |
---|
[693] | 45 | pdf_ptr Mc = M1.condition ( y ); |
---|
| 46 | pdf_ptr Ac = E1->condition ( y ); |
---|
[477] | 47 | |
---|
[504] | 48 | mlnorm<ldmat> *wacnd = dynamic_cast<mlnorm<ldmat> *>( Ac.get() ); |
---|
[500] | 49 | CHECK(wacnd); |
---|
| 50 | if ( wacnd ) { |
---|
| 51 | CHECK_CLOSE ( mat ( "-0.349953" ), wacnd->_A(), epsilon ); |
---|
| 52 | CHECK_CLOSE ( vec ( "1.39564" ), wacnd->_mu_const(), epsilon ); |
---|
| 53 | CHECK_CLOSE ( mat ( "0.939557" ), wacnd->_R(), epsilon ); |
---|
| 54 | } |
---|
[477] | 55 | |
---|
[500] | 56 | double same = -1.46433; |
---|
| 57 | CHECK_CLOSE ( same, Mm->evallog ( vec_1 ( 0.0 ) ), epsilon ); |
---|
| 58 | CHECK_CLOSE ( same, Am->evallog ( vec_1 ( 0.0 ) ), epsilon ); |
---|
| 59 | CHECK_CLOSE ( 0.145974, Mc->evallogcond ( vec_1 ( 0.0 ), vec_1 ( 0.0 ) ), epsilon ); |
---|
| 60 | CHECK_CLOSE ( -1.92433, Ac->evallogcond ( vec_1 ( 0.0 ), vec_1 ( 0.0 ) ), epsilon ); |
---|
[477] | 61 | |
---|
[500] | 62 | // mixture with two components |
---|
[529] | 63 | epdf_array A2 ( 2 ); |
---|
[504] | 64 | A2 ( 0 ) = E1; |
---|
| 65 | A2 ( 1 ) = E2; |
---|
[477] | 66 | |
---|
| 67 | emix M2; |
---|
| 68 | M2.set_rv ( xy ); |
---|
[504] | 69 | M2.set_parameters ( vec ( "1" ), A2 ); |
---|
[477] | 70 | |
---|
| 71 | |
---|
[500] | 72 | // mixture normalization |
---|
| 73 | CHECK_CLOSE ( 1.0, normcoef ( &M2, vec ( "-3 3 " ), vec ( "-3 3 " ) ), 0.1 ); |
---|
[477] | 74 | |
---|
| 75 | int N = 3; |
---|
[713] | 76 | mat Smp = M2.sample_mat ( N ); |
---|
[520] | 77 | |
---|
| 78 | vec exp_ll ( "-5.0 -2.53563 -2.62171" ); |
---|
[713] | 79 | vec ll = M2.evallog_mat ( Smp ); |
---|
[520] | 80 | CHECK_CLOSE ( exp_ll, ll, 5.0 ); |
---|
[193] | 81 | |
---|
[520] | 82 | check_mean ( M2, N, mu0, 1.0 ); |
---|
[500] | 83 | |
---|
[520] | 84 | mat observedR ( "0.740142 -0.259015; -0.259015 1.0302" ); |
---|
| 85 | check_covariance ( M2, N, observedR, 2.0); |
---|
[193] | 86 | |
---|
[529] | 87 | epdf_ptr Mg = M2.marginal ( y ); |
---|
[504] | 88 | CHECK ( Mg.get() ); |
---|
[693] | 89 | pdf_ptr Cn = M2.condition ( x ); |
---|
[504] | 90 | CHECK ( Cn.get() ); |
---|
[193] | 91 | |
---|
[500] | 92 | // marginal mean |
---|
| 93 | CHECK_CLOSE ( vec ( "1.0" ), Mg->mean(), 0.1 ); |
---|
[193] | 94 | } |
---|
[520] | 95 | |
---|
[522] | 96 | |
---|
[520] | 97 | static void check_mean ( emix &distrib_obj, int nsamples, const vec &mean, double tolerance ) { |
---|
| 98 | int tc = 0; |
---|
| 99 | Array<vec> actual(CurrentContext::max_trial_count); |
---|
| 100 | do { |
---|
[713] | 101 | mat smp = distrib_obj.sample_mat ( nsamples ); |
---|
[520] | 102 | vec emu = sum ( smp, 2 ) / nsamples; |
---|
| 103 | actual( tc ) = emu; |
---|
| 104 | ++tc; |
---|
| 105 | } while ( ( tc < CurrentContext::max_trial_count ) && |
---|
| 106 | !UnitTest::AreClose ( mean, actual( tc - 1 ), tolerance ) ); |
---|
| 107 | if ( ( tc == CurrentContext::max_trial_count ) && |
---|
| 108 | ( !UnitTest::AreClose ( mean, actual( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) { |
---|
| 109 | UnitTest::MemoryOutStream stream; |
---|
| 110 | UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), __LINE__); |
---|
| 111 | stream << "Expected " << mean << " +/- " << tolerance << " but was " << actual; |
---|
| 112 | |
---|
| 113 | UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | static void check_covariance ( emix &distrib_obj, int nsamples, const mat &R, double tolerance) { |
---|
| 118 | int tc = 0; |
---|
| 119 | Array<mat> actual(CurrentContext::max_trial_count); |
---|
| 120 | do { |
---|
[713] | 121 | mat smp = distrib_obj.sample_mat ( nsamples ); |
---|
[520] | 122 | vec emu = sum ( smp, 2 ) / nsamples; |
---|
| 123 | mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu ); |
---|
| 124 | actual( tc ) = er; |
---|
| 125 | ++tc; |
---|
| 126 | } while ( ( tc < CurrentContext::max_trial_count ) && |
---|
| 127 | !UnitTest::AreClose ( R, actual( tc - 1 ), tolerance ) ); |
---|
| 128 | if ( ( tc == CurrentContext::max_trial_count ) && |
---|
| 129 | ( !UnitTest::AreClose ( R, actual( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) { |
---|
| 130 | UnitTest::MemoryOutStream stream; |
---|
| 131 | UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), __LINE__); |
---|
| 132 | stream << "Expected " << R << " +/- " << tolerance << " but was " << actual; |
---|
| 133 | |
---|
| 134 | UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() ); |
---|
| 135 | } |
---|
| 136 | } |
---|