Changeset 165
- Timestamp:
- 09/16/08 10:46:21 (16 years ago)
- Files:
-
- 6 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/stat/emix.cpp
r145 r165 22 22 return Coms ( i )->sample(); 23 23 } 24 25 mprod::mprod ( Array<mpdf*> mFacs, bool overlap) : mpdf ( RV(), RV() ), n ( mFacs.length() ), epdfs ( n ), mpdfs ( mFacs ), rvinds ( n ), rvcinrv ( n ), rvcinds ( n ) { 26 int i; 27 bool rvaddok; 28 // Create rv 29 for ( i = 0;i < n;i++ ) { 30 rvaddok=rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs. 31 // If rvaddok==false, mpdfs overlap => assert error. 32 it_assert_debug(rvaddok||overlap,"mprod::mprod() input mpdfs overlap in rv!"); 33 epdfs ( i ) = & ( mpdfs ( i )->_epdf() ); // add pointer to epdf 34 }; 35 // Create rvc 36 for ( i = 0;i < n;i++ ) { 37 rvc.add ( mpdfs ( i )->_rvc().subt ( rv ) ); //add rv to common rvs. 38 }; 39 40 independent = true; 41 //test rvc of mpdfs and fill rvinds 42 for ( i = 0;i < n;i++ ) { 43 // find ith rv in common rv 44 rvinds ( i ) = mpdfs ( i )->_rv().dataind ( rv ); 45 // find ith rvc in common rv 46 rvcinrv ( i ) = mpdfs ( i )->_rvc().dataind ( rv ); 47 // find ith rvc in common rv 48 rvcinds ( i ) = mpdfs ( i )->_rvc().dataind ( rvc ); 49 // 50 if ( rvcinds ( i ).length() >0 ) {independent = false;} 51 if ( rvcinds ( i ).length() >0 ) {independent = false;} 52 } 53 }; -
bdm/stat/emix.h
r162 r165 87 87 // bool intermpdfs; 88 88 public: 89 //!Constructor from list of eFacs or list of mFacs 90 mprod ( Array<mpdf*> mFacs ) : mpdf ( RV(), RV() ), n ( mFacs.length() ), epdfs ( n ), mpdfs ( mFacs ), rvinds ( n ), rvcinrv ( n ), rvcinds ( n ) { 91 int i; 92 // Create rv 93 for ( i = 0;i < n;i++ ) { 94 rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs. 95 epdfs ( i ) = & ( mpdfs ( i )->_epdf() ); // add pointer to epdf 96 }; 97 // Create rvc 98 for ( i = 0;i < n;i++ ) { 99 rvc.add ( mpdfs ( i )->_rv().subt ( rv ) ); //add rv to common rvs. 100 }; 101 102 independent = true; 103 //test rvc of mpdfs and fill rvinds 104 for ( i = 0;i < n;i++ ) { 105 // find ith rv in common rv 106 rvinds ( i ) = mpdfs ( i )->_rv().dataind ( rv ); 107 // find ith rvc in common rv 108 rvcinrv ( i ) = mpdfs ( i )->_rvc().dataind ( rv ); 109 // find ith rvc in common rv 110 rvcinds ( i ) = mpdfs ( i )->_rvc().dataind ( rvc ); 111 // 112 if ( rvcinds ( i ).length() >0 ) {independent = false;} 113 if ( rvcinds ( i ).length() >0 ) {independent = false;} 114 } 115 }; 89 /*!\brief Constructor from list of mFacs, 90 Additional parameter overlap is left for future use. Do not set to true for mprod. 91 */ 92 mprod ( Array<mpdf*> mFacs, bool overlap=false ); 116 93 117 94 double evalpdflog ( const vec &val ) const { … … 129 106 vec smp=zeros ( rv.count() ); 130 107 vec condi; 108 vec smpi; 109 ll = 0; 131 110 for ( int i = ( n - 1 );i >= 0;i-- ) { 132 111 if ( rvcinds ( i ).length() > 0 ) { … … 139 118 mpdfs ( i )->condition ( condi ); 140 119 } 120 smpi = epdfs ( i )->sample(); 141 121 // copy contribution of this pdf into smp 142 set_subvector ( smp,rvinds ( i ), epdfs ( i )->sample() ); 122 set_subvector ( smp,rvinds ( i ), smpi ); 123 // add ith likelihood contribution 124 ll+=epdfs(i)->evalpdflog(smpi); 143 125 } 144 126 return smp; -
bdm/stat/libBM.cpp
r162 r165 70 70 tsize = sum ( sizes ); 71 71 len = ids.length(); 72 return ( index.length() <rv2.len );72 return ( index.length() ==rv2.len ); 73 73 } 74 74 else { //rv2 is empty … … 141 141 142 142 ivec RV::dataind ( RV rv2 ) const { 143 str str2 = rv2.tostr();144 143 ivec res ( 0 ); 145 ivec part; 146 int i; 147 for ( i = 0;i < len;i++ ) { 148 part = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) ); 149 res = concat ( res, part ); 144 if ( rv2.count()>0 ) { 145 str str2 = rv2.tostr(); 146 ivec part; 147 int i; 148 for ( i = 0;i < len;i++ ) { 149 part = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) ); 150 res = concat ( res, part ); 151 } 150 152 } 151 153 return res; 154 152 155 } 153 156 154 157 RV RV::subt ( const RV rv2 ) const { 155 cout << *this <<endl;156 cout << rv2 <<endl;157 158 158 ivec res = this->findself ( rv2 ); // nonzeros 159 159 ivec valid = itpp::find ( res == -1 ); //-1 => value not found => it remains -
bdm/stat/libBM.h
r162 r165 77 77 //! Compare if \c rv2 is identical to this \c RV 78 78 bool equal (const RV &rv2 ) const; 79 //! Add (concat) another variable to the current one, \return 0 if all rv2 were added, 1if rv2 is in conflict79 //! Add (concat) another variable to the current one, \return true if all rv2 were added, false if rv2 is in conflict 80 80 bool add ( const RV &rv2 ); 81 81 //! Subtract another variable from the current one -
bdm/stat/merger.h
r163 r165 28 28 */ 29 29 30 class merger {30 class merger : public mprod{ 31 31 protected: 32 mat Smp; 33 mprod joint; 32 //! Additional pdf on the part in condition (if undefined); 33 enorm<fsqmat> condpdf; 34 //! Find potential overlaps in rv 35 Array<ivec> overlaps; 34 36 public: 35 37 //!Default constructor 36 merger (const Array<mpdf*> &in_sources) : joint(in_sources) { 37 RV rvc=joint._rvc(); 38 merger (const Array<mpdf*> &in_sources) : mprod(in_sources,true), condpdf(rvc), overlaps(rv.count()) { 39 if (rvc.count()>0) { 40 vec mu = zeros(rvc.count()); 41 mat R = 100*eye(rvc.count()); 42 condpdf.set_parameters(mu,R); 43 } 44 for (int i=0;i<rv.count();i++){ // cycle over rv 45 ivec tmp(0); 46 for (int j=0;j<n;j++){//cycle over rvinds 47 ivec id=itpp::find(rvinds(j)==i); 48 tmp=concat(tmp,id); // add i to tmp if found 49 } 50 overlaps(i)=tmp; 51 } 38 52 } 53 //! sample from merged density 54 //! weight w is a 55 vec sample(double &w, vec smp0=zeros ( rv.count()+rvc.count())){ 56 // result 57 vec smp=smp0; 58 vec cond=sample(condpdf); // Just like in samplecond, here it is not given! 59 // temporary 60 mat smpi=zeros(rv.count()+rvc.count(), n); 61 vec condi; 62 vec ll(n); 63 64 // Consider arithmetic mean as a proposal density 65 ll(i) = 0; 66 for ( int i = ( n - 1 );i >= 0;i-- ) { 67 if ( rvcinds ( i ).length() > 0 ) { 68 condi = zeros ( rvcinrv.length() + rvcinds.length() ); 69 // copy data in condition 70 set_subvector ( condi,rvcinds ( i ), cond ); 71 // copy data in already generated sample 72 set_subvector ( condi,rvcinrv ( i ), smp ); 39 73 74 mpdfs ( i )->condition ( condi ); 75 } 76 smpi.set_col(i, epdfs ( i )->sample()); 77 // add ith likelihood contribution 78 ll+=epdfs(i)->evalpdflog(smpi); 79 } 80 // Now lets analyze samples smpi 81 for (int i=0;i<rv.count();i++){ 82 // number of components 83 int noc = overlaps(i).length(); 84 if (noc<2) { // only one sample in this dimension, take it 85 smp(i) = smpi(i,overlaps(i)(0)); 86 } 87 else { 88 //pick dimension 89 double unis = UniRNG.sample(); 90 dim = 0; 91 while(unis<(dim+1)/noc) {dim++;} // 92 //in dim we now have randomly picked dimension 93 smp(i) = smpi(i,overlaps(i)(dim)); 94 } 95 } 96 97 // now we have sample how to evaluate weight? 98 return smp; 99 100 // copied from mprod.sample 101 }; 40 102 // project 41 103 //! for future use -
tests/merger_test.cpp
r164 r165 26 26 27 27 merger M(A); 28 28 eEmp res=M.merge(100); 29 30 cout << res.mean() << end; 29 31 //Exit program: 30 32 return 0;