root/library/tests/emix_test.cpp @ 504

Revision 504, 2.3 kB (checked in by vbarta, 15 years ago)

returning shared pointers from epdf::marginal & epdf::condition; testsuite run leaks down from 8402 to 6510 bytes

  • Property svn:eol-style set to native
RevLine 
[461]1#include "shared_ptr.h"
[386]2#include "stat/exp_family.h"
[394]3#include "stat/emix.h"
[500]4#include "mat_checks.h"
5#include "UnitTest++.h"
6#include "test_util.h"
[461]7
[500]8const double epsilon = 0.00001;
9
[254]10using namespace bdm;
[193]11
[500]12TEST ( test_emix_old ) {
[193]13        RV x ( "{x }" );
14        RV y ( "{y }" );
[477]15        RV xy = concat ( x, y );
[193]16
[504]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" ) );
[477]20
[504]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" ) );
[477]24
[504]25        Array<shared_ptr<epdf> > A1 ( 1 );
26        A1 ( 0 ) = E1;
[477]27
28        emix M1;
29        M1.set_rv ( xy );
[504]30        M1.set_parameters ( vec ( "1" ), A1 );
[477]31
[500]32        // test if ARX and emix with one ARX are the same
[504]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 );
[477]37
[504]38        mlnorm<ldmat> *wacnd = dynamic_cast<mlnorm<ldmat> *>( Ac.get() );
[500]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        }
[477]45
[500]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 );
[477]51
[500]52        // mixture with two components
[504]53        Array<shared_ptr<epdf> > A2 ( 2 );
54        A2 ( 0 ) = E1;
55        A2 ( 1 ) = E2;
[477]56
57        emix M2;
58        M2.set_rv ( xy );
[504]59        M2.set_parameters ( vec ( "1" ), A2 );
[477]60
61
[500]62        // mixture normalization
63        CHECK_CLOSE ( 1.0, normcoef ( &M2, vec ( "-3 3 " ), vec ( "-3 3 " ) ), 0.1 );
[477]64
65        int N = 3;
[193]66        vec ll2 ( N );
[477]67        mat Smp = M2.sample_m ( N );
[500]68        vec ll = M2.evallog_m ( Smp );
[193]69
[477]70        vec Emu = sum ( Smp, 2 ) / N;
[500]71        CHECK_CLOSE ( vec ( "1.00054 1.0455" ), Emu, 1.0 );
72
[477]73        mat Er = ( Smp * Smp.transpose() ) / N - outer_product ( Emu, Emu );
[500]74        CHECK_CLOSE ( mat ( "0.740142 -0.259015; -0.259015 1.0302" ), Er, 2.0 );
[193]75
[504]76        shared_ptr<epdf> Mg = M2.marginal ( y );
77        CHECK ( Mg.get() );
78        shared_ptr<mpdf> Cn = M2.condition ( x );
79        CHECK ( Cn.get() );
[193]80
[500]81        // marginal mean
82        CHECK_CLOSE ( vec ( "1.0" ), Mg->mean(), 0.1 );
[193]83}
Note: See TracBrowser for help on using the browser.