Changeset 197
- Timestamp:
- 11/04/08 14:54:33 (16 years ago)
- Location:
- bdm
- Files:
-
- 5 modified
Legend:
- Unmodified
- Added
- Removed
-
bdm/estim/merger.cpp
r193 r197 47 47 mat V0=1e-8*eye ( rv.count() +1 ); 48 48 ARX A0 ( RV ( "{th_r }", vec_1 ( rv.count() * ( rv.count() +1 ) ) ),\ 49 V0, rv.count() *rv.count() +3.0 ); //initial guess of Mix: zero mean, large variance 49 V0, rv.count() *rv.count() +5.0 ); //initial guess of Mix: zero mean, large variance 50 51 Mix.init ( &A0, Smp_ex, Nc ); 52 //Preserve initial mixture for repetitive estimation via flattening 53 MixEF Mix_init(Mix); 50 54 51 55 // ============= MAIN LOOP ================== … … 59 63 //Re-estimate Mix 60 64 //Re-Initialize Mixture model 61 Mix. init ( &A0, Smp_ex, Nc);65 Mix.flatten(&Mix_init); 62 66 Mix.bayesB ( Smp_ex, w*Ns ); 63 67 Mpred = Mix.predictor ( rv ); // Allocation => must be deleted at the end!! 64 68 65 if ( 1 ) { 66 // Generate new samples 67 eSmp.set_samples ( Mpred ); 68 for ( int i=0;i<Ns;i++ ) { set_col_part ( Smp_ex,i,Smp ( i ) );} 69 } 70 else { 71 for ( int ii=0;ii<10;ii++ ) { 72 for ( int jj=0; jj<10; jj++ ) { 73 Smp ( ii+jj*10 ) =vec_2 ( -1.0+6*ii/10.0, -1.0+6*jj/10.0 ); 74 } 75 } 76 for ( int i=0;i<Ns;i++ ) { set_col_part ( Smp_ex,i,Smp ( i ) );} 77 } 69 sprintf ( str,"Mpred_mean%d",niter ); 70 dbg << Name ( str ) << Mpred->mean(); 71 72 // Generate new samples 73 eSmp.set_samples ( Mpred ); 74 for ( int i=0;i<Ns;i++ ) { set_col_part ( Smp_ex,i,Smp ( i ) );} 78 75 79 76 sprintf ( str,"Mpdf%d",niter ); … … 109 106 if ( rv.count() > ( mpdfs ( i )->_rv().count() + mpdfs ( i )->_rvc().count() ) ) { 110 107 /////////////// 111 cout << Mpred->mean() <<endl;112 108 // There are variales unknown to mpdfs(i) : rvzs 113 109 mpdf* tmp_cond = Mpred->condition ( rvzs ( i ) ); … … 127 123 lw_src ( k ) += mpdfs ( i )->_epdf().evalpdflog ( dls ( i )->get_val ( Smp ( k ) ) ); 128 124 } 129 125 130 126 } 131 127 lW.set_row ( i, lw_src ); // do not divide by mix … … 161 157 // ==== stopping rule === 162 158 niter++; 163 converged = ( niter> 4);159 converged = ( niter>9 ); 164 160 } 165 161 -
bdm/estim/mixef.cpp
r195 r197 31 31 int ind=floor(ndat*UniRNG.sample()); 32 32 Coms ( i )->bayes ( Data.get_col ( ind ),1.0 ); 33 //flatten back to oringinal 34 Coms(i)->flatten(Com0); 33 35 } 34 36 … … 139 141 return tmp; 140 142 } 143 144 void MixEF::flatten(const BMEF* M2){ 145 const MixEF* Mix2=dynamic_cast<const MixEF*>(M2); 146 it_assert_debug(Mix2->n==n,"Different no of coms"); 147 //Flatten each component 148 for (int i=0; i<n;i++){ 149 Coms(i)->flatten(Mix2->Coms(i)); 150 } 151 //Faltten weights 152 weights.flatten(&(Mix2->weights)); 153 } -
bdm/estim/mixef.h
r193 r197 39 39 TODO: Extend BM to use rvc. 40 40 */ 41 class MixEF: public BM {41 class MixEF: public BMEF { 42 42 protected: 43 43 //!Number of components … … 69 69 //! Full constructor 70 70 MixEF ( const Array<BMEF*> &Coms0, const vec &alpha0 ) : 71 BM ( RV() ), n ( Coms0.length() ), Coms ( n ),71 BMEF ( RV() ), n ( Coms0.length() ), Coms ( n ), 72 72 weights ( RV ( "{w }", vec_1 ( n ) ),alpha0 ), method(QB) { 73 73 // it_assert_debug ( n>0,"MixEF::MixEF : Empty Component list" ); … … 76 76 build_est(); 77 77 }; 78 //! Constructor of empty mixture 78 79 MixEF () : 79 BM ( RV() ), n ( 0 ), Coms ( 0 ),80 BMEF ( RV() ), n ( 0 ), Coms ( 0 ), 80 81 weights ( RV ( "{w }", vec_1 ( 0 ) ),vec ( 0 ) ),method(QB) {build_est();} 82 //! Copy constructor 83 MixEF(const MixEF &M2): BMEF ( RV() ), n ( M2.n ), Coms ( n ), 84 weights ( M2.weights ), method(M2.method) { 85 // it_assert_debug ( n>0,"MixEF::MixEF : Empty Component list" ); 86 87 for ( int i=0;i<n;i++ ) {Coms ( i ) = M2.Coms ( i )->_copy_();} 88 build_est(); 89 } 81 90 //! Initializing the mixture by a random pick of centroids from data 82 91 //! \param Com0 Initial component - necessary to determine its type. … … 98 107 emix* predictor(const RV &rv); 99 108 //! Flatten the density as if it was not estimated from the data 100 void flatten( double sumw=1.0);109 void flatten(const BMEF* M2); 101 110 //! Access function 102 111 BMEF* _Coms(int i){return Coms(i);} -
bdm/stat/libEF.cpp
r193 r197 98 98 R= ldR.to_mat() ; 99 99 } 100 101 void egiw::pow(double p){V*=p;nu*=p;}102 100 103 101 vec egamma::sample() const { -
bdm/stat/libEF.h
r193 r197 93 93 virtual void flatten ( const BMEF * B ) {it_error ( "Not implemented" );} 94 94 //!Flatten the posterior as if to keep nu0 data 95 virtual void flatten ( double nu0 ) {it_error ( "Not implemented" );} 95 // virtual void flatten ( double nu0 ) {it_error ( "Not implemented" );} 96 97 BMEF* _copy_ ( bool changerv=false ) {it_error ( "function _copy_ not implemented for this BM" ); return NULL;}; 96 98 }; 97 99 … … 190 192 //! returns a pointer to the internal statistics. Use with Care! 191 193 double& _nu() {return nu;} 192 void pow ( double p ) ;194 void pow ( double p ){V*=p;nu*=p;}; 193 195 }; 194 196 … … 266 268 } 267 269 void flatten ( const BMEF* B ) { 268 const eDirich* E=dynamic_cast<const eDirich*> ( B );270 const multiBM* E=dynamic_cast<const multiBM*> ( B ); 269 271 // sum(beta) should be equal to sum(B.beta) 270 const vec &Eb= const_cast<eDirich*> ( E )->_beta();271 est.pow ( sum ( beta ) /sum ( Eb) );272 const vec &Eb=E->beta;//const_cast<multiBM*> ( E )->_beta(); 273 beta*= ( sum ( Eb) /sum ( beta ) ); 272 274 if ( evalll ) {last_lognc=est.lognc();} 273 275 }