1 | #include "shared_ptr.h" |
---|
2 | #include "stat/exp_family.h" |
---|
3 | #include "stat/emix.h" |
---|
4 | #include "mat_checks.h" |
---|
5 | #include "UnitTest++.h" |
---|
6 | #include "test_util.h" |
---|
7 | |
---|
8 | const double epsilon = 0.00001; |
---|
9 | |
---|
10 | using namespace bdm; |
---|
11 | |
---|
12 | TEST ( test_emix_old ) { |
---|
13 | RV x ( "{x }" ); |
---|
14 | RV y ( "{y }" ); |
---|
15 | RV xy = concat ( x, y ); |
---|
16 | |
---|
17 | enorm<ldmat> E1; |
---|
18 | E1.set_rv ( xy ); |
---|
19 | |
---|
20 | E1.set_parameters ( "1.00054 1.0455" , mat ( "0.740142 -0.259015; -0.259015 1.0302" ) ); |
---|
21 | enorm<ldmat> E2; |
---|
22 | E2.set_rv ( xy ); |
---|
23 | E2.set_parameters ( "-1.2 -0.1" , mat ( "1 0.4; 0.4 0.5" ) ); |
---|
24 | |
---|
25 | Array<epdf*> A1 ( 1 ); |
---|
26 | A1 ( 0 ) = &E1; |
---|
27 | |
---|
28 | emix M1; |
---|
29 | M1.set_rv ( xy ); |
---|
30 | M1.set_parameters ( vec ( "1" ), A1, false ); |
---|
31 | |
---|
32 | // test if ARX and emix with one ARX are the same |
---|
33 | epdf* Mm = M1.marginal ( y ); |
---|
34 | epdf* Am = E1.marginal ( y ); |
---|
35 | mpdf* Mc = M1.condition ( y ); |
---|
36 | mpdf* Ac = E1.condition ( y ); |
---|
37 | |
---|
38 | mlnorm<ldmat> *wacnd = dynamic_cast<mlnorm<ldmat> *>(Ac); |
---|
39 | CHECK(wacnd); |
---|
40 | if ( wacnd ) { |
---|
41 | CHECK_CLOSE ( mat ( "-0.349953" ), wacnd->_A(), epsilon ); |
---|
42 | CHECK_CLOSE ( vec ( "1.39564" ), wacnd->_mu_const(), epsilon ); |
---|
43 | CHECK_CLOSE ( mat ( "0.939557" ), wacnd->_R(), epsilon ); |
---|
44 | } |
---|
45 | |
---|
46 | double same = -1.46433; |
---|
47 | CHECK_CLOSE ( same, Mm->evallog ( vec_1 ( 0.0 ) ), epsilon ); |
---|
48 | CHECK_CLOSE ( same, Am->evallog ( vec_1 ( 0.0 ) ), epsilon ); |
---|
49 | CHECK_CLOSE ( 0.145974, Mc->evallogcond ( vec_1 ( 0.0 ), vec_1 ( 0.0 ) ), epsilon ); |
---|
50 | CHECK_CLOSE ( -1.92433, Ac->evallogcond ( vec_1 ( 0.0 ), vec_1 ( 0.0 ) ), epsilon ); |
---|
51 | |
---|
52 | // mixture with two components |
---|
53 | Array<epdf*> A2 ( 2 ); |
---|
54 | A2 ( 0 ) = &E1; |
---|
55 | A2 ( 1 ) = &E2; |
---|
56 | |
---|
57 | emix M2; |
---|
58 | M2.set_rv ( xy ); |
---|
59 | M2.set_parameters ( vec ( "1" ), A2, false ); |
---|
60 | |
---|
61 | |
---|
62 | // mixture normalization |
---|
63 | CHECK_CLOSE ( 1.0, normcoef ( &M2, vec ( "-3 3 " ), vec ( "-3 3 " ) ), 0.1 ); |
---|
64 | |
---|
65 | int N = 3; |
---|
66 | vec ll2 ( N ); |
---|
67 | mat Smp = M2.sample_m ( N ); |
---|
68 | vec ll = M2.evallog_m ( Smp ); |
---|
69 | |
---|
70 | vec Emu = sum ( Smp, 2 ) / N; |
---|
71 | CHECK_CLOSE ( vec ( "1.00054 1.0455" ), Emu, 1.0 ); |
---|
72 | |
---|
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 ); |
---|
75 | |
---|
76 | epdf *nm2mrg = M2.marginal ( y ); |
---|
77 | CHECK ( nm2mrg ); |
---|
78 | shared_ptr<epdf> Mg ( nm2mrg ); |
---|
79 | mpdf *Cn = M2.condition ( x ); |
---|
80 | CHECK ( Cn ); |
---|
81 | |
---|
82 | #if false |
---|
83 | it_file it ( "emix_test.it" ); |
---|
84 | it << Name ( "Smp" ) << Smp; |
---|
85 | #endif |
---|
86 | |
---|
87 | // marginal mean |
---|
88 | CHECK_CLOSE ( vec ( "1.0" ), Mg->mean(), 0.1 ); |
---|
89 | |
---|
90 | #if false |
---|
91 | // putting them back together |
---|
92 | mepdf mMg ( Mg ); |
---|
93 | Array<mpdf*> AA ( 2 ); |
---|
94 | AA ( 0 ) = Cn; |
---|
95 | AA ( 1 ) = &mMg; |
---|
96 | mprod mEp ( AA ); |
---|
97 | |
---|
98 | for ( int j = 0; j < N; j++ ) { |
---|
99 | ll2 ( j ) = mEp.evallogcond ( Smp.get_col ( j ), vec ( 0 ) ); |
---|
100 | } |
---|
101 | it << Name ( "ll" ) << ll; |
---|
102 | it << Name ( "ll2" ) << ll2; |
---|
103 | #endif |
---|
104 | } |
---|