[461] | 1 | #include "shared_ptr.h" |
---|
[386] | 2 | #include "stat/exp_family.h" |
---|
[394] | 3 | #include "stat/emix.h" |
---|
[500] | 4 | #include "mat_checks.h" |
---|
| 5 | #include "UnitTest++.h" |
---|
| 6 | #include "test_util.h" |
---|
[461] | 7 | |
---|
[500] | 8 | const double epsilon = 0.00001; |
---|
| 9 | |
---|
[254] | 10 | using namespace bdm; |
---|
[193] | 11 | |
---|
[520] | 12 | static void check_mean ( emix &distrib_obj, int nsamples, const vec &mean, double tolerance ); |
---|
| 13 | |
---|
| 14 | static void check_covariance ( emix &distrib_obj, int nsamples, const mat &R, double tolerance); |
---|
| 15 | |
---|
[500] | 16 | TEST ( test_emix_old ) { |
---|
[193] | 17 | RV x ( "{x }" ); |
---|
| 18 | RV y ( "{y }" ); |
---|
[477] | 19 | RV xy = concat ( x, y ); |
---|
[520] | 20 | vec mu0 ( "1.00054 1.0455" ); |
---|
[193] | 21 | |
---|
[504] | 22 | shared_ptr<enorm<ldmat> > E1 = new enorm<ldmat>(); |
---|
| 23 | E1->set_rv ( xy ); |
---|
[520] | 24 | E1->set_parameters ( mu0 , mat ( "0.740142 -0.259015; -0.259015 1.0302" ) ); |
---|
[477] | 25 | |
---|
[504] | 26 | shared_ptr<enorm<ldmat> > E2 = new enorm<ldmat>(); |
---|
| 27 | E2->set_rv ( xy ); |
---|
| 28 | E2->set_parameters ( "-1.2 -0.1" , mat ( "1 0.4; 0.4 0.5" ) ); |
---|
[477] | 29 | |
---|
[504] | 30 | Array<shared_ptr<epdf> > A1 ( 1 ); |
---|
| 31 | A1 ( 0 ) = E1; |
---|
[477] | 32 | |
---|
| 33 | emix M1; |
---|
| 34 | M1.set_rv ( xy ); |
---|
[504] | 35 | M1.set_parameters ( vec ( "1" ), A1 ); |
---|
[477] | 36 | |
---|
[500] | 37 | // test if ARX and emix with one ARX are the same |
---|
[504] | 38 | shared_ptr<epdf> Mm = M1.marginal ( y ); |
---|
| 39 | shared_ptr<epdf> Am = E1->marginal ( y ); |
---|
| 40 | shared_ptr<mpdf> Mc = M1.condition ( y ); |
---|
| 41 | shared_ptr<mpdf> Ac = E1->condition ( y ); |
---|
[477] | 42 | |
---|
[504] | 43 | mlnorm<ldmat> *wacnd = dynamic_cast<mlnorm<ldmat> *>( Ac.get() ); |
---|
[500] | 44 | CHECK(wacnd); |
---|
| 45 | if ( wacnd ) { |
---|
| 46 | CHECK_CLOSE ( mat ( "-0.349953" ), wacnd->_A(), epsilon ); |
---|
| 47 | CHECK_CLOSE ( vec ( "1.39564" ), wacnd->_mu_const(), epsilon ); |
---|
| 48 | CHECK_CLOSE ( mat ( "0.939557" ), wacnd->_R(), epsilon ); |
---|
| 49 | } |
---|
[477] | 50 | |
---|
[500] | 51 | double same = -1.46433; |
---|
| 52 | CHECK_CLOSE ( same, Mm->evallog ( vec_1 ( 0.0 ) ), epsilon ); |
---|
| 53 | CHECK_CLOSE ( same, Am->evallog ( vec_1 ( 0.0 ) ), epsilon ); |
---|
| 54 | CHECK_CLOSE ( 0.145974, Mc->evallogcond ( vec_1 ( 0.0 ), vec_1 ( 0.0 ) ), epsilon ); |
---|
| 55 | CHECK_CLOSE ( -1.92433, Ac->evallogcond ( vec_1 ( 0.0 ), vec_1 ( 0.0 ) ), epsilon ); |
---|
[477] | 56 | |
---|
[500] | 57 | // mixture with two components |
---|
[504] | 58 | Array<shared_ptr<epdf> > A2 ( 2 ); |
---|
| 59 | A2 ( 0 ) = E1; |
---|
| 60 | A2 ( 1 ) = E2; |
---|
[477] | 61 | |
---|
| 62 | emix M2; |
---|
| 63 | M2.set_rv ( xy ); |
---|
[504] | 64 | M2.set_parameters ( vec ( "1" ), A2 ); |
---|
[477] | 65 | |
---|
| 66 | |
---|
[500] | 67 | // mixture normalization |
---|
| 68 | CHECK_CLOSE ( 1.0, normcoef ( &M2, vec ( "-3 3 " ), vec ( "-3 3 " ) ), 0.1 ); |
---|
[477] | 69 | |
---|
| 70 | int N = 3; |
---|
| 71 | mat Smp = M2.sample_m ( N ); |
---|
[520] | 72 | |
---|
| 73 | vec exp_ll ( "-5.0 -2.53563 -2.62171" ); |
---|
[500] | 74 | vec ll = M2.evallog_m ( Smp ); |
---|
[520] | 75 | CHECK_CLOSE ( exp_ll, ll, 5.0 ); |
---|
[193] | 76 | |
---|
[520] | 77 | check_mean ( M2, N, mu0, 1.0 ); |
---|
[500] | 78 | |
---|
[520] | 79 | mat observedR ( "0.740142 -0.259015; -0.259015 1.0302" ); |
---|
| 80 | check_covariance ( M2, N, observedR, 2.0); |
---|
[193] | 81 | |
---|
[504] | 82 | shared_ptr<epdf> Mg = M2.marginal ( y ); |
---|
| 83 | CHECK ( Mg.get() ); |
---|
| 84 | shared_ptr<mpdf> Cn = M2.condition ( x ); |
---|
| 85 | CHECK ( Cn.get() ); |
---|
[193] | 86 | |
---|
[500] | 87 | // marginal mean |
---|
| 88 | CHECK_CLOSE ( vec ( "1.0" ), Mg->mean(), 0.1 ); |
---|
[193] | 89 | } |
---|
[520] | 90 | |
---|
| 91 | static 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 | |
---|
| 111 | static 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 | } |
---|