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 | shared_ptr<enorm<ldmat> > E1 = new enorm<ldmat>(); |
---|
18 | E1->set_rv ( xy ); |
---|
19 | E1->set_parameters ( "1.00054 1.0455" , mat ( "0.740142 -0.259015; -0.259015 1.0302" ) ); |
---|
20 | |
---|
21 | shared_ptr<enorm<ldmat> > E2 = new enorm<ldmat>(); |
---|
22 | E2->set_rv ( xy ); |
---|
23 | E2->set_parameters ( "-1.2 -0.1" , mat ( "1 0.4; 0.4 0.5" ) ); |
---|
24 | |
---|
25 | Array<shared_ptr<epdf> > A1 ( 1 ); |
---|
26 | A1 ( 0 ) = E1; |
---|
27 | |
---|
28 | emix M1; |
---|
29 | M1.set_rv ( xy ); |
---|
30 | M1.set_parameters ( vec ( "1" ), A1 ); |
---|
31 | |
---|
32 | // test if ARX and emix with one ARX are the same |
---|
33 | shared_ptr<epdf> Mm = M1.marginal ( y ); |
---|
34 | shared_ptr<epdf> Am = E1->marginal ( y ); |
---|
35 | shared_ptr<mpdf> Mc = M1.condition ( y ); |
---|
36 | shared_ptr<mpdf> Ac = E1->condition ( y ); |
---|
37 | |
---|
38 | mlnorm<ldmat> *wacnd = dynamic_cast<mlnorm<ldmat> *>( Ac.get() ); |
---|
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<shared_ptr<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 ); |
---|
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 | shared_ptr<epdf> Mg = M2.marginal ( y ); |
---|
77 | CHECK ( Mg.get() ); |
---|
78 | shared_ptr<mpdf> Cn = M2.condition ( x ); |
---|
79 | CHECK ( Cn.get() ); |
---|
80 | |
---|
81 | // marginal mean |
---|
82 | CHECK_CLOSE ( vec ( "1.0" ), Mg->mean(), 0.1 ); |
---|
83 | } |
---|