- Timestamp:
- 05/27/10 23:07:57 (14 years ago)
- Location:
- library/bdm/estim
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/estim/mixtures.cpp
r1013 r1014 18 18 // Coms(0)->set_evalll(false); 19 19 Coms ( 0 )->bayes_batch ( Data ); 20 // Flatten it to its original shape 21 shared_ptr<BMEF> SharpCom((BMEF*)Coms(0)->_copy()); 20 22 21 Coms ( 0 )->flatten ( Com0 ); 23 22 … … 34 33 } else { // pick at random 35 34 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()); 37 37 } 38 38 //sharpen to the sharp component 39 39 //Coms ( i )->flatten ( SharpCom.get(), 1.0/Coms.length() ); 40 40 } 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; 41 52 } 42 53 … … 83 94 84 95 maxll = max ( ll, maxi ); 85 switch ( method ) {96 switch ( options.method ) { 86 97 case QB: 87 98 w = exp ( ll - maxll ); -
library/bdm/estim/mixtures.h
r1013 r1014 39 39 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 under EM-style approximate estimation this class itself belongs to the exponential family. 40 40 41 TODO: Extend BM to use rvc. 41 Two 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 45 These 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 42 49 */ 43 50 class MixEF: public BMEF { … … 60 67 ////!indices of component rvc in common rvc 61 68 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; 64 73 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; 67 94 public: 68 95 //! Full constructor 69 96 MixEF ( const Array<BMEF*> &Coms0, const vec &alpha0 ) : 70 97 BMEF ( ), Coms ( Coms0.length() ), 71 weights (), est(*this), method ( QB ), max_niter(10) {98 weights (), est(*this), options() { 72 99 for ( int i = 0; i < Coms0.length(); i++ ) { 73 100 Coms ( i ) = ( BMEF* ) Coms0 ( i )->_copy(); … … 80 107 MixEF () : 81 108 BMEF ( ), Coms ( 0 ), 82 weights (), est(*this), method ( QB ), max_niter(10) {109 weights (), est(*this), options() { 83 110 } 84 111 //! Copy constructor 85 112 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) { 87 114 for ( int i = 0; i < M2.Coms.length(); i++ ) { 88 115 Coms ( i ) = (BMEF*) M2.Coms ( i )->_copy(); … … 94 121 //! \param Data Data on which the initialization will be done 95 122 //! \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. 96 124 void init ( BMEF* Com0, const mat &Data, const int c = 5 ); 97 125 //Destructor … … 119 147 //!Set which method is to be used 120 148 void set_method ( MixEF_METHOD M ) { 121 method = M;149 options.method = M; 122 150 } 123 151 … … 126 154 UI::save ( Coms, set, "Coms" ); 127 155 UI::save ( &weights, set, "weights" ); 156 UI::save (options, set, "options"); 128 157 } 129 158 /*! \brief reads data from setting … … 133 162 UI::get ( Coms, set, "Coms" ); 134 163 UI::get ( weights, set, "weights" ); 164 UI::get (options, set, "options",UI::optional); 135 165 } 136 166 };