Changeset 1014 for library

Show
Ignore:
Timestamp:
05/27/10 23:07:57 (14 years ago)
Author:
smidl
Message:

Mixtures example

Location:
library/bdm/estim
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/estim/mixtures.cpp

    r1013 r1014  
    1818//      Coms(0)->set_evalll(false); 
    1919        Coms ( 0 )->bayes_batch ( Data ); 
    20         // Flatten it to its original shape 
    21         shared_ptr<BMEF> SharpCom((BMEF*)Coms(0)->_copy()); 
     20 
    2221        Coms ( 0 )->flatten ( Com0 );  
    2322 
     
    3433                } else { // pick at random 
    3534                        int ind = (int) floor ( ndat * UniRNG.sample() ); 
    36                         Coms ( i )->bayes_weighted ( Data.get_col ( ind ), empty_vec, ndat/Coms.length() ); 
     35                        Coms ( i )->bayes_weighted ( Data.get_col ( ind ), empty_vec, ndat ); 
     36                        Coms (i)->flatten(Com0,ndat/Coms.length()); 
    3737                } 
    3838                //sharpen to the sharp component 
    3939                //Coms ( i )->flatten ( SharpCom.get(), 1.0/Coms.length() ); 
    4040        } 
     41        MixEF_options old_opt =options; 
     42        MixEF_options ini_opt=options; 
     43        ini_opt.method = EM; 
     44        ini_opt.max_niter= 1; 
     45        bayes_batch(Data, empty_vec); 
     46         
     47        for ( i = 0; i < Coms.length(); i++ ) { 
     48                Coms (i)->flatten(Com0); 
     49        } 
     50         
     51        options = old_opt; 
    4152} 
    4253 
     
    8394 
    8495                        maxll = max ( ll, maxi ); 
    85                         switch ( method ) { 
     96                        switch ( options.method ) { 
    8697                        case QB: 
    8798                                w = exp ( ll - maxll ); 
  • library/bdm/estim/mixtures.h

    r1013 r1014  
    3939This 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. 
    4040 
    41 TODO: Extend BM to use rvc. 
     41Two methods are provided: 
     42 - QB where the probability of being from one component is computed using predictors, 
     43 - EM where the data is assigned to component with the highest likelihood (winner takes all) 
     44 
     45These methods are stored in attribute options: 
     46 - method: ["EM","QB"] estimation method as mentioned above, QB is default 
     47 - max_niter: maximum of iterations in bayes_batch() 
     48 
    4249*/ 
    4350class MixEF: public BMEF { 
     
    6067        ////!indices of component rvc in common rvc 
    6168 
    62         //! Flag for a method that is used in the inference 
    63         MixEF_METHOD method; 
     69        class MixEF_options: public root{ 
     70                public: 
     71                //! Flag for a method that is used in the inference 
     72                MixEF_METHOD method; 
    6473 
    65         //! maximum number of iterations 
    66         int max_niter; 
     74                //! maximum number of iterations 
     75                int max_niter; 
     76         
     77        MixEF_options():method(QB),max_niter(10){}; 
     78                 
     79                void from_setting(const Setting &set){ 
     80                        string meth; 
     81                        UI::get(meth,set,"method",UI::optional); 
     82                        if (meth=="EM") {method=EM;} 
     83                        max_niter =10; 
     84                        UI::get(max_niter,set,"max_niter",UI::optional); 
     85                }; 
     86                void to_setting(Setting &set)const{ 
     87                        string meth=(method==EM ? "EM" : "QB"); 
     88                        UI::save(meth,set,"method"); 
     89                        UI::save(max_niter,set,"max_niter"); 
     90                }; 
     91        }; 
     92         
     93        MixEF_options options; 
    6794public: 
    6895        //! Full constructor 
    6996        MixEF ( const Array<BMEF*> &Coms0, const vec &alpha0 ) : 
    7097                        BMEF ( ), Coms ( Coms0.length() ), 
    71                         weights (), est(*this), method ( QB ), max_niter(10) { 
     98                        weights (), est(*this), options() { 
    7299                for ( int i = 0; i < Coms0.length(); i++ ) { 
    73100                        Coms ( i ) = ( BMEF* ) Coms0 ( i )->_copy(); 
     
    80107        MixEF () : 
    81108                        BMEF ( ), Coms ( 0 ), 
    82                         weights (), est(*this), method ( QB ), max_niter(10) { 
     109                        weights (), est(*this), options() { 
    83110        } 
    84111        //! Copy constructor 
    85112        MixEF ( const MixEF &M2 ) : BMEF ( ),  Coms ( M2.Coms.length() ), 
    86                         weights ( M2.weights ), est(*this), method ( M2.method ), max_niter(M2.max_niter) { 
     113                        weights ( M2.weights ), est(*this), options(M2.options) { 
    87114                for ( int i = 0; i < M2.Coms.length(); i++ ) { 
    88115                        Coms ( i ) = (BMEF*) M2.Coms ( i )->_copy(); 
     
    94121        //! \param Data Data on which the initialization will be done 
    95122        //! \param c Initial number of components, default=5 
     123        //! when the number of data records (columns of Data) is equal to the number of requested components, each data is used, otherwise, they are picked randomly. 
    96124        void init ( BMEF* Com0, const mat &Data, const int c = 5 ); 
    97125        //Destructor 
     
    119147        //!Set which method is to be used 
    120148        void set_method ( MixEF_METHOD M ) { 
    121                 method = M; 
     149                options.method = M; 
    122150        } 
    123151 
     
    126154        UI::save ( Coms, set, "Coms" ); 
    127155        UI::save ( &weights, set, "weights" ); 
     156        UI::save (options, set, "options"); 
    128157} 
    129158/*! \brief reads data from setting 
     
    133162        UI::get ( Coms, set, "Coms" ); 
    134163        UI::get ( weights, set, "weights" ); 
     164        UI::get (options, set, "options",UI::optional); 
    135165} 
    136166};