Changeset 192
- Timestamp:
- 10/22/08 10:46:40 (16 years ago)
- Files:
-
- 8 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/estim/merger.cpp
r190 r192 33 33 vec &w = eSmp._w(); //aux 34 34 35 mat Smp_ex =ones ( rv.count() +1,Ns ); // Extended samples for the ARX model - the last row is ones 35 mat Smp_ex =ones ( rv.count() +1,Ns ); // Extended samples for the ARX model - the last row is ones 36 36 for ( int i=0;i<Ns;i++ ) { set_col_part ( Smp_ex,i,Smp ( i ) );} 37 37 … … 60 60 //Re-Initialize Mixture model 61 61 Mix.init ( &A0, Smp_ex, Nc ); 62 Mix.bayesB ( Smp_ex, ones (Ns));//w*Ns );63 Mpred = Mix.predictor (rv); // Allocation => must be deleted at the end!!64 62 Mix.bayesB ( Smp_ex, ones ( Ns ) );//w*Ns ); 63 Mpred = Mix.predictor ( rv ); // Allocation => must be deleted at the end!! 64 65 65 // Generate new samples 66 66 eSmp.set_samples ( Mpred ); … … 79 79 //======== Same RVs =========== 80 80 //Split according to dependency in rvs 81 if ( rvsinrv ( i ).length() ==rv.count() ) {81 if ( mpdfs ( i )->_rv().count() ==rv.count() ) { 82 82 // no need for conditioning or marginalization 83 83 for ( int j=0;j<Ns; j++ ) { // Smp is Array<> => for cycle … … 86 86 } 87 87 else { 88 vec smpk;89 88 // compute likelihood of marginal on the conditional variable 90 89 if ( mpdfs ( i )->_rvc().count() >0 ) { 91 90 // Make marginal on rvc_i 92 91 epdf* tmp_marg = Mpred->marginal ( mpdfs ( i )->_rvc() ); 93 for(int k=0;k<Ns;k++){ 94 lw_src(k) += tmp_marg->evalpdflog ( get_vec(Smp(i), irv_rvcs(i) ) );} 92 //compute vector of lw_src 93 for ( int k=0;k<Ns;k++ ) { 94 lw_src ( k ) += tmp_marg->evalpdflog ( dls ( i )->get_val ( Smp ( i ) ) ); 95 } 95 96 delete tmp_marg; 96 97 } 97 98 // Compute likelihood of the missing variable 98 if ( rv.count() > mpdfs ( i )->_rv().count() + mpdfs ( i )->_rvc().count() ) { 99 // There are variales unknown to mpdfs 100 RV z = ( rv.subt ( mpdfs ( i )->_rv() ) ).subt ( mpdfs ( i )->_rvc() ); 101 mpdf* tmp_cond = Mpred->condition ( z ); 102 // Indeces of rest rv in Smp 103 ivec zinrv=z.dataind ( rv ) ; 104 // Indeces of rest rvc in Smp 105 ivec zinrvc=tmp_cond->_rvc().dataind ( rv ); 99 if ( rv.count() > ( mpdfs ( i )->_rv().count() + mpdfs ( i )->_rvc().count() ) ) { 100 // There are variales unknown to mpdfs(i) : rvzs 101 mpdf* tmp_cond = Mpred->condition ( rvzs ( i ) ); 106 102 // Compute likelihood 107 103 for ( int k= 0; k<Ns; k++ ) { 108 smpk=Smp( k ); 109 lw_src ( k ) += log( tmp_cond->evalcond ( get_vec ( smpk,zinrv ), get_vec ( smpk,zinrvc ) )); 104 lw_src ( k ) += log ( 105 tmp_cond->evalcond ( 106 zdls ( i )->get_val ( Smp ( k ) ), 107 zdls ( i )->get_cond ( Smp ( k ) ) ) ); 110 108 } 111 109 delete tmp_cond; … … 113 111 // Compute likelihood of the partial source 114 112 for ( int k= 0; k<Ns; k++ ) { 115 smpk=Smp( k ); 116 mpdfs ( i )->condition ( get_vec ( smpk,irv_rvcs(i) ) ); 117 lw_src ( k ) += mpdfs ( i )->_epdf().evalpdflog ( get_vec ( smpk, rvsinrv ( i ) ) ); 113 mpdfs ( i )->condition ( dls ( i )->get_cond ( Smp(k) ) ); 114 lw_src ( k ) += mpdfs ( i )->_epdf().evalpdflog ( dls ( i )->get_val ( Smp(k) ) ); 118 115 } 119 116 } … … 128 125 129 126 w = lognorm_merge ( lW ); //merge 130 127 131 128 sprintf ( str,"w%d",niter ); 132 129 dbg << Name ( str ) << w; … … 150 147 // ==== stopping rule === 151 148 niter++; 152 converged = ( niter>20 );149 converged = ( niter>20 ); 153 150 } 154 151 -
bdm/estim/merger.h
r190 r192 30 30 //!Internal mixture of EF models 31 31 MixEF Mix; 32 //! Data link for each mpdf in mpdfs 33 Array<datalink_m2e*> dls; 34 //! Array of rvs that are not modelled by mpdfs at all (aux) 35 Array<RV> rvzs; 36 //! Data Links of rv0 mpdfs - these will be conditioned the (rv,rvc) of mpdfs 37 Array<datalink_m2e*> zdls; 38 32 39 //!Number of samples used in approximation 33 40 int Ns; … … 40 47 merger ( const Array<mpdf*> &S ) : 41 48 compositepdf ( S ), epdf ( getrv ( false ) ), 42 Mix ( Array<BMEF*> ( 0 ),vec ( 0 ) ) 43 {setindices(rv); beta=2.0; Ns=100; Nc=10; Mix.set_method(EM);} 44 //! Set internal parameters used in approximation 49 Mix ( Array<BMEF*> ( 0 ),vec ( 0 ) ), dls(n), rvzs(n), zdls(n) { 50 RV ztmp; 51 for ( int i=0;i<n;i++ ) { 52 //Establich connection between mpdfs and merger 53 dls ( i ) = new datalink_m2e ( mpdfs ( i )->_rv(), mpdfs(i)->_rvc(), rv ); 54 // find out what is missing in each mpdf 55 ztmp= mpdfs(i)->_rv(); 56 ztmp.add(mpdfs(i)->_rvc()); 57 rvzs(i)=rv.subt(ztmp); 58 zdls(i) = new datalink_m2e (rvzs(i), ztmp, rv ) ; 59 }; 60 //Set Default values of parameters 61 beta=2.0; 62 Ns=100; 63 Nc=10; 64 Mix.set_method ( EM ); 65 } 66 //! Set internal parameters used in approximation 45 67 void set_parameters ( double beta0, int Ns0, int Nc0 ) { beta=beta0;Ns=Ns0;Nc=Nc0;} 46 47 void init() { 68 //!Initialize the proposal density. This function must be called before merge()! 69 void init() { ////////////// NOT FINISHED 48 70 Array<vec> Smps ( n ); 49 71 //Gibbs sampling 50 72 for ( int i=0;i<n;i++ ) {Smps ( i ) =zeros ( 0 );} 51 73 } 52 74 //!Create a mixture density using known proposal 53 75 void merge ( const epdf* g0 ); 54 76 //!Create a mixture density, make sure to call init() before the first call 55 77 void merge () {merge ( & ( Mix._epdf() ) );}; 56 78 57 79 //! Merge log-likelihood values 58 80 vec lognorm_merge ( mat &lW ); 59 //! sample from merged density 60 //! weight w is a 61 vec sample ( )const { return Mix._epdf().sample();} 62 double evalpdflog ( const vec &dt ) const{ 63 vec dtf=ones(dt.length()+1); 64 dtf.set_subvector(0,dt); 65 return Mix.logpred ( dtf );} 66 vec mean()const {return Mix._epdf().mean();} 67 //! for future use 68 virtual ~merger() {}; 69 70 //! Access function 81 //! sample from merged density 82 //! weight w is a 83 vec sample ( ) const { return Mix._epdf().sample();} 84 double evalpdflog ( const vec &dt ) const { 85 vec dtf=ones ( dt.length() +1 ); 86 dtf.set_subvector ( 0,dt ); 87 return Mix.logpred ( dtf ); 88 } 89 vec mean() const {return Mix._epdf().mean();} 90 //! for future use 91 virtual ~merger() { 92 for (int i=0; i<n; i++){ 93 delete dls(i); 94 delete zdls(i); 95 } 96 }; 97 98 //! Access function 71 99 MixEF& _Mix() {return Mix;} 72 100 }; -
bdm/stat/emix.cpp
r183 r192 11 11 if ( copy ) { 12 12 Coms.set_length(Coms0.length()); 13 for ( i=0;i<w.length();i++ ) {*Coms ( i ) =*Coms0 ( i );} 13 for ( i=0;i<w.length();i++ ) {it_error("Not imp..."); 14 *Coms ( i ) =*Coms0 ( i );} 14 15 destroyComs=true; 15 16 } -
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 ); -
bdm/stat/libBM.cpp
r182 r192 234 234 } 235 235 236 void compositepdf::setindices(const RV &rv){237 for (int i = 0;i < n;i++ ) {238 // find ith rv in common rv239 rvsinrv ( i ) = mpdfs ( i )->_rv().dataind ( rv );240 // establish link between rv and rvc241 rv.dataind(mpdfs(i)->_rvc(), irv_rvcs(i), irvcs_rv(i));242 }243 }244 245 236 void BM::bayesB(const mat &Data){ 246 237 for(int t=0;t<Data.cols();t++){bayes(Data.get_col(t));} -
bdm/stat/libBM.h
r190 r192 247 247 }; 248 248 249 //!DataLink is a connection between epdf and its superordinate(Up)249 //!DataLink is a connection between an epdf and its superordinate epdf (Up) 250 250 //! It is assumed that my val is fully present in "Up" 251 class datalink {251 class datalink_e2e { 252 252 protected: 253 253 //! Remember how long val should be … … 257 257 //! val-to-val link, indeces of the upper val 258 258 ivec v2v_up; 259 259 public: 260 260 //! Constructor 261 datalink ( const RV &rv, const RV &rv_up ) : 262 valsize(rv.count()), valupsize(rv_up.count()), v2v_up ( rv.dataind ( rv_up ) ) {it_assert_debug(v2v_up.length()==valsize,"rv is not fully in rv_up");} 261 datalink_e2e ( const RV &rv, const RV &rv_up ) : 262 valsize ( rv.count() ), valupsize ( rv_up.count() ), v2v_up ( rv.dataind ( rv_up ) ) { 263 it_assert_debug ( v2v_up.length() ==valsize,"rv is not fully in rv_up" ); 264 } 263 265 //! Get val for myself from val of "Up" 264 vec get_val ( const vec &val_up ) {it_assert_debug (valupsize==val_up.length(),"Wrong val_up"); return get_vec ( val_up,v2v_up );}266 vec get_val ( const vec &val_up ) {it_assert_debug ( valupsize==val_up.length(),"Wrong val_up" ); return get_vec ( val_up,v2v_up );} 265 267 //! Fill val of "Up" by my pieces 266 void fill_val ( vec &val_up, const vec &val ) {it_assert_debug(valsize==val.length(),"Wrong val");it_assert_debug(valupsize==val_up.length(),"Wrong val_up");set_subvector ( val_up, v2v_up, val );} 267 }; 268 269 //!DataLink is a connection between mpdf and its superordinate (Up) 270 //! This class links 271 class datalink_mpdf: public datalink { 272 protected: 268 void fill_val ( vec &val_up, const vec &val ) { 269 it_assert_debug ( valsize==val.length(),"Wrong val" ); 270 it_assert_debug ( valupsize==val_up.length(),"Wrong val_up" ); 271 set_subvector ( val_up, v2v_up, val ); 272 } 273 }; 274 275 //! data link between 276 class datalink_m2e: public datalink_e2e { 277 protected: 278 //! Remember how long cond should be 279 int condsize; 273 280 //!upper_val-to-local_cond link, indeces of the upper val 274 281 ivec v2c_up; 275 282 //!upper_val-to-local_cond link, ideces of the local cond 276 283 ivec v2c_lo; 284 285 public: 286 //! Constructor 287 datalink_m2e ( const RV &rv, const RV &rvc, const RV &rv_up ) : 288 datalink_e2e ( rv,rv_up ), condsize ( rvc.count() ) { 289 //establish v2c connection 290 rvc.dataind ( rv_up, v2c_lo, v2c_up ); 291 } 292 //!Construct condition 293 vec get_cond ( const vec &val_up ) { 294 vec tmp ( condsize ); 295 set_subvector ( tmp,v2c_lo,val_up ( v2c_up ) ); 296 return tmp; 297 } 298 }; 299 //!DataLink is a connection between mpdf and its superordinate (Up) 300 //! This class links 301 class datalink_m2m: public datalink_m2e { 302 protected: 277 303 //!cond-to-cond link, indeces of the upper cond 278 304 ivec c2c_up; 279 305 //!cond-to-cond link, indeces of the local cond 280 306 ivec c2c_lo; 281 //! size of cond282 int csize;283 307 public: 284 308 //! Constructor 285 datalink_mpdf ( const mpdf &Me, const mpdf &Up ) : 286 datalink ( Me._rv(),Up._rv() ) { 287 //establish v2c connection 288 Me._rvc().dataind ( Up._rv(), v2c_lo, v2c_up ); 309 datalink_m2m ( const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up ) : 310 datalink_m2e ( rv, rvc, rv_up) { 289 311 //establish c2c connection 290 Me._rvc().dataind ( Up._rvc(), c2c_lo, c2c_up );291 csize = Me._rvc().count();312 rvc.dataind ( rvc_up, c2c_lo, c2c_up ); 313 it_assert_debug(c2c_lo.length()+v2c_lo.length()==condsize, "cond is not fully given"); 292 314 } 293 315 //! Get cond for myself from val and cond of "Up" 294 316 vec get_cond ( const vec &val_up, const vec &cond_up ) { 295 vec tmp (csize);296 set_subvector (tmp,v2c_lo,val_up(v2c_up));297 set_subvector (tmp,c2c_lo,cond_up(c2c_up));317 vec tmp ( condsize ); 318 set_subvector ( tmp,v2c_lo,val_up ( v2c_up ) ); 319 set_subvector ( tmp,c2c_lo,cond_up ( c2c_up ) ); 298 320 return tmp; 299 321 } 300 //! Fill 322 //! Fill 301 323 302 324 }; … … 304 326 /*! \brief Unconditional mpdf, allows using epdf in the role of mpdf. 305 327 306 WARNING: the class does not check validity of the \c ep pointer nor its existence.307 328 */ 308 329 class mepdf : public mpdf { … … 314 335 315 336 //!\brief Abstract composition of pdfs, a base for specific classes 337 //!this abstract class is common to epdf and mpdf 316 338 class compositepdf { 317 339 protected: … … 320 342 //! Elements of composition 321 343 Array<mpdf*> mpdfs; 322 //! Indeces of rvs in common rv 323 Array<ivec> rvsinrv; 324 //! Indeces of rvc in common rv 325 Array<ivec> irvcs_rv; 326 //! Indeces of common rv in rvc 327 Array<ivec> irv_rvcs; 328 public: 329 compositepdf ( Array<mpdf*> A0 ) : n ( A0.length() ), mpdfs ( A0 ), rvsinrv ( n ), irvcs_rv ( n ),irv_rvcs ( n ) {}; 344 public: 345 compositepdf ( Array<mpdf*> A0 ) : n ( A0.length() ), mpdfs ( A0 ) {}; 330 346 //! find common rv, flag \param checkoverlap modifies whether overlaps are acceptable 331 347 RV getrv ( bool checkoverlap=false ); 332 348 //! common rvc of all mpdfs is written to rvc 333 349 void setrvc ( const RV &rv, RV &rvc ); 334 //! fill all rv*inrv* according to335 void setindices ( const RV &rv );336 350 }; 337 351 -
bdm/stat/libEF.h
r185 r192 380 380 //! Set \c A and \c R 381 381 void set_parameters ( const mat &A, const vec &mu0, const sq_T &R ); 382 //!Generate one sample of the posterior383 vec samplecond (const vec &cond, double &lik );384 //!Generate matrix of samples of the posterior385 mat samplecond (const vec &cond, vec &lik, int n );382 // //!Generate one sample of the posterior 383 // vec samplecond (const vec &cond, double &lik ); 384 // //!Generate matrix of samples of the posterior 385 // mat samplecond (const vec &cond, vec &lik, int n ); 386 386 //! Set value of \c rvc . Result of this operation is stored in \c epdf use function \c _ep to access it. 387 387 void condition (const vec &cond ); … … 567 567 } 568 568 569 template<class sq_T>570 vec mlnorm<sq_T>::samplecond (const vec &cond, double &lik ) {571 this->condition ( cond );572 vec smp = epdf.sample();573 lik = epdf.eval ( smp );574 return smp;575 }576 577 template<class sq_T>578 mat mlnorm<sq_T>::samplecond (const vec &cond, vec &lik, int n ) {579 int i;580 int dim = rv.count();581 mat Smp ( dim,n );582 vec smp ( dim );583 this->condition ( cond );584 585 for ( i=0; i<n; i++ ) {586 smp = epdf.sample();587 lik ( i ) = epdf.eval ( smp );588 Smp.set_col ( i ,smp );589 }590 591 return Smp;592 }569 // template<class sq_T> 570 // vec mlnorm<sq_T>::samplecond (const vec &cond, double &lik ) { 571 // this->condition ( cond ); 572 // vec smp = epdf.sample(); 573 // lik = epdf.eval ( smp ); 574 // return smp; 575 // } 576 577 // template<class sq_T> 578 // mat mlnorm<sq_T>::samplecond (const vec &cond, vec &lik, int n ) { 579 // int i; 580 // int dim = rv.count(); 581 // mat Smp ( dim,n ); 582 // vec smp ( dim ); 583 // this->condition ( cond ); 584 // 585 // for ( i=0; i<n; i++ ) { 586 // smp = epdf.sample(); 587 // lik ( i ) = epdf.eval ( smp ); 588 // Smp.set_col ( i ,smp ); 589 // } 590 // 591 // return Smp; 592 // } 593 593 594 594 template<class sq_T> -
tests/datalink_test.cpp
r190 r192 10 10 using std::endl; 11 11 12 int main() 13 { 14 RV a = RV ( "{a }","2"); 15 RV b = RV ( "{b }"); 16 RV c = RV ( "{c }"); 17 18 mpdf M1(concat(a,b),c); 19 mpdf M2(a, concat(b,c)); 20 21 datalink_mpdf dl(M2,M1); 22 23 vec val("1 1.5 2"); 24 vec cond("3"); 25 12 int main() { 13 RV a = RV ( "{a }","2" ); 14 RV b = RV ( "{b }" ); 15 RV c = RV ( "{c }" ); 16 17 mpdf M1 ( concat ( a,b ),c ); 18 mpdf M2 ( a, concat ( b,c ) ); 19 20 datalink_m2m dl ( a,M2._rvc(),M1._rv(), M1._rvc() ); 21 22 vec val ( "1 1.5 2" ); 23 vec cond ( "3" ); 24 26 25 cout << "val: " << val << endl; 27 26 cout << "cond: " << cond << endl; 28 29 cout << "lo val: " << dl.get_val (val) <<endl;30 cout << "lo cond: " << dl.get_cond (val,cond) <<endl;31 27 28 cout << "lo val: " << dl.get_val ( val ) <<endl; 29 cout << "lo cond: " << dl.get_cond ( val,cond ) <<endl; 30 32 31 //getchar(); 33 32 return 0;