Changeset 1004
- Timestamp:
- 05/27/10 13:08:44 (15 years ago)
- Location:
- library/bdm/estim
- Files:
-
- 2 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/estim/mixtures.cpp
r943 r1004 8 8 //prepare sizes 9 9 Coms.set_size ( c ); 10 n = c;11 10 weights.set_parameters ( ones ( c ) ); //assume at least one observation in each comp. 12 11 //est will be done at the end … … 22 21 23 22 //Copy it to the rest 24 for ( i = 1; i < n; i++ ) {23 for ( i = 1; i < Coms.length(); i++ ) { 25 24 //copy Com0 and create new rvs for them 26 25 Coms ( i ) = (BMEF*) Coms ( 0 )->_copy ( ); 27 26 } 28 27 //Pick some data for each component and update it 29 for ( i = 0; i < n; i++ ) {28 for ( i = 0; i < Coms.length(); i++ ) { 30 29 //pick one datum 31 30 int ind = (int) floor ( ndat * UniRNG.sample() ); … … 44 43 multiBM weights0 ( weights ); 45 44 45 int n = Coms.length(); 46 46 Array<BMEF*> Coms0 ( n ); 47 47 for ( i = 0; i < n; i++ ) { … … 131 131 vec w = weights.posterior().mean(); 132 132 double exLL = 0.0; 133 for ( int i = 0; i < n; i++ ) {133 for ( int i = 0; i < Coms.length(); i++ ) { 134 134 exLL += w ( i ) * exp ( Coms ( i )->logpred ( yt ) ); 135 135 } … … 138 138 139 139 emix* MixEF::epredictor ( const vec &vec) const { 140 Array<shared_ptr<epdf> > pC ( n);141 for ( int i = 0; i < n; i++ ) {140 Array<shared_ptr<epdf> > pC ( Coms.length() ); 141 for ( int i = 0; i < Coms.length(); i++ ) { 142 142 pC ( i ) = Coms ( i )->epredictor ( ); 143 143 pC (i) -> set_rv(_yrv()); … … 153 153 void MixEF::flatten ( const BMEF* M2 ) { 154 154 const MixEF* Mix2 = dynamic_cast<const MixEF*> ( M2 ); 155 bdm_assert_debug ( Mix2-> n == n, "Different no of coms" );155 bdm_assert_debug ( Mix2->Coms.length() == Coms.length(), "Different no of coms" ); 156 156 //Flatten each component 157 for ( int i = 0; i < n; i++ ) {157 for ( int i = 0; i < Coms.length(); i++ ) { 158 158 Coms ( i )->flatten ( Mix2->Coms ( i ) ); 159 159 } -
library/bdm/estim/mixtures.h
r997 r1004 43 43 class MixEF: public BMEF { 44 44 protected: 45 //!Number of components46 int n;47 45 //! Models for Components of \f$\theta_i\f$ 48 46 Array<BMEF*> Coms; … … 57 55 public: 58 56 eprod_mix(const MixEF &m):mix(m){} 59 const epdf* factor(int i) const {return (i==(mix. n-1)) ? &mix.weights.posterior() : &mix.Coms(i)->posterior();}60 const int no_factors()const {return mix. n+1;}57 const epdf* factor(int i) const {return (i==(mix.Coms.length()-1)) ? &mix.weights.posterior() : &mix.Coms(i)->posterior();} 58 const int no_factors()const {return mix.Coms.length()+1;} 61 59 } est; 62 60 ////!indices of component rvc in common rvc … … 68 66 //! Full constructor 69 67 MixEF ( const Array<BMEF*> &Coms0, const vec &alpha0 ) : 70 BMEF ( ), n ( Coms0.length() ), Coms ( n),68 BMEF ( ), Coms ( Coms0.length() ), 71 69 weights (), est(*this), method ( QB ) { 72 for ( int i = 0; i < n; i++ ) {70 for ( int i = 0; i < Coms0.length(); i++ ) { 73 71 Coms ( i ) = ( BMEF* ) Coms0 ( i )->_copy(); 74 72 } … … 79 77 //! Constructor of empty mixture 80 78 MixEF () : 81 BMEF ( ), n ( 0 ),Coms ( 0 ),79 BMEF ( ), Coms ( 0 ), 82 80 weights (), est(*this), method ( QB ) { 83 81 } 84 82 //! Copy constructor 85 MixEF ( const MixEF &M2 ) : BMEF ( ), n ( M2.n ), Coms ( n),83 MixEF ( const MixEF &M2 ) : BMEF ( ), Coms ( M2.Coms.length() ), 86 84 weights ( M2.weights ), est(*this), method ( M2.method ) { 87 for ( int i = 0; i < n; i++ ) {85 for ( int i = 0; i < M2.Coms.length(); i++ ) { 88 86 Coms ( i ) = (BMEF*) M2.Coms ( i )->_copy(); 89 87 } … … 121 119 } 122 120 123 void to_setting ( Setting &set ) const { 124 BMEF::to_setting( set ); 125 UI::save ( Coms, set, "Coms" ); 126 UI::save ( &weights, set, "weights" ); 127 } 121 void to_setting ( Setting &set ) const { 122 BMEF::to_setting( set ); 123 UI::save ( Coms, set, "Coms" ); 124 UI::save ( &weights, set, "weights" ); 125 } 126 /*! \brief reads data from setting 127 */ 128 void from_setting (const Setting &set ) { 129 BMEF::from_setting( set ); 130 UI::get ( Coms, set, "Coms" ); 131 UI::get ( weights, set, "weights" ); 132 } 128 133 }; 129 134 UIREGISTER ( MixEF );