Changeset 193
- Timestamp:
- 10/22/08 10:46:42 (16 years ago)
- Files:
-
- 1 added
- 9 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/estim/merger.cpp
r192 r193 60 60 //Re-Initialize Mixture model 61 61 Mix.init ( &A0, Smp_ex, Nc ); 62 Mix.bayesB ( Smp_ex, ones ( Ns ) );//w*Ns );62 Mix.bayesB ( Smp_ex, w*Ns ); 63 63 Mpred = Mix.predictor ( rv ); // Allocation => must be deleted at the end!! 64 64 65 // Generate new samples 66 eSmp.set_samples ( Mpred ); 67 for ( int i=0;i<Ns;i++ ) { set_col_part ( Smp_ex,i,Smp ( i ) );} 65 if ( 1 ) { 66 // Generate new samples 67 eSmp.set_samples ( Mpred ); 68 for ( int i=0;i<Ns;i++ ) { set_col_part ( Smp_ex,i,Smp ( i ) );} 69 } 70 else { 71 for ( int ii=0;ii<10;ii++ ) { 72 for ( int jj=0; jj<10; jj++ ) { 73 Smp ( ii+jj*10 ) =vec_2 ( -1.0+6*ii/10.0, -1.0+6*jj/10.0 ); 74 } 75 } 76 for ( int i=0;i<Ns;i++ ) { set_col_part ( Smp_ex,i,Smp ( i ) );} 77 } 68 78 69 79 sprintf ( str,"Mpdf%d",niter ); … … 98 108 // Compute likelihood of the missing variable 99 109 if ( rv.count() > ( mpdfs ( i )->_rv().count() + mpdfs ( i )->_rvc().count() ) ) { 110 /////////////// 111 cout << Mpred->mean() <<endl; 100 112 // There are variales unknown to mpdfs(i) : rvzs 101 113 mpdf* tmp_cond = Mpred->condition ( rvzs ( i ) ); 102 114 // Compute likelihood 115 vec lw_dbg=lw_src; 103 116 for ( int k= 0; k<Ns; k++ ) { 104 117 lw_src ( k ) += log ( … … 111 124 // Compute likelihood of the partial source 112 125 for ( int k= 0; k<Ns; k++ ) { 113 mpdfs ( i )->condition ( dls ( i )->get_cond ( Smp (k) ) );114 lw_src ( k ) += mpdfs ( i )->_epdf().evalpdflog ( dls ( i )->get_val ( Smp (k) ) );126 mpdfs ( i )->condition ( dls ( i )->get_cond ( Smp ( k ) ) ); 127 lw_src ( k ) += mpdfs ( i )->_epdf().evalpdflog ( dls ( i )->get_val ( Smp ( k ) ) ); 115 128 } 129 116 130 } 117 131 lW.set_row ( i, lw_src ); // do not divide by mix … … 139 153 dbg << Name ( str ) << w; 140 154 141 eSmp.resample(); // So that it can be used in bayes142 for ( int i=0;i<Ns;i++ ) { set_col_part ( Smp_ex,i,Smp ( i ) );}155 // eSmp.resample(); // So that it can be used in bayes 156 // for ( int i=0;i<Ns;i++ ) { set_col_part ( Smp_ex,i,Smp ( i ) );} 143 157 144 158 sprintf ( str,"Smp_res%d",niter ); … … 147 161 // ==== stopping rule === 148 162 niter++; 149 converged = ( niter> 20);163 converged = ( niter>4 ); 150 164 } 151 165 -
bdm/estim/mixef.h
r189 r193 99 99 //! Flatten the density as if it was not estimated from the data 100 100 void flatten(double sumw=1.0); 101 //! Access function 102 BMEF* _Coms(int i){return Coms(i);} 101 103 102 104 //!Set which method is to be used -
bdm/stat/emix.h
r192 r193 42 42 //!flag for destructor 43 43 bool destroynom; 44 //!datalink between conditional and nom 45 datalink_m2e dl; 44 46 public: 45 47 //!Default constructor. By default, the given epdf is not copied! 46 48 //! 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()) {49 mratio ( const epdf* nom0, const RV &rv, bool copy=false ) :mpdf ( rv,nom0->_rv().subt(rv) ), dl(rv,rvc,nom0->_rv()) { 48 50 if ( copy ) { 49 51 // nom = nom0->_copy_(); 52 it_error("todo"); 50 53 destroynom=true; 51 54 } … … 54 57 destroynom = false; 55 58 } 56 rvc = nom->_rv().subt ( rv );57 59 it_assert_debug ( rvc.length() >0,"Makes no sense to use this object!" ); 58 60 den = nom->marginal ( rvc ); 59 61 }; 60 62 double evalcond ( const vec &val, const vec &cond ) { 61 return exp ( nom->evalpdflog ( concat ( val,cond ) ) - den->evalpdflog ( cond ) ); 63 vec nom_val(rv.count()+rvc.count()); 64 dl.fill_val_cond(nom_val,val,cond); 65 return exp ( nom->evalpdflog ( nom_val ) - den->evalpdflog ( cond ) ); 62 66 } 63 67 //! Object takes ownership of nom and will destroy it … … 105 109 }; 106 110 vec evalpdflog_m ( const mat &Val ) const { 107 vec x=ones ( Val.cols() ); 108 vec y ( Val.cols() ); 111 vec x=zeros ( Val.cols() ); 109 112 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 113 x+= w ( i ) *exp ( Coms ( i )->evalpdflog_m ( Val ) ); 112 114 } 113 115 return log ( x ); … … 130 132 //! Auxiliary function for taking ownership of the Coms() 131 133 void ownComs() {destroyComs=true;} 134 135 //!access function 136 epdf* _Coms(int i){return Coms(i);} 132 137 }; 133 138 … … 153 158 // rv and rvc established = > we can link them with mpdfs 154 159 for ( int i = 0;i < n;i++ ) { 155 dls ( i ) = new datalink_m2m ( mpdfs ( i )->_rv(), mpdfs (i)->_rvc(), rv, rvc );160 dls ( i ) = new datalink_m2m ( mpdfs ( i )->_rv(), mpdfs ( i )->_rvc(), rv, rvc ); 156 161 } 157 162 … … 163 168 double evalcond ( const vec &val, const vec &cond ) { 164 169 int i; 165 double res = 0.0;170 double res = 1.0; 166 171 for ( i = n - 1;i >= 0;i-- ) { 167 if ( mpdfs(i)->_rvc().count() >0) { 168 mpdfs ( i )->condition ( dls ( i )->get_cond ( val,cond ) ); 169 } 170 // add logarithms 171 res += epdfs ( i )->evalpdflog ( dls ( i )->get_val ( val ) ); 172 } 173 return exp ( res ); 172 /* if ( mpdfs(i)->_rvc().count() >0) { 173 mpdfs ( i )->condition ( dls ( i )->get_cond ( val,cond ) ); 174 } 175 // add logarithms 176 res += epdfs ( i )->evalpdflog ( dls ( i )->get_val ( val ) );*/ 177 res *= mpdfs ( i )->evalcond ( 178 dls ( i )->get_val ( val ), 179 dls ( i )->get_cond ( val, cond ) 180 ); 181 } 182 return res; 174 183 } 175 184 vec samplecond ( const vec &cond, double &ll ) { … … 180 189 // Hard assumption here!!! We are going backwards, to assure that samples that are needed from smp are already generated! 181 190 for ( int i = ( n - 1 );i >= 0;i-- ) { 182 if ( mpdfs (i)->_rvc().count() ) {191 if ( mpdfs ( i )->_rvc().count() ) { 183 192 mpdfs ( i )->condition ( dls ( i )->get_cond ( smp ,cond ) ); // smp is val here!! 184 193 } 185 194 smpi = epdfs ( i )->sample(); 186 195 // copy contribution of this pdf into smp 187 dls (i)->fill_val(smp, smpi);196 dls ( i )->fill_val ( smp, smpi ); 188 197 // add ith likelihood contribution 189 198 ll+=epdfs ( i )->evalpdflog ( smpi ); … … 206 215 Array<const epdf*> epdfs; 207 216 //! Array of indeces 208 Array< ivec> rvinds;209 public: 210 eprod ( const Array<const epdf*> epdfs0 ) : epdf ( RV() ),epdfs ( epdfs0 ), rvinds ( epdfs.length() ) {217 Array<datalink_e2e*> dls; 218 public: 219 eprod ( const Array<const epdf*> epdfs0 ) : epdf ( RV() ),epdfs ( epdfs0 ),dls ( epdfs.length() ) { 211 220 bool independent=true; 212 221 for ( int i=0;i<epdfs.length();i++ ) { 213 222 independent=rv.add ( epdfs ( i )->_rv() ); 214 223 it_assert_debug ( independent==true, "eprod:: given components are not independent ." ); 215 rvinds ( i ) = ( epdfs ( i )->_rv() ).dataind (rv );224 dls ( i ) = new datalink_e2e( epdfs ( i )->_rv() , rv ); 216 225 } 217 226 } … … 221 230 for ( int i=0;i<epdfs.length();i++ ) { 222 231 vec pom = epdfs ( i )->mean(); 223 set_subvector ( tmp,rvinds ( i ), pom );232 dls(i)->fill_val(tmp, pom ); 224 233 } 225 234 return tmp; … … 229 238 for ( int i=0;i<epdfs.length();i++ ) { 230 239 vec pom = epdfs ( i )->sample(); 231 set_subvector ( tmp,rvinds ( i ), pom );240 dls(i)->fill_val(tmp, pom ); 232 241 } 233 242 return tmp; … … 236 245 double tmp=0; 237 246 for ( int i=0;i<epdfs.length();i++ ) { 238 tmp+=epdfs ( i )->evalpdflog ( val ( rvinds ( i )) );247 tmp+=epdfs ( i )->evalpdflog ( dls(i)->get_val ( val ) ); 239 248 } 240 249 return tmp; … … 242 251 //!access function 243 252 const epdf* operator () ( int i ) const {it_assert_debug ( i<epdfs.length(),"wrong index" );return epdfs ( i );} 253 254 //!Destructor 255 ~eprod(){for(int i=0;i<epdfs.length();i++){delete dls(i);}} 244 256 }; 245 257 -
bdm/stat/libBM.h
r192 r193 296 296 return tmp; 297 297 } 298 void fill_val_cond ( vec &val_up, const vec &val, const vec &cond ) { 299 it_assert_debug ( valsize==val.length(),"Wrong val" ); 300 it_assert_debug ( valupsize==val_up.length(),"Wrong val_up" ); 301 set_subvector ( val_up, v2v_up, val ); 302 set_subvector ( val_up, v2c_up, cond ); 303 } 298 304 }; 299 305 //!DataLink is a connection between mpdf and its superordinate (Up) -
bdm/stat/libEF.cpp
r182 r193 82 82 mat R; 83 83 mean_mat(M,R); 84 return cvectorize (concat_vertical(M ,R));84 return cvectorize (concat_vertical(M.T(),R)); 85 85 } 86 86 -
bdm/stat/libEF.h
r192 r193 620 620 621 621 //fixme - could this be done in general for all sq_T? 622 mat S=R .to_mat();622 mat S=Rn.to_mat(); 623 623 //fixme 624 624 int n=rvn.count()-1; -
matlab/merger_iter_debug.m
r190 r193 7 7 8 8 figure(1); 9 for it=0: 209 for it=0:4 10 10 figure(it+1) 11 11 subplot(2,2,1); … … 24 24 subplot(2,2,3); 25 25 eval(['contour_2(Smp' si '(1,:),Smp' si '(2,:),exp(lW' si '(2,:)))']); 26 %eval(['contour_2(Smp' si '(1,:),Smp' si '(2,:),exp(lw_cond' si '))']); 26 27 title('Second source'); 27 28 set(gca,'XLim',XL); … … 30 31 31 32 subplot(2,2,4); 32 eval(['contour_2(Smp' si '(1,:),Smp' si '(2,:),w' si ')']); 33 hold off 34 eval(['contour_2(Smp' si '(1,:),Smp' si '(2,:),w' si ','':'')']); 33 35 title('Merged density'); 34 36 set(gca,'XLim',XL); 35 37 set(gca,'YLim',YL); 38 hold on 39 eval(['contour_2(Smp' si '(1,:),Smp' si '(2,:),w_is_' si ')']); 36 40 37 41 end -
tests/CMakeLists.txt
r190 r193 34 34 TEST(enorm_test) 35 35 TEST(egiw_test) 36 TEST(emix_test) 36 37 TEST(test0) 37 38 TEST(testResample) -
tests/merger_iter_test.cpp
r190 r193 11 11 int main() { 12 12 13 RNG_randomize();13 //RNG_randomize(); 14 14 15 15 RV x ( "{x }","1" ); … … 21 21 enorm<fsqmat> f2 ( x ); 22 22 23 f1.set_parameters ( " 0 0",mat ( "0.5 0; 0 0.5" ) );23 f1.set_parameters ( "3 2",mat ( "0.5 0; 0 0.5" ) ); 24 24 f2.set_parameters ( "3",mat ( "0.5" ) ); 25 25 … … 44 44 g0.set_parameters(vec("1 1"),mat("1 0; 0 1")); 45 45 46 M.set_parameters(1.2, 200,2);46 M.set_parameters(1.2,100,1); 47 47 M.merge(&g0); 48 48