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

Mixtures example

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • 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};