[2] | 1 | /*! |
---|
[5] | 2 | \file |
---|
| 3 | \brief Bayesian Models (bm) that use Bayes rule to learn from observations |
---|
| 4 | \author Vaclav Smidl. |
---|
[2] | 5 | |
---|
[5] | 6 | ----------------------------------- |
---|
| 7 | BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
---|
| 8 | |
---|
| 9 | Using IT++ for numerical operations |
---|
| 10 | ----------------------------------- |
---|
| 11 | */ |
---|
| 12 | |
---|
[2] | 13 | #ifndef BM_H |
---|
| 14 | #define BM_H |
---|
| 15 | |
---|
[351] | 16 | #include <map> |
---|
[263] | 17 | |
---|
[190] | 18 | #include "../itpp_ext.h" |
---|
[344] | 19 | #include "../libconfig/libconfig.h++" |
---|
[2] | 20 | |
---|
[351] | 21 | |
---|
[340] | 22 | using namespace libconfig; |
---|
| 23 | |
---|
[270] | 24 | using namespace itpp; |
---|
| 25 | using namespace std; |
---|
[2] | 26 | |
---|
[340] | 27 | namespace bdm { |
---|
| 28 | |
---|
[254] | 29 | //! Root class of BDM objects |
---|
[340] | 30 | |
---|
[270] | 31 | class bdmroot { |
---|
| 32 | public: |
---|
| 33 | //! make sure this is a virtual object |
---|
[340] | 34 | virtual ~bdmroot() |
---|
| 35 | { |
---|
| 36 | } |
---|
[339] | 37 | |
---|
[344] | 38 | //! This method returns a basic info about the current instance |
---|
[340] | 39 | virtual string ToString() |
---|
| 40 | { |
---|
| 41 | return ""; |
---|
| 42 | } |
---|
| 43 | |
---|
[339] | 44 | //! This method arrange instance properties according the data stored in the Setting structure |
---|
[345] | 45 | virtual void from_setting( const Setting &root ) |
---|
[339] | 46 | { |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | //! This method save all the instance properties into the Setting structure |
---|
[351] | 50 | virtual void to_setting( Setting &root ) const |
---|
[339] | 51 | { |
---|
| 52 | } |
---|
[270] | 53 | }; |
---|
[254] | 54 | |
---|
[270] | 55 | typedef std::map<string, int> RVmap; |
---|
| 56 | extern ivec RV_SIZES; |
---|
| 57 | extern Array<string> RV_NAMES; |
---|
| 58 | |
---|
[181] | 59 | //! Structure of RV (used internally), i.e. expanded RVs |
---|
[270] | 60 | class str { |
---|
| 61 | public: |
---|
| 62 | //! vector id ids (non-unique!) |
---|
| 63 | ivec ids; |
---|
| 64 | //! vector of times |
---|
| 65 | ivec times; |
---|
| 66 | //!Default constructor |
---|
| 67 | str ( ivec ids0, ivec times0 ) :ids ( ids0 ),times ( times0 ) { |
---|
| 68 | it_assert_debug ( times0.length() ==ids0.length(),"Incompatible input" ); |
---|
[145] | 69 | }; |
---|
[270] | 70 | }; |
---|
[145] | 71 | |
---|
[270] | 72 | /*! |
---|
| 73 | * \brief Class representing variables, most often random variables |
---|
[5] | 74 | |
---|
[270] | 75 | The purpose of this class is to decribe a vector of data. Such description is used for connecting various vectors between each other, see class datalink. |
---|
[32] | 76 | |
---|
[270] | 77 | The class is implemented using global variables to assure uniqueness of description: |
---|
[2] | 78 | |
---|
[270] | 79 | In is a vector |
---|
| 80 | \dot |
---|
| 81 | digraph datalink { |
---|
| 82 | rankdir=LR; |
---|
| 83 | subgraph cluster0 { |
---|
| 84 | node [shape=record]; |
---|
| 85 | label = "RV_MAP \n std::map<string,int>"; |
---|
| 86 | map [label="{{\"a\"| \"b\" | \"c\"} | {<3> 3 |<1> 1|<2> 2}}"]; |
---|
| 87 | color = "white" |
---|
| 88 | } |
---|
| 89 | subgraph cluster1{ |
---|
| 90 | node [shape=record]; |
---|
| 91 | label = "RV_NAMES"; |
---|
| 92 | names [label="{<1> \"b\" | <2> \"c\" | <3>\"a\" }"]; |
---|
| 93 | color = "white" |
---|
| 94 | } |
---|
| 95 | subgraph cluster2{ |
---|
| 96 | node [shape=record]; |
---|
| 97 | label = "RV_SIZES"; |
---|
| 98 | labelloc = b; |
---|
| 99 | sizes [label="{<1>1 |<2> 4 |<3> 1}"]; |
---|
| 100 | color = "white" |
---|
| 101 | } |
---|
| 102 | map:1 -> names:1; |
---|
| 103 | map:1 -> sizes:1; |
---|
| 104 | map:3 -> names:3; |
---|
| 105 | map:3 -> sizes:3; |
---|
| 106 | } |
---|
| 107 | \enddot |
---|
| 108 | */ |
---|
[32] | 109 | |
---|
[270] | 110 | class RV :public bdmroot { |
---|
| 111 | protected: |
---|
| 112 | //! size of the data vector |
---|
| 113 | int dsize; |
---|
| 114 | //! number of individual rvs |
---|
| 115 | int len; |
---|
| 116 | //! Vector of unique IDs |
---|
| 117 | ivec ids; |
---|
| 118 | //! Vector of shifts from current time |
---|
| 119 | ivec times; |
---|
[5] | 120 | |
---|
[270] | 121 | private: |
---|
| 122 | //! auxiliary function used in constructor |
---|
| 123 | void init ( Array<std::string> in_names, ivec in_sizes, ivec in_times ); |
---|
| 124 | int init ( const string &name, int size ); |
---|
| 125 | public: |
---|
[271] | 126 | //! \name Constructors |
---|
[270] | 127 | //!@{ |
---|
[271] | 128 | |
---|
| 129 | //! Full constructor |
---|
[270] | 130 | RV ( Array<std::string> in_names, ivec in_sizes, ivec in_times ) {init ( in_names,in_sizes,in_times );}; |
---|
[271] | 131 | //! Constructor with times=0 |
---|
[270] | 132 | RV ( Array<std::string> in_names, ivec in_sizes ) {init ( in_names,in_sizes,zeros_i ( in_names.length() ) );}; |
---|
[271] | 133 | //! Constructor with sizes=1, times=0 |
---|
[270] | 134 | RV ( Array<std::string> in_names ) {init ( in_names,ones_i ( in_names.length() ),zeros_i ( in_names.length() ) );} |
---|
[271] | 135 | //! Constructor of empty RV |
---|
[270] | 136 | RV () :dsize ( 0 ),len ( 0 ),ids ( 0 ),times ( 0 ) {}; |
---|
| 137 | //! Constructor of a single RV with given id |
---|
| 138 | RV ( string name, int sz, int tm=0 ); |
---|
| 139 | //!@} |
---|
[271] | 140 | |
---|
[270] | 141 | //! \name Access functions |
---|
| 142 | //!@{ |
---|
[271] | 143 | |
---|
[270] | 144 | //! Printing output e.g. for debugging. |
---|
| 145 | friend std::ostream &operator<< ( std::ostream &os, const RV &rv ); |
---|
| 146 | int _dsize() const {return dsize;} ; |
---|
| 147 | //! Recount size of the corresponding data vector |
---|
| 148 | int countsize() const; |
---|
[271] | 149 | ivec cumsizes() const; |
---|
[270] | 150 | int length() const {return len;} ; |
---|
| 151 | int id ( int at ) const{return ids ( at );}; |
---|
[283] | 152 | int size ( int at ) const {return RV_SIZES ( ids ( at ) );}; |
---|
[270] | 153 | int time ( int at ) const{return times ( at );}; |
---|
[283] | 154 | std::string name ( int at ) const {return RV_NAMES ( ids ( at ) );}; |
---|
[270] | 155 | void set_time ( int at, int time0 ) {times ( at ) =time0;}; |
---|
| 156 | //!@} |
---|
[271] | 157 | |
---|
[270] | 158 | //TODO why not inline and later?? |
---|
[32] | 159 | |
---|
[270] | 160 | //! \name Algebra on Random Variables |
---|
| 161 | //!@{ |
---|
[271] | 162 | |
---|
[270] | 163 | //! Find indices of self in another rv, \return ivec of the same size as self. |
---|
| 164 | ivec findself ( const RV &rv2 ) const; |
---|
| 165 | //! Compare if \c rv2 is identical to this \c RV |
---|
| 166 | bool equal ( const RV &rv2 ) const; |
---|
| 167 | //! Add (concat) another variable to the current one, \return true if all rv2 were added, false if rv2 is in conflict |
---|
| 168 | bool add ( const RV &rv2 ); |
---|
| 169 | //! Subtract another variable from the current one |
---|
| 170 | RV subt ( const RV &rv2 ) const; |
---|
| 171 | //! Select only variables at indeces ind |
---|
| 172 | RV subselect ( const ivec &ind ) const; |
---|
| 173 | //! Select only variables at indeces ind |
---|
| 174 | RV operator() ( const ivec &ind ) const {return subselect ( ind );}; |
---|
[271] | 175 | //! Select from data vector starting at di1 to di2 |
---|
| 176 | RV operator() ( int di1, int di2 ) const { |
---|
| 177 | ivec sz=cumsizes(); |
---|
| 178 | int i1=0; |
---|
| 179 | while ( sz ( i1 ) <di1 ) i1++; |
---|
| 180 | int i2=i1; |
---|
| 181 | while ( sz ( i2 ) <di2 ) i2++; |
---|
| 182 | return subselect ( linspace ( i1,i2 ) ); |
---|
| 183 | }; |
---|
[270] | 184 | //! Shift \c time shifted by delta. |
---|
| 185 | void t ( int delta ); |
---|
| 186 | //!@} |
---|
[271] | 187 | |
---|
| 188 | //!\name Relation to vectors |
---|
[270] | 189 | //!@{ |
---|
[271] | 190 | |
---|
[270] | 191 | //! generate \c str from rv, by expanding sizes |
---|
| 192 | str tostr() const; |
---|
| 193 | //! when this rv is a part of bigger rv, this function returns indeces of self in the data vector of the bigger crv. |
---|
| 194 | //! Then, data can be copied via: data_of_this = cdata(ind); |
---|
| 195 | ivec dataind ( const RV &crv ) const; |
---|
| 196 | //! generate mutual indeces when copying data betwenn self and crv. |
---|
| 197 | //! Data are copied via: data_of_this(selfi) = data_of_rv2(rv2i) |
---|
| 198 | void dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const; |
---|
| 199 | //! Minimum time-offset |
---|
| 200 | int mint () const {return min ( times );}; |
---|
| 201 | //!@} |
---|
[271] | 202 | |
---|
[270] | 203 | }; |
---|
[32] | 204 | |
---|
[51] | 205 | |
---|
[145] | 206 | //! Concat two random variables |
---|
[270] | 207 | RV concat ( const RV &rv1, const RV &rv2 ); |
---|
[2] | 208 | |
---|
[211] | 209 | //!Default empty RV that can be used as default argument |
---|
[270] | 210 | extern RV RV0; |
---|
[145] | 211 | |
---|
[85] | 212 | //! Class representing function \f$f(x)\f$ of variable \f$x\f$ represented by \c rv |
---|
[2] | 213 | |
---|
[270] | 214 | class fnc :public bdmroot { |
---|
| 215 | protected: |
---|
| 216 | //! Length of the output vector |
---|
| 217 | int dimy; |
---|
| 218 | public: |
---|
| 219 | //!default constructor |
---|
| 220 | fnc ( ) {}; |
---|
| 221 | //! function evaluates numerical value of \f$f(x)\f$ at \f$x=\f$ \c cond |
---|
| 222 | virtual vec eval ( const vec &cond ) { |
---|
| 223 | return vec ( 0 ); |
---|
| 224 | }; |
---|
[27] | 225 | |
---|
[270] | 226 | //! function substitutes given value into an appropriate position |
---|
| 227 | virtual void condition ( const vec &val ) {}; |
---|
[28] | 228 | |
---|
[270] | 229 | //! access function |
---|
[283] | 230 | int dimension() const{return dimy;} |
---|
[270] | 231 | }; |
---|
[2] | 232 | |
---|
[270] | 233 | class mpdf; |
---|
[7] | 234 | |
---|
[4] | 235 | //! Probability density function with numerical statistics, e.g. posterior density. |
---|
[32] | 236 | |
---|
[270] | 237 | class epdf :public bdmroot { |
---|
| 238 | protected: |
---|
| 239 | //! dimension of the random variable |
---|
| 240 | int dim; |
---|
| 241 | //! Description of the random variable |
---|
| 242 | RV rv; |
---|
[32] | 243 | |
---|
[270] | 244 | public: |
---|
| 245 | /*! \name Constructors |
---|
[271] | 246 | Construction of each epdf should support two types of constructors: |
---|
| 247 | \li empty constructor, |
---|
[270] | 248 | \li copy constructor, |
---|
[271] | 249 | |
---|
[270] | 250 | The following constructors should be supported for convenience: |
---|
[271] | 251 | \li constructor followed by calling \c set_parameters() |
---|
[270] | 252 | \li constructor accepting random variables calling \c set_rv() |
---|
[271] | 253 | |
---|
[270] | 254 | All internal data structures are constructed as empty. Their values (including sizes) will be set by method \c set_parameters(). This way references can be initialized in constructors. |
---|
| 255 | @{*/ |
---|
[271] | 256 | epdf() :dim ( 0 ),rv ( ) {}; |
---|
| 257 | epdf ( const epdf &e ) :dim ( e.dim ),rv ( e.rv ) {}; |
---|
| 258 | epdf ( const RV &rv0 ) {set_rv ( rv0 );}; |
---|
| 259 | void set_parameters ( int dim0 ) {dim=dim0;} |
---|
[270] | 260 | //!@} |
---|
[271] | 261 | |
---|
[270] | 262 | //! \name Matematical Operations |
---|
| 263 | //!@{ |
---|
[271] | 264 | |
---|
[270] | 265 | //! Returns a sample, \f$ x \f$ from density \f$ f_x()\f$ |
---|
[271] | 266 | virtual vec sample () const {it_error ( "not implemneted" );return vec ( 0 );}; |
---|
[270] | 267 | //! Returns N samples, \f$ [x_1 , x_2 , \ldots \ \f$ from density \f$ f_x(rv)\f$ |
---|
| 268 | virtual mat sample_m ( int N ) const; |
---|
| 269 | //! Compute log-probability of argument \c val |
---|
[271] | 270 | virtual double evallog ( const vec &val ) const {it_error ( "not implemneted" );return 0.0;}; |
---|
[270] | 271 | //! Compute log-probability of multiple values argument \c val |
---|
| 272 | virtual vec evallog_m ( const mat &Val ) const { |
---|
| 273 | vec x ( Val.cols() ); |
---|
| 274 | for ( int i=0;i<Val.cols();i++ ) {x ( i ) =evallog ( Val.get_col ( i ) ) ;} |
---|
| 275 | return x; |
---|
| 276 | } |
---|
| 277 | //! Return conditional density on the given RV, the remaining rvs will be in conditioning |
---|
| 278 | virtual mpdf* condition ( const RV &rv ) const {it_warning ( "Not implemented" ); return NULL;} |
---|
| 279 | //! Return marginal density on the given RV, the remainig rvs are intergrated out |
---|
| 280 | virtual epdf* marginal ( const RV &rv ) const {it_warning ( "Not implemented" ); return NULL;} |
---|
| 281 | //! return expected value |
---|
[271] | 282 | virtual vec mean() const {it_error ( "not implemneted" );return vec ( 0 );}; |
---|
[270] | 283 | //! return expected variance (not covariance!) |
---|
[271] | 284 | virtual vec variance() const {it_error ( "not implemneted" );return vec ( 0 );}; |
---|
[283] | 285 | //! Lower and upper bounds of \c percentage % quantile, returns mean-2*sigma as default |
---|
| 286 | virtual void qbounds ( vec &lb, vec &ub, double percentage=0.95 ) const { |
---|
[286] | 287 | vec mea=mean(); vec std=sqrt ( variance() ); |
---|
[283] | 288 | lb = mea-2*std; ub=mea+2*std; |
---|
| 289 | }; |
---|
[270] | 290 | //!@} |
---|
[271] | 291 | |
---|
[270] | 292 | //! \name Connection to other classes |
---|
[271] | 293 | //! Description of the random quantity via attribute \c rv is optional. |
---|
| 294 | //! For operations such as sampling \c rv does not need to be set. However, for \c marginalization |
---|
| 295 | //! and \c conditioning \c rv has to be set. NB: |
---|
[270] | 296 | //! @{ |
---|
[271] | 297 | |
---|
[270] | 298 | //!Name its rv |
---|
| 299 | void set_rv ( const RV &rv0 ) {rv = rv0; }//it_assert_debug(isnamed(),""); }; |
---|
[271] | 300 | //! True if rv is assigned |
---|
| 301 | bool isnamed() const {bool b= ( dim==rv._dsize() );return b;} |
---|
[270] | 302 | //! Return name (fails when isnamed is false) |
---|
| 303 | const RV& _rv() const {it_assert_debug ( isnamed(),"" ); return rv;} |
---|
| 304 | //!@} |
---|
[271] | 305 | |
---|
[270] | 306 | //! \name Access to attributes |
---|
| 307 | //! @{ |
---|
[271] | 308 | |
---|
[270] | 309 | //! Size of the random variable |
---|
| 310 | int dimension() const {return dim;} |
---|
| 311 | //!@} |
---|
[271] | 312 | |
---|
[270] | 313 | }; |
---|
[32] | 314 | |
---|
[190] | 315 | |
---|
[5] | 316 | //! Conditional probability density, e.g. modeling some dependencies. |
---|
[32] | 317 | //TODO Samplecond can be generalized |
---|
| 318 | |
---|
[270] | 319 | class mpdf : public bdmroot { |
---|
| 320 | protected: |
---|
| 321 | //!dimension of the condition |
---|
| 322 | int dimc; |
---|
| 323 | //! random variable in condition |
---|
| 324 | RV rvc; |
---|
| 325 | //! pointer to internal epdf |
---|
| 326 | epdf* ep; |
---|
| 327 | public: |
---|
| 328 | //! \name Constructors |
---|
| 329 | //! @{ |
---|
[271] | 330 | |
---|
| 331 | mpdf ( ) :dimc ( 0 ),rvc ( ) {}; |
---|
[270] | 332 | //! copy constructor does not set pointer \c ep - has to be done in offsprings! |
---|
[271] | 333 | mpdf ( const mpdf &m ) :dimc ( m.dimc ),rvc ( m.rvc ) {}; |
---|
[270] | 334 | //!@} |
---|
[2] | 335 | |
---|
[270] | 336 | //! \name Matematical operations |
---|
| 337 | //!@{ |
---|
[271] | 338 | |
---|
[270] | 339 | //! 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 |
---|
| 340 | virtual vec samplecond ( const vec &cond ) { |
---|
| 341 | this->condition ( cond ); |
---|
| 342 | vec temp= ep->sample(); |
---|
| 343 | return temp; |
---|
| 344 | }; |
---|
[294] | 345 | //! 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 |
---|
[270] | 346 | virtual mat samplecond_m ( const vec &cond, int N ) { |
---|
| 347 | this->condition ( cond ); |
---|
| 348 | mat temp ( ep->dimension(),N ); vec smp ( ep->dimension() ); |
---|
| 349 | for ( int i=0;i<N;i++ ) {smp=ep->sample() ;temp.set_col ( i, smp );} |
---|
| 350 | return temp; |
---|
| 351 | }; |
---|
| 352 | //! Update \c ep so that it represents this mpdf conditioned on \c rvc = cond |
---|
| 353 | virtual void condition ( const vec &cond ) {it_error ( "Not implemented" );}; |
---|
[102] | 354 | |
---|
[270] | 355 | //! Shortcut for conditioning and evaluation of the internal epdf. In some cases, this operation can be implemented efficiently. |
---|
| 356 | virtual double evallogcond ( const vec &dt, const vec &cond ) { |
---|
| 357 | double tmp; this->condition ( cond );tmp = ep->evallog ( dt ); it_assert_debug ( std::isfinite ( tmp ),"Infinite value" ); return tmp; |
---|
| 358 | }; |
---|
[32] | 359 | |
---|
[270] | 360 | //! Matrix version of evallogcond |
---|
| 361 | virtual vec evallogcond_m ( const mat &Dt, const vec &cond ) {this->condition ( cond );return ep->evallog_m ( Dt );}; |
---|
[201] | 362 | |
---|
[270] | 363 | //! \name Access to attributes |
---|
| 364 | //! @{ |
---|
[271] | 365 | |
---|
[270] | 366 | RV _rv() {return ep->_rv();} |
---|
| 367 | RV _rvc() {it_assert_debug ( isnamed(),"" ); return rvc;} |
---|
| 368 | int dimension() {return ep->dimension();} |
---|
| 369 | int dimensionc() {return dimc;} |
---|
| 370 | epdf& _epdf() {return *ep;} |
---|
| 371 | epdf* _e() {return ep;} |
---|
| 372 | //!@} |
---|
[271] | 373 | |
---|
[270] | 374 | //! \name Connection to other objects |
---|
| 375 | //!@{ |
---|
| 376 | void set_rvc ( const RV &rvc0 ) {rvc=rvc0;} |
---|
[271] | 377 | void set_rv ( const RV &rv0 ) {ep->set_rv ( rv0 );} |
---|
| 378 | bool isnamed() {return ( ep->isnamed() ) && ( dimc==rvc._dsize() );} |
---|
[270] | 379 | //!@} |
---|
| 380 | }; |
---|
[32] | 381 | |
---|
[270] | 382 | /*! \brief DataLink is a connection between two data vectors Up and Down |
---|
[2] | 383 | |
---|
[270] | 384 | Up can be longer than Down. Down must be fully present in Up (TODO optional) |
---|
| 385 | See chart: |
---|
| 386 | \dot |
---|
| 387 | digraph datalink { |
---|
| 388 | node [shape=record]; |
---|
| 389 | subgraph cluster0 { |
---|
| 390 | label = "Up"; |
---|
| 391 | up [label="<1>|<2>|<3>|<4>|<5>"]; |
---|
| 392 | color = "white" |
---|
| 393 | } |
---|
| 394 | subgraph cluster1{ |
---|
| 395 | label = "Down"; |
---|
| 396 | labelloc = b; |
---|
| 397 | down [label="<1>|<2>|<3>"]; |
---|
| 398 | color = "white" |
---|
| 399 | } |
---|
| 400 | up:1 -> down:1; |
---|
| 401 | up:3 -> down:2; |
---|
| 402 | up:5 -> down:3; |
---|
| 403 | } |
---|
| 404 | \enddot |
---|
[263] | 405 | |
---|
[270] | 406 | */ |
---|
| 407 | class datalink { |
---|
| 408 | protected: |
---|
| 409 | //! Remember how long val should be |
---|
| 410 | int downsize; |
---|
| 411 | //! Remember how long val of "Up" should be |
---|
| 412 | int upsize; |
---|
| 413 | //! val-to-val link, indeces of the upper val |
---|
| 414 | ivec v2v_up; |
---|
| 415 | public: |
---|
| 416 | //! Constructor |
---|
[271] | 417 | datalink () {}; |
---|
| 418 | datalink ( const RV &rv, const RV &rv_up ) {set_connection ( rv,rv_up );}; |
---|
| 419 | //! set connection, rv must be fully present in rv_up |
---|
| 420 | void set_connection ( const RV &rv, const RV &rv_up ) { |
---|
| 421 | downsize = rv._dsize(); |
---|
| 422 | upsize = rv_up._dsize(); |
---|
| 423 | v2v_up= ( rv.dataind ( rv_up ) ); |
---|
| 424 | |
---|
[270] | 425 | it_assert_debug ( v2v_up.length() ==downsize,"rv is not fully in rv_up" ); |
---|
[192] | 426 | } |
---|
[286] | 427 | //! set connection using indeces |
---|
| 428 | void set_connection ( int ds, int us, const ivec &upind ) { |
---|
| 429 | downsize = ds; |
---|
| 430 | upsize = us; |
---|
| 431 | v2v_up= upind; |
---|
| 432 | |
---|
| 433 | it_assert_debug ( v2v_up.length() ==downsize,"rv is not fully in rv_up" ); |
---|
| 434 | } |
---|
[270] | 435 | //! Get val for myself from val of "Up" |
---|
| 436 | vec pushdown ( const vec &val_up ) { |
---|
| 437 | it_assert_debug ( upsize==val_up.length(),"Wrong val_up" ); |
---|
| 438 | return get_vec ( val_up,v2v_up ); |
---|
[192] | 439 | } |
---|
[270] | 440 | //! Fill val of "Up" by my pieces |
---|
| 441 | void pushup ( vec &val_up, const vec &val ) { |
---|
| 442 | it_assert_debug ( downsize==val.length(),"Wrong val" ); |
---|
| 443 | it_assert_debug ( upsize==val_up.length(),"Wrong val_up" ); |
---|
| 444 | set_subvector ( val_up, v2v_up, val ); |
---|
[263] | 445 | } |
---|
[270] | 446 | }; |
---|
[115] | 447 | |
---|
[192] | 448 | //! data link between |
---|
[270] | 449 | class datalink_m2e: public datalink { |
---|
| 450 | protected: |
---|
| 451 | //! Remember how long cond should be |
---|
| 452 | int condsize; |
---|
| 453 | //!upper_val-to-local_cond link, indeces of the upper val |
---|
| 454 | ivec v2c_up; |
---|
| 455 | //!upper_val-to-local_cond link, ideces of the local cond |
---|
| 456 | ivec v2c_lo; |
---|
[192] | 457 | |
---|
[270] | 458 | public: |
---|
[286] | 459 | datalink_m2e() {}; |
---|
[270] | 460 | //! Constructor |
---|
[286] | 461 | void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up ) { |
---|
| 462 | datalink::set_connection ( rv,rv_up ); |
---|
| 463 | condsize= rvc._dsize(); |
---|
[270] | 464 | //establish v2c connection |
---|
| 465 | rvc.dataind ( rv_up, v2c_lo, v2c_up ); |
---|
| 466 | } |
---|
| 467 | //!Construct condition |
---|
| 468 | vec get_cond ( const vec &val_up ) { |
---|
| 469 | vec tmp ( condsize ); |
---|
| 470 | set_subvector ( tmp,v2c_lo,val_up ( v2c_up ) ); |
---|
| 471 | return tmp; |
---|
| 472 | } |
---|
| 473 | void pushup_cond ( vec &val_up, const vec &val, const vec &cond ) { |
---|
| 474 | it_assert_debug ( downsize==val.length(),"Wrong val" ); |
---|
| 475 | it_assert_debug ( upsize==val_up.length(),"Wrong val_up" ); |
---|
| 476 | set_subvector ( val_up, v2v_up, val ); |
---|
| 477 | set_subvector ( val_up, v2c_up, cond ); |
---|
| 478 | } |
---|
| 479 | }; |
---|
[192] | 480 | //!DataLink is a connection between mpdf and its superordinate (Up) |
---|
| 481 | //! This class links |
---|
[270] | 482 | class datalink_m2m: public datalink_m2e { |
---|
| 483 | protected: |
---|
| 484 | //!cond-to-cond link, indeces of the upper cond |
---|
| 485 | ivec c2c_up; |
---|
| 486 | //!cond-to-cond link, indeces of the local cond |
---|
| 487 | ivec c2c_lo; |
---|
| 488 | public: |
---|
| 489 | //! Constructor |
---|
[286] | 490 | datalink_m2m() {}; |
---|
| 491 | void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up ) { |
---|
| 492 | datalink_m2e::set_connection ( rv, rvc, rv_up ); |
---|
[270] | 493 | //establish c2c connection |
---|
| 494 | rvc.dataind ( rvc_up, c2c_lo, c2c_up ); |
---|
| 495 | it_assert_debug ( c2c_lo.length() +v2c_lo.length() ==condsize, "cond is not fully given" ); |
---|
| 496 | } |
---|
| 497 | //! Get cond for myself from val and cond of "Up" |
---|
| 498 | vec get_cond ( const vec &val_up, const vec &cond_up ) { |
---|
| 499 | vec tmp ( condsize ); |
---|
| 500 | set_subvector ( tmp,v2c_lo,val_up ( v2c_up ) ); |
---|
| 501 | set_subvector ( tmp,c2c_lo,cond_up ( c2c_up ) ); |
---|
| 502 | return tmp; |
---|
| 503 | } |
---|
| 504 | //! Fill |
---|
[190] | 505 | |
---|
[270] | 506 | }; |
---|
[190] | 507 | |
---|
[270] | 508 | /*! |
---|
| 509 | @brief Class for storing results (and semi-results) of an experiment |
---|
[267] | 510 | |
---|
[270] | 511 | This class abstracts logging of results from implementation. This class replaces direct logging of results (e.g. to files or to global variables) by calling methods of a logger. Specializations of this abstract class for specific storage method are designed. |
---|
| 512 | */ |
---|
| 513 | class logger : public bdmroot { |
---|
| 514 | protected: |
---|
| 515 | //! RVs of all logged variables. |
---|
| 516 | Array<RV> entries; |
---|
| 517 | //! Names of logged quantities, e.g. names of algorithm variants |
---|
| 518 | Array<string> names; |
---|
| 519 | public: |
---|
| 520 | //!Default constructor |
---|
| 521 | logger ( ) : entries ( 0 ),names ( 0 ) {} |
---|
[267] | 522 | |
---|
[271] | 523 | //! returns an identifier which will be later needed for calling the \c logit() function |
---|
| 524 | //! For empty RV it returns -1, this entry will be ignored by \c logit(). |
---|
[347] | 525 | virtual int add ( const RV &rv, string prefix="" ) { |
---|
[271] | 526 | int id; |
---|
| 527 | if ( rv._dsize() >0 ) { |
---|
| 528 | id=entries.length(); |
---|
[347] | 529 | names=concat ( names, prefix); // diff |
---|
[271] | 530 | entries.set_length ( id+1,true ); |
---|
| 531 | entries ( id ) = rv; |
---|
| 532 | } |
---|
| 533 | else { id =-1;} |
---|
[270] | 534 | return id; // identifier of the last entry |
---|
| 535 | } |
---|
[267] | 536 | |
---|
[270] | 537 | //! log this vector |
---|
| 538 | virtual void logit ( int id, const vec &v ) =0; |
---|
[338] | 539 | //! log this double |
---|
| 540 | virtual void logit ( int id, const double &d ) =0; |
---|
[267] | 541 | |
---|
[270] | 542 | //! Shifts storage position for another time step. |
---|
| 543 | virtual void step() =0; |
---|
[267] | 544 | |
---|
[270] | 545 | //! Finalize storing information |
---|
| 546 | virtual void finalize() {}; |
---|
[267] | 547 | |
---|
[270] | 548 | //! Initialize the storage |
---|
| 549 | virtual void init() {}; |
---|
[267] | 550 | |
---|
[270] | 551 | }; |
---|
[267] | 552 | |
---|
[270] | 553 | /*! \brief Unconditional mpdf, allows using epdf in the role of mpdf. |
---|
[190] | 554 | |
---|
[270] | 555 | */ |
---|
| 556 | class mepdf : public mpdf { |
---|
| 557 | public: |
---|
| 558 | //!Default constructor |
---|
[301] | 559 | mepdf ( epdf* em ) :mpdf ( ) {ep= em ;}; |
---|
| 560 | mepdf (const epdf* em ) :mpdf ( ) {ep=const_cast<epdf*>( em );}; |
---|
[270] | 561 | void condition ( const vec &cond ) {} |
---|
| 562 | }; |
---|
[115] | 563 | |
---|
[256] | 564 | //!\brief Abstract composition of pdfs, will be used for specific classes |
---|
[192] | 565 | //!this abstract class is common to epdf and mpdf |
---|
[270] | 566 | class compositepdf { |
---|
| 567 | protected: |
---|
| 568 | //!Number of mpdfs in the composite |
---|
| 569 | int n; |
---|
| 570 | //! Elements of composition |
---|
| 571 | Array<mpdf*> mpdfs; |
---|
| 572 | public: |
---|
| 573 | compositepdf ( Array<mpdf*> A0 ) : n ( A0.length() ), mpdfs ( A0 ) {}; |
---|
| 574 | //! find common rv, flag \param checkoverlap modifies whether overlaps are acceptable |
---|
| 575 | RV getrv ( bool checkoverlap=false ); |
---|
| 576 | //! common rvc of all mpdfs is written to rvc |
---|
| 577 | void setrvc ( const RV &rv, RV &rvc ); |
---|
| 578 | }; |
---|
[175] | 579 | |
---|
[270] | 580 | /*! \brief Abstract class for discrete-time sources of data. |
---|
[12] | 581 | |
---|
[270] | 582 | The class abstracts operations of: (i) data aquisition, (ii) data-preprocessing, (iii) scaling of data, and (iv) data resampling from the task of estimation and control. |
---|
| 583 | Moreover, for controlled systems, it is able to receive the desired control action and perform it in the next step. (Or as soon as possible). |
---|
[12] | 584 | |
---|
[270] | 585 | */ |
---|
[32] | 586 | |
---|
[270] | 587 | class DS : public bdmroot { |
---|
| 588 | protected: |
---|
| 589 | int dtsize; |
---|
| 590 | int utsize; |
---|
| 591 | //!Description of data returned by \c getdata(). |
---|
| 592 | RV Drv; |
---|
| 593 | //!Description of data witten by by \c write(). |
---|
| 594 | RV Urv; // |
---|
| 595 | //! Remember its own index in Logger L |
---|
| 596 | int L_dt, L_ut; |
---|
| 597 | public: |
---|
| 598 | //! default constructors |
---|
[271] | 599 | DS() :Drv ( ),Urv ( ) {}; |
---|
[270] | 600 | //! Returns full vector of observed data=[output, input] |
---|
| 601 | virtual void getdata ( vec &dt ) {it_error ( "abstract class" );}; |
---|
| 602 | //! Returns data records at indeces. |
---|
| 603 | virtual void getdata ( vec &dt, const ivec &indeces ) {it_error ( "abstract class" );}; |
---|
| 604 | //! Accepts action variable and schedule it for application. |
---|
| 605 | virtual void write ( vec &ut ) {it_error ( "abstract class" );}; |
---|
| 606 | //! Accepts action variables at specific indeces |
---|
| 607 | virtual void write ( vec &ut, const ivec &indeces ) {it_error ( "abstract class" );}; |
---|
[32] | 608 | |
---|
[270] | 609 | //! Moves from \f$ t \f$ to \f$ t+1 \f$, i.e. perfroms the actions and reads response of the system. |
---|
| 610 | virtual void step() =0; |
---|
[32] | 611 | |
---|
[270] | 612 | //! Register DS for logging into logger L |
---|
| 613 | virtual void log_add ( logger &L ) { |
---|
| 614 | it_assert_debug ( dtsize==Drv._dsize(),"" ); |
---|
| 615 | it_assert_debug ( utsize==Urv._dsize(),"" ); |
---|
[32] | 616 | |
---|
[270] | 617 | L_dt=L.add ( Drv,"" ); |
---|
| 618 | L_ut=L.add ( Urv,"" ); |
---|
| 619 | } |
---|
| 620 | //! Register DS for logging into logger L |
---|
| 621 | virtual void logit ( logger &L ) { |
---|
| 622 | vec tmp ( Drv._dsize() +Urv._dsize() ); |
---|
| 623 | getdata ( tmp ); |
---|
| 624 | // d is first in getdata |
---|
| 625 | L.logit ( L_dt,tmp.left ( Drv._dsize() ) ); |
---|
| 626 | // u follows after d in getdata |
---|
| 627 | L.logit ( L_ut,tmp.mid ( Drv._dsize(), Urv._dsize() ) ); |
---|
| 628 | } |
---|
| 629 | //!access function |
---|
| 630 | virtual RV _drv() const {return concat ( Drv,Urv );} |
---|
| 631 | //!access function |
---|
| 632 | const RV& _urv() const {return Urv;} |
---|
[342] | 633 | //! set random rvariables |
---|
| 634 | virtual void set_drv (const RV &drv, const RV &urv) { Drv=drv;Urv=urv;} |
---|
[270] | 635 | }; |
---|
[18] | 636 | |
---|
[270] | 637 | /*! \brief Bayesian Model of a system, i.e. all uncertainty is modeled by probabilities. |
---|
[32] | 638 | |
---|
[283] | 639 | This object represents exact or approximate evaluation of the Bayes rule: |
---|
| 640 | \f[ |
---|
| 641 | 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})} |
---|
| 642 | \f] |
---|
| 643 | |
---|
| 644 | Access to the resulting posterior density is via function \c posterior(). |
---|
| 645 | |
---|
| 646 | As a "side-effect" it also evaluates log-likelihood of the data, which can be accessed via function _ll(). |
---|
| 647 | It can also evaluate predictors of future values of \f$y_t\f$, see functions epredictor() and predictor(). |
---|
| 648 | |
---|
| 649 | Alternatively, it can evaluate posterior density conditioned by a known constant, \f$ c_t \f$: |
---|
| 650 | \f[ |
---|
| 651 | 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}) |
---|
| 652 | \f] |
---|
| 653 | |
---|
| 654 | The value of \f$ c_t \f$ is set by function condition(). |
---|
| 655 | |
---|
[270] | 656 | */ |
---|
[32] | 657 | |
---|
[270] | 658 | class BM :public bdmroot { |
---|
| 659 | protected: |
---|
| 660 | //! Random variable of the data (optional) |
---|
| 661 | RV drv; |
---|
| 662 | //!Logarithm of marginalized data likelihood. |
---|
| 663 | double ll; |
---|
| 664 | //! 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. |
---|
| 665 | bool evalll; |
---|
| 666 | public: |
---|
| 667 | //! \name Constructors |
---|
| 668 | //! @{ |
---|
[271] | 669 | |
---|
[338] | 670 | BM () :ll ( 0 ),evalll ( true ), LIDs ( 4 ), LFlags(4) { |
---|
| 671 | LIDs=-1;/*empty IDs*/ LFlags=0; LFlags(0)=1;/*log only mean*/}; |
---|
[271] | 672 | BM ( const BM &B ) : drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {} |
---|
[270] | 673 | //! Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! |
---|
[283] | 674 | //! Prototype: \code BM* _copy_() const {return new BM(*this);} \endcode |
---|
| 675 | virtual BM* _copy_ () const {return NULL;}; |
---|
[270] | 676 | //!@} |
---|
[18] | 677 | |
---|
[270] | 678 | //! \name Mathematical operations |
---|
| 679 | //!@{ |
---|
[271] | 680 | |
---|
[270] | 681 | /*! \brief Incremental Bayes rule |
---|
| 682 | @param dt vector of input data |
---|
| 683 | */ |
---|
| 684 | virtual void bayes ( const vec &dt ) = 0; |
---|
| 685 | //! Batch Bayes rule (columns of Dt are observations) |
---|
| 686 | virtual void bayesB ( const mat &Dt ); |
---|
| 687 | //! Evaluates predictive log-likelihood of the given data record |
---|
| 688 | //! I.e. marginal likelihood of the data with the posterior integrated out. |
---|
| 689 | virtual double logpred ( const vec &dt ) const{it_error ( "Not implemented" );return 0.0;} |
---|
| 690 | //! Matrix version of logpred |
---|
| 691 | vec logpred_m ( const mat &dt ) const{vec tmp ( dt.cols() );for ( int i=0;i<dt.cols();i++ ) {tmp ( i ) =logpred ( dt.get_col ( i ) );}return tmp;} |
---|
[32] | 692 | |
---|
[270] | 693 | //!Constructs a predictive density \f$ f(d_{t+1} |d_{t}, \ldots d_{0}) \f$ |
---|
[271] | 694 | virtual epdf* epredictor ( ) const {it_error ( "Not implemented" );return NULL;}; |
---|
[270] | 695 | //!Constructs a conditional density 1-step ahead predictor \f$ f(d_{t+1} |d_{t+h-1}, \ldots d_{t}) |
---|
| 696 | virtual mpdf* predictor ( ) const {it_error ( "Not implemented" );return NULL;}; |
---|
| 697 | //!@} |
---|
[271] | 698 | |
---|
[283] | 699 | //! \name Extension to conditional BM |
---|
| 700 | //! This extension is useful e.g. in Marginalized Particle Filter (\ref bdm::MPF). |
---|
| 701 | //! Alternatively, it can be used for automated connection to DS when the condition is observed |
---|
| 702 | //!@{ |
---|
| 703 | |
---|
| 704 | //! Name of extension variable |
---|
| 705 | RV rvc; |
---|
| 706 | //! access function |
---|
| 707 | const RV& _rvc() const {return rvc;} |
---|
| 708 | |
---|
| 709 | //! Substitute \c val for \c rvc. |
---|
| 710 | virtual void condition ( const vec &val ) {it_error ( "Not implemented!" );}; |
---|
| 711 | |
---|
| 712 | //!@} |
---|
| 713 | |
---|
| 714 | |
---|
[270] | 715 | //! \name Access to attributes |
---|
| 716 | //!@{ |
---|
[271] | 717 | |
---|
[270] | 718 | const RV& _drv() const {return drv;} |
---|
| 719 | void set_drv ( const RV &rv ) {drv=rv;} |
---|
[271] | 720 | void set_rv ( const RV &rv ) {const_cast<epdf&> ( posterior() ).set_rv ( rv );} |
---|
[270] | 721 | double _ll() const {return ll;} |
---|
| 722 | void set_evalll ( bool evl0 ) {evalll=evl0;} |
---|
[271] | 723 | virtual const epdf& posterior() const =0; |
---|
[270] | 724 | virtual const epdf* _e() const =0; |
---|
| 725 | //!@} |
---|
[28] | 726 | |
---|
[283] | 727 | //! \name Logging of results |
---|
| 728 | //!@{ |
---|
[200] | 729 | |
---|
[338] | 730 | //! Set boolean options from a string recognized are: "logbounds,logll" |
---|
[325] | 731 | virtual void set_options ( const string &opt ) { |
---|
[338] | 732 | LFlags(0)=1; |
---|
| 733 | if ( opt.find ( "logbounds" ) !=string::npos ) {LFlags(1)=1; LFlags(2)=1;} |
---|
| 734 | if ( opt.find ( "logll" ) !=string::npos ) {LFlags(3)=1;} |
---|
[283] | 735 | } |
---|
[338] | 736 | //! IDs of storages in loggers 4:[1=mean,2=lb,3=ub,4=ll] |
---|
[283] | 737 | ivec LIDs; |
---|
[190] | 738 | |
---|
[338] | 739 | //! Flags for logging - same size as LIDs, each entry correspond to the same in LIDs |
---|
| 740 | ivec LFlags; |
---|
[283] | 741 | //! Add all logged variables to a logger |
---|
[317] | 742 | virtual void log_add ( logger &L, const string &name="" ) { |
---|
[283] | 743 | // internal |
---|
| 744 | RV r; |
---|
| 745 | if ( posterior().isnamed() ) {r=posterior()._rv();} |
---|
| 746 | else{r=RV ( "est", posterior().dimension() );}; |
---|
[190] | 747 | |
---|
[283] | 748 | // Add mean value |
---|
[347] | 749 | if (LFlags(0)) LIDs ( 0 ) =L.add ( r,name+"mean_" ); |
---|
| 750 | if (LFlags(1)) LIDs ( 1 ) =L.add ( r,name+"lb_" ); |
---|
| 751 | if (LFlags(2)) LIDs ( 2 ) =L.add ( r,name+"ub_" ); |
---|
| 752 | if (LFlags(3)) LIDs ( 3 ) =L.add ( RV("ll",1),name ); //TODO: "local" RV |
---|
[283] | 753 | } |
---|
[317] | 754 | virtual void logit ( logger &L ) { |
---|
| 755 | L.logit ( LIDs ( 0 ), posterior().mean() ); |
---|
[338] | 756 | if ( LFlags(1) || LFlags(2)) { //if one of them is off, its LID==-1 and will not be stored |
---|
[283] | 757 | vec ub,lb; |
---|
[286] | 758 | posterior().qbounds ( lb,ub ); |
---|
[338] | 759 | L.logit ( LIDs ( 1 ), lb ); |
---|
[317] | 760 | L.logit ( LIDs ( 2 ), ub ); |
---|
[283] | 761 | } |
---|
[338] | 762 | if (LFlags(3)) L.logit ( LIDs ( 3 ), ll ); |
---|
[283] | 763 | } |
---|
| 764 | //!@} |
---|
[270] | 765 | }; |
---|
[32] | 766 | |
---|
[339] | 767 | |
---|
[254] | 768 | }; //namespace |
---|
[2] | 769 | #endif // BM_H |
---|