Changeset 1009
- Timestamp:
- 05/27/10 23:07:16 (14 years ago)
- Location:
- library
- Files:
-
- 10 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/bdmbase.cpp
r979 r1009 575 575 } 576 576 577 void BM::bayes_batch ( const mat &Data, const vec &cond ) { 577 double BM::bayes_batch ( const mat &Data, const vec &cond ) { 578 double levid=0.0; 578 579 for ( int t = 0; t < Data.cols(); t++ ) { 579 580 bayes ( Data.get_col ( t ), cond ); 580 } 581 } 582 583 void BM::bayes_batch ( const mat &Data, const mat &Cond ) { 581 levid+=ll; 582 } 583 return levid; 584 } 585 586 double BM::bayes_batch ( const mat &Data, const mat &Cond ) { 587 double levid=0.0; 584 588 for ( int t = 0; t < Data.cols(); t++ ) { 585 589 bayes ( Data.get_col ( t ), Cond.get_col ( t ) ); 586 590 } 587 } 588 589 } 591 return levid; 592 } 593 594 } -
library/bdm/base/bdmbase.h
r989 r1009 1293 1293 @param dt vector of input data 1294 1294 */ 1295 virtual void bayes ( const vec &yt, const vec &cond = empty_vec ) = 1295 virtual void bayes ( const vec &yt, const vec &cond = empty_vec ) =0; 1296 1296 //! Batch Bayes rule (columns of Dt are observations) 1297 virtual voidbayes_batch ( const mat &Dt, const vec &cond = empty_vec );1297 virtual double bayes_batch ( const mat &Dt, const vec &cond = empty_vec ); 1298 1298 //! Batch Bayes rule (columns of Dt are observations, columns of Cond are conditions) 1299 virtual voidbayes_batch ( const mat &Dt, const mat &Cond );1299 virtual double bayes_batch ( const mat &Dt, const mat &Cond ); 1300 1300 //! Evaluates predictive log-likelihood of the given data record 1301 1301 //! I.e. marginal likelihood of the data with the posterior integrated out. 1302 1302 //! This function evaluates only \f$ y_t \f$, condition is assumed to be the last used in bayes(). 1303 1303 //! See bdm::BM::predictor for conditional version. 1304 virtual double logpred ( const vec &yt ) const NOT_IMPLEMENTED(0.0);1304 virtual double logpred ( const vec &yt, const vec &cond ) const NOT_IMPLEMENTED(0.0); 1305 1305 1306 1306 //! Matrix version of logpred 1307 vec logpred_mat ( const mat &Yt ) const {1307 vec logpred_mat ( const mat &Yt, const mat &Cond ) const { 1308 1308 vec tmp ( Yt.cols() ); 1309 1309 for ( int i = 0; i < Yt.cols(); i++ ) { 1310 tmp ( i ) = logpred ( Yt.get_col ( i ) );1310 tmp ( i ) = logpred ( Yt.get_col ( i ), Cond.get_col(i) ); 1311 1311 } 1312 1312 return tmp; -
library/bdm/estim/arx.cpp
r1003 r1009 3 3 4 4 void ARX::bayes_weighted ( const vec &yt, const vec &cond, const double w ) { 5 bdm_assert_debug ( yt.length() == dimy, " ARX::bayes yt is of size "+num2str(yt.length())+" expected dimension is "+num2str(dimy) );6 bdm_assert_debug ( cond.length() == rgrlen , " ARX::bayes cond is of size "+num2str(cond.length())+" expected dimension is "+num2str(rgrlen) );5 bdm_assert_debug ( yt.length() == dimy, "BM::bayes yt is of size "+num2str(yt.length())+" expected dimension is "+num2str(dimy) ); 6 bdm_assert_debug ( cond.length() == rgrlen , "BM::bayes cond is of size "+num2str(cond.length())+" expected dimension is "+num2str(rgrlen) ); 7 7 8 8 BMEF::bayes_weighted(yt,cond,w); //potential discount scheduling … … 46 46 } 47 47 48 double ARX::logpred ( const vec &yt ) const {48 double ARX::logpred ( const vec &yt, const vec &cond ) const { 49 49 egiw pred ( est ); 50 50 ldmat &V = pred._V(); … … 54 54 vec dyad_p = dyad; 55 55 dyad_p.set_subvector ( 0, yt ); 56 56 dyad_p.set_subvector(dimy,cond); 57 57 58 if ( frg < 1.0 ) { 58 59 pred.pow ( frg ); -
library/bdm/estim/arx.h
r1003 r1009 88 88 bayes_weighted ( yt, cond, 1.0 ); 89 89 }; 90 double logpred ( const vec &yt ) const;90 double logpred ( const vec &yt, const vec &cond ) const; 91 91 void flatten ( const BMEF* B ); 92 92 //! Conditioned version of the predictor … … 103 103 //! Smarter structure estimation by Ludvik Tesar.\return indices of accepted regressors. 104 104 ivec structure_est_LT ( const egiw &Eg0 ); 105 //! reduce to105 //! reduce structure to the given ivec of matrix V 106 106 void reduce_structure(ivec &inds_in_V){ 107 107 ldmat V = posterior()._V(); … … 110 110 ldmat newV(V,inds_in_V); 111 111 est.set_parameters(dimy,newV, posterior()._nu()); 112 113 if (have_constant){ 114 ivec rgr_elem= find(inds_in_V<(V.rows()-1)); // < -- find non-constant 115 rgr = rgr.subselect(rgr_elem); 116 rgrlen = rgr_elem.length(); 117 } else{ 118 rgr = rgr.subselect(inds_in_V); 119 } 112 120 validate(); 113 121 } … … 184 192 185 193 } 194 //! access function 195 RV & _rgr() {return rgr;} 196 bool _have_constant() {return have_constant;} 197 int _rgrlen() {return rgrlen;} 186 198 }; 187 199 UIREGISTER ( ARX ); -
library/bdm/estim/mixtures.cpp
r1004 r1009 28 28 for ( i = 0; i < Coms.length(); i++ ) { 29 29 //pick one datum 30 int ind = (int) floor ( ndat * UniRNG.sample() ); 31 Coms ( i )->bayes ( Data.get_col ( ind ), empty_vec ); 30 if (ndat==Coms.length()){ //take the ith vector 31 Coms ( i )->bayes ( Data.get_col ( i ), empty_vec ); 32 } else { // pick at random 33 int ind = (int) floor ( ndat * UniRNG.sample() ); 34 Coms ( i )->bayes ( Data.get_col ( ind ), empty_vec ); 35 } 32 36 //flatten back to oringinal 33 37 Coms ( i )->flatten ( Com0 ); … … 36 40 } 37 41 38 voidMixEF::bayes_batch ( const mat &data , const mat &cond, const vec &wData ) {42 double MixEF::bayes_batch ( const mat &data , const mat &cond, const vec &wData ) { 39 43 int ndat = data.cols(); 40 44 int t, i, niter; … … 58 62 int maxi; 59 63 double maxll; 64 65 double levid=0.0; 60 66 //Estim 61 67 while ( !converged ) { 68 levid=0.0; 62 69 // Copy components back to their initial values 63 70 // All necessary information is now in w and Coms0. … … 68 75 //#pragma omp parallel for 69 76 for ( i = 0; i < n; i++ ) { 70 ll ( i ) = Coms ( i )->logpred ( data.get_col ( t ) );77 ll ( i ) = Coms ( i )->logpred ( data.get_col ( t ) , empty_vec); 71 78 wtmp = 0.0; 72 79 wtmp ( i ) = 1.0; … … 99 106 // !!!! note wData ==> this is extra weight of the data record 100 107 // !!!! For typical cases wData=1. 108 vec logevid(n); 101 109 for ( t = 0; t < ndat; t++ ) { 102 110 //#pragma omp parallel for 103 111 for ( i = 0; i < n; i++ ) { 104 112 Coms ( i )-> bayes_weighted ( data.get_col ( t ), empty_vec, W ( i, t ) * wData ( t ) ); 113 logevid(i) = Coms(i)->_ll(); 105 114 } 106 115 weights.bayes ( W.get_col ( t ) * wData ( t ) ); 107 116 } 108 117 levid += weights._ll()+log(weights.posterior().mean() * exp(logevid)); // inner product w*exp(evid) 118 109 119 niter++; 110 120 //TODO better convergence rule. … … 116 126 delete Coms0 ( i ); 117 127 } 128 return levid; 118 129 } 119 130 … … 127 138 128 139 129 double MixEF::logpred ( const vec &yt 140 double MixEF::logpred ( const vec &yt, const vec &cond =empty_vec) const { 130 141 131 142 vec w = weights.posterior().mean(); 132 143 double exLL = 0.0; 133 144 for ( int i = 0; i < Coms.length(); i++ ) { 134 exLL += w ( i ) * exp ( Coms ( i )->logpred ( yt ) );145 exLL += w ( i ) * exp ( Coms ( i )->logpred ( yt , cond ) ); 135 146 } 136 147 return log ( exLL ); -
library/bdm/estim/mixtures.h
r1004 r1009 63 63 MixEF_METHOD method; 64 64 65 //! maximum number of iterations 66 int max_niter; 65 67 public: 66 68 //! Full constructor 67 69 MixEF ( const Array<BMEF*> &Coms0, const vec &alpha0 ) : 68 70 BMEF ( ), Coms ( Coms0.length() ), 69 weights (), est(*this), method ( QB ) {71 weights (), est(*this), method ( QB ), max_niter(10) { 70 72 for ( int i = 0; i < Coms0.length(); i++ ) { 71 73 Coms ( i ) = ( BMEF* ) Coms0 ( i )->_copy(); … … 78 80 MixEF () : 79 81 BMEF ( ), Coms ( 0 ), 80 weights (), est(*this), method ( QB ) {82 weights (), est(*this), method ( QB ), max_niter(10) { 81 83 } 82 84 //! Copy constructor 83 85 MixEF ( const MixEF &M2 ) : BMEF ( ), Coms ( M2.Coms.length() ), 84 weights ( M2.weights ), est(*this), method ( M2.method ) {86 weights ( M2.weights ), est(*this), method ( M2.method ), max_niter(M2.max_niter) { 85 87 for ( int i = 0; i < M2.Coms.length(); i++ ) { 86 88 Coms ( i ) = (BMEF*) M2.Coms ( i )->_copy(); … … 99 101 void bayes ( const mat &yt, const vec &cond ); 100 102 //! batch weighted Bayes rule 101 voidbayes_batch ( const mat &yt, const mat &cond, const vec &wData );102 double logpred ( const vec &yt ) const;103 double bayes_batch ( const mat &yt, const mat &cond, const vec &wData ); 104 double logpred ( const vec &yt, const vec &cond ) const; 103 105 //! return correctly typed posterior (covariant return) 104 106 const eprod_mix& posterior() const { -
library/bdm/stat/merger.cpp
r957 r1009 258 258 sprintf ( dbg_str, "pdf%d", niter ); 259 259 for ( int i = 0; i < Npoints; i++ ) { 260 Mix_pdf ( i ) = Mix.logpred ( Smp_ex.get_col ( i ) );260 Mix_pdf ( i ) = Mix.logpred ( Smp_ex.get_col ( i ), empty_vec ); 261 261 } 262 262 *dbg_file << Name ( dbg_str ) << Mix_pdf; -
library/bdm/stat/merger.h
r957 r1009 233 233 vec dtf = ones ( yt.length() + 1 ); 234 234 dtf.set_subvector ( 0, yt ); 235 return Mix.logpred ( dtf );235 return Mix.logpred ( dtf, vec(0) ); 236 236 } 237 237 //!@} -
library/tests/stresssuite/arx_elem_stress.cpp
r722 r1009 41 41 mlstudent* Ap = Ar.predictor_student(); 42 42 vec Ap_x = Ap->evallogcond_mat ( X, empty_vec ); 43 vec ll_x = Ar.logpred_mat ( X2 );43 vec ll_x = Ar.logpred_mat ( X2 , empty_vec); 44 44 45 45 cout << "normalize : " << xstep*sum ( exp ( Ap_x ) ) << endl; -
library/tests/stresssuite/mixtures_stress.cpp
r886 r1009 35 35 for ( double y = yb ( 0 ); y <= yb ( 1 ); y += ystep, j++ ) { 36 36 rgr ( 1 ) = y; 37 PPdf ( i, j ) = exp ( M.logpred ( rgr ) );37 PPdf ( i, j ) = exp ( M.logpred ( rgr, empty_vec ) ); 38 38 } 39 39 }