Changeset 192 for bdm/stat/emix.h
- Timestamp:
- 10/22/08 10:46:40 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/stat/emix.h
r189 r192 35 35 */ 36 36 class mratio: public mpdf { 37 37 protected: 38 38 //! Nominator in the form of mpdf 39 39 const epdf* nom; 40 40 //!Denominator in the form of epdf 41 42 //!flag for destructor 43 44 45 //!Default constructor. By default, the given epdf is not copied! 41 epdf* den; 42 //!flag for destructor 43 bool destroynom; 44 public: 45 //!Default constructor. By default, the given epdf is not copied! 46 46 //! It is assumed that this function will be used only temporarily. 47 mratio(const epdf* nom0, const RV &rv, bool copy=false):mpdf(rv,RV()){48 if (copy){47 mratio ( const epdf* nom0, const RV &rv, bool copy=false ) :mpdf ( rv,RV() ) { 48 if ( copy ) { 49 49 // nom = nom0->_copy_(); 50 destroynom=true; 51 } else { 52 nom = nom0; 53 destroynom = false; 54 } 55 rvc = nom->_rv().subt(rv); 56 it_assert_debug(rvc.length()>0,"Makes no sense to use this object!"); 57 den = nom->marginal(rvc); 58 }; 59 double evalcond(const vec &val, const vec &cond) { 60 return exp(nom->evalpdflog(concat(val,cond)) - den->evalpdflog(cond)); 61 } 50 destroynom=true; 51 } 52 else { 53 nom = nom0; 54 destroynom = false; 55 } 56 rvc = nom->_rv().subt ( rv ); 57 it_assert_debug ( rvc.length() >0,"Makes no sense to use this object!" ); 58 den = nom->marginal ( rvc ); 59 }; 60 double evalcond ( const vec &val, const vec &cond ) { 61 return exp ( nom->evalpdflog ( concat ( val,cond ) ) - den->evalpdflog ( cond ) ); 62 } 62 63 //! Object takes ownership of nom and will destroy it 63 void ownnom(){destroynom=true;}64 void ownnom() {destroynom=true;} 64 65 //! Default destructor 65 ~mratio(){delete den; if (destroynom) {delete nom;}}66 ~mratio() {delete den; if ( destroynom ) {delete nom;}} 66 67 }; 67 68 … … 100 101 int i; 101 102 double sum = 0.0; 102 for ( i = 0;i < w.length();i++ ) {sum += w ( i ) * exp (Coms ( i )->evalpdflog ( val ));}103 for ( i = 0;i < w.length();i++ ) {sum += w ( i ) * exp ( Coms ( i )->evalpdflog ( val ) );} 103 104 return log ( sum ); 104 105 }; 105 106 vec evalpdflog_m ( const mat &Val ) const { 106 vec x=ones (Val.cols());107 vec y (Val.cols());108 for ( int i = 0; i < w.length(); i++ ) {109 y = w ( i ) *exp(Coms ( i )->evalpdflog_m ( Val ) );110 elem_mult_inplace (y,x); //result is in x111 } 112 return log (x);107 vec x=ones ( Val.cols() ); 108 vec y ( Val.cols() ); 109 for ( int i = 0; i < w.length(); i++ ) { 110 y = w ( i ) *exp ( Coms ( i )->evalpdflog_m ( Val ) ); 111 elem_mult_inplace ( y,x ); //result is in x 112 } 113 return log ( x ); 113 114 }; 114 115 mat evalpdflog_M ( const mat &Val ) const { 115 mat X (w.length(), Val.cols());116 for ( int i = 0; i < w.length(); i++ ) {117 X.set_row (i, w ( i )*exp(Coms ( i )->evalpdflog_m ( Val ) ));116 mat X ( w.length(), Val.cols() ); 117 for ( int i = 0; i < w.length(); i++ ) { 118 X.set_row ( i, w ( i ) *exp ( Coms ( i )->evalpdflog_m ( Val ) ) ); 118 119 } 119 120 return X; … … 143 144 //! pointers to epdfs - shortcut to mpdfs()._epdf() 144 145 Array<epdf*> epdfs; 145 //! Indeces of rvc in common rvc 146 Array<ivec> irvcs_rvc; 147 //! Indeces of common rvc in rvcs 148 Array<ivec> irvc_rvcs; 146 //! Data link for each mpdfs 147 Array<datalink_m2m*> dls; 149 148 public: 150 149 /*!\brief Constructor from list of mFacs, 151 150 */ 152 mprod ( Array<mpdf*> mFacs ) : compositepdf ( mFacs ), mpdf ( getrv ( true ),RV() ), epdfs ( n ), irvcs_rvc ( n ), irvc_rvcs ( n ) {151 mprod ( Array<mpdf*> mFacs ) : compositepdf ( mFacs ), mpdf ( getrv ( true ),RV() ), epdfs ( n ), dls ( n ) { 153 152 setrvc ( rv,rvc ); 154 setindices ( rv );153 // rv and rvc established = > we can link them with mpdfs 155 154 for ( int i = 0;i < n;i++ ) { 156 // establish link between rvc and rvcs 157 rvc.dataind ( mpdfs ( i )->_rvc(), irvc_rvcs ( i ), irvcs_rvc ( i ) ); 155 dls ( i ) = new datalink_m2m ( mpdfs ( i )->_rv(), mpdfs(i)->_rvc(), rv, rvc ); 158 156 } 159 157 … … 166 164 int i; 167 165 double res = 0.0; 168 vec condi;169 166 for ( i = n - 1;i >= 0;i-- ) { 170 if ( ( irvcs_rvc ( i ).length() > 0 ) || ( irvcs_rv ( i ).length() > 0 ) ) { 171 condi = zeros ( irvcs_rvc ( i ).length() +irvcs_rv ( i ).length() ); 172 //copy part of the rvc into condi 173 set_subvector ( condi, irvcs_rvc ( i ), get_vec ( cond,irvc_rvcs ( i ) ) ); 174 //copy part of the rv into condi 175 set_subvector ( condi, irvcs_rv ( i ), get_vec ( val,irv_rvcs ( i ) ) ); 176 mpdfs ( i )->condition ( condi ); 167 if ( mpdfs(i)->_rvc().count() >0) { 168 mpdfs ( i )->condition ( dls ( i )->get_cond ( val,cond ) ); 177 169 } 178 170 // add logarithms 179 res += epdfs ( i )->evalpdflog ( val ( rvsinrv ( i )) );171 res += epdfs ( i )->evalpdflog ( dls ( i )->get_val ( val ) ); 180 172 } 181 173 return exp ( res ); 182 174 } 183 175 vec samplecond ( const vec &cond, double &ll ) { 184 vec smp=zeros ( rv.count() );185 vec condi;176 //! Ugly hack to help to discover if mpfs are not in proper order. Correct solution = check that explicitely. 177 vec smp= std::numeric_limits<double>::infinity() * ones ( rv.count() ); 186 178 vec smpi; 187 179 ll = 0; 180 // Hard assumption here!!! We are going backwards, to assure that samples that are needed from smp are already generated! 188 181 for ( int i = ( n - 1 );i >= 0;i-- ) { 189 if ( ( irvcs_rvc ( i ).length() > 0 ) || ( irvcs_rv ( i ).length() > 0 ) ) { 190 condi = zeros ( irvcs_rv ( i ).length() + irvcs_rvc ( i ).length() ); 191 // copy data in condition 192 set_subvector ( condi,irvcs_rvc ( i ), get_vec ( cond, irvc_rvcs ( i ) ) ); 193 // copy data in already generated sample 194 set_subvector ( condi,irvcs_rv ( i ), get_vec ( smp,irv_rvcs ( i ) ) ); 195 196 mpdfs ( i )->condition ( condi ); 182 if ( mpdfs(i)->_rvc().count() ) { 183 mpdfs ( i )->condition ( dls ( i )->get_cond ( smp ,cond ) ); // smp is val here!! 197 184 } 198 185 smpi = epdfs ( i )->sample(); 199 186 // copy contribution of this pdf into smp 200 set_subvector ( smp,rvsinrv ( i ), smpi);187 dls(i)->fill_val(smp, smpi); 201 188 // add ith likelihood contribution 202 189 ll+=epdfs ( i )->evalpdflog ( smpi );