Changeset 487 for library/bdm/base/bdmbase.h
- Timestamp:
- 08/08/09 13:42:18 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/bdmbase.h
r477 r487 399 399 //! Conditional probability density, e.g. modeling some dependencies. 400 400 //TODO Samplecond can be generalized 401 402 401 class mpdf : public root { 403 402 protected: … … 406 405 //! random variable in condition 407 406 RV rvc; 408 409 407 private: 410 //! pointer to internal epdf 411 shared_ptr<epdf> shep; 408 //! internal epdf, used only as cache to avoid virtual calls of \c _rv() and \c _dimension() 409 epdf* ep; 410 411 protected: 412 void set_ep(epdf &iepdf) { 413 ep = &iepdf; 414 } 412 415 413 416 public: … … 415 418 //! @{ 416 419 417 mpdf() : dimc ( 0 ), rvc() { }418 419 mpdf ( const mpdf &m ) : dimc ( m.dimc ), rvc ( m.rvc ), shep ( m.shep ) { }420 mpdf() : dimc ( 0 ), rvc(), ep(NULL) { } 421 422 mpdf ( const mpdf &m ) : dimc ( m.dimc ), rvc ( m.rvc ), ep ( m.ep ) { } 420 423 //!@} 421 424 … … 424 427 425 428 //! 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 426 virtual vec samplecond ( const vec &cond ) ;429 virtual vec samplecond ( const vec &cond ){it_error("Not implemented");return vec(0);}; 427 430 428 431 //! Returns \param N samples from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv 429 virtual mat samplecond_m ( const vec &cond, int N ); 430 431 //! Update \c ep so that it represents this mpdf conditioned on \c rvc = cond 432 virtual void condition ( const vec &cond ) { 433 it_error ( "Not implemented" ); 434 }; 432 virtual mat samplecond_m ( const vec &cond, int N ){it_error("Not implemented");return mat();} 433 435 434 436 435 //! Shortcut for conditioning and evaluation of the internal epdf. In some cases, this operation can be implemented efficiently. 437 virtual double evallogcond ( const vec &dt, const vec &cond ) ;436 virtual double evallogcond ( const vec &dt, const vec &cond ){it_error("Not implemented");return 0.0;} 438 437 439 438 //! Matrix version of evallogcond 440 virtual vec evallogcond_m ( const mat &Dt, const vec &cond ) ;439 virtual vec evallogcond_m ( const mat &Dt, const vec &cond ){it_error("Not implemented");return vec(0);} 441 440 442 441 //! Array<vec> version of evallogcond 443 virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ) ;442 virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ){it_error("Not implemented");return vec(0);} 444 443 445 444 //! \name Access to attributes … … 447 446 448 447 RV _rv() { 449 return shep->_rv();448 return ep->_rv(); 450 449 } 451 450 RV _rvc() { … … 454 453 } 455 454 int dimension() { 456 return shep->dimension();455 return ep->dimension(); 457 456 } 458 457 int dimensionc() { … … 460 459 } 461 460 462 epdf *e() {463 return shep.get();464 }465 466 void set_ep ( shared_ptr<epdf> ep ) {467 shep = ep;468 }469 470 461 //! Load from structure with elements: 471 462 //! \code 472 //! { rv = {class="RV", names=(...),}; // RV describing meaning of random variable 463 //! { class = "mpdf_offspring", 464 //! rv = {class="RV", names=(...),}; // RV describing meaning of random variable 473 465 //! rvc= {class="RV", names=(...),}; // RV describing meaning of random variable in condition 474 466 //! // elements of offsprings … … 485 477 } 486 478 void set_rv ( const RV &rv0 ) { 487 shep->set_rv ( rv0 );479 ep->set_rv ( rv0 ); 488 480 } 489 481 bool isnamed() { 490 return ( shep->isnamed() ) && ( dimc == rvc._dsize() ); 491 } 492 //!@} 482 return ( ep->isnamed() ) && ( dimc == rvc._dsize() ); 483 } 484 //!@} 485 }; 486 487 template <class EPDF> 488 class mpdf_internal: public mpdf{ 489 protected : 490 EPDF iepdf; 491 public: 492 //! constructor 493 mpdf_internal(): mpdf(),iepdf(){set_ep(iepdf);} 494 //! Update \c iepdf so that it represents this mpdf conditioned on \c rvc = cond 495 //! This function provides convenient reimplementation in offsprings 496 virtual void condition ( const vec &cond ) { 497 it_error ( "Not implemented" ); 498 }; 499 //!access function to iepdf 500 EPDF& e(){return iepdf;} 501 502 //! Reimplements samplecond using \c condition() 503 vec samplecond ( const vec &cond ); 504 //! Reimplements evallogcond using \c condition() 505 double evallogcond ( const vec &val, const vec &cond ); 506 //! Efficient version of evallogcond for matrices 507 virtual vec evallogcond_m ( const mat &Dt, const vec &cond ); 508 //! Efficient version of evallogcond for Array<vec> 509 virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ); 510 //! Efficient version of samplecond 511 virtual mat samplecond_m ( const vec &cond, int N ); 493 512 }; 494 513 … … 686 705 */ 687 706 class mepdf : public mpdf { 707 708 shared_ptr<epdf> ipdf; 688 709 public: 689 710 //!Default constructor … … 691 712 692 713 mepdf ( shared_ptr<epdf> em ) { 693 set_ep ( em ); 714 ipdf = em; 715 set_ep(*ipdf.get()); 694 716 dimc = 0; 695 717 } … … 701 723 //! \code 702 724 //! { class = "mepdf", 703 //! epdf s = {class="epdfs",...}725 //! epdf = {class="epdf_offspring",...} 704 726 //! } 705 727 //! \endcode