Changeset 180 for bdm/estim

Show
Ignore:
Timestamp:
10/15/08 19:08:06 (16 years ago)
Author:
smidl
Message:

Modifications of BDM to reflect changes in basics

Location:
bdm/estim
Files:
6 modified

Legend:

Unmodified
Added
Removed
  • bdm/estim/arx.cpp

    r174 r180  
    5454        set_parameters ( A0->V,A0->nu ); 
    5555} 
     56 
     57enorm<ldmat>* ARX::predictor(const RV &rv){ 
     58        mat mu(rv.count(), 1); 
     59        mat R(rv.count(),rv.count()); 
     60        enorm<ldmat>* tmp;  
     61        tmp=new enorm<ldmat>(rv); 
     62         
     63        est.mean_mat(mu,R); 
     64        tmp->set_parameters(mu.get_col(0),ldmat(R)); 
     65        return tmp; 
     66} 
     67 
    5668/*! \brief Return the best structure 
    5769@param Eg a copy of GiW density that is being examined 
  • bdm/estim/arx.h

    r174 r180  
    6666        const epdf& _epdf() const {return est;} 
    6767        double logpred ( const vec &dt ) const; 
    68         void flatten (BMEF* B ) { 
    69                 ARX* A=dynamic_cast<ARX*>(B); 
     68        void flatten (const BMEF* B ) { 
     69                const ARX* A=dynamic_cast<const ARX*>(B); 
    7070                // nu should be equal to B.nu 
    7171                est.pow ( A->nu/nu); 
     
    7373        } 
    7474 
     75        enorm<ldmat>* predictor(const RV &rv);  
    7576        //! Brute force structure estimation.\return indeces of accepted regressors. 
    7677        ivec structure_est ( egiw Eg0 ); 
  • bdm/estim/merger.cpp

    r176 r180  
    1818                        break; 
    1919        } 
    20         return vec(0); 
     20        return vec ( 0 ); 
    2121} 
    2222 
    2323void merger::merge ( const epdf* g0 ) { 
    24         mat Smp ( rv.count(),Ns ); 
    25         mat lW=zeros ( n,Ns ); 
     24        it_file dbg ( "merger_debug.it" ); 
    2625 
     26        it_assert_debug ( rv.equal ( g0->_rv() ),"Incompatible g0" ); 
     27        //Empirical density - samples 
     28        eEmp eSmp ( rv,Ns ); 
     29        eSmp.set_parameters ( ones ( Ns ), g0 ); 
     30        Array<vec> &Smp = eSmp._samples(); //aux 
     31        vec &w = eSmp._w(); //aux 
     32 
     33        mat Smp_ex =ones ( rv.count() +1,Ns ); // Extended samples The last row is ones for the ARX model 
     34        for ( int i=0;i<Ns;i++ ) {      set_col_part ( Smp_ex,i,Smp ( i ) );} 
     35 
     36        dbg << Name ( "Smp0" ) << Smp_ex; 
     37 
     38        // Stuff for merging 
    2739        vec lw_src ( Ns ); 
    2840        vec lw_mix ( Ns ); 
     41        mat lW=zeros ( n,Ns ); 
    2942        vec vec0 ( 0 ); 
    3043 
    31         //Make sure g0 is compatible 
    32         it_assert_debug ( rv.equal(g0->_rv()),"Incompatible g0" ); 
     44        // Initial component in the mixture model 
     45        mat V0=1e-8*eye ( rv.count() +1 ); 
     46        ARX A0 ( RV ( "{th_r  }", vec_1 ( rv.count() * ( rv.count() +1 ) ) ),\ 
     47                 V0, 4.0 ); //initial guess of Mix: zero mean, large variance 
    3348 
    34         // Initial component in the mixture model from g0 
    35         mat V0=1e-8*eye ( rv.count() +1 ); 
    36         ARX A0 ( rv, V0, 3.0 ); //initial guess of Mix: zero mean, large variance 
    3749 
    38         //generate samples from the proposal g0 
    39         for ( int i =0; i<Ns; i++ ) {Smp.set_col ( i,g0->sample() );} 
    4050 
    41         //Initialize Mixture model 
    42         Mix.init ( &A0, Smp, Nc ); 
     51        // ============= MAIN LOOP ================== 
     52        bool converged=false; 
     53        int niter = 0; 
     54        char str[100]; 
     55         
     56        vec Mix_pdf(Ns); 
     57        while ( !converged ) { 
     58                //Re-estimate Mix 
     59                //Re-Initialize Mixture model 
     60                Mix.init ( &A0, Smp_ex, Nc ); 
     61                Mix.bayesB ( Smp_ex ); 
    4362 
    44         bool converged=false; 
    45         while ( !converged ) { 
    46                 Mix.bayesB ( Smp ); 
    47                 //Generate weight for each particle 
     63                // Generate new samples 
     64                eSmp.set_samples ( Mix.predictor ( rv ) ); 
     65                for ( int i=0;i<Ns;i++ ) {      set_col_part ( Smp_ex,i,Smp ( i ) );} 
     66 
     67                sprintf ( str,"Mpdf%d",niter ); 
     68                for (int i=0;i<Ns;i++){Mix_pdf(i) = Mix.logpred(Smp_ex.get_col(i));} 
     69                dbg << Name ( str ) << Mix_pdf; 
     70 
     71                sprintf ( str,"Smp%d",niter ); 
     72                dbg << Name ( str ) << Smp_ex; 
     73 
     74                //Importace weighting 
    4875                for ( int i=0;i<n;i++ ) { 
    4976                        //Split according to dependency in rvs 
     
    5178                        if ( rvsinrv ( i ).length() ==rv.count() ) { 
    5279                                // no need for conditioning or marginalization 
    53                                 lw_src=mpdfs ( i )->_epdf().evalpdflog_m ( Smp ); 
    54                                 lw_mix=Mix.logpred_m ( Smp ); 
     80                                for ( int j=0;j<Ns;j++ ) { 
     81                                        lw_src ( j ) =mpdfs ( i )->_epdf().evalpdflog ( Smp ( j ) ); 
     82                                } 
    5583                        } 
    56                         lW.set_row ( i, lw_src-lw_mix ); 
     84                        lW.set_row ( i, lw_src ); // do not divide by mix 
    5785                } 
    58                 converged = true; 
     86                //Importance of the mixture 
     87                for ( int j=0;j<Ns;j++ ) { 
     88                        lw_mix ( j ) =Mix.logpred ( Smp_ex.get_col ( j ) ); 
     89                } 
     90                sprintf ( str,"lW%d",niter ); 
     91                dbg << Name ( str ) << lW; 
     92 
     93                w = lognorm_merge ( lW ); //merge 
     94 
     95                sprintf ( str,"w%d",niter ); 
     96                dbg << Name ( str ) << w; 
     97 
     98                //Importance weighting 
     99                w /=exp ( lw_mix ); // hoping that it is not numerically sensitive... 
     100                //renormalize 
     101                w /=sum ( w ); 
     102 
     103                eSmp.resample(); // So that it can be used in bayes 
     104                for ( int i=0;i<Ns;i++ ) {      set_col_part ( Smp_ex,i,Smp ( i ) );} 
     105 
     106                sprintf ( str,"Smp_res%d",niter ); 
     107                dbg << Name ( str ) << Smp; 
     108 
     109                // ==== stopping rule === 
     110                niter++; 
     111                converged = ( niter>3 ); 
    59112        } 
    60113 
  • bdm/estim/merger.h

    r177 r180  
    4141                        compositepdf ( S ), epdf ( getrv ( false ) ), 
    4242                        Mix ( Array<BMEF*> ( 0 ),vec ( 0 ) ) 
    43         { beta=2.0; Ns=100; Nc=10;} 
     43        {setindices(rv); beta=2.0; Ns=100; Nc=10;} 
    4444        //! Set internal parameters used in approximation 
    4545        void set_parameters ( double beta0, int Ns0, int Nc0 ) {        beta=beta0;Ns=Ns0;Nc=Nc0;} 
     
    6060        //! weight w is a 
    6161        vec sample ( )const { return Mix._epdf().sample();} 
    62         double evalpdflog ( const vec &dt ) const{ return Mix._epdf().evalpdflog ( dt );} 
     62        double evalpdflog ( const vec &dt ) const{ vec dtf=zeros(dt.length()+1); dtf.set_subvector(0,dt); return Mix.logpred ( dtf );} 
    6363        vec mean()const {return Mix._epdf().mean();} 
    6464        //! for future use 
  • bdm/estim/mixef.cpp

    r176 r180  
    99        Coms.set_size ( c ); 
    1010        n=c; 
    11         weights.set_parameters ( 1/ ( double ) c*ones ( c ) ); //assume at least one observation in each comp. 
     11        weights.set_parameters ( ones ( c ) ); //assume at least one observation in each comp. 
    1212        //est will be done at the end 
    1313        // 
     
    3030                //pick one datum 
    3131                int ind=ndat*UniRNG.sample(); 
    32                 Coms ( i )->bayes ( Data.get_col ( ind ),ndat/n ); 
     32                Coms ( i )->bayes ( Data.get_col ( ind ),1.0 ); 
    3333        } 
    3434 
     
    110110        return log ( exLL ); 
    111111} 
     112 
     113emix* MixEF::predictor(const RV &rv){ 
     114        Array<epdf*> pC(n); 
     115        for(int i=0;i<n;i++){pC(i)=Coms(i)->predictor(rv);} 
     116        emix* tmp; 
     117        tmp = new emix(rv); 
     118        tmp->set_parameters(weights._epdf().mean(), pC, false); 
     119        tmp->ownComs(); 
     120        return tmp; 
     121} 
  • bdm/estim/mixef.h

    r177 r180  
    3333The characteristic feature of this model is that if the exact values of the latent variable were known, estimation of the parameters can be handled by a single model. For example, for the case of mixture models, posterior density for each component parameters would be a BayesianModel from Exponential Family. 
    3434 
    35 This class uses EM-style type algorithms for estimation of its parameters. Under this simplification, the posterior density is a product of exponential family members, hence approximate estimation project this class itself belongs to the exponential family. 
     35This class uses EM-style type algorithms for estimation of its parameters. Under this simplification, the posterior density is a product of exponential family members, hence under EM-style approximate estimation this class itself belongs to the exponential family. 
    3636 
    3737TODO: Extend BM to use rvc. 
     
    4848        eprod* est; 
    4949        ////!Indeces of component rvc in common rvc 
    50  
     50         
    5151        //! Auxiliary function for use in constructors 
    5252        void build_est() { 
     
    9191        double logpred ( const vec &dt ) const; 
    9292        const epdf& _epdf() const {return *est;} 
     93        emix* predictor(const RV &rv); 
     94        //! Flatten the density as if it was not estimated from the data 
     95        void flatten(double sumw=1.0); 
    9396}; 
    9497