root/library/tests/emix_test.cpp @ 522

Revision 522, 4.7 kB (checked in by vbarta, 15 years ago)

more emix tests

  • 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
12static void check_mean ( emix &distrib_obj, int nsamples, const vec &mean, double tolerance );
13
14static void check_covariance ( emix &distrib_obj, int nsamples, const mat &R, double tolerance);
15
16TEST ( test_emix_1 ) {
17        RV x ( "{emixx }" );
18        RV y ( "{emixy }" );
19        RV xy = concat ( x, y );
20        vec mu0 ( "1.00054 1.0455" );
21
22        shared_ptr<enorm<ldmat> > E1 = new enorm<ldmat>();
23        E1->set_rv ( xy );
24        E1->set_parameters ( mu0 , mat ( "0.740142 -0.259015; -0.259015 1.0302" ) );
25
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" ) );
29
30        Array<shared_ptr<epdf> > A1 ( 1 );
31        A1 ( 0 ) = E1;
32
33        emix M1;
34        M1.set_rv ( xy );
35        M1.set_parameters ( vec ( "1" ), A1 );
36
37        // test if ARX and emix with one ARX are the same
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 );
42
43        mlnorm<ldmat> *wacnd = dynamic_cast<mlnorm<ldmat> *>( Ac.get() );
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        }
50
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 );
56
57        // mixture with two components
58        Array<shared_ptr<epdf> > A2 ( 2 );
59        A2 ( 0 ) = E1;
60        A2 ( 1 ) = E2;
61
62        emix M2;
63        M2.set_rv ( xy );
64        M2.set_parameters ( vec ( "1" ), A2 );
65
66
67        // mixture normalization
68        CHECK_CLOSE ( 1.0, normcoef ( &M2, vec ( "-3 3 " ), vec ( "-3 3 " ) ), 0.1 );
69
70        int N = 3;
71        mat Smp = M2.sample_m ( N );
72
73        vec exp_ll ( "-5.0 -2.53563 -2.62171" );
74        vec ll = M2.evallog_m ( Smp );
75        CHECK_CLOSE ( exp_ll, ll, 5.0 );
76
77        check_mean ( M2, N, mu0, 1.0 );
78
79        mat observedR ( "0.740142 -0.259015; -0.259015 1.0302" );
80        check_covariance ( M2, N, observedR, 2.0);
81
82        shared_ptr<epdf> Mg = M2.marginal ( y );
83        CHECK ( Mg.get() );
84        shared_ptr<mpdf> Cn = M2.condition ( x );
85        CHECK ( Cn.get() );
86
87        // marginal mean
88        CHECK_CLOSE ( vec ( "1.0" ), Mg->mean(), 0.1 );
89}
90
91TEST ( test_emix_2 ) {
92        int N = 10000; //number of samples
93        vec mu0 ( "1.5 1.7" );
94        mat V0 ( "1.2 0.3; 0.3 5" );
95        ldmat R = ldmat ( V0 );
96
97        shared_ptr<enorm<ldmat> > eN = new enorm<ldmat>();
98        eN->set_parameters ( mu0, R );
99
100        vec a = "100000,10000";
101        vec b = a / 10.0;
102        shared_ptr<egamma> eG = new egamma();
103        eG->set_parameters ( a, b );
104
105        emix eMix;
106        Array<shared_ptr<epdf> > Coms ( 2 );
107        Coms ( 0 ) = eG;
108        Coms ( 1 ) = eN;
109
110        eMix.set_parameters ( vec_2 ( 0.5, 0.5 ), Coms );
111        check_mean ( eMix, N, eMix.mean(), 0.1 );
112}
113
114static void check_mean ( emix &distrib_obj, int nsamples, const vec &mean, double tolerance ) {
115        int tc = 0;
116        Array<vec> actual(CurrentContext::max_trial_count);
117        do {
118                mat smp = distrib_obj.sample_m ( nsamples );
119                vec emu = sum ( smp, 2 ) / nsamples;
120                actual( tc ) = emu;
121                ++tc;
122        } while ( ( tc < CurrentContext::max_trial_count ) &&
123                  !UnitTest::AreClose ( mean, actual( tc - 1 ), tolerance ) );
124        if ( ( tc == CurrentContext::max_trial_count ) &&
125             ( !UnitTest::AreClose ( mean, actual( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) {
126                UnitTest::MemoryOutStream stream;
127                UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), __LINE__);
128                stream << "Expected " << mean << " +/- " << tolerance << " but was " << actual;
129
130                UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() );
131        }
132}
133
134static void check_covariance ( emix &distrib_obj, int nsamples, const mat &R, double tolerance) {
135        int tc = 0;
136        Array<mat> actual(CurrentContext::max_trial_count);
137        do {
138                mat smp = distrib_obj.sample_m ( nsamples );
139                vec emu = sum ( smp, 2 ) / nsamples;
140                mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu );
141                actual( tc ) = er;
142                ++tc;
143        } while ( ( tc < CurrentContext::max_trial_count ) &&
144                  !UnitTest::AreClose ( R, actual( tc - 1 ), tolerance ) );
145        if ( ( tc == CurrentContext::max_trial_count ) &&
146             ( !UnitTest::AreClose ( R, actual( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) {
147                UnitTest::MemoryOutStream stream;
148                UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), __LINE__);
149                stream << "Expected " << R << " +/- " << tolerance << " but was " << actual;
150
151                UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() );
152       }
153}
Note: See TracBrowser for help on using the browser.