[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 | |
---|
[213] | 12 | /*! The purpose of this experiment is to test merging fragmental pdfs between two participants sharing the same parameter, a. However, this parameter has a different role in each model. |
---|
| 13 | |
---|
| 14 | Lets assume that: |
---|
| 15 | u -> |a,r| -> y -> |a,r| -> z |
---|
| 16 | */ |
---|
| 17 | |
---|
[204] | 18 | int main() { |
---|
| 19 | // Setup model |
---|
| 20 | RV y ( "{y }" ); |
---|
[211] | 21 | RV u ( "{u }" ); |
---|
| 22 | RV z ( "{z }" ); |
---|
[213] | 23 | RV a ("{a }"); |
---|
| 24 | RV b ("{b }"); RV ab = a; ab.add(b); |
---|
| 25 | RV c ("{c }"); RV ac = a; ac.add(c); |
---|
[211] | 26 | RV r ("{r }"); |
---|
| 27 | |
---|
| 28 | double at = 1.5; |
---|
[213] | 29 | double bt = 0.5; |
---|
| 30 | double ct = -0.5; |
---|
| 31 | double rt = 0.30; |
---|
[204] | 32 | // Full system |
---|
[211] | 33 | vec thy =vec_2 ( at,bt ); //Simulated system - zero for constant term |
---|
[213] | 34 | vec thz =vec_2 ( at,ct ); //Simulated system - zero for constant term |
---|
| 35 | vec Thy = concat ( thy, vec_1(rt) ); //Full parameter |
---|
| 36 | vec Thz = concat ( thz, vec_1(rt) ); //Full parameter |
---|
[204] | 37 | |
---|
| 38 | //ARX constructor |
---|
[211] | 39 | mat V0 = 0.001*eye ( 3 ); V0 ( 0,0 ) = 1; // |
---|
[204] | 40 | |
---|
[213] | 41 | ARX P1 ( concat ( ab,r ), V0, -1 ); |
---|
| 42 | ARX P2 ( concat ( ac,r ), V0, -1 ); |
---|
[204] | 43 | |
---|
| 44 | //Test estimation |
---|
| 45 | int ndat = 100; |
---|
| 46 | int t; |
---|
| 47 | |
---|
| 48 | // Logging |
---|
[213] | 49 | dirfilelog L ( "exp/merg_2a",3 ); |
---|
| 50 | int Li_Data = L.add ( RV ( "{U Y Z }" ), "" ); |
---|
| 51 | // int Li_LL = L.add ( RV ( "{P1 P2 M1 M2 }" ), "LL" ); |
---|
| 52 | int Li_P1m = L.add ( concat ( ab,r ), "P1m" ); |
---|
| 53 | int Li_P2m = L.add ( concat ( ac,r ), "P2m" ); |
---|
| 54 | int Li_Mm = L.add ( concat ( ab,concat(r,c) ), "Mm" ); |
---|
[204] | 55 | L.init(); |
---|
| 56 | |
---|
[213] | 57 | vec Ut ( ndat ); |
---|
[204] | 58 | vec Yt ( ndat ); |
---|
[213] | 59 | vec Zt ( ndat ); |
---|
[204] | 60 | vec yt ( 1 ); |
---|
| 61 | |
---|
| 62 | //Proposal |
---|
[213] | 63 | enorm<ldmat> g0 ( ab ); g0.set_parameters ( "1 1 ",mat ( "1 0; 0 1" ) ); |
---|
[204] | 64 | egamma g1 ( r ); g1.set_parameters ( "2 ", "2" ); |
---|
[213] | 65 | enorm<ldmat> g2 ( c ); g2.set_parameters ( "1 ",mat ( "1" ) ); |
---|
[204] | 66 | |
---|
[213] | 67 | Array<const epdf*> A ( 3 ); A ( 0 ) = &g0; A ( 1 ) =&g1; A(2) = &g2; |
---|
[205] | 68 | eprod G0 ( A ); |
---|
| 69 | |
---|
[213] | 70 | vec rgru(2); |
---|
| 71 | vec rgry(2); |
---|
| 72 | Yt(0) = 0.1; |
---|
| 73 | Ut(0) = 0.0; |
---|
| 74 | for ( t=1; t<ndat; t++ ) { |
---|
[204] | 75 | // True system |
---|
[213] | 76 | Ut ( t ) = pow ( sin ( ( t/40.0 ) *pi ),3 ); |
---|
| 77 | rgru(0) = Ut(t); rgru(1) = Ut(t-1); |
---|
| 78 | Yt ( t ) = thy*rgru + rt * NorRNG(); |
---|
| 79 | rgry(0) = Yt(t); rgry(1) = Yt(t-1); |
---|
| 80 | Zt ( t ) = thz*rgry + rt * NorRNG(); |
---|
[204] | 81 | |
---|
| 82 | // Bayes for all |
---|
[213] | 83 | P1.bayes ( concat ( Yt ( t ),rgru ) ); |
---|
| 84 | P2.bayes ( concat ( Zt ( t ),rgry ) ); |
---|
[204] | 85 | |
---|
| 86 | // Merge estimates |
---|
[205] | 87 | mepdf eG1 ( P1._e() ); |
---|
| 88 | mepdf eG2 ( P2._e() ); |
---|
[204] | 89 | Array<mpdf*> A ( 2 ); A ( 0 ) =&eG1;A ( 1 ) =&eG2; |
---|
| 90 | merger M ( A ); |
---|
[211] | 91 | M.set_parameters ( 10, 100,3 ); //M._Mix().set_method(QB); |
---|
[205] | 92 | //M2.set_parameters ( 100.0, 1000,3 ); //M2._Mix().set_method(QB); |
---|
[204] | 93 | M.merge ( &G0 ); |
---|
| 94 | |
---|
| 95 | //Likelihood |
---|
| 96 | yt ( 0 ) = Yt ( t ); |
---|
| 97 | |
---|
| 98 | //Logger |
---|
[213] | 99 | L.logit(Li_Data, vec_3(Ut(t), Yt(t), Zt(t))); |
---|
| 100 | L.logit(Li_P1m, P1._e()->mean()); |
---|
| 101 | L.logit(Li_P2m, P2._e()->mean()); |
---|
| 102 | L.logit(Li_Mm, M.mean()); |
---|
[204] | 103 | L.step ( ); |
---|
[205] | 104 | |
---|
[204] | 105 | } |
---|
| 106 | L.finalize( ); |
---|
[213] | 107 | L.itsave ( "merg_2a.it" ); |
---|
[205] | 108 | cout << endl; |
---|
[204] | 109 | } |
---|