root/library/tests/testSmp.cpp @ 504

Revision 504, 3.6 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 
[262]1
[386]2#include "stat/exp_family.h"
[394]3#include "stat/emix.h"
[13]4
[254]5using namespace bdm;
[13]6
7//These lines are needed for use of cout and endl
8using std::cout;
9using std::endl;
10
[477]11void disp ( const vec &tmu, const mat &tR, const mat &Smp ) {
[32]12        int N = Smp.cols();
[477]13        vec Emu = Smp * ones ( N ) / N ;
14        mat Er = ( Smp * Smp.transpose() ) / N - outer_product ( Emu, Emu );
15        cout << "True mu:" << tmu << endl;
16        cout << "Emp  mu:" << Emu << endl;
17
18        cout << "True R:" << tR << endl;
19        cout << "Emp  R:" << Er << endl;
[32]20}
21
[13]22int main() {
23
[32]24        RNG_randomize();
[13]25
[477]26        RV x ( "{x }", "2" );
27        RV y ( "{y }", "2" );
[13]28        int N = 10000; //number of samples
29        vec mu0 = "1.5 1.7";
[477]30        mat V0 ( "1.2 0.3; 0.3 5" );
31        ldmat R = ldmat ( V0 );
32
33        cout << "====== ENorm ====== " << endl;
[504]34        shared_ptr<enorm<ldmat> > eN = new enorm<ldmat>();
35        eN->set_parameters ( mu0, R );
36        mat Smp = eN->sample_m ( N );
[32]37
[477]38        disp ( mu0, R.to_mat(), Smp );
[32]39
[477]40        cout << "====== MlNorm ====== " << endl;
41        mat I = eye ( 2 );
[270]42        mlnorm<ldmat> ML;
[477]43        ML.set_parameters ( I, zeros ( 2 ), R );
44        Smp = ML.samplecond_m ( mu0, N );
[13]45
[477]46        disp ( mu0, R.to_mat(), Smp );
47
48        cout << "====== EGamma ====== " << endl;
[32]49        vec a = "100000,10000";
[477]50        vec b = a / 10.0;
[504]51        shared_ptr<egamma> eG = new egamma();
52        eG->set_parameters ( a, b );
[32]53
[504]54        cout << eG->evallog ( a ) << endl;
55        Smp = eG->sample_m ( N );
[32]56
[477]57        vec g_mu = elem_div ( a, b );
58        vec g_var = elem_div ( a, pow ( b, 2.0 ) );
59        disp ( g_mu, diag ( g_var ), Smp );
60
61        cout << "====== MGamma ====== " << endl;
[504]62        shared_ptr<mgamma> mG = new mgamma();
[32]63        double k = 10.0;
[504]64        mG->set_parameters ( k, mu0 );
[32]65
[504]66        Smp = mG->samplecond_m ( mu0, N );
[477]67        disp ( mu0, pow ( mu0, 2.0 ) / k, Smp );
68
[104]69        cout << "======= EMix ======== " << endl;
[504]70        shared_ptr<emix> eMix = new emix();
71        Array<shared_ptr<epdf> > Coms ( 2 );
72        Coms ( 0 ) = eG;
73        Coms ( 1 ) = eN;
[477]74
[504]75        eMix->set_parameters ( vec_2 ( 0.5, 0.5 ), Coms );
76        vec smp = eMix->sample();
77        Smp = eMix->sample_m ( N );
78        disp ( eMix->mean(), zeros ( 2 ), Smp );
[32]79
[125]80        cout << "======= MEpdf ======== " << endl;
[504]81        mepdf meMix ( eMix );
[104]82
[477]83        Smp = meMix.samplecond_m ( mu0, N );
[504]84        disp ( eMix->mean(), zeros ( 2 ), Smp );
[477]85
[125]86        cout << "======= MMix ======== " << endl;
[270]87        mmix mMix;
[488]88        Array<shared_ptr<mpdf> > mComs ( 2 );
[504]89
90        // emix::set_parameters requires the first mpdf to be named
91        mG->set_rv(x);
92        mG->set_rvc(y);
93        mComs ( 0 ) = mG;
94
95        eN->set_mu ( vec_2 ( 0.0, 0.0 ) );
96        shared_ptr<mepdf> mEnorm = new mepdf ( eN );
97        mComs ( 1 ) = mEnorm;
[477]98        mMix.set_parameters ( vec_2 ( 0.5, 0.5 ), mComs );
[125]99
[477]100        Smp = mMix.samplecond_m ( mu0, N );
[504]101        disp ( 0.5 * eN->mean() + 0.4 * eG->mean(), zeros ( 2 ), Smp );
[477]102
[145]103        cout << "======= EProd ======== " << endl;
[504]104        // we have to change eG->rv to y
105        eN->set_rv ( x );
106        eG->set_rv ( y );
[477]107        //create array
108        Array<mpdf*> A ( 2 );
[504]109        mepdf meN ( eN.get() );
110        mepdf meG ( eG.get() );
[477]111        A ( 0 ) = &meN;
112        A ( 1 ) = &meG;
113
114        mprod eP ( A );
115        mat epV = zeros ( 4, 4 );
116        epV.set_submatrix ( 0, 0, V0 );
117        epV.set_submatrix ( 2, 2, diag ( g_var ) );
118
119        vec v0 = vec ( 0 );
120        Smp = eP.samplecond ( v0, N );
[504]121        disp ( concat ( eN->mean(), eG->mean() ), epV, Smp );
[477]122
[294]123        cout << "======= eWishart ======== " << endl;
[477]124        mat wM = "1.0 0.9; 0.9 1.0";
125        eWishartCh eW;
126        eW.set_parameters ( wM / 100, 100 );
127        mat mea = zeros ( 2, 2 );
128        mat Ch ( 2, 2 );
129        for ( int i = 0; i < 100; i++ ) {
130                Ch = eW.sample_mat();
131                mea += Ch.T() * Ch;
132        }
133        cout << mea / 100 << endl;
134
[294]135        cout << "======= rwiWishart ======== " << endl;
[477]136        rwiWishartCh rwW;
137        rwW.set_parameters ( 2, 0.1, "1 1", 0.9 );
138        mea = zeros ( 2, 2 );
139        mat wMch = chol ( wM );
140        for ( int i = 0; i < 100; i++ ) {
141                vec tmp = rwW.samplecond ( vec ( wMch._data(), 4 ) );
142                copy_vector ( 4, tmp._data(), Ch._data() );
143                mea += Ch.T() * Ch;
[294]144        }
[477]145        cout << mea / 100 << endl;
[13]146        //Exit program:
147        return 0;
148
149}
Note: See TracBrowser for help on using the browser.