Changeset 197

Show
Ignore:
Timestamp:
11/04/08 14:54:33 (15 years ago)
Author:
smidl
Message:

opravy v bdm

Location:
bdm
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • bdm/estim/merger.cpp

    r193 r197  
    4747        mat V0=1e-8*eye ( rv.count() +1 ); 
    4848        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); 
    5054 
    5155        // ============= MAIN LOOP ================== 
     
    5963                //Re-estimate Mix 
    6064                //Re-Initialize Mixture model 
    61                 Mix.init ( &A0, Smp_ex, Nc ); 
     65                Mix.flatten(&Mix_init); 
    6266                Mix.bayesB ( Smp_ex, w*Ns ); 
    6367                Mpred = Mix.predictor ( rv ); // Allocation => must be deleted at the end!! 
    6468 
    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 ) );} 
    7875 
    7976                sprintf ( str,"Mpdf%d",niter ); 
     
    109106                                if ( rv.count() > ( mpdfs ( i )->_rv().count() + mpdfs ( i )->_rvc().count() ) ) { 
    110107                                        /////////////// 
    111                                         cout << Mpred->mean() <<endl; 
    112108                                        // There are variales unknown to mpdfs(i) : rvzs 
    113109                                        mpdf* tmp_cond = Mpred->condition ( rvzs ( i ) ); 
     
    127123                                        lw_src ( k ) += mpdfs ( i )->_epdf().evalpdflog ( dls ( i )->get_val ( Smp ( k ) ) ); 
    128124                                } 
    129                                  
     125 
    130126                        } 
    131127                        lW.set_row ( i, lw_src ); // do not divide by mix 
     
    161157                // ==== stopping rule === 
    162158                niter++; 
    163                 converged = ( niter>4 ); 
     159                converged = ( niter>9 ); 
    164160        } 
    165161 
  • bdm/estim/mixef.cpp

    r195 r197  
    3131                int ind=floor(ndat*UniRNG.sample()); 
    3232                Coms ( i )->bayes ( Data.get_col ( ind ),1.0 ); 
     33                //flatten back to oringinal 
     34                Coms(i)->flatten(Com0); 
    3335        } 
    3436 
     
    139141        return tmp; 
    140142} 
     143 
     144void 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  
    3939TODO: Extend BM to use rvc. 
    4040*/ 
    41 class MixEF: public BM { 
     41class MixEF: public BMEF { 
    4242protected: 
    4343        //!Number of components 
     
    6969        //! Full constructor 
    7070        MixEF ( const Array<BMEF*> &Coms0, const vec &alpha0 ) : 
    71                         BM ( RV() ), n ( Coms0.length() ), Coms ( n ), 
     71                        BMEF ( RV() ), n ( Coms0.length() ), Coms ( n ), 
    7272                        weights ( RV ( "{w }", vec_1 ( n ) ),alpha0 ), method(QB) { 
    7373        //      it_assert_debug ( n>0,"MixEF::MixEF : Empty Component list" ); 
     
    7676                build_est(); 
    7777        }; 
     78        //! Constructor of empty mixture 
    7879        MixEF () : 
    79                         BM ( RV() ), n ( 0 ), Coms ( 0 ), 
     80                        BMEF ( RV() ), n ( 0 ), Coms ( 0 ), 
    8081                        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                   } 
    8190        //! Initializing the mixture by a random pick of centroids from data 
    8291        //! \param Com0 Initial component - necessary to determine its type. 
     
    98107        emix* predictor(const RV &rv); 
    99108        //! 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); 
    101110        //! Access function 
    102111        BMEF* _Coms(int i){return Coms(i);} 
  • bdm/stat/libEF.cpp

    r193 r197  
    9898        R= ldR.to_mat()  ; 
    9999} 
    100  
    101 void egiw::pow(double p){V*=p;nu*=p;} 
    102100 
    103101vec egamma::sample() const { 
  • bdm/stat/libEF.h

    r193 r197  
    9393        virtual void flatten ( const BMEF * B ) {it_error ( "Not implemented" );} 
    9494        //!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;}; 
    9698}; 
    9799 
     
    190192        //! returns a pointer to the internal statistics. Use with Care! 
    191193        double& _nu() {return nu;} 
    192         void pow ( double p ); 
     194        void pow ( double p ){V*=p;nu*=p;}; 
    193195}; 
    194196 
     
    266268        } 
    267269        void flatten ( const BMEF* B ) { 
    268                 const eDirich* E=dynamic_cast<const eDirich*> ( B ); 
     270                const multiBM* E=dynamic_cast<const multiBM*> ( B ); 
    269271                // 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 ) ); 
    272274                if ( evalll ) {last_lognc=est.lognc();} 
    273275        }