Changeset 766 for library/bdm/stat
- Timestamp:
- 01/11/10 22:55:57 (14 years ago)
- Location:
- library/bdm/stat
- Files:
-
- 5 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/stat/discrete.cpp
r760 r766 81 81 inds ( j ) = gridsizes ( j ) - 1; 82 82 else { 83 inds ( j ) = ::round ( val ( j ) - ranges ( j ) ( 0 ) / steps ( j ) );83 inds ( j ) = (int) ::round ( val ( j ) - ranges ( j ) ( 0 ) / steps ( j ) ); 84 84 } 85 85 } -
library/bdm/stat/emix.cpp
r760 r766 3 3 namespace bdm { 4 4 5 void emix::set_parameters ( const vec &w0, const Array<shared_ptr<epdf> > &Coms0 ) {6 w = w0 / sum ( w0 );7 8 bool isnamed = Coms0 ( 0 )->isnamed();9 RV tmp_rv;10 if ( isnamed ) tmp_rv = Coms0 ( 0 )->_rv();11 Coms = Coms0;12 13 if ( isnamed ) epdf::set_rv ( tmp_rv ); //coms aer already OK, no need for set_rv14 }15 16 5 void emix::validate (){ 17 dim = Coms ( 0 )->dimension(); 18 bool isnamed = Coms ( 0 )->isnamed(); 19 int i; 20 RV tmp_rv; 21 if ( isnamed ) tmp_rv = Coms ( 0 )->_rv(); 22 for ( i = 0; i < w.length(); i++ ) { 6 bdm_assert ( Coms.length() > 0, "There has to be at least one component." ); 7 8 bdm_assert ( Coms.length() == w.length(), "It is obligatory to define weights of all the components." ); 9 10 double sum_w = sum ( w ); 11 bdm_assert ( sum_w != 0, "There has to be a component with non-zero weight." ); 12 w = w / sum_w; 13 14 int dim = Coms ( 0 )->dimension(); 15 for ( int i = 1; i < Coms.length(); i++ ) { 23 16 bdm_assert ( dim == ( Coms ( i )->dimension() ), "Component sizes do not match!" ); 24 bdm_assert ( !isnamed || tmp_rv.equal ( Coms ( i )->_rv() ), "Component RVs do not match!" ); 25 } 26 27 } 28 29 17 bdm_assert ( Coms(i)->isnamed() , "An unnamed component is forbidden here!" ); 18 } 19 20 set_rv ( Coms ( 0 )->_rv() ); 21 } 22 23 void emix::from_setting ( const Setting &set ) { 24 UI::get ( Coms, set, "pdfs", UI::compulsory ); 25 26 if ( !UI::get ( w, set, "weights", UI::optional ) ) { 27 int len = Coms.length(); 28 w.set_length ( len ); 29 w = 1.0 / len; 30 } 31 32 validate(); 33 } 30 34 31 35 … … 109 113 } 110 114 111 target.set_parameters ( w, Cn ); 115 target._w() = w; 116 target._Coms() = Cn; 112 117 target.validate(); 113 118 } … … 190 195 } 191 196 192 target.set_parameters ( w, Cn ); 197 target._w() = w; 198 target._Coms() = Cn; 193 199 target.validate(); 194 200 } … … 313 319 dls ( i ) = new datalink_m2m; 314 320 dls ( i )->set_connection ( pdfs ( i )->_rv(), pdfs ( i )->_rvc(), _rv(), _rvc() ); 321 } 322 } 323 324 void mmix::validate() 325 { 326 bdm_assert ( Coms.length() > 0, "There has to be at least one component." ); 327 328 bdm_assert ( Coms.length() == w.length(), "It is obligatory to define weights of all the components." ); 329 330 double sum_w = sum ( w ); 331 bdm_assert ( sum_w != 0, "There has to be a component with non-zero weight." ); 332 w = w / sum_w; 333 334 int dim = Coms ( 0 )->dimension(); 335 int dimc = Coms ( 0 )->dimensionc(); 336 for ( int i = 1; i < Coms.length(); i++ ) { 337 bdm_assert ( dim == ( Coms ( i )->dimension() ), "Component sizes do not match!" ); 338 bdm_assert ( dimc == ( Coms ( i )->dimensionc() ), "Component sizes do not match!" ); 339 bdm_assert ( Coms(i)->isnamed() , "An unnamed component is forbidden here!" ); 340 } 341 342 set_rv ( Coms ( 0 )->_rv() ); 343 set_rvc ( Coms ( 0 )->_rvc() ); 344 } 345 346 void mmix::from_setting ( const Setting &set ) { 347 UI::get ( Coms, set, "pdfs", UI::compulsory ); 348 349 if ( !UI::get ( w, set, "weights", UI::optional ) ) { 350 int len = Coms.length(); 351 w.set_length ( len ); 352 w = 1.0 / len; 315 353 } 316 354 } -
library/bdm/stat/emix.h
r750 r766 53 53 // adjust rv and rvc 54 54 55 set_rv ( rv ); // TODO co kdyby tohle samo uz nastavovalo dimension?!?!55 set_rv ( rv ); 56 56 dim = rv._dsize(); 57 57 … … 81 81 return tmp; 82 82 } 83 84 //! Returns a sample from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv 85 virtual vec samplecond ( const vec &cond ) NOT_IMPLEMENTED(0); 86 83 87 //! Object takes ownership of nom and will destroy it 84 88 void ownnom() { … … 91 95 } 92 96 } 97 98 99 93 100 94 101 private: … … 120 127 emix ( ) : epdf ( ) { } 121 128 122 /*!123 \brief Set weights \c w and components \c Coms124 125 Shared pointers in Coms are kept inside this instance and126 shouldn't be modified after being passed to this method.127 */128 void set_parameters ( const vec &w, const Array<shared_ptr<epdf> > &Coms );129 130 129 virtual void validate (); 131 130 … … 148 147 shared_ptr<pdf> condition ( const RV &rv ) const; 149 148 150 //Access methods 151 //! returns a pointer to the internal mean value. Use with Care!149 //Access methods 150 //! returns a reference to the internal weights. Use with Care! 152 151 vec& _w() { 153 152 return w; 154 153 } 155 154 156 //!access function 155 /*! 156 \brief returns a reference to the internal array of components. Use with Care! Set components \c Coms 157 158 Shared pointers in Coms are kept inside this instance and 159 shouldn't be modified after being passed to this method. 160 */ 161 Array<shared_ptr<epdf>>& _Coms ( ) { 162 return Coms; 163 } 164 165 //! returns a reference to the internal components specified by index i. Use with Care! 157 166 shared_ptr<epdf> _Coms ( int i ) { 158 167 return Coms ( i ); … … 174 183 //! \endcode 175 184 //!@} 176 void from_setting ( const Setting &set ) { 177 178 vec w0; 179 Array<shared_ptr<epdf> > Coms0; 180 181 UI::get ( Coms0, set, "pdfs", UI::compulsory ); 182 183 if ( !UI::get ( w0, set, "weights", UI::optional ) ) { 184 int len = Coms.length(); 185 w0.set_length ( len ); 186 w0 = 1.0 / len; 187 } 188 189 // TODO asi lze nacitat primocare do w a coms, jen co bude hotovy validate() 190 set_parameters ( w0, Coms0 ); 191 validate(); 192 } 185 void from_setting ( const Setting &set ); 193 186 }; 194 187 SHAREDPTR ( emix ); … … 284 277 //! Data link for each pdfs 285 278 Array<shared_ptr<datalink_m2m> > dls; 286 287 protected:288 //! dummy epdf used only as storage for RV and dim289 epdf iepdf;290 279 291 280 public: … … 330 319 //!@} 331 320 void from_setting ( const Setting &set ) { 332 Array<shared_ptr<pdf> > atmp; //temporary Array333 UI::get ( atmp, set, "pdfs", UI::compulsory );334 set_elements ( atmp);321 Array<shared_ptr<pdf> > temp_array; 322 UI::get ( temp_array, set, "pdfs", UI::compulsory ); 323 set_elements ( temp_array ); 335 324 } 336 325 }; … … 418 407 mmix() : Coms ( 0 ) { } 419 408 420 //! Set weights \c w and components \c R421 void set_parameters ( const vec &w0, const Array<shared_ptr<pdf> > &Coms0 ) {422 //!\todo check if all components are OK423 Coms = Coms0;424 w = w0;425 426 if ( Coms0.length() > 0 ) {427 set_rv ( Coms ( 0 )->_rv() );428 dim = rv._dsize();429 set_rvc ( Coms ( 0 )->_rvc() );430 dimc = rvc._dsize();431 }432 }433 409 double evallogcond ( const vec &dt, const vec &cond ) { 434 410 double ll = 0.0; … … 449 425 //! \endcode 450 426 //!@} 451 void from_setting ( const Setting &set ) { 452 UI::get ( Coms, set, "pdfs", UI::compulsory ); 453 454 // TODO ma byt zde, ci ve validate()? 455 if ( Coms.length() > 0 ) { 456 set_rv ( Coms ( 0 )->_rv() ); 457 dim = rv._dsize(); 458 set_rvc ( Coms ( 0 )->_rvc() ); 459 dimc = rvc._dsize(); 460 } 461 462 if ( !UI::get ( w, set, "weights", UI::optional ) ) { 463 int len = Coms.length(); 464 w.set_length ( len ); 465 w = 1.0 / len; 466 } 467 } 427 void from_setting ( const Setting &set ); 428 429 virtual void validate(); 468 430 }; 469 431 SHAREDPTR ( mmix ); -
library/bdm/stat/exp_family.h
r763 r766 44 44 45 45 //!Evaluate normalized log-probability 46 virtual double evallog_nn ( const vec &val ) const { 47 bdm_error ( "Not implemented" ); 48 return 0.0; 49 } 46 virtual double evallog_nn ( const vec &val ) const NOT_IMPLEMENTED(0); 50 47 51 48 //!Evaluate normalized log-probability … … 73 70 74 71 //!Power of the density, used e.g. to flatten the density 75 virtual void pow ( double p ) { 76 bdm_error ( "Not implemented" ); 77 } 72 virtual void pow ( double p ) NOT_IMPLEMENTED_VOID; 78 73 }; 79 74 … … 92 87 BMEF ( const BMEF &B ) : BM ( B ), frg ( B.frg ), last_lognc ( B.last_lognc ) {} 93 88 //!get statistics from another model 94 virtual void set_statistics ( const BMEF* BM0 ) { 95 bdm_error ( "Not implemented" ); 96 } 89 virtual void set_statistics ( const BMEF* BM0 ) NOT_IMPLEMENTED_VOID; 97 90 98 91 //! Weighted update of sufficient statistics (Bayes rule) … … 102 95 103 96 //!Flatten the posterior according to the given BMEF (of the same type!) 104 virtual void flatten ( const BMEF * B ) { 105 bdm_error ( "Not implemented" ); 106 } 107 108 BMEF* _copy_ () const { 109 bdm_error ( "function _copy_ not implemented for this BM" ); 110 return NULL; 111 } 97 virtual void flatten ( const BMEF * B ) NOT_IMPLEMENTED_VOID; 98 99 double logpred ( const vec &yt ) const NOT_IMPLEMENTED(0); 100 101 virtual epdf* epredictor() const NOT_IMPLEMENTED(NULL); 102 103 virtual pdf* predictor() const NOT_IMPLEMENTED(NULL); 112 104 113 105 void to_setting ( Setting &set ) const 114 106 { 115 107 BM::to_setting( set ); 116 // TODO DOPLNIT? CHYBI FROM_SETTING PRO INSPIRACI108 // TODO DOPLNIT? ALE MOMENTALNE CHYBI FROM_SETTING PRO INSPIRACI 117 109 } 118 110 }; … … 1112 1104 //! Constructor 1113 1105 migamma_ref () : migamma (), refl () {}; 1106 1114 1107 //! Set value of \c k 1115 1108 void set_parameters ( double k0 , vec ref0, double l0 ) { … … 1266 1259 return X*Y._Ch();// return upper triangular part of the decomposition 1267 1260 } 1261 1268 1262 vec sample () const { 1269 1263 return vec ( sample_mat()._data(), p*p ); 1270 1264 } 1265 1266 virtual vec mean() const NOT_IMPLEMENTED(0); 1267 1268 //! return expected variance (not covariance!) 1269 virtual vec variance() const NOT_IMPLEMENTED(0); 1270 1271 virtual double evallog ( const vec &val ) const NOT_IMPLEMENTED(0); 1272 1271 1273 //! fast access function y0 will be copied into Y.Ch. 1272 1274 void setY ( const mat &Ch0 ) { 1273 1275 copy_vector ( dim, Ch0._data(), Y._Ch()._data() ); 1274 1276 } 1277 1275 1278 //! fast access function y0 will be copied into Y.Ch. 1276 1279 void _setY ( const vec &ch0 ) { 1277 1280 copy_vector ( dim, ch0._data(), Y._Ch()._data() ); 1278 1281 } 1282 1279 1283 //! access function 1280 1284 const chmat& getY() const { … … 1321 1325 W.setY ( iCh ); 1322 1326 } 1327 1323 1328 virtual double evallog ( const vec &val ) const { 1324 1329 chmat X ( p ); … … 1345 1350 }; 1346 1351 1352 virtual vec mean() const NOT_IMPLEMENTED(0); 1353 1354 //! return expected variance (not covariance!) 1355 virtual vec variance() const NOT_IMPLEMENTED(0); 1347 1356 }; 1348 1357 … … 1464 1473 1465 1474 //! inherited operation : NOT implemented 1466 vec sample() const { 1467 bdm_error ( "Not implemented" ); 1468 return vec(); 1469 } 1475 vec sample() const NOT_IMPLEMENTED(0); 1470 1476 1471 1477 //! inherited operation : NOT implemented 1472 double evallog ( const vec &val ) const { 1473 bdm_error ( "Not implemented" ); 1474 return 0.0; 1475 } 1478 double evallog ( const vec &val ) const NOT_IMPLEMENTED(0); 1476 1479 1477 1480 vec mean() const { -
library/bdm/stat/merger.h
r763 r766 148 148 149 149 vec variance() const; 150 151 //! Compute log-probability of argument \c val 152 virtual double evallog ( const vec &val ) const NOT_IMPLEMENTED(0); 153 154 //! Returns a sample, \f$ x \f$ from density \f$ f_x()\f$ 155 virtual vec sample() const NOT_IMPLEMENTED(0); 150 156 151 157 //!@}