root/library/tests/emix_test.cpp @ 461

Revision 461, 2.6 kB (checked in by vbarta, 15 years ago)

mpdf (& its dependencies) reformat: now using shared_ptr, moved virtual method bodies to .cpp

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