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
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        bool isnamed = Coms0 ( 0 )->isnamed();
9        int i;
10        RV tmp_rv;
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!" );
16        }
17        if ( copy ) {
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 {
25                Coms = Coms0;
26                destroyComs = false;
27        }
28        if ( isnamed ) epdf::set_rv ( tmp_rv ); //coms aer already OK, no need for set_rv
29}
30
31vec emix::sample() const {
32        //Sample which component
33        vec cumDist = cumsum ( w );
34        double u0;
35#pragma omp critical
36        u0 = UniRNG.sample();
37
38        int i = 0;
39        while ( ( cumDist ( i ) < u0 ) && ( i < ( w.length() - 1 ) ) ) {
40                i++;
41        }
42
43        return Coms ( i )->sample();
44}
45
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        }
53        emix* tmp = new emix();
54        tmp->set_parameters ( w, Cn, false );
55        tmp->ownComs();
56        return tmp;
57}
58
59mratio* emix::condition ( const RV &rv ) const {
60        it_assert_debug ( isnamed(), "rvs are not assigned" );
61        return new mratio ( this, rv );
62};
63
64void egiwmix::set_parameters ( const vec &w0, const Array<egiw*> &Coms0, bool copy ) {
65        w = w0 / sum ( w0 );
66        dim = Coms0 ( 0 )->dimension();
67        int i;
68        for ( i = 0; i < w.length(); i++ ) {
69                it_assert_debug ( dim == ( Coms0 ( i )->dimension() ), "Component sizes do not match!" );
70        }
71        if ( copy ) {
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 {
79                Coms = Coms0;
80                destroyComs = false;
81        }
82}
83
84vec egiwmix::sample() const {
85        //Sample which component
86        vec cumDist = cumsum ( w );
87        double u0;
88#pragma omp critical
89        u0 = UniRNG.sample();
90
91        int i = 0;
92        while ( ( cumDist ( i ) < u0 ) && ( i < ( w.length() - 1 ) ) ) {
93                i++;
94        }
95
96        return Coms ( i )->sample();
97}
98
99vec egiwmix::mean() const {
100        int i;
101        vec mu = zeros ( dim );
102        for ( i = 0; i < w.length(); i++ ) {
103                mu += w ( i ) * Coms ( i )->mean();
104        }
105        return mu;
106}
107
108vec egiwmix::variance() const {
109        // non-central moment
110        vec mom2 = zeros ( dim );
111        for ( int i = 0; i < w.length(); i++ ) {
112                // pow is overloaded, we have to use another approach
113                mom2 += w ( i ) * ( Coms ( i )->variance() + elem_mult ( Coms ( i )->mean(), Coms ( i )->mean() ) );
114        }
115        // central moment
116        // pow is overloaded, we have to use another approach
117        return mom2 - elem_mult ( mean(), mean() );
118}
119
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        }
127        emix* tmp = new emix();
128        tmp->set_parameters ( w, Cn, false );
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.
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
144        mat matVecsTheta;                               // matrix which rows are theta vects.
145
146        // fill in the vectors vecNu, vecD and matVecsTheta
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() );
151        }
152
153        // calculate the common parts and their sum
154        vecCommon = elem_mult ( w, elem_div ( vecNu, vecD ) );
155        sumVecCommon = sum ( vecCommon );
156
157        // LS estimator of theta
158        vec aprEstTheta ( dimLS );
159        aprEstTheta.zeros();
160        for ( int i = 0; i < len; i++ ) {
161                aprEstTheta +=  matVecsTheta.get_row ( i ) * vecCommon ( i );
162        }
163        aprEstTheta /= sumVecCommon;
164
165
166        // LS estimator of dfm
167        double aprNu;
168        double A = log ( sumVecCommon );                // Term 'A' in equation
169
170        for ( int i = 0; i < len; i++ ) {
171                A += w ( i ) * ( log ( vecD ( i ) ) - psi ( 0.5 * vecNu ( i ) ) );
172        }
173
174        aprNu = ( 1 + sqrt ( 1 + 2 * ( A - LOG2 ) / 3 ) ) / ( 2 * ( A - LOG2 ) );
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
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 );
188        }
189
190        // Construct GiW pdf :: BEGIN
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 );
197
198        egiw* aprgiw = new egiw ( 1, aprLD, aprNu );
199        return aprgiw;
200};
201
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);
215}
216
217}
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 ) {
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!");
226//                      epdfs ( i ) = & ( mpdfs ( i )->posterior() ); // add pointer to epdf
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//              };
232//
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
241//                      irvcs_rvc ( i ) = mpdfs ( i )->_rvc().dataind ( rvc );
242//                      //
243// /*                   if ( rvcinrv ( i ).length() >0 ) {independent = false;}
244//                      if ( irvcs_rvc ( i ).length() >0 ) {independent = false;}*/
245//              }
246//      };
Note: See TracBrowser for help on using the browser.