root/library/tests/enorm_test.cpp @ 394

Revision 394, 2.0 kB (checked in by mido, 15 years ago)

1) UI_File renamed to UIFile
2) part UI's documentation
3) stat/mixtures.h renamed to stat/emix.h and related changes applied

  • 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        cout << "Testing enorm(2,2)"<<endl;
31        // Setup model
32        vec mu ( "1.1 -1" );
33        ldmat R ( mat ( "1 -0.5; -0.5 2" ) );
34
35        RV x ( "{x }" );
36        RV y ( "{y }" );
37
38        enorm<ldmat> E;
39        E.set_rv(concat(x,y));
40        E.set_parameters ( mu,R );
41        cout << "enorm mean value:" << E.mean() <<endl;
42        cout << "enorm mean variance:" << R.to_mat() <<endl;
43        cout << "enorm normalizing constant:" << E.lognc() <<endl;
44        cout << " Numerically integrates to [1.0]: " << normcoef ( &E, vec ( "-5 5" ), vec ( "-5 5" ) ) << endl;
45
46        int N=1000;
47        vec ll ( N );
48        mat Smp=E.sample ( 1000 );
49        vec Emu = sum ( Smp,2 ) /N;
50        mat Er = ( Smp*Smp.transpose() ) /N - outer_product ( Emu,Emu );
51
52        cout << "original empirical mean: " <<Emu <<endl;
53        cout << "original empirical variance: " <<Er <<endl;
54
55        epdf* Mg = E.marginal ( y );
56        mpdf* Cn = E.condition ( x );
57
58
59        cout<< "marginal mean: " << Mg->mean() <<endl;
60
61        cout << "========== putting them back together ======= "<<endl;
62        mepdf mMg ( Mg );
63        Array<mpdf*> A ( 2 );
64        A ( 0 ) = Cn;
65        A ( 1 ) = &mMg;
66        mprod mEp ( A );
67
68        Smp=mEp.samplecond ( vec ( 0 ),1000 );
69        Emu = sum ( Smp,2 ) /N;
70        Er = ( Smp*Smp.transpose() ) /N - outer_product ( Emu,Emu );
71
72        cout << "composite mean: " <<Emu <<endl;
73        cout << "composite variance: " <<Er <<endl;
74       
75        cout << endl << " test of pdflog at zero "<<endl;
76        cout << "original: " << E.evallog(vec("0 0")) <<endl;
77        cout << "composite: " << mEp.evallogcond(vec("0 0"),vec(0)) << endl;
78}
Note: See TracBrowser for help on using the browser.