root/library/tests/testsuite/emix_test.cpp @ 766

Revision 766, 4.2 kB (checked in by mido, 14 years ago)

abstract methods restored wherever they are meaningful
macros NOT_IMPLEMENTED and NOT_IMPLEMENTED_VOID defined to make sources shorter
emix::set_parameters and mmix::set_parameters removed, corresponding acces methods created and the corresponding validate methods improved appropriately
some compilator warnings were avoided
and also a few other things cleaned up

  • Property svn:eol-style set to native
RevLine 
[386]1#include "stat/exp_family.h"
[394]2#include "stat/emix.h"
[717]3#include "../mat_checks.h"
[500]4#include "UnitTest++.h"
[717]5#include "../test_util.h"
[721]6#include "../pdf_harness.h"
[461]7
[721]8
[500]9const double epsilon = 0.00001;
10
[254]11using namespace bdm;
[193]12
[520]13static void check_mean ( emix &distrib_obj, int nsamples, const vec &mean, double tolerance );
14
[722]15static void check_covariance ( emix &distrib_obj, int nsamples, const mat &R, double tolerance );
[520]16
[721]17TEST ( emix_test ) {
18        pdf_harness::test_config ( "emix.cfg" );
19}
20
[689]21TEST ( emix_1_test ) {
[522]22        RV x ( "{emixx }" );
23        RV y ( "{emixy }" );
[477]24        RV xy = concat ( x, y );
[520]25        vec mu0 ( "1.00054 1.0455" );
[193]26
[529]27        enorm_ldmat_ptr E1;
[504]28        E1->set_rv ( xy );
[520]29        E1->set_parameters ( mu0 , mat ( "0.740142 -0.259015; -0.259015 1.0302" ) );
[477]30
[529]31        enorm_ldmat_ptr E2;
[504]32        E2->set_rv ( xy );
33        E2->set_parameters ( "-1.2 -0.1" , mat ( "1 0.4; 0.4 0.5" ) );
[477]34
[529]35        epdf_array A1 ( 1 );
[504]36        A1 ( 0 ) = E1;
[477]37
38        emix M1;
39        M1.set_rv ( xy );
[766]40        M1._Coms() = A1;
41        M1._w() = 1;
[750]42        M1.validate();
[477]43
[500]44        // test if ARX and emix with one ARX are the same
[529]45        epdf_ptr Mm = M1.marginal ( y );
46        epdf_ptr Am = E1->marginal ( y );
[693]47        pdf_ptr Mc = M1.condition ( y );
48        pdf_ptr Ac = E1->condition ( y );
[477]49
[722]50        mlnorm<ldmat> *wacnd = dynamic_cast<mlnorm<ldmat> *> ( Ac.get() );
51        CHECK ( wacnd );
[500]52        if ( wacnd ) {
53                CHECK_CLOSE ( mat ( "-0.349953" ), wacnd->_A(), epsilon );
54                CHECK_CLOSE ( vec ( "1.39564" ), wacnd->_mu_const(), epsilon );
55                CHECK_CLOSE ( mat ( "0.939557" ), wacnd->_R(), epsilon );
56        }
[477]57
[500]58        double same = -1.46433;
59        CHECK_CLOSE ( same, Mm->evallog ( vec_1 ( 0.0 ) ), epsilon );
60        CHECK_CLOSE ( same, Am->evallog ( vec_1 ( 0.0 ) ), epsilon );
61        CHECK_CLOSE ( 0.145974, Mc->evallogcond ( vec_1 ( 0.0 ), vec_1 ( 0.0 ) ), epsilon );
62        CHECK_CLOSE ( -1.92433, Ac->evallogcond ( vec_1 ( 0.0 ), vec_1 ( 0.0 ) ), epsilon );
[477]63
[500]64        // mixture with two components
[529]65        epdf_array A2 ( 2 );
[504]66        A2 ( 0 ) = E1;
67        A2 ( 1 ) = E2;
[477]68
69        emix M2;
70        M2.set_rv ( xy );
[766]71        M2._Coms() = A2;
72        M2._w() = 1;
[750]73        M2.validate();
[477]74
[500]75        // mixture normalization
76        CHECK_CLOSE ( 1.0, normcoef ( &M2, vec ( "-3 3 " ), vec ( "-3 3 " ) ), 0.1 );
[477]77
78        int N = 3;
[713]79        mat Smp = M2.sample_mat ( N );
[520]80
81        vec exp_ll ( "-5.0 -2.53563 -2.62171" );
[713]82        vec ll = M2.evallog_mat ( Smp );
[520]83        CHECK_CLOSE ( exp_ll, ll, 5.0 );
[193]84
[520]85        check_mean ( M2, N, mu0, 1.0 );
[500]86
[520]87        mat observedR ( "0.740142 -0.259015; -0.259015 1.0302" );
[722]88        check_covariance ( M2, N, observedR, 2.0 );
[193]89
[529]90        epdf_ptr Mg = M2.marginal ( y );
[504]91        CHECK ( Mg.get() );
[693]92        pdf_ptr Cn = M2.condition ( x );
[504]93        CHECK ( Cn.get() );
[193]94
[500]95        // marginal mean
96        CHECK_CLOSE ( vec ( "1.0" ), Mg->mean(), 0.1 );
[193]97}
[520]98
[522]99
[520]100static void check_mean ( emix &distrib_obj, int nsamples, const vec &mean, double tolerance ) {
101        int tc = 0;
[722]102        Array<vec> actual ( CurrentContext::max_trial_count );
[520]103        do {
[713]104                mat smp = distrib_obj.sample_mat ( nsamples );
[520]105                vec emu = sum ( smp, 2 ) / nsamples;
[722]106                actual ( tc ) = emu;
[520]107                ++tc;
108        } while ( ( tc < CurrentContext::max_trial_count ) &&
[722]109                  !UnitTest::AreClose ( mean, actual ( tc - 1 ), tolerance ) );
[520]110        if ( ( tc == CurrentContext::max_trial_count ) &&
[722]111                ( !UnitTest::AreClose ( mean, actual ( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) {
[520]112                UnitTest::MemoryOutStream stream;
[722]113                UnitTest::TestDetails details ( *UnitTest::CurrentTest::Details(), __LINE__ );
[520]114                stream << "Expected " << mean << " +/- " << tolerance << " but was " << actual;
115
116                UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() );
117        }
118}
119
[722]120static void check_covariance ( emix &distrib_obj, int nsamples, const mat &R, double tolerance ) {
[520]121        int tc = 0;
[722]122        Array<mat> actual ( CurrentContext::max_trial_count );
[520]123        do {
[713]124                mat smp = distrib_obj.sample_mat ( nsamples );
[520]125                vec emu = sum ( smp, 2 ) / nsamples;
126                mat er = ( smp * smp.T() ) / nsamples - outer_product ( emu, emu );
[722]127                actual ( tc ) = er;
[520]128                ++tc;
129        } while ( ( tc < CurrentContext::max_trial_count ) &&
[722]130                  !UnitTest::AreClose ( R, actual ( tc - 1 ), tolerance ) );
[520]131        if ( ( tc == CurrentContext::max_trial_count ) &&
[722]132                ( !UnitTest::AreClose ( R, actual ( CurrentContext::max_trial_count - 1 ), tolerance ) ) ) {
[520]133                UnitTest::MemoryOutStream stream;
[722]134                UnitTest::TestDetails details ( *UnitTest::CurrentTest::Details(), __LINE__ );
[520]135                stream << "Expected " << R << " +/- " << tolerance << " but was " << actual;
136
137                UnitTest::CurrentTest::Results()->OnTestFailure ( details, stream.GetText() );
[722]138        }
[520]139}
Note: See TracBrowser for help on using the browser.