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