Changeset 1014

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

Mixtures example

Files:
3 modified

Legend:

Unmodified
Added
Removed
  • applications/bdmtoolbox/tutorial/userguide/mixef_basic.m

    r1013 r1014  
    55com.rgr = RV({},[],[]); 
    66 
    7 Data = [randn(2,50) randn(2,50)+10*ones(2,50)]; 
     7Data = [randn(2,50) randn(2,50)+[5*ones(1,50); 10*ones(1,50)]]; 
    88 
    9 [Mix0,P0]=mixef_init(Data,com); 
     9[Mix0,P0]=mixef_init(Data,com,4); 
    1010% show predictor 
    1111 
     
    1616hold on 
    1717epdf_2dplot(Pred); 
     18hold on 
     19for i=1:length(Pred.pdfs) 
     20    mea=epdf_mean(Pred.pdfs{i}); 
     21    plot(mea(1),mea(2),'+r'); 
     22end 
    1823 
    1924% Do Quasi Bayes 
     
    3237    % normalize weights 
    3338    w = exp(log_w_nn-max(log_w_nn)); 
    34     w = w/sum(w) 
     39    w = w/sum(w); 
    3540     
    3641    for c=1:n 
     
    5661epdf_2dplot(PredQB); 
    5762 
     63PredQBc = bm_epredictor(MixQBc); 
     64figure(3); 
     65hold off 
     66plot(Data(1,:), Data(2,:),'.'); 
     67hold on 
     68epdf_2dplot(PredQBc); 
     69 
    5870%% relations between components 
    5971Batta_dist=zeros(n); 
  • 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};