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

Revision 488, 6.8 kB (checked in by smidl, 15 years ago)

changes in mpdf -> compile OK, broken tests!

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