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
RevLine 
[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>
[254]6using namespace bdm;
[204]7
8//These lines are needed for use of cout and endl
9using std::cout;
10using std::endl;
11
[288]12/*! \file
13Experiment for distributed identification using log-normal merging
[213]14
[288]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
[213]17Lets assume that:
[288]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
[213]37*/
38
[204]39int main() {
40        // Setup model
41        RV y ( "{y }" );
[211]42        RV u ( "{u }" );
43        RV z ( "{z }" );
[213]44        RV a ("{a }"); 
45        RV b ("{b }"); RV ab = a; ab.add(b);
46        RV c ("{c }"); RV ac = a; ac.add(c);
[211]47        RV r ("{r }");
48       
49        double at = 1.5;
[213]50        double bt = 0.5;
51        double ct = -0.5;
52        double rt = 0.30;
[204]53        // Full system
[211]54        vec thy =vec_2 ( at,bt ); //Simulated system - zero for constant term
[213]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
[204]58
59        //ARX constructor
[211]60        mat V0 = 0.001*eye ( 3 ); V0 ( 0,0 ) = 1; //
[204]61
[288]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 );
[204]66
67        //Test estimation
68        int ndat = 100;
69        int t;
70
71        // Logging
[213]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" );
[217]78        int Li_Th   = L.add ( concat ( ab,concat(c,r) ), "T" );
[204]79        L.init();
80
[213]81        vec Ut ( ndat );
[204]82        vec Yt ( ndat );
[213]83        vec Zt ( ndat );
[204]84        vec yt ( 1 );
85
86        //Proposal
[288]87        enorm<ldmat> g0; g0.set_rv(ab); 
[270]88        g0.set_parameters ( "1 1 ",mat ( "1 0; 0 1" ) );
[288]89        egamma g1;g1.set_rv(r); 
[270]90        g1.set_parameters ( "2  ", "2" );
[288]91        enorm<ldmat> g2; g2.set_rv(c); 
[270]92        g2.set_parameters ( "1 ",mat ( "1" ) );
[204]93
[213]94        Array<const epdf*> A ( 3 ); A ( 0 ) = &g0; A ( 1 ) =&g1; A(2) = &g2; 
[288]95        eprod G0; G0.set_parameters ( A );
[205]96
[213]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++ ) {
[204]102                // True system
[213]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();
[204]108
109                // Bayes for all
[213]110                P1.bayes ( concat ( Yt ( t ),rgru ) );
111                P2.bayes ( concat ( Zt ( t ),rgry ) );
[204]112
113                // Merge estimates
[205]114                mepdf eG1 ( P1._e() );
115                mepdf eG2 ( P2._e() );
[204]116                Array<mpdf*> A ( 2 ); A ( 0 ) =&eG1;A ( 1 ) =&eG2;
117                merger M ( A );
[211]118                M.set_parameters ( 10, 100,3 ); //M._Mix().set_method(QB);
[205]119                //M2.set_parameters ( 100.0, 1000,3 ); //M2._Mix().set_method(QB);
[204]120                M.merge ( &G0 );
121
122                //Likelihood
123                yt ( 0 ) = Yt ( t );
124
125                //Logger
[213]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());
[288]130                L.logit(Li_Th, concat(thy,vec_2(ct,rt*rt)));
[204]131                L.step ( );
[205]132
[204]133        }
134        L.finalize( );
[213]135        L.itsave ( "merg_2a.it" );
[205]136        cout << endl;
[204]137}
Note: See TracBrowser for help on using the browser.