Show
Ignore:
Timestamp:
10/23/09 00:05:25 (15 years ago)
Author:
smidl
Message:

Major changes in BM -- OK is only test suite and tests/tutorial -- the rest is broken!!!

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/bdmbase.h

    r678 r679  
    406406 
    407407        mpdf ( const mpdf &m ) : dimc ( m.dimc ), rvc ( m.rvc ), dim( m.dim), rv( m.rv ) { } 
     408         
     409        //! copy of the current object - make sure to implement 
     410        virtual mpdf* _copy_() const {return new mpdf(*this);} 
    408411        //!@} 
    409412 
     
    421424 
    422425        //! Shortcut for conditioning and evaluation of the internal epdf. In some cases,  this operation can be implemented efficiently. 
    423         virtual double evallogcond ( const vec &dt, const vec &cond ) { 
     426        virtual double evallogcond ( const vec &yt, const vec &cond ) { 
    424427                bdm_error ( "Not implemented" ); 
    425428                return 0.0; 
     
    427430 
    428431        //! Matrix version of evallogcond 
    429         virtual vec evallogcond_m ( const mat &Dt, const vec &cond ) { 
    430                 vec v ( Dt.cols() ); 
    431                 for ( int i = 0; i < Dt.cols(); i++ ) { 
    432                         v ( i ) = evallogcond ( Dt.get_col ( i ), cond ); 
     432        virtual vec evallogcond_m ( const mat &Yt, const vec &cond ) { 
     433                vec v ( Yt.cols() ); 
     434                for ( int i = 0; i < Yt.cols(); i++ ) { 
     435                        v ( i ) = evallogcond ( Yt.get_col ( i ), cond ); 
    433436                } 
    434437                return v; 
     
    436439 
    437440        //! Array<vec> version of evallogcond 
    438         virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ) { 
     441        virtual vec evallogcond_m ( const Array<vec> &Yt, const vec &cond ) { 
    439442                bdm_error ( "Not implemented" ); 
    440443                return vec(); 
     
    458461        } 
    459462 
     463        //! access function 
     464        void set_dim(int d) {dim=d;} 
     465        //! access function 
     466        void set_dimc(int d) {dimc=d;} 
    460467        //! Load from structure with elements: 
    461468        //!  \code 
     
    506513                dim = dim0; 
    507514        } 
     515        epdf* _copy_() const {return new epdf(*this);} 
    508516        //!@} 
    509517 
     
    10201028This object represents exact or approximate evaluation of the Bayes rule: 
    10211029\f[ 
    1022 f(\theta_t | d_1,\ldots,d_t) = \frac{f(y_t|\theta_t,\cdot) f(\theta_t|d_1,\ldots,d_{t-1})}{f(y_t|d_1,\ldots,d_{t-1})} 
     1030f(\theta_t | y_1,\ldots,y_t, u_1,\ldots,u_t) = \frac{f(y_t|\theta_t,\cdot) f(\theta_t|d_1,\ldots,d_{t-1})}{f(y_t|d_1,\ldots,d_{t-1})} 
    10231031\f] 
    1024  
     1032where: 
     1033 * \f$ y_t \f$ is the variable  
    10251034Access to the resulting posterior density is via function \c posterior(). 
    10261035 
     
    10281037It can also evaluate predictors of future values of \f$y_t\f$, see functions epredictor() and predictor(). 
    10291038 
    1030 Alternatively, it can evaluate posterior density conditioned by a known constant, \f$ c_t \f$: 
     1039Alternatively, it can evaluate posterior density with rvc replaced by the given values, \f$ c_t \f$: 
    10311040\f[ 
    10321041f(\theta_t | c_t, d_1,\ldots,d_t) \propto  f(y_t,\theta_t|c_t,\cdot, d_1,\ldots,d_{t-1}) 
    10331042\f] 
    10341043 
    1035 The value of \f$ c_t \f$ is set by function condition(). 
    10361044 
    10371045*/ 
     
    10401048protected: 
    10411049        //! Random variable of the data (optional) 
    1042         RV drv; 
     1050        RV yrv; 
     1051        //! size of the data record 
     1052        int dimy; 
     1053        //! Name of extension variable 
     1054        RV rvc; 
     1055        //! size of the conditioning vector 
     1056        int dimc; 
     1057         
    10431058        //!Logarithm of marginalized data likelihood. 
    10441059        double ll; 
    10451060        //!  If true, the filter will compute likelihood of the data record and store it in \c ll . Set to false if you want to save computational time. 
    10461061        bool evalll; 
    1047          
     1062 
    10481063public: 
    10491064        //! \name Constructors 
    10501065        //! @{ 
    10511066 
    1052         BM() : ll ( 0 ), evalll ( true ) { }; 
    1053         BM ( const BM &B ) :  drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {} 
     1067        BM() : yrv(),dimy(0),rvc(),dimc(0), ll ( 0 ), evalll ( true ) { }; 
     1068        BM ( const BM &B ) :  yrv ( B.yrv ), dimy(B.dimy), rvc ( B.rvc ), ll ( B.ll ), evalll ( B.evalll ) {} 
    10541069        //! \brief Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! 
    10551070        //! Prototype: \code BM* _copy_() const {return new BM(*this);} \endcode 
    1056         virtual BM* _copy_() const { 
    1057                 return NULL; 
    1058         }; 
     1071        virtual BM* _copy_() const { return NULL; }; 
    10591072        //!@} 
    10601073 
     
    10651078        @param dt vector of input data 
    10661079        */ 
    1067         virtual void bayes ( const vec &dt ) = 0; 
     1080        virtual void bayes ( const vec &yt, const vec &cond=empty_vec ) = 0; 
    10681081        //! Batch Bayes rule (columns of Dt are observations) 
    10691082        virtual void bayesB ( const mat &Dt ); 
    10701083        //! Evaluates predictive log-likelihood of the given data record 
    1071         //! I.e. marginal likelihood of the data with the posterior integrated out. 
    1072         virtual double logpred ( const vec &dt ) const { 
     1084        //! I.e. marginal likelihood of the data with the posterior integrated out.  
     1085        //! This function evaluates only \f$ y_t \f$, condition is assumed to be the last used in bayes().  
     1086        //! See bdm::BM::predictor for conditional version. 
     1087        virtual double logpred ( const vec &yt ) const { 
    10731088                bdm_error ( "Not implemented" ); 
    10741089                return 0.0; 
     
    10761091 
    10771092        //! Matrix version of logpred 
    1078         vec logpred_m ( const mat &dt ) const { 
    1079                 vec tmp ( dt.cols() ); 
    1080                 for ( int i = 0; i < dt.cols(); i++ ) { 
    1081                         tmp ( i ) = logpred ( dt.get_col ( i ) ); 
     1093        vec logpred_m ( const mat &Yt ) const { 
     1094                vec tmp ( Yt.cols() ); 
     1095                for ( int i = 0; i < Yt.cols(); i++ ) { 
     1096                        tmp ( i ) = logpred ( Yt.get_col ( i ) ); 
    10821097                } 
    10831098                return tmp; 
     
    10961111        //!@} 
    10971112 
    1098         //! \name Extension to conditional BM 
    1099         //! This extension is useful e.g. in Marginalized Particle Filter (\ref bdm::MPF). 
    1100         //! Alternatively, it can be used for automated connection to DS when the condition is observed 
    1101         //!@{ 
    1102  
    1103         //! Name of extension variable 
    1104         RV rvc; 
    1105         //! access function 
    1106         const RV& _rvc() const { 
    1107                 return rvc; 
    1108         } 
    1109  
    1110         //! Substitute \c val for \c rvc. 
    1111         virtual void condition ( const vec &val ) { 
    1112                 bdm_error ( "Not implemented!" ); 
    1113         } 
    1114  
    1115         //!@} 
    1116  
    11171113 
    11181114        //! \name Access to attributes 
    11191115        //!@{ 
     1116                //! access function 
     1117                const RV& _rvc() const { 
     1118                        return rvc; 
     1119                } 
     1120                //! access function 
     1121                int dimensionc() const { 
     1122                        return dimc; 
     1123                } 
     1124                //! access function 
     1125                int dimensiony() const { 
     1126                        return dimy; 
     1127                } 
     1128                //! access function 
     1129                int dimension() const { 
     1130                        return posterior().dimension(); 
     1131                } 
     1132                //! access function 
     1133        const RV& _yrv() const { 
     1134                return yrv; 
     1135        } 
    11201136        //! access function 
    1121         const RV& _drv() const { 
    1122                 return drv; 
    1123         } 
    1124         //! access function 
    1125         void set_drv ( const RV &rv ) { 
    1126                 drv = rv; 
     1137        void set_yrv ( const RV &rv ) { 
     1138                yrv = rv; 
    11271139        } 
    11281140        //! access to rv of the posterior 
    11291141        void set_rv ( const RV &rv ) { 
    1130                 const_cast<epdf&> ( posterior() ).set_rv ( rv ); 
     1142                const_cast<epdf&>(posterior()).set_rv ( rv ); 
     1143        } 
     1144        //! access function 
     1145        void set_dim ( int dim ) { 
     1146                const_cast<epdf&>(posterior()).set_dim ( dim ); 
    11311147        } 
    11321148        //! return internal log-likelihood of the last data vector 
     
    11801196                shared_ptr<RV> r = UI::build<RV> ( set, "drv", UI::optional ); 
    11811197                if ( r ) { 
    1182                         set_drv ( *r ); 
     1198                        set_yrv ( *r ); 
    11831199                } 
    11841200                string opt; 
     
    12151231 
    12161232template<class EPDF> 
    1217 double mpdf_internal<EPDF>::evallogcond ( const vec &dt, const vec &cond ) { 
     1233double mpdf_internal<EPDF>::evallogcond ( const vec &yt, const vec &cond ) { 
    12181234        double tmp; 
    12191235        condition ( cond ); 
    1220         tmp = iepdf.evallog ( dt ); 
     1236        tmp = iepdf.evallog ( yt ); 
    12211237        return tmp; 
    12221238} 
    12231239 
    12241240template<class EPDF> 
    1225 vec mpdf_internal<EPDF>::evallogcond_m ( const mat &Dt, const vec &cond ) { 
     1241vec mpdf_internal<EPDF>::evallogcond_m ( const mat &Yt, const vec &cond ) { 
    12261242        condition ( cond ); 
    1227         return iepdf.evallog_m ( Dt ); 
     1243        return iepdf.evallog_m ( Yt ); 
    12281244} 
    12291245 
    12301246template<class EPDF> 
    1231 vec mpdf_internal<EPDF>::evallogcond_m ( const Array<vec> &Dt, const vec &cond ) { 
     1247vec mpdf_internal<EPDF>::evallogcond_m ( const Array<vec> &Yt, const vec &cond ) { 
    12321248        condition ( cond ); 
    1233         return iepdf.evallog_m ( Dt ); 
     1249        return iepdf.evallog_m ( Yt ); 
    12341250} 
    12351251