Changeset 189
- Timestamp:
- 10/22/08 10:46:36 (16 years ago)
- Location:
- bdm
- Files:
-
- 3 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/estim/mixef.cpp
r180 r189 21 21 Coms ( 0 )->flatten ( Com0 ); 22 22 23 //Copy it to the rest 23 //Copy it to the rest 24 24 for ( i=1;i<n;i++ ) { 25 25 //copy Com0 and create new rvs for them … … 38 38 39 39 } 40 void MixEF::bayesB ( const mat &Data ) {41 this->bayes ( Data );42 }43 40 44 void MixEF::bayes ( const vec &data ) { 45 46 }; 47 48 void MixEF::bayes ( const mat &data ) { 41 void MixEF::bayesB ( const mat &data , const vec &wData ) { 49 42 int ndat=data.cols(); 50 43 int t,i,niter; … … 63 56 // tmp for weights 64 57 vec wtmp = zeros ( n ); 58 int maxi; 59 double maxll; 65 60 //Estim 66 61 while ( !converged ) { … … 75 70 ll ( i ) += weights.logpred ( wtmp ); 76 71 } 77 w = exp ( ll-max ( ll ) ); 78 W.set_col ( t, w/sum ( w ) ); 72 73 maxll = max(ll,maxi); 74 switch (method) { 75 case QB: 76 w = exp ( ll-maxll ); 77 w/=sum(w); 78 break; 79 case EM: 80 w = 0.0; 81 w(maxi) = 1.0; 82 break; 83 } 84 85 W.set_col ( t, w ); 79 86 } 80 87 88 // copy initial statistics 81 89 for ( i=0;i<n;i++ ) { 82 90 Coms ( i )-> set_statistics ( Coms0 ( i ) ); … … 84 92 weights.set_statistics ( &weights0 ); 85 93 94 // Update statistics 95 // !!!! note wData ==> this is extra weight of the data record 96 // !!!! For typical cases wData=1. 86 97 for ( t=0;t<ndat;t++ ) { 87 98 for ( i=0;i<n;i++ ) { 88 Coms ( i )-> bayes ( data.get_col ( t ),W ( i,t ) );99 Coms ( i )-> bayes ( data.get_col ( t ),W ( i,t ) * wData ( t ) ); 89 100 } 90 weights.bayes ( W.get_col ( t ) );101 weights.bayes ( W.get_col ( t ) * wData ( t ) ); 91 102 } 92 103 … … 98 109 //Clean Coms0 99 110 for ( i=0;i<n;i++ ) {delete Coms0 ( i );} 111 } 112 113 void MixEF::bayes ( const vec &data ) { 114 115 }; 116 117 void MixEF::bayes ( const mat &data ) { 118 this->bayesB ( data, ones ( data.cols() ) ); 100 119 }; 101 120 … … 111 130 } 112 131 113 emix* MixEF::predictor (const RV &rv){114 Array<epdf*> pC (n);115 for (int i=0;i<n;i++){pC(i)=Coms(i)->predictor(rv);}132 emix* MixEF::predictor ( const RV &rv ) { 133 Array<epdf*> pC ( n ); 134 for ( int i=0;i<n;i++ ) {pC ( i ) =Coms ( i )->predictor ( rv );} 116 135 emix* tmp; 117 tmp = new emix (rv);118 tmp->set_parameters (weights._epdf().mean(), pC, false);136 tmp = new emix ( rv ); 137 tmp->set_parameters ( weights._epdf().mean(), pC, false ); 119 138 tmp->ownComs(); 120 139 return tmp; -
bdm/estim/mixef.h
r180 r189 20 20 21 21 using namespace itpp; 22 23 enum MixEF_METHOD { EM = 0, QB = 1}; 22 24 23 25 /*! … … 49 51 ////!Indeces of component rvc in common rvc 50 52 53 //! Flag for a method that is used in the inference 54 MixEF_METHOD method; 55 51 56 //! Auxiliary function for use in constructors 52 57 void build_est() { … … 65 70 MixEF ( const Array<BMEF*> &Coms0, const vec &alpha0 ) : 66 71 BM ( RV() ), n ( Coms0.length() ), Coms ( n ), 67 weights ( RV ( "{w }", vec_1 ( n ) ),alpha0 ) {72 weights ( RV ( "{w }", vec_1 ( n ) ),alpha0 ), method(QB) { 68 73 // it_assert_debug ( n>0,"MixEF::MixEF : Empty Component list" ); 69 74 … … 73 78 MixEF () : 74 79 BM ( RV() ), n ( 0 ), Coms ( 0 ), 75 weights ( RV ( "{w }", vec_1 ( 0 ) ),vec ( 0 ) ) {build_est();}80 weights ( RV ( "{w }", vec_1 ( 0 ) ),vec ( 0 ) ),method(QB) {build_est();} 76 81 //! Initializing the mixture by a random pick of centroids from data 77 82 //! \param Com0 Initial component - necessary to determine its type. … … 88 93 //! EM algorithm 89 94 void bayes ( const mat &dt ); 90 void bayesB ( const mat &dt );95 void bayesB ( const mat &dt, const vec &wData ); 91 96 double logpred ( const vec &dt ) const; 92 97 const epdf& _epdf() const {return *est;} … … 94 99 //! Flatten the density as if it was not estimated from the data 95 100 void flatten(double sumw=1.0); 101 102 //!Set which method is to be used 103 void set_method(MixEF_METHOD M){method=M;} 96 104 }; 97 105 -
bdm/stat/emix.h
r183 r189 112 112 return log(x); 113 113 }; 114 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 ) )); 118 } 119 return X; 120 }; 114 121 115 122 emix* marginal ( const RV &rv ) const;