Changeset 679 for library/bdm/base/bdmbase.h
- Timestamp:
- 10/23/09 00:05:25 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/base/bdmbase.h
r678 r679 406 406 407 407 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);} 408 411 //!@} 409 412 … … 421 424 422 425 //! 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 ) { 424 427 bdm_error ( "Not implemented" ); 425 428 return 0.0; … … 427 430 428 431 //! 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 ); 433 436 } 434 437 return v; … … 436 439 437 440 //! 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 ) { 439 442 bdm_error ( "Not implemented" ); 440 443 return vec(); … … 458 461 } 459 462 463 //! access function 464 void set_dim(int d) {dim=d;} 465 //! access function 466 void set_dimc(int d) {dimc=d;} 460 467 //! Load from structure with elements: 461 468 //! \code … … 506 513 dim = dim0; 507 514 } 515 epdf* _copy_() const {return new epdf(*this);} 508 516 //!@} 509 517 … … 1020 1028 This object represents exact or approximate evaluation of the Bayes rule: 1021 1029 \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})}1030 f(\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})} 1023 1031 \f] 1024 1032 where: 1033 * \f$ y_t \f$ is the variable 1025 1034 Access to the resulting posterior density is via function \c posterior(). 1026 1035 … … 1028 1037 It can also evaluate predictors of future values of \f$y_t\f$, see functions epredictor() and predictor(). 1029 1038 1030 Alternatively, it can evaluate posterior density conditioned by a known constant, \f$ c_t \f$:1039 Alternatively, it can evaluate posterior density with rvc replaced by the given values, \f$ c_t \f$: 1031 1040 \f[ 1032 1041 f(\theta_t | c_t, d_1,\ldots,d_t) \propto f(y_t,\theta_t|c_t,\cdot, d_1,\ldots,d_{t-1}) 1033 1042 \f] 1034 1043 1035 The value of \f$ c_t \f$ is set by function condition().1036 1044 1037 1045 */ … … 1040 1048 protected: 1041 1049 //! 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 1043 1058 //!Logarithm of marginalized data likelihood. 1044 1059 double ll; 1045 1060 //! 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. 1046 1061 bool evalll; 1047 1062 1048 1063 public: 1049 1064 //! \name Constructors 1050 1065 //! @{ 1051 1066 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 ) {} 1054 1069 //! \brief Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! 1055 1070 //! 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; }; 1059 1072 //!@} 1060 1073 … … 1065 1078 @param dt vector of input data 1066 1079 */ 1067 virtual void bayes ( const vec & dt) = 0;1080 virtual void bayes ( const vec &yt, const vec &cond=empty_vec ) = 0; 1068 1081 //! Batch Bayes rule (columns of Dt are observations) 1069 1082 virtual void bayesB ( const mat &Dt ); 1070 1083 //! 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 { 1073 1088 bdm_error ( "Not implemented" ); 1074 1089 return 0.0; … … 1076 1091 1077 1092 //! 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 ) ); 1082 1097 } 1083 1098 return tmp; … … 1096 1111 //!@} 1097 1112 1098 //! \name Extension to conditional BM1099 //! 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 observed1101 //!@{1102 1103 //! Name of extension variable1104 RV rvc;1105 //! access function1106 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 1117 1113 1118 1114 //! \name Access to attributes 1119 1115 //!@{ 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 } 1120 1136 //! 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; 1127 1139 } 1128 1140 //! access to rv of the posterior 1129 1141 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 ); 1131 1147 } 1132 1148 //! return internal log-likelihood of the last data vector … … 1180 1196 shared_ptr<RV> r = UI::build<RV> ( set, "drv", UI::optional ); 1181 1197 if ( r ) { 1182 set_ drv ( *r );1198 set_yrv ( *r ); 1183 1199 } 1184 1200 string opt; … … 1215 1231 1216 1232 template<class EPDF> 1217 double mpdf_internal<EPDF>::evallogcond ( const vec & dt, const vec &cond ) {1233 double mpdf_internal<EPDF>::evallogcond ( const vec &yt, const vec &cond ) { 1218 1234 double tmp; 1219 1235 condition ( cond ); 1220 tmp = iepdf.evallog ( dt );1236 tmp = iepdf.evallog ( yt ); 1221 1237 return tmp; 1222 1238 } 1223 1239 1224 1240 template<class EPDF> 1225 vec mpdf_internal<EPDF>::evallogcond_m ( const mat & Dt, const vec &cond ) {1241 vec mpdf_internal<EPDF>::evallogcond_m ( const mat &Yt, const vec &cond ) { 1226 1242 condition ( cond ); 1227 return iepdf.evallog_m ( Dt );1243 return iepdf.evallog_m ( Yt ); 1228 1244 } 1229 1245 1230 1246 template<class EPDF> 1231 vec mpdf_internal<EPDF>::evallogcond_m ( const Array<vec> & Dt, const vec &cond ) {1247 vec mpdf_internal<EPDF>::evallogcond_m ( const Array<vec> &Yt, const vec &cond ) { 1232 1248 condition ( cond ); 1233 return iepdf.evallog_m ( Dt );1249 return iepdf.evallog_m ( Yt ); 1234 1250 } 1235 1251