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