Changeset 175
Legend:
- Unmodified
- Added
- Removed
-
bdm/stat/emix.cpp
r168 r175 23 23 } 24 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 rv29 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 epdf34 };35 // Create rvc36 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 rvinds42 for ( i = 0;i < n;i++ ) {43 // find ith rv in common rv44 rvinds( i ) = mpdfs ( i )->_rv().dataind ( rv );45 // find ith rvc in common rv46 rvcinrv ( i ) = mpdfs ( i )->_rvc().dataind ( rv );47 // find ith rvc in common rv48 rvcinds ( i ) = mpdfs ( i )->_rvc().dataind ( rvc );49 //50 / * if ( rvcinrv ( i ).length() >0 ) {independent = false;}51 if ( rvcinds ( i ).length() >0 ) {independent = false;}*/52 }53 };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 // rvsinrv ( 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 ( rvcinrv ( i ).length() >0 ) {independent = false;} 51 // if ( rvcinds ( i ).length() >0 ) {independent = false;}*/ 52 // } 53 // }; -
bdm/stat/emix.h
r170 r175 68 68 Note that 69 69 */ 70 class mprod: public mpdf { 71 protected: 72 // 73 int n; 70 class mprod: public compositepdf, public mpdf { 71 protected: 74 72 // pointers to epdfs 75 73 Array<epdf*> epdfs; 76 Array<mpdf*> mpdfs;77 //78 //! Indeces of rvs in common rv79 Array<ivec> rvinds;80 //! Indeces of rvc in common rv81 Array<ivec> rvcinrv;82 74 //! Indeces of rvc in common rvc 83 75 Array<ivec> rvcinds; 84 // //! Indicate independence of its factors85 // bool independent;86 // //! Indicate internal creation of mpdfs which must be destroyed87 // bool intermpdfs;88 76 public: 89 77 /*!\brief Constructor from list of mFacs, 90 78 Additional parameter overlap is left for future use. Do not set to true for mprod. 91 79 */ 92 mprod ( Array<mpdf*> mFacs, bool overlap=false ); 80 mprod ( Array<mpdf*> mFacs): compositepdf(mFacs), mpdf(getrv(true),RV()), rvcinds(n), epdfs(n) 81 { setrvc(rv,rvc); 82 setrvcinrv(rvc,rvcinds); 83 setindices(rv); 84 for(int i=0;i<n;i++){epdfs(i)=&(mpdfs(i)->_epdf());} 85 }; 93 86 94 87 double evalpdflog ( const vec &val ) const { … … 99 92 {mpdfs ( i )->condition ( val ( rvcinds ( i ) ) );} 100 93 // add logarithms 101 res += epdfs ( i )->evalpdflog ( val ( rv inds( i ) ) );94 res += epdfs ( i )->evalpdflog ( val ( rvsinrv ( i ) ) ); 102 95 } 103 96 return res; … … 110 103 for ( int i = ( n - 1 );i >= 0;i-- ) { 111 104 if ( rvcinds ( i ).length() > 0 ) { 112 condi = zeros ( rvc inrv.length() + rvcinds.length() );105 condi = zeros ( rvcsinrv.length() + rvcinds.length() ); 113 106 // copy data in condition 114 107 set_subvector ( condi,rvcinds ( i ), cond ); 115 108 // copy data in already generated sample 116 set_subvector ( condi,rvc inrv ( i ), smp );109 set_subvector ( condi,rvcsinrv ( i ), smp ); 117 110 118 111 mpdfs ( i )->condition ( condi ); … … 120 113 smpi = epdfs ( i )->sample(); 121 114 // copy contribution of this pdf into smp 122 set_subvector ( smp,rv inds( i ), smpi );115 set_subvector ( smp,rvsinrv ( i ), smpi ); 123 116 // add ith likelihood contribution 124 117 ll+=epdfs ( i )->evalpdflog ( smpi ); -
bdm/stat/libBM.cpp
r170 r175 183 183 void RV::newids(){ids=linspace ( RVcounter+1, RVcounter+len ),RVcounter+=len;} 184 184 185 RV compositepdf::getrv(bool checkoverlap){ 186 RV rv; //empty rv 187 bool rvaddok; 188 for (int i = 0;i < n;i++ ) { 189 rvaddok=rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs. 190 // If rvaddok==false, mpdfs overlap => assert error. 191 it_assert_debug(rvaddok||(!checkoverlap),"mprod::mprod() input mpdfs overlap in rv!"); 192 }; 193 return rv; 194 } 195 196 void compositepdf::setrvc(const RV &rv, RV &rvc){ 197 for (int i = 0;i < n;i++ ) { 198 rvc.add ( mpdfs ( i )->_rvc().subt ( rv ) ); //add rv to common rvc 199 }; 200 } 201 202 void compositepdf::setindices(const RV &rv){ 203 for (int i = 0;i < n;i++ ) { 204 // find ith rv in common rv 205 rvsinrv ( i ) = mpdfs ( i )->_rv().dataind ( rv ); 206 rvcsinrv ( i ) = mpdfs ( i )->_rvc().dataind ( rv ); 207 } 208 } 209 210 void compositepdf::setrvcinrv(const RV &rvc, Array<ivec> &rvinds){ 211 for (int i = 0;i < n;i++ ) { 212 // find ith rv in common rv 213 rvinds ( i ) = mpdfs ( i )->_rvc().dataind ( rvc ); 214 } 215 } 216 217 185 218 void BM::bayesB(const mat &Data){ 186 219 for(int t=0;t<Data.cols();t++){bayes(Data.get_col(t));} -
bdm/stat/libBM.h
r170 r175 233 233 }; 234 234 235 //!\brief Abstract composition of pdfs, a base for specific classes 236 class compositepdf{ 237 protected: 238 //!Number of mpdfs in the composite 239 int n; 240 //! Elements of composition 241 Array<mpdf*> mpdfs; 242 //! Indeces of rvs in common rv 243 Array<ivec> rvsinrv; 244 //! Indeces of rvc in common rv 245 Array<ivec> rvcsinrv; 246 public: 247 compositepdf(Array<mpdf*> A0): n(A0.length()), mpdfs(A0), rvsinrv(n), rvcsinrv(n){}; 248 RV getrv(bool checkoverlap=false); 249 void setrvc(const RV &rv, RV &rvc); 250 void setindices(const RV &rv); 251 void setrvcinrv(const RV &rvc, Array<ivec> &rvcind); 252 }; 253 235 254 /*! \brief Abstract class for discrete-time sources of data. 236 255