Show
Ignore:
Timestamp:
08/08/09 13:42:18 (15 years ago)
Author:
smidl
Message:

1st step of mpdf redesign - BROKEN compile

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/stat/exp_family.h

    r476 r487  
    5555                                return tmp;} 
    5656                        //!Evaluate normalized log-probability for many samples 
    57                         virtual vec evallog ( const mat &Val ) const 
     57                        virtual vec evallog_m ( const mat &Val ) const 
    5858                        { 
    5959                                vec x ( Val.cols() ); 
     
    6565        }; 
    6666 
    67         /*! 
    68         * \brief Exponential family model. 
    69  
    70         * More?... 
    71         */ 
    72  
    73         class mEF : public mpdf 
    74         { 
    75  
    76                 public: 
    77                         //! Default constructor 
    78                         mEF ( ) :mpdf ( ) {}; 
    79         }; 
    8067 
    8168//! Estimator for Exponential family 
     
    10693        }; 
    10794 
    108         template<class sq_T> 
     95        template<class sq_T, template <typename> class TEpdf > 
    10996        class mlnorm; 
    11097 
     
    527514         Mean value \f$mu=A*rvc+mu_0\f$. 
    528515        */ 
    529         template<class sq_T> 
    530         class mlnorm : public mEF 
     516        template<class sq_T, template <typename> class TEpdf =enorm> 
     517        class mlnorm : public mpdf_internal< TEpdf<sq_T> > 
    531518        { 
    532519                protected: 
    533520                        //! Internal epdf that arise by conditioning on \c rvc 
    534                         shared_ptr<enorm<sq_T> > iepdf; 
    535521                        mat A; 
    536522                        vec mu_const; 
    537                         vec& _mu; //cached epdf.mu; 
     523//                      vec& _mu; //cached epdf.mu; !!!!!! WHY NOT? 
    538524                public: 
    539525                        //! \name Constructors 
    540526                        //!@{ 
    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 ); 
    545531                        } 
    546532 
    547533                        //! 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                        } 
    549543                        //!@} 
    550544                        //! 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                        } 
    552549 
    553550                        //!access function 
     
    556553                        mat& _A() {return A;} 
    557554                        //!access function 
    558                         mat _R() { return iepdf->_R().to_mat(); } 
    559  
    560                         template<class sq_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 ); 
    562559                         
    563560                        void from_setting(const Setting &set){ 
     
    577574//! Mpdf with general function for mean value 
    578575        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 &mu; 
     576        class mgnorm : public mpdf_internal< enorm< sq_T > > 
     577        { 
     578                protected: 
     579//                      vec &mu; WHY NOT? 
    585580                        fnc* g; 
    586581                public: 
    587582                        //!default constructor 
    588                         mgnorm():iepdf(new enorm<sq_T>()), mu(iepdf->_mu()) { set_ep(iepdf); } 
     583                        mgnorm():mpdf_internal<enorm<sq_T> >() { } 
    589584                        //!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 ); 
    592587 
    593588 
     
    644639        Perhaps a moment-matching technique? 
    645640        */ 
    646         class mlstudent : public mlnorm<ldmat> 
     641        class mlstudent : public mlnorm<ldmat,enorm> 
    647642        { 
    648643                protected: 
     
    651646                        ldmat Re; 
    652647                public: 
    653                         mlstudent ( ) :mlnorm<ldmat> (), 
    654                                         Lambda (),      _R ( iepdf->_R() ) {} 
     648                        mlstudent ( ) :mlnorm<ldmat,enorm> (), 
     649                                        Lambda (),      _R ( iepdf._R() ) {} 
    655650                        void set_parameters ( const mat &A0, const vec &mu0, const ldmat &R0, const ldmat& Lambda0 ) 
    656651                        { 
     
    658653                                it_assert_debug ( R0.rows() ==A0.rows(),"" ); 
    659654 
    660                                 iepdf->set_parameters ( mu0,Lambda ); // 
     655                                iepdf.set_parameters ( mu0,Lambda ); // 
    661656                                A = A0; 
    662657                                mu_const = mu0; 
     
    666661                        void condition ( const vec &cond ) 
    667662                        { 
    668                                 _mu = A*cond + mu_const; 
     663                                iepdf._mu() = A*cond + mu_const; 
    669664                                double zeta; 
    670665                                //ugly hack! 
     
    691686        The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. 
    692687        */ 
    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: 
    698691 
    699692                        //! Constant \f$k\f$ 
     
    705698                public: 
    706699                        //! 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()) { 
    710702                        } 
    711703 
     
    742734        The standard deviation of the walk is then: \f$ \mu/\sqrt(k)\f$. 
    743735         */ 
    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: 
    750739                        //! Constant \f$k\f$ 
    751740                        double k; 
     
    760749                        //! \name Constructors 
    761750                        //!@{ 
    762                         migamma():iepdf(new eigamma()), 
     751                        migamma():mpdf_internal<eigamma>(), 
    763752                            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>(), 
    770758                            k(0), 
    771                             _alpha(iepdf->_alpha()), 
    772                             _beta(iepdf->_beta()) { 
    773                             set_ep(iepdf); 
     759                            _alpha(iepdf._alpha()), 
     760                            _beta(iepdf._beta()) { 
    774761                        } 
    775762                        //!@} 
     
    779766                        { 
    780767                                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*/ ); 
    782769                                dimc = dimension(); 
    783770                        }; 
     
    915902        The standard deviation of the walk is then: \f$\mu/\sqrt(k)\f$. 
    916903         */ 
    917         class mlognorm : public mpdf 
    918         { 
    919                 protected: 
    920                         shared_ptr<elognorm> eno; 
    921  
     904        class mlognorm : public mpdf_internal<elognorm> 
     905        { 
     906                protected: 
    922907                        //! parameter 1/2*sigma^2 
    923908                        double sig2; 
     
    927912                public: 
    928913                        //! Constructor 
    929                         mlognorm():eno(new elognorm()), 
     914                        mlognorm():mpdf_internal<elognorm>(), 
    930915                            sig2(0), 
    931                             mu(eno->_mu()) { 
    932                             set_ep(eno); 
     916                            mu(iepdf._mu()) { 
    933917                        } 
    934918 
     
    937921                        { 
    938922                                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 ) ); 
    940924 
    941925                                dimc = size; 
     
    10711055        { 
    10721056                protected: 
    1073                         shared_ptr<eiWishartCh> eiW; 
     1057                        eiWishartCh eiW; 
    10741058                        //!square root of \f$ \nu-p-1 \f$ - needed for computation of \f$ \Psi \f$ from conditions 
    10751059                        double sqd; 
     
    10801064 
    10811065                public: 
    1082                         rwiWishartCh():eiW(new eiWishartCh()), 
     1066                        rwiWishartCh():eiW(), 
    10831067                            sqd(0), l(0), p(0) { 
    10841068                            set_ep(eiW); 
     
    10931077                                refl=pow(ref0,1-l); 
    10941078                                 
    1095                                 eiW->set_parameters ( eye ( p ),delta ); 
    1096                                 dimc=eiW->dimension(); 
     1079                                eiW.set_parameters ( eye ( p ),delta ); 
     1080                                dimc=eiW.dimension(); 
    10971081                        } 
    10981082                        void condition ( const vec &c ) { 
     
    11041088                                } 
    11051089 
    1106                                 eiW->_setY ( sqd*z ); 
     1090                                eiW._setY ( sqd*z ); 
    11071091                        } 
    11081092        }; 
     
    12561240        }; 
    12571241 
    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         } 
    12691242 
    12701243// template<class sq_T> 
     
    12931266// } 
    12941267 
    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         } 
    13011268 
    13021269        template<class sq_T> 
     
    13481315        } 
    13491316 
    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 );}; 
    13511323 
    13521324        template<class sq_T>