Changeset 487 for library/bdm/stat/exp_family.h
- Timestamp:
- 08/08/09 13:42:18 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/stat/exp_family.h
r476 r487 55 55 return tmp;} 56 56 //!Evaluate normalized log-probability for many samples 57 virtual vec evallog ( const mat &Val ) const57 virtual vec evallog_m ( const mat &Val ) const 58 58 { 59 59 vec x ( Val.cols() ); … … 65 65 }; 66 66 67 /*!68 * \brief Exponential family model.69 70 * More?...71 */72 73 class mEF : public mpdf74 {75 76 public:77 //! Default constructor78 mEF ( ) :mpdf ( ) {};79 };80 67 81 68 //! Estimator for Exponential family … … 106 93 }; 107 94 108 template<class sq_T >95 template<class sq_T, template <typename> class TEpdf > 109 96 class mlnorm; 110 97 … … 527 514 Mean value \f$mu=A*rvc+mu_0\f$. 528 515 */ 529 template<class sq_T >530 class mlnorm : public m EF516 template<class sq_T, template <typename> class TEpdf =enorm> 517 class mlnorm : public mpdf_internal< TEpdf<sq_T> > 531 518 { 532 519 protected: 533 520 //! Internal epdf that arise by conditioning on \c rvc 534 shared_ptr<enorm<sq_T> > iepdf;535 521 mat A; 536 522 vec mu_const; 537 vec& _mu; //cached epdf.mu; 523 // vec& _mu; //cached epdf.mu; !!!!!! WHY NOT? 538 524 public: 539 525 //! \name Constructors 540 526 //!@{ 541 mlnorm(): iepdf(new enorm<sq_T>()), _mu(iepdf->_mu()) { set_ep(iepdf);};542 mlnorm (const mat &A, const vec &mu0, const sq_T &R ) : iepdf(new enorm<sq_T>()), _mu(iepdf->_mu())543 { 544 set_ ep(iepdf); set_parameters ( A,mu0,R );527 mlnorm():mpdf_internal< TEpdf<sq_T> >() {}; 528 mlnorm (const mat &A, const vec &mu0, const sq_T &R ) : mpdf_internal< TEpdf<sq_T> >() 529 { 530 set_parameters ( A,mu0,R ); 545 531 } 546 532 547 533 //! Set \c A and \c R 548 void set_parameters ( const mat &A, const vec &mu0, const sq_T &R ); 534 void set_parameters ( const mat &A0, const vec &mu0, const sq_T &R0 ){ 535 it_assert_debug ( A0.rows() ==mu0.length(),"" ); 536 it_assert_debug ( A0.rows() ==R0.rows(),"" ); 537 538 this->iepdf.set_parameters(zeros(A0.rows()), R0); 539 A = A0; 540 mu_const = mu0; 541 this->dimc = A0.cols(); 542 } 549 543 //!@} 550 544 //! Set value of \c rvc . Result of this operation is stored in \c epdf use function \c _ep to access it. 551 void condition ( const vec &cond ); 545 void condition ( const vec &cond ){ 546 this->iepdf._mu()= A*cond + mu_const; 547 //R is already assigned; 548 } 552 549 553 550 //!access function … … 556 553 mat& _A() {return A;} 557 554 //!access function 558 mat _R() { return iepdf->_R().to_mat(); }559 560 template< classsq_M>561 friend std::ostream &operator<< ( std::ostream &os, mlnorm<sq_M > &ml );555 mat _R(){ return this->iepdf._R().to_mat(); } 556 557 template<typename sq_M> 558 friend std::ostream &operator<< ( std::ostream &os, mlnorm<sq_M,enorm> &ml ); 562 559 563 560 void from_setting(const Setting &set){ … … 577 574 //! Mpdf with general function for mean value 578 575 template<class sq_T> 579 class mgnorm : public mEF 580 { 581 protected: 582 //! Internal epdf that arise by conditioning on \c rvc 583 shared_ptr<enorm<sq_T> > iepdf; 584 vec μ 576 class mgnorm : public mpdf_internal< enorm< sq_T > > 577 { 578 protected: 579 // vec μ WHY NOT? 585 580 fnc* g; 586 581 public: 587 582 //!default constructor 588 mgnorm(): iepdf(new enorm<sq_T>()), mu(iepdf->_mu()) { set_ep(iepdf);}583 mgnorm():mpdf_internal<enorm<sq_T> >() { } 589 584 //!set mean function 590 void set_parameters ( fnc* g0, const sq_T &R0 ) {g=g0; iepdf->set_parameters ( zeros ( g->dimension() ), R0 );}591 void condition ( const vec &cond ) {mu=g->eval ( cond );};585 inline void set_parameters ( fnc* g0, const sq_T &R0 ); 586 inline void condition ( const vec &cond ); 592 587 593 588 … … 644 639 Perhaps a moment-matching technique? 645 640 */ 646 class mlstudent : public mlnorm<ldmat >641 class mlstudent : public mlnorm<ldmat,enorm> 647 642 { 648 643 protected: … … 651 646 ldmat Re; 652 647 public: 653 mlstudent ( ) :mlnorm<ldmat > (),654 Lambda (), _R ( iepdf ->_R() ) {}648 mlstudent ( ) :mlnorm<ldmat,enorm> (), 649 Lambda (), _R ( iepdf._R() ) {} 655 650 void set_parameters ( const mat &A0, const vec &mu0, const ldmat &R0, const ldmat& Lambda0 ) 656 651 { … … 658 653 it_assert_debug ( R0.rows() ==A0.rows(),"" ); 659 654 660 iepdf ->set_parameters ( mu0,Lambda ); //655 iepdf.set_parameters ( mu0,Lambda ); // 661 656 A = A0; 662 657 mu_const = mu0; … … 666 661 void condition ( const vec &cond ) 667 662 { 668 _mu= A*cond + mu_const;663 iepdf._mu() = A*cond + mu_const; 669 664 double zeta; 670 665 //ugly hack! … … 691 686 The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. 692 687 */ 693 class mgamma : public mEF 694 { 695 protected: 696 //! Internal epdf that arise by conditioning on \c rvc 697 shared_ptr<egamma> iepdf; 688 class mgamma : public mpdf_internal<egamma> 689 { 690 protected: 698 691 699 692 //! Constant \f$k\f$ … … 705 698 public: 706 699 //! Constructor 707 mgamma():iepdf(new egamma()), k(0), 708 _beta(iepdf->_beta()) { 709 set_ep(iepdf); 700 mgamma():mpdf_internal<egamma>(), k(0), 701 _beta(iepdf._beta()) { 710 702 } 711 703 … … 742 734 The standard deviation of the walk is then: \f$ \mu/\sqrt(k)\f$. 743 735 */ 744 class migamma : public mEF 745 { 746 protected: 747 //! Internal epdf that arise by conditioning on \c rvc 748 shared_ptr<eigamma> iepdf; 749 736 class migamma : public mpdf_internal<eigamma> 737 { 738 protected: 750 739 //! Constant \f$k\f$ 751 740 double k; … … 760 749 //! \name Constructors 761 750 //!@{ 762 migamma(): iepdf(new eigamma()),751 migamma():mpdf_internal<eigamma>(), 763 752 k(0), 764 _alpha(iepdf->_alpha()), 765 _beta(iepdf->_beta()) { 766 set_ep(iepdf); 767 } 768 769 migamma(const migamma &m):iepdf(m.iepdf), 753 _alpha(iepdf._alpha()), 754 _beta(iepdf._beta()) { 755 } 756 757 migamma(const migamma &m):mpdf_internal<eigamma>(), 770 758 k(0), 771 _alpha(iepdf->_alpha()), 772 _beta(iepdf->_beta()) { 773 set_ep(iepdf); 759 _alpha(iepdf._alpha()), 760 _beta(iepdf._beta()) { 774 761 } 775 762 //!@} … … 779 766 { 780 767 k=k0; 781 iepdf ->set_parameters ( ( 1.0/ ( k*k ) +2.0 ) *ones ( len ) /*alpha*/, ones ( len ) /*beta*/ );768 iepdf.set_parameters ( ( 1.0/ ( k*k ) +2.0 ) *ones ( len ) /*alpha*/, ones ( len ) /*beta*/ ); 782 769 dimc = dimension(); 783 770 }; … … 915 902 The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. 916 903 */ 917 class mlognorm : public mpdf 918 { 919 protected: 920 shared_ptr<elognorm> eno; 921 904 class mlognorm : public mpdf_internal<elognorm> 905 { 906 protected: 922 907 //! parameter 1/2*sigma^2 923 908 double sig2; … … 927 912 public: 928 913 //! Constructor 929 mlognorm(): eno(new elognorm()),914 mlognorm():mpdf_internal<elognorm>(), 930 915 sig2(0), 931 mu(eno->_mu()) { 932 set_ep(eno); 916 mu(iepdf._mu()) { 933 917 } 934 918 … … 937 921 { 938 922 sig2 = 0.5*log ( k*k+1 ); 939 eno->set_parameters ( zeros ( size ),2*sig2*eye ( size ) );923 iepdf.set_parameters ( zeros ( size ),2*sig2*eye ( size ) ); 940 924 941 925 dimc = size; … … 1071 1055 { 1072 1056 protected: 1073 shared_ptr<eiWishartCh>eiW;1057 eiWishartCh eiW; 1074 1058 //!square root of \f$ \nu-p-1 \f$ - needed for computation of \f$ \Psi \f$ from conditions 1075 1059 double sqd; … … 1080 1064 1081 1065 public: 1082 rwiWishartCh():eiW( new eiWishartCh()),1066 rwiWishartCh():eiW(), 1083 1067 sqd(0), l(0), p(0) { 1084 1068 set_ep(eiW); … … 1093 1077 refl=pow(ref0,1-l); 1094 1078 1095 eiW ->set_parameters ( eye ( p ),delta );1096 dimc=eiW ->dimension();1079 eiW.set_parameters ( eye ( p ),delta ); 1080 dimc=eiW.dimension(); 1097 1081 } 1098 1082 void condition ( const vec &c ) { … … 1104 1088 } 1105 1089 1106 eiW ->_setY ( sqd*z );1090 eiW._setY ( sqd*z ); 1107 1091 } 1108 1092 }; … … 1256 1240 }; 1257 1241 1258 template<class sq_T>1259 void mlnorm<sq_T>::set_parameters ( const mat &A0, const vec &mu0, const sq_T &R0 )1260 {1261 it_assert_debug ( A0.rows() ==mu0.length(),"" );1262 it_assert_debug ( A0.rows() ==R0.rows(),"" );1263 1264 iepdf->set_parameters(zeros(A0.rows()), R0);1265 A = A0;1266 mu_const = mu0;1267 dimc = A0.cols();1268 }1269 1242 1270 1243 // template<class sq_T> … … 1293 1266 // } 1294 1267 1295 template<class sq_T>1296 void mlnorm<sq_T>::condition ( const vec &cond )1297 {1298 _mu = A*cond + mu_const;1299 //R is already assigned;1300 }1301 1268 1302 1269 template<class sq_T> … … 1348 1315 } 1349 1316 1350 /////////// 1317 //// 1318 /////// 1319 template<class sq_T> 1320 void mgnorm<sq_T >::set_parameters ( fnc* g0, const sq_T &R0 ) {g=g0; this->iepdf.set_parameters ( zeros ( g->dimension() ), R0 );} 1321 template<class sq_T> 1322 void mgnorm<sq_T >::condition ( const vec &cond ) {this->iepdf._mu()=g->eval ( cond );}; 1351 1323 1352 1324 template<class sq_T>