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