Changeset 182 for bdm/stat/emix.h
- Timestamp:
- 10/17/08 10:28:15 (16 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/stat/emix.h
r181 r182 19 19 20 20 using namespace itpp; 21 22 //this comes first because it is used inside emix! 23 24 /*! \brief Class representing ratio of two densities 25 which arise e.g. by applying the Bayes rule. 26 It represents density in the form: 27 \f[ 28 f(rv|rvc) = \frac{f(rv,rvc)}{f(rvc)} 29 \f] 30 where \f$ f(rvc) = \int f(rv,rvc) d\ rv \f$. 31 32 In particular this type of arise by conditioning of a mixture model. 33 34 At present the only supported operation is evalcond(). 35 */ 36 class mratio: public mpdf { 37 protected: 38 //! Nominator in the form of mpdf 39 const epdf* nom; 40 //!Denominator in the form of epdf 41 epdf* den; 42 //!flag for destructor 43 bool destroynom; 44 public: 45 //!Default constructor. By default, the given epdf is not copied! 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){ 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) const { 60 return exp(nom->evalpdflog(concat(val,cond)) - den->evalpdflog(cond)); 61 } 62 //! Object takes ownership of nom and will destroy it 63 void ownnom(){destroynom=true;} 64 //! Default destructor 65 ~mratio(){delete den; if (destroynom) {delete nom;}} 66 }; 21 67 22 68 /*! … … 41 87 //!Default constructor 42 88 emix ( const RV &rv ) : epdf ( rv ) {}; 43 //! Set weights \c w and components \c Coms , Coms are not copied! 89 //! Set weights \c w and components \c Coms 90 //!By default Coms are copied inside. \param copy can be set to false if Coms live externally. Use method ownComs() if Coms should be destroyed by the destructor. 44 91 void set_parameters ( const vec &w, const Array<epdf*> &Coms, bool copy=true ); 45 92 … … 56 103 return log ( sum ); 57 104 }; 105 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 x 111 } 112 return log(x); 113 }; 114 115 emix* marginal ( const RV &rv ) const; 116 mratio* condition ( const RV &rv ) const; //why not mratio!! 58 117 59 118 //Access methods … … 78 137 Array<epdf*> epdfs; 79 138 //! Indeces of rvc in common rvc 80 Array<ivec> rvcsinrvc;139 Array<ivec> irvcs_rvc; 81 140 //! Indeces of common rvc in rvcs 82 Array<ivec> rvcinrvcs;141 Array<ivec> irvc_rvcs; 83 142 public: 84 143 /*!\brief Constructor from list of mFacs, 85 144 */ 86 mprod ( Array<mpdf*> mFacs ) : compositepdf ( mFacs ), mpdf ( getrv ( true ),RV() ), epdfs ( n ), rvcsinrvc( n ) {145 mprod ( Array<mpdf*> mFacs ) : compositepdf ( mFacs ), mpdf ( getrv ( true ),RV() ), epdfs ( n ), irvcs_rvc ( n ), irvc_rvcs ( n ) { 87 146 setrvc ( rv,rvc ); 88 147 setindices ( rv ); 89 148 for ( int i = 0;i < n;i++ ) { 90 // find ith rvc in common rv 91 rvcsinrvc ( i ) = mpdfs ( i )->_rvc().dataind ( rvc ); 92 //and vice-versa 93 rvcinrvcs ( i ) = rvc.dataind ( mpdfs ( i )->_rvc() ); 149 // establish link between rvc and rvcs 150 rvc.dataind ( mpdfs ( i )->_rvc(), irvc_rvcs ( i ), irvcs_rvc ( i ) ); 94 151 } 95 152 … … 99 156 }; 100 157 101 double evalcond ( const vec &val, const vec &cond ) const{158 double evalcond ( const vec &val, const vec &cond ) { 102 159 int i; 103 160 double res = 0.0; 104 161 vec condi; 105 for ( i = n - 1;i > 0;i++) {106 if ( ( rvcsinrvc ( i ).length() > 0 ) || ( rvcsinrv ( i ).length() > 0 ) ) {107 condi = zeros ( rvcsinrvc ( i ).length() +rvcsinrv ( i ).length() );162 for ( i = n - 1;i >= 0;i-- ) { 163 if ( ( irvcs_rvc ( i ).length() > 0 ) || ( irvcs_rv ( i ).length() > 0 ) ) { 164 condi = zeros ( irvcs_rvc ( i ).length() +irvcs_rv ( i ).length() ); 108 165 //copy part of the rvc into condi 109 set_subvector ( condi, rvcinrvcs ( i ), get_vec ( cond,rvcsinrvc( i ) ) );166 set_subvector ( condi, irvcs_rvc ( i ), get_vec ( cond,irvc_rvcs ( i ) ) ); 110 167 //copy part of the rv into condi 111 set_subvector ( condi, rvinrvcs ( i ), get_vec ( val,rvcsinrv( i ) ) );168 set_subvector ( condi, irvcs_rv ( i ), get_vec ( val,irv_rvcs ( i ) ) ); 112 169 mpdfs ( i )->condition ( condi ); 113 170 } … … 115 172 res += epdfs ( i )->evalpdflog ( val ( rvsinrv ( i ) ) ); 116 173 } 117 return res;174 return exp ( res ); 118 175 } 119 176 vec samplecond ( const vec &cond, double &ll ) { … … 123 180 ll = 0; 124 181 for ( int i = ( n - 1 );i >= 0;i-- ) { 125 if ( ( rvcsinrvc ( i ).length() > 0 ) || ( rvcsinrv ( i ).length() > 0 ) ) {126 condi = zeros ( rvcsinrv ( i ).length() + rvcsinrvc ( i ).length() );182 if ( ( irvcs_rvc ( i ).length() > 0 ) || ( irvcs_rv ( i ).length() > 0 ) ) { 183 condi = zeros ( irvcs_rv ( i ).length() + irvcs_rvc ( i ).length() ); 127 184 // copy data in condition 128 set_subvector ( condi, rvcsinrvc ( i ), cond);185 set_subvector ( condi,irvcs_rvc ( i ), get_vec ( cond, irvc_rvcs ( i ) ) ); 129 186 // copy data in already generated sample 130 set_subvector ( condi, rvinrvcs ( i ), get_vec ( smp,rvcsinrv( i ) ) );187 set_subvector ( condi,irvcs_rv ( i ), get_vec ( smp,irv_rvcs ( i ) ) ); 131 188 132 189 mpdfs ( i )->condition ( condi ); … … 220 277 }; 221 278 }; 279 222 280 #endif //MX_H