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 u ( "{u }" ); |
---|
16 | RV um = u; um.t(-1); |
---|
17 | RV z ( "{z }" ); |
---|
18 | RV a ("{a }"); |
---|
19 | RV b ("{b }"); |
---|
20 | RV c ("{c }"); |
---|
21 | RV r ("{r }"); |
---|
22 | |
---|
23 | double at = 1.5; |
---|
24 | double bt = 0.8; |
---|
25 | double ct = 0.50; |
---|
26 | double sig = 0.10; |
---|
27 | // Full system |
---|
28 | vec thy =vec_2 ( at,bt ); //Simulated system - zero for constant term |
---|
29 | vec thu =vec_2 ( at,ct ); //Simulated system - zero for constant term |
---|
30 | vec Thy = concat ( thy, vec_1(sig) ); //Full parameter |
---|
31 | vec Thu = concat ( thu, vec_1(sig) ); //Full parameter |
---|
32 | |
---|
33 | //ARX constructor |
---|
34 | mat V0 = 0.001*eye ( 3 ); V0 ( 0,0 ) = 1; // |
---|
35 | |
---|
36 | ARX P1 ( concat ( a, concat(b,r) ), V0, -1 ); |
---|
37 | ARX P2 ( concat ( a, concat(c,r) ), V0, -1 ); |
---|
38 | |
---|
39 | //Test estimation |
---|
40 | int ndat = 100; |
---|
41 | int t; |
---|
42 | |
---|
43 | // Logging |
---|
44 | dirfilelog L ( "exp/merg_2a",ndat ); |
---|
45 | int Li_Data = L.add ( RV ( "{Y U1 U2 }" ), "" ); |
---|
46 | int Li_LL = L.add ( RV ( "{G M }" ), "LL" ); |
---|
47 | int Li_P1m = L.add ( RV ( "{a b r }" ), "P1" ); |
---|
48 | int Li_P2m = L.add ( RV ( "{a c r }" ), "P2" ); |
---|
49 | L.init(); |
---|
50 | |
---|
51 | vec Yt ( ndat ); |
---|
52 | vec yt ( 1 ); |
---|
53 | |
---|
54 | //Proposal |
---|
55 | enorm<ldmat> g0 ( a1 ); g0.set_parameters ( "1 ",mat ( "1" ) ); |
---|
56 | egamma g1 ( r ); g1.set_parameters ( "2 ", "2" ); |
---|
57 | enorm<ldmat> g2 ( a2 ); g2.set_parameters ( "1 ",mat ( "1" ) ); |
---|
58 | |
---|
59 | Array<const epdf*> A ( 3 ); A ( 0 ) = &g0; A ( 1 ) =&g1; A ( 2 ) = &g2; |
---|
60 | eprod G0 ( A ); |
---|
61 | |
---|
62 | for ( t=0; t<ndat; t++ ) { |
---|
63 | // True system |
---|
64 | rgrg ( 0 ) = pow ( sin ( ( t/40.0 ) *pi ),3 ); |
---|
65 | rgrg ( 1 ) = pow ( cos ( ( t/40.0 +0.1 ) *pi ),3 ); |
---|
66 | |
---|
67 | Yt ( t ) = thg*rgrg + sqr * NorRNG(); |
---|
68 | |
---|
69 | // Bayes for all |
---|
70 | P1.bayes ( concat ( Yt ( t ),vec_1 ( rgrg ( 0 ) ) ) ); |
---|
71 | P2.bayes ( concat ( Yt ( t ),vec_1 ( rgrg ( 1 ) ) ) ); |
---|
72 | PG.bayes ( concat ( Yt ( t ),rgrg ) ); |
---|
73 | |
---|
74 | |
---|
75 | // crippling PGk by substituting zeros. |
---|
76 | /* ldmat &Vk=const_cast<egiw*>(PGk._e())->_V(); //PG ldmat does not like 0! |
---|
77 | mat fVk=PG._e()->_V().to_mat(); |
---|
78 | fVk(1,2) = 0.0; |
---|
79 | fVk(2,1) = 0.0; |
---|
80 | Vk = ldmat(fVk); |
---|
81 | */ //PGk is now krippled |
---|
82 | |
---|
83 | // Merge estimates |
---|
84 | mepdf eG1 ( P1._e() ); |
---|
85 | mepdf eG2 ( P2._e() ); |
---|
86 | Array<mpdf*> A ( 2 ); A ( 0 ) =&eG1;A ( 1 ) =&eG2; |
---|
87 | merger M ( A ); |
---|
88 | M.set_parameters ( 10, 100,3 ); //M._Mix().set_method(QB); |
---|
89 | //M2.set_parameters ( 100.0, 1000,3 ); //M2._Mix().set_method(QB); |
---|
90 | M.merge ( &G0 ); |
---|
91 | //M2.merge ( &G0 ); |
---|
92 | |
---|
93 | //Likelihood |
---|
94 | yt ( 0 ) = Yt ( t ); |
---|
95 | |
---|
96 | // LLs ( 0 ) = P1._e()->evalpdflog ( get_vec(Th, "1 2") ); |
---|
97 | // LLs ( 1 ) = P2._e()->evalpdflog ( get_vec(Th, "3 2") ); |
---|
98 | LLs ( 0 ) = PG._e()->evalpdflog ( Th ); |
---|
99 | LLs ( 1 ) = M._Mix().logpred ( concat ( Thj, vec_1 ( 1.0 ) ) ); |
---|
100 | // LLs ( 3 ) = M2._Mix().logpred ( concat(Thj, vec_1(1.0)) ); |
---|
101 | L.logit ( Li_LL, LLs ); //log-normal |
---|
102 | |
---|
103 | //Logger |
---|
104 | L.logit ( Li_Data, vec_3 ( Yt ( t ), rgrg ( 0 ), rgrg ( 1 ) ) ); |
---|
105 | L.logit ( Li_P1m, P1._e()->mean() ); |
---|
106 | L.logit ( Li_P2m, P2._e()->mean() ); |
---|
107 | L.logit ( Li_Gm, PG._e()->mean() ); |
---|
108 | L.logit ( Li_Mm, M.mean() ); |
---|
109 | |
---|
110 | L.step ( ); |
---|
111 | |
---|
112 | cout << "Vg: " << PG._e()->_V().to_mat() <<endl; |
---|
113 | vec mea = M.mean(); |
---|
114 | cout << "Ve: " << M.variance() <<endl; |
---|
115 | } |
---|
116 | L.finalize( ); |
---|
117 | L.itsave ( "merg_egiw.it" ); |
---|
118 | cout << endl; |
---|
119 | } |
---|