[204] | 1 | #include <estim/arx.h> |
---|
| 2 | #include <estim/merger.h> |
---|
| 3 | #include <stat/libEF.h> |
---|
| 4 | #include <stat/loggers.h> |
---|
| 5 | //#include <stat/merger.h> |
---|
| 6 | using namespace itpp; |
---|
| 7 | |
---|
| 8 | //These lines are needed for use of cout and endl |
---|
| 9 | using std::cout; |
---|
| 10 | using std::endl; |
---|
| 11 | |
---|
| 12 | int main() { |
---|
| 13 | // Setup model |
---|
| 14 | RV y ( "{y }" ); |
---|
| 15 | RV u1 ( "{u1 }" ); |
---|
| 16 | RV u2 ( "{u2 }" ); |
---|
| 17 | RV uu=u1; uu.add ( u2 ); |
---|
| 18 | |
---|
| 19 | // Full system |
---|
| 20 | vec thg ( "1 1" ); //Simulated system - zero for constant term |
---|
| 21 | double sqr=1.0; |
---|
| 22 | vec Th = concat ( thg, sqr ); //Full parameter |
---|
| 23 | |
---|
| 24 | // Estimated systems ARX(2) |
---|
| 25 | RV a1 ( "{a1 }" ); |
---|
| 26 | RV a2 ( "{a2 }" ); |
---|
| 27 | RV r ( "{r }" ); |
---|
| 28 | RV all =a1; all.add ( a2 ); all.add ( r ); |
---|
| 29 | RV allj =a1; allj.add ( r ); allj.add ( a2 ); |
---|
| 30 | vec Thj("1 1 1"); |
---|
| 31 | // Setup values |
---|
| 32 | |
---|
| 33 | //ARX constructor |
---|
| 34 | mat V0 = 0.001*eye ( 2 ); V0 ( 0,0 ) = 1; // |
---|
| 35 | mat V0g = 0.001*eye ( 3 ); V0g ( 0,0 ) = 1; // |
---|
| 36 | |
---|
| 37 | ARX P1 ( concat ( a1,r ), V0, -1 ); |
---|
| 38 | ARX P2 ( concat ( a2,r ), V0, -1 ); |
---|
| 39 | ARX PG ( all, V0g, -1 ); |
---|
| 40 | |
---|
| 41 | //Test estimation |
---|
| 42 | int ndat = 100; |
---|
| 43 | int t; |
---|
| 44 | |
---|
| 45 | // Logging |
---|
| 46 | dirfilelog L ( "exp/merg_giw",1 ); |
---|
| 47 | int Li_Data = L.add ( RV ( "{Y U1 U2 }" ), "" ); |
---|
| 48 | int Li_LL = L.add ( RV ( "{1 2 G M }" ), "LL" ); |
---|
| 49 | int Li_Gm = L.add ( RV ( "{a1 a2 r }" ), "G" ); |
---|
| 50 | int Li_Mm = L.add ( RV ( "{a1 r a2 }" ), "M" ); |
---|
| 51 | L.init(); |
---|
| 52 | |
---|
| 53 | vec Yt ( ndat ); |
---|
| 54 | vec yt ( 1 ); |
---|
| 55 | |
---|
| 56 | vec LLs ( 4 ); |
---|
| 57 | vec rgrg ( 2 ); |
---|
| 58 | |
---|
| 59 | //Proposal |
---|
| 60 | enorm<ldmat> g0 ( a1 ); g0.set_parameters ( "1 ",mat("1") ); |
---|
| 61 | egamma g1 ( r ); g1.set_parameters ( "2 ", "2" ); |
---|
| 62 | enorm<ldmat> g2 ( a2 ); g2.set_parameters ( "1 ",mat("1") ); |
---|
| 63 | |
---|
| 64 | Array<const epdf*> A(3); A(0) = &g0; A(1)=&g1; A(2) = &g2; |
---|
| 65 | eprod G0(A); |
---|
| 66 | |
---|
| 67 | for ( t=0; t<ndat; t++ ) { |
---|
| 68 | // True system |
---|
| 69 | rgrg ( 0 ) = pow ( sin ( ( t/40.0 ) *pi ),3 ); |
---|
| 70 | rgrg ( 1 ) = pow ( cos ( ( t/40.0 ) *pi ),3 ); |
---|
| 71 | |
---|
| 72 | Yt ( t ) = thg*rgrg + sqr * NorRNG(); |
---|
| 73 | |
---|
| 74 | // Bayes for all |
---|
| 75 | P1.bayes ( concat ( Yt ( t ),vec_1(rgrg ( 0 ) )) ); |
---|
| 76 | P2.bayes ( concat ( Yt ( t ),vec_1(rgrg ( 1 ) )) ); |
---|
| 77 | PG.bayes ( concat ( Yt ( t ),rgrg ) ); |
---|
| 78 | |
---|
| 79 | // Merge estimates |
---|
| 80 | mepdf eG1(P1._e()); |
---|
| 81 | mepdf eG2(P2._e()); |
---|
| 82 | Array<mpdf*> A ( 2 ); A ( 0 ) =&eG1;A ( 1 ) =&eG2; |
---|
| 83 | merger M ( A ); |
---|
| 84 | M.set_parameters ( 10.0, 100,2 ); |
---|
| 85 | M.merge ( &G0 ); |
---|
| 86 | |
---|
| 87 | //Likelihood |
---|
| 88 | yt ( 0 ) = Yt ( t ); |
---|
| 89 | |
---|
| 90 | LLs ( 0 ) = P1._e()->evalpdflog ( get_vec(Th, "1 2") ); |
---|
| 91 | LLs ( 1 ) = P2._e()->evalpdflog ( get_vec(Th, "3 2") ); |
---|
| 92 | LLs ( 2 ) = PG._e()->evalpdflog (Th ); |
---|
| 93 | LLs ( 3 ) = M._Mix().logpred ( concat(Thj, vec_1(1.0)) ); |
---|
| 94 | L.logit ( Li_LL, LLs ); //log-normal |
---|
| 95 | |
---|
| 96 | //Logger |
---|
| 97 | L.logit ( Li_Data, vec_3 ( Yt ( t ), rgrg ( 0 ), rgrg ( 1 ) ) ); |
---|
| 98 | L.logit ( Li_Gm, PG._e()->mean() ); |
---|
| 99 | emix *tm =M._Mix().predictor(allj); |
---|
| 100 | L.logit ( Li_Mm, tm->mean() ); |
---|
| 101 | delete tm; |
---|
| 102 | L.step ( ); |
---|
| 103 | } |
---|
| 104 | L.finalize( ); |
---|
| 105 | L.itsave ( "merg_egiw.it" ); |
---|
| 106 | } |
---|