root/library/tests/emix_test.cpp @ 454

Revision 454, 2.6 kB (checked in by smidl, 15 years ago)

configurations for epdf tests & minor fixes

  • Property svn:eol-style set to native
Line 
1#include "stat/exp_family.h"
2#include "stat/emix.h"
3using namespace bdm;
4
5//These lines are needed for use of cout and endl
6using std::cout;
7using std::endl;
8
9double normcoef ( const epdf* ep,const vec &xb, const vec &yb, int Ngr=100 ) {
10        mat PPdf ( Ngr+1,Ngr+1 );
11        vec rgr ( 2 );
12
13        int i=0,j=0;
14        double xstep= ( xb ( 1 )-xb ( 0 ) ) /Ngr;
15        double ystep= ( yb ( 1 )-yb ( 0 ) ) /Ngr;
16
17        for ( double x=xb ( 0 );x<=xb ( 1 );x+= xstep,i++ ) {
18                rgr ( 0 ) =x;j=0;
19                for ( double y=yb ( 0 );y<=yb ( 1 );y+=ystep,j++ ) {
20                        rgr ( 1 ) =y;
21                        PPdf ( i,j ) =exp ( ep->evallog ( rgr ) );
22                }
23        }
24        return sumsum ( PPdf ) *xstep*ystep;
25}
26
27int main() {
28        RNG_randomize();
29
30        RV x ( "{x }" );
31        RV y ( "{y }" );
32        RV xy=concat(x,y);
33       
34        enorm<ldmat> E1; E1.set_rv ( xy );
35               
36        E1.set_parameters( "1.00054 1.0455" , mat ( "0.740142 -0.259015; -0.259015 1.0302" ));
37        enorm<ldmat> E2;E2.set_rv ( xy );
38        E2.set_parameters( "-1.2 -0.1" , mat ( "1 0.4; 0.4 0.5" ));
39
40        Array<epdf*> A1(1);
41        A1(0) = &E1;
42       
43        emix M1; M1.set_rv(xy);
44        M1.set_parameters(vec("1"), A1, false);
45        cout << "======== test if ARX and emix with one ARX are the same ==="<<endl;
46       
47        epdf* Mm = M1.marginal(y);
48        epdf* Am = E1.marginal(y);
49        mpdf* Mc = M1.condition(y);
50        mpdf* Ac = E1.condition(y);
51       
52        cout << *((mlnorm<ldmat>*)Ac) <<endl;
53       
54        cout << "Mix marg at 0: " << Mm->evallog(vec_1(0.0)) <<endl;
55        cout << "ARX marg at 0: " << Am->evallog(vec_1(0.0)) <<endl;
56        cout << "Mix cond at 0,0: " << Mc->evallogcond(vec_1(0.0),vec_1(0.0)) <<endl;
57        cout << "ARX cond at 0,0: " << Ac->evallogcond(vec_1(0.0),vec_1(0.0)) <<endl;
58       
59        cout << "======== Mixture with two components ==="<<endl;
60        Array<epdf*> A2(2);
61        A2(0) = &E1;
62        A2(1) = &E2;
63       
64        emix M2; M2.set_rv(xy);
65        M2.set_parameters(vec("1"), A2, false);
66       
67       
68        cout << "Mixture normalization: " << normcoef(&M2, vec("-3 3 "), vec("-3 3 ")) <<endl;
69       
70        int N=3;
71        vec ll ( N );
72        vec ll2 ( N );
73        mat Smp=M2.sample_m ( N );
74        ll = M2.evallog_m(Smp);
75       
76        vec Emu = sum ( Smp,2 ) /N;
77        mat Er = ( Smp*Smp.transpose() ) /N - outer_product ( Emu,Emu );
78
79        cout << "original empirical mean: " <<Emu <<endl;
80        cout << "original empirical variance: " <<Er <<endl;
81
82        epdf* Mg = (epdf*)M2.marginal ( y );
83        mpdf* Cn = (mpdf*)M2.condition ( x );
84
85        it_file it("emix_test.it");
86        it << Name("Smp") << Smp;
87
88        cout<< "marginal mean: " << Mg->mean() <<endl;
89
90        cout << "========== putting them back together ======= "<<endl;
91        mepdf mMg ( Mg );
92        Array<mpdf*> AA ( 2 );
93        AA ( 0 ) = Cn;
94        AA ( 1 ) = &mMg;
95        mprod mEp ( AA );
96
97        for (int j=0; j<N; j++){
98                ll2(j)=mEp.evallogcond (Smp.get_col(j), vec ( 0 ));
99        }
100        it << Name("ll") << ll;
101        it << Name("ll2") << ll2;
102       
103       
104}
Note: See TracBrowser for help on using the browser.