root/library/tests/emix_test.cpp @ 520

Revision 520, 4.2 kB (checked in by vbarta, 15 years ago)

repeating emix mean & covariance 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_old ) {
17        RV x ( "{x }" );
18        RV y ( "{y }" );
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
91static void check_mean ( emix &distrib_obj, int nsamples, const vec &mean, double tolerance ) {
92        int tc = 0;
93        Array<vec> actual(CurrentContext::max_trial_count);
94        do {
95                mat smp = distrib_obj.sample_m ( nsamples );
96                vec emu = sum ( smp, 2 ) / nsamples;
97                actual( tc ) = emu;
98                ++tc;
99        } while ( ( tc < CurrentContext::max_trial_count ) &&
100                  !UnitTest::AreClose ( mean, actual( tc - 1 ), tolerance ) );
101        if ( ( tc == CurrentContext::max_trial_count ) &&
102             ( !UnitTest::AreClose ( mean, actual( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) {
103                UnitTest::MemoryOutStream stream;
104                UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), __LINE__);
105                stream << "Expected " << mean << " +/- " << tolerance << " but was " << actual;
106
107                UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() );
108        }
109}
110
111static void check_covariance ( emix &distrib_obj, int nsamples, const mat &R, double tolerance) {
112        int tc = 0;
113        Array<mat> actual(CurrentContext::max_trial_count);
114        do {
115                mat smp = distrib_obj.sample_m ( nsamples );
116                vec emu = sum ( smp, 2 ) / nsamples;
117                mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu );
118                actual( tc ) = er;
119                ++tc;
120        } while ( ( tc < CurrentContext::max_trial_count ) &&
121                  !UnitTest::AreClose ( R, actual( tc - 1 ), tolerance ) );
122        if ( ( tc == CurrentContext::max_trial_count ) &&
123             ( !UnitTest::AreClose ( R, actual( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) {
124                UnitTest::MemoryOutStream stream;
125                UnitTest::TestDetails details(*UnitTest::CurrentTest::Details(), __LINE__);
126                stream << "Expected " << R << " +/- " << tolerance << " but was " << actual;
127
128                UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() );
129       }
130}
Note: See TracBrowser for help on using the browser.