root/mpdm/merg_2a.cpp @ 288

Revision 288, 3.8 kB (checked in by smidl, 15 years ago)

experiments

  • Property svn:eol-style set to native
Line 
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>
6using namespace bdm;
7
8//These lines are needed for use of cout and endl
9using std::cout;
10using std::endl;
11
12/*! \file
13Experiment for distributed identification using log-normal merging
14
15The 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.
16
17Lets assume that:
18\dot
19digraph compart{
20
21  edge [fontname="FreeSans",fontsize=10,labelfontname="FreeSans",labelfontsize=10];
22  node [fontname="FreeSans",fontsize=10,shape=record];
23
24rankdir=LR;
25
26 U [label="u",height=0.2,width=0.4,color="white", fillcolor="white", style="filled" fontcolor="black"];
27 AR1 [label="a,b,r",height=0.2,width=0.4,color="black", fillcolor="white", style="filled" fontcolor="black"]
28 U -> AR1 [color="midnightblue",style="solid"];
29 AR2 [label="a,c,r",height=0.2,width=0.4,color="black", fillcolor="white", style="filled" fontcolor="black"]
30 AR1 -> AR2 [color="midnightblue",style="solid",label="y"];
31 Z [label="z",height=0.2,width=0.4,color="white", fillcolor="white", style="filled" fontcolor="black"];
32 AR2 -> Z [color="midnightblue",style="solid"];
33
34 
35}
36\enddot
37*/
38
39int main() {
40        // Setup model
41        RV y ( "{y }" );
42        RV u ( "{u }" );
43        RV z ( "{z }" );
44        RV a ("{a }"); 
45        RV b ("{b }"); RV ab = a; ab.add(b);
46        RV c ("{c }"); RV ac = a; ac.add(c);
47        RV r ("{r }");
48       
49        double at = 1.5;
50        double bt = 0.5;
51        double ct = -0.5;
52        double rt = 0.30;
53        // Full system
54        vec thy =vec_2 ( at,bt ); //Simulated system - zero for constant term
55        vec thz =vec_2 ( at,ct ); //Simulated system - zero for constant term
56        vec Thy = concat ( thy, vec_1(rt) ); //Full parameter
57        vec Thz = concat ( thz, vec_1(rt) ); //Full parameter
58
59        //ARX constructor
60        mat V0 = 0.001*eye ( 3 ); V0 ( 0,0 ) = 1; //
61
62        ARX P1; P1.set_rv(concat(ab,r));
63        P1.set_statistics(1, V0, -1 );
64        ARX P2; P2.set_rv(concat(ac,r));
65         P2.set_statistics(1, V0, -1 );
66
67        //Test estimation
68        int ndat = 100;
69        int t;
70
71        // Logging
72        dirfilelog L ( "exp/merg_2a",3 );
73        int Li_Data = L.add ( RV ( "{U Y Z }" ), "" );
74//      int Li_LL   = L.add ( RV ( "{P1 P2 M1 M2 }" ), "LL" );
75        int Li_P1m   = L.add ( concat ( ab,r ), "P1m" );
76        int Li_P2m   = L.add ( concat ( ac,r ), "P2m" );
77        int Li_Mm   = L.add ( concat ( ab,concat(r,c) ), "Mm" );
78        int Li_Th   = L.add ( concat ( ab,concat(c,r) ), "T" );
79        L.init();
80
81        vec Ut ( ndat );
82        vec Yt ( ndat );
83        vec Zt ( ndat );
84        vec yt ( 1 );
85
86        //Proposal
87        enorm<ldmat> g0; g0.set_rv(ab); 
88        g0.set_parameters ( "1 1 ",mat ( "1 0; 0 1" ) );
89        egamma g1;g1.set_rv(r); 
90        g1.set_parameters ( "2  ", "2" );
91        enorm<ldmat> g2; g2.set_rv(c); 
92        g2.set_parameters ( "1 ",mat ( "1" ) );
93
94        Array<const epdf*> A ( 3 ); A ( 0 ) = &g0; A ( 1 ) =&g1; A(2) = &g2; 
95        eprod G0; G0.set_parameters ( A );
96
97        vec rgru(2);
98        vec rgry(2);
99        Yt(0) = 0.1;
100        Ut(0) = 0.0;
101        for ( t=1; t<ndat; t++ ) {
102                // True system
103                Ut ( t ) = pow ( sin ( ( t/40.0 ) *pi ),3 );
104                rgru(0) = Ut(t); rgru(1) = Ut(t-1);
105                Yt ( t ) = thy*rgru + rt * NorRNG();
106                rgry(0) = Yt(t); rgry(1) = Yt(t-1);
107                Zt ( t ) = thz*rgry + rt * NorRNG();
108
109                // Bayes for all
110                P1.bayes ( concat ( Yt ( t ),rgru ) );
111                P2.bayes ( concat ( Zt ( t ),rgry ) );
112
113                // Merge estimates
114                mepdf eG1 ( P1._e() );
115                mepdf eG2 ( P2._e() );
116                Array<mpdf*> A ( 2 ); A ( 0 ) =&eG1;A ( 1 ) =&eG2;
117                merger M ( A );
118                M.set_parameters ( 10, 100,3 ); //M._Mix().set_method(QB);
119                //M2.set_parameters ( 100.0, 1000,3 ); //M2._Mix().set_method(QB);
120                M.merge ( &G0 );
121
122                //Likelihood
123                yt ( 0 ) = Yt ( t );
124
125                //Logger
126                L.logit(Li_Data, vec_3(Ut(t), Yt(t), Zt(t)));
127                L.logit(Li_P1m, P1._e()->mean());
128                L.logit(Li_P2m, P2._e()->mean());
129                L.logit(Li_Mm, M.mean());
130                L.logit(Li_Th, concat(thy,vec_2(ct,rt*rt)));
131                L.step ( );
132
133        }
134        L.finalize( );
135        L.itsave ( "merg_2a.it" );
136        cout << endl;
137}
Note: See TracBrowser for help on using the browser.