root/library/bdm/stat/emix.cpp @ 394

Revision 394, 6.0 kB (checked in by mido, 15 years ago)

1) UI_File renamed to UIFile
2) part UI's documentation
3) stat/mixtures.h renamed to stat/emix.h and related changes applied

  • Property svn:eol-style set to native
Line 
1#include "emix.h"
2
3namespace bdm{
4
5void emix::set_parameters ( const vec &w0, const Array<epdf*> &Coms0, bool copy ) {
6        w = w0/sum ( w0 );
7        dim = Coms0(0)->dimension();
8        int i;
9        for ( i=0;i<w.length();i++ ) {
10                it_assert_debug ( dim== ( Coms0 ( i )->dimension() ),"Component sizes do not match!" );
11        }
12        if ( copy ) {
13                Coms.set_length(Coms0.length());
14                for ( i=0;i<w.length();i++ ) {it_error("Not imp...");
15                        *Coms ( i ) =*Coms0 ( i );}
16                destroyComs=true;
17        }
18        else {
19                Coms = Coms0;
20                destroyComs=false;
21        }
22}
23
24vec emix::sample() const {
25        //Sample which component
26        vec cumDist = cumsum ( w );
27        double u0;
28        #pragma omp critical
29        u0 = UniRNG.sample();
30
31        int i=0;
32        while ( ( cumDist ( i ) <u0 ) && ( i< ( w.length()-1 ) ) ) {i++;}
33
34        return Coms ( i )->sample();
35}
36
37emix* emix::marginal(const RV &rv) const{
38        it_assert_debug(isnamed(), "rvs are not assigned");
39                       
40        Array<epdf*> Cn(Coms.length());
41        for(int i=0;i<Coms.length();i++){Cn(i)=Coms(i)->marginal(rv);}
42        emix* tmp = new emix();
43        tmp->set_parameters(w,Cn,false);
44        tmp->ownComs();
45        return tmp;
46}
47
48mratio* emix::condition(const RV &rv) const{
49        it_assert_debug(isnamed(), "rvs are not assigned");
50        return new mratio(this,rv);
51};
52
53void egiwmix::set_parameters ( const vec &w0, const Array<egiw*> &Coms0, bool copy ) {
54        w = w0/sum ( w0 );
55        dim = Coms0(0)->dimension();
56        int i;
57        for ( i=0;i<w.length();i++ ) {
58                it_assert_debug ( dim== ( Coms0 ( i )->dimension() ),"Component sizes do not match!" );
59        }
60        if ( copy ) {
61                Coms.set_length(Coms0.length());
62                for ( i=0;i<w.length();i++ ) {it_error("Not imp...");
63                        *Coms ( i ) =*Coms0 ( i );}
64                destroyComs=true;
65        }
66        else {
67                Coms = Coms0;
68                destroyComs=false;
69        }
70}
71
72vec egiwmix::sample() const {
73        //Sample which component
74        vec cumDist = cumsum ( w );
75        double u0;
76        #pragma omp critical
77        u0 = UniRNG.sample();
78
79        int i=0;
80        while ( ( cumDist ( i ) <u0 ) && ( i< ( w.length()-1 ) ) ) {i++;}
81
82        return Coms ( i )->sample();
83}
84
85vec egiwmix::mean() const {
86        int i; vec mu = zeros ( dim );
87        for ( i = 0;i < w.length();i++ ) {mu += w ( i ) * Coms ( i )->mean(); }
88        return mu;
89}
90
91vec egiwmix::variance() const {
92        // non-central moment
93        vec mom2 = zeros ( dim );
94        for ( int i = 0;i < w.length();i++ ) {
95                // pow is overloaded, we have to use another approach
96                mom2 += w ( i ) * (Coms(i)->variance() + elem_mult ( Coms(i)->mean(), Coms(i)->mean() )); 
97        }
98        // central moment
99        // pow is overloaded, we have to use another approach
100        return mom2 - elem_mult( mean(), mean() );
101}
102
103emix* egiwmix::marginal(const RV &rv) const{
104        it_assert_debug(isnamed(), "rvs are not assigned");
105                       
106        Array<epdf*> Cn(Coms.length());
107        for(int i=0;i<Coms.length();i++){Cn(i)=Coms(i)->marginal(rv);}
108        emix* tmp = new emix();
109        tmp->set_parameters(w,Cn,false);
110        tmp->ownComs();
111        return tmp;
112}
113
114egiw*   egiwmix::approx() {
115        // NB: dimx == 1 !!!
116        // The following code might look a bit spaghetti-like,
117        // consult Dedecius, K. et al.: Partial forgetting in AR models.
118
119        double sumVecCommon;                            // common part for many terms in eq.
120        int len = w.length();                           // no. of mix components       
121        int dimLS = Coms(1)->_V()._D().length() - 1;    // dim of LS
122        vec vecNu(len);                                 // vector of dfms of components
123        vec vecD(len);                                  // vector of LS reminders of comps.
124        vec vecCommon(len);                             // vector of common parts
125        mat matVecsTheta;                               // matrix which rows are theta vects.
126
127        // fill in the vectors vecNu, vecD and matVecsTheta
128        for ( int i=0; i<len; i++ ) {
129                vecNu.shift_left( Coms(i)->_nu() );
130                vecD.shift_left( Coms(i)->_V()._D()(0) );
131                matVecsTheta.append_row( Coms(i)->est_theta()  );
132        }
133
134        // calculate the common parts and their sum
135        vecCommon = elem_mult ( w, elem_div(vecNu, vecD) );
136        sumVecCommon = sum(vecCommon);
137
138        // LS estimator of theta
139        vec aprEstTheta(dimLS);  aprEstTheta.zeros();
140        for ( int i=0; i<len; i++ ) {
141                aprEstTheta +=  matVecsTheta.get_row( i ) * vecCommon ( i );
142        }
143        aprEstTheta /= sumVecCommon;
144       
145       
146        // LS estimator of dfm
147        double aprNu;
148        double A = log( sumVecCommon );         // Term 'A' in equation
149
150        for ( int i=0; i<len; i++ ) {
151                A += w(i) * ( log( vecD(i) ) - psi( 0.5 * vecNu(i) ) );
152        }
153
154        aprNu = ( 1 + sqrt(1 + 2 * (A - LOG2)/3 ) ) / ( 2 * (A - LOG2) );
155
156
157        // LS reminder (term D(0,0) in C-syntax)
158        double aprD = aprNu / sumVecCommon;
159
160        // Aproximation of cov
161        // the following code is very numerically sensitive, thus
162        // we have to eliminate decompositions etc. as much as possible
163        mat aprC = zeros(dimLS, dimLS);
164        for ( int i=0; i<len; i++ ) {
165                aprC += Coms(i)->est_theta_cov().to_mat() * w(i); 
166                vec tmp = ( matVecsTheta.get_row(i) - aprEstTheta );
167                aprC += vecCommon(i) * outer_product( tmp, tmp);
168        }
169
170        // Construct GiW pdf :: BEGIN
171        ldmat aprCinv ( inv(aprC) );
172        vec D = concat( aprD, aprCinv._D() );
173        mat L = eye(dimLS+1);
174        L.set_submatrix(1,0, aprCinv._L() * aprEstTheta);
175        L.set_submatrix(1,1, aprCinv._L());
176        ldmat aprLD (L, D);
177
178        egiw* aprgiw = new egiw(1, aprLD, aprNu);
179        return aprgiw;
180};
181
182}
183// mprod::mprod ( Array<mpdf*> mFacs, bool overlap) : mpdf ( RV(), RV() ), n ( mFacs.length() ), epdfs ( n ), mpdfs ( mFacs ), rvinds ( n ), rvcinrv ( n ), irvcs_rvc ( n ) {
184//              int i;
185//              bool rvaddok;
186//              // Create rv
187//              for ( i = 0;i < n;i++ ) {
188//                      rvaddok=rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs.
189//                      // If rvaddok==false, mpdfs overlap => assert error.
190//                      it_assert_debug(rvaddok||overlap,"mprod::mprod() input mpdfs overlap in rv!");
191//                      epdfs ( i ) = & ( mpdfs ( i )->posterior() ); // add pointer to epdf
192//              };
193//              // Create rvc
194//              for ( i = 0;i < n;i++ ) {
195//                      rvc.add ( mpdfs ( i )->_rvc().subt ( rv ) ); //add rv to common rvs.
196//              };
197//
198// //           independent = true;
199//              //test rvc of mpdfs and fill rvinds
200//              for ( i = 0;i < n;i++ ) {
201//                      // find ith rv in common rv
202//                      rvsinrv ( i ) = mpdfs ( i )->_rv().dataind ( rv );
203//                      // find ith rvc in common rv
204//                      rvcinrv ( i ) = mpdfs ( i )->_rvc().dataind ( rv );
205//                      // find ith rvc in common rv
206//                      irvcs_rvc ( i ) = mpdfs ( i )->_rvc().dataind ( rvc );
207//                      //
208// /*                   if ( rvcinrv ( i ).length() >0 ) {independent = false;}
209//                      if ( irvcs_rvc ( i ).length() >0 ) {independent = false;}*/
210//              }
211//      };
Note: See TracBrowser for help on using the browser.