root/library/tests/emix_test.cpp @ 500

Revision 500, 2.6 kB (checked in by vbarta, 16 years ago)

moved emix_test to testsuite

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