[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 | |
---|
| 16 | #include <itpp/itbase.h> |
---|
| 17 | //#include <std> |
---|
| 18 | |
---|
| 19 | using namespace itpp; |
---|
| 20 | |
---|
[5] | 21 | /*! |
---|
| 22 | * \brief Class representing variables, most often random variables |
---|
| 23 | |
---|
| 24 | * More?... |
---|
| 25 | */ |
---|
[2] | 26 | class RV { |
---|
[12] | 27 | int size,len; |
---|
[5] | 28 | ivec ids; |
---|
| 29 | ivec sizes; |
---|
| 30 | ivec times; |
---|
| 31 | ivec obs; |
---|
| 32 | Array<std::string> names; |
---|
[2] | 33 | |
---|
[5] | 34 | private: |
---|
| 35 | void init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times, ivec in_obs ); |
---|
[2] | 36 | public: |
---|
[5] | 37 | //! Full constructor which is called by the others |
---|
| 38 | RV ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times, ivec in_obs ); |
---|
| 39 | //! default constructor |
---|
| 40 | RV ( ivec ids ); |
---|
[8] | 41 | //! Empty constructor will be set later |
---|
| 42 | RV (); |
---|
| 43 | |
---|
[5] | 44 | //! Printing output e.g. for debugging. |
---|
| 45 | friend std::ostream &operator<< ( std::ostream &os, const RV &rv ); |
---|
| 46 | |
---|
[8] | 47 | //! Return length (number of scalars) of the RV. |
---|
[12] | 48 | int count() {return size;} |
---|
| 49 | //TODO why not inline and later?? |
---|
| 50 | |
---|
[5] | 51 | //! Find indexes of another rv in self |
---|
[12] | 52 | ivec find(RV rv2); |
---|
[5] | 53 | //! Add (concat) another variable to the current one |
---|
[12] | 54 | RV add(RV rv2); |
---|
[5] | 55 | //! Subtract another variable from the current one |
---|
[12] | 56 | RV subt(RV rv2); |
---|
[5] | 57 | //! Select only variables at indeces ind |
---|
[12] | 58 | RV subselect(ivec ind); |
---|
[5] | 59 | //! Select only variables at indeces ind |
---|
| 60 | RV operator()(ivec ind); |
---|
[8] | 61 | //! Generate new \c RV with \c time shifted by delta. |
---|
| 62 | void t(int delta); |
---|
[2] | 63 | }; |
---|
| 64 | |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | |
---|
| 68 | //! Class representing function of variables |
---|
| 69 | class fnc { |
---|
[5] | 70 | RV rv; |
---|
[2] | 71 | }; |
---|
| 72 | |
---|
[4] | 73 | //! Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities. |
---|
[2] | 74 | class BM { |
---|
| 75 | public: |
---|
[7] | 76 | //!Logarithm of marginalized data likelihood. |
---|
| 77 | double ll; |
---|
| 78 | |
---|
| 79 | /*! \brief Incremental Bayes rule |
---|
| 80 | @param dt vector of input data |
---|
| 81 | @param evall If true, the filter will compute likelihood of the data record and store it in \c ll |
---|
| 82 | */ |
---|
| 83 | virtual void bayes ( const vec &dt, bool evall=true ) = 0; |
---|
[5] | 84 | //! Batch Bayes rule (columns of Dt are observations) |
---|
[7] | 85 | void bayes ( mat Dt ); |
---|
[2] | 86 | }; |
---|
| 87 | |
---|
[4] | 88 | //! Probability density function with numerical statistics, e.g. posterior density. |
---|
[2] | 89 | class epdf { |
---|
[5] | 90 | RV rv; |
---|
[2] | 91 | public: |
---|
[5] | 92 | //! Returns the required moment of the epdf |
---|
[8] | 93 | // virtual vec moment ( const int order = 1 ); |
---|
| 94 | //! Returns a sample from the density, $x \sim epdf(rv)$ |
---|
| 95 | virtual vec sample (){}; |
---|
| 96 | virtual double eval(const vec &val){}; |
---|
[2] | 97 | }; |
---|
| 98 | |
---|
[5] | 99 | //! Conditional probability density, e.g. modeling some dependencies. |
---|
[2] | 100 | class mpdf { |
---|
[5] | 101 | //! modeled random variable |
---|
| 102 | RV rv; |
---|
| 103 | //! random variable in condition |
---|
| 104 | RV rvc; |
---|
[2] | 105 | public: |
---|
| 106 | |
---|
[5] | 107 | //! Returns the required moment of the epdf |
---|
[8] | 108 | // virtual fnc moment ( const int order = 1 ); |
---|
| 109 | //! Returns a sample from the density conditioned on \c cond, $x \sim epdf(rv|cond)$ |
---|
| 110 | virtual vec samplecond (vec &cond, double lik){}; |
---|
| 111 | virtual void condition (vec &cond){}; |
---|
[2] | 112 | }; |
---|
| 113 | |
---|
[18] | 114 | /*! \brief Abstract class for discrete-time sources of data. |
---|
[12] | 115 | |
---|
[18] | 116 | 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. |
---|
| 117 | 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] | 118 | |
---|
[18] | 119 | */ |
---|
| 120 | class DS { |
---|
| 121 | //!Observed variables, returned by \c getdata(). |
---|
| 122 | RV Drv; |
---|
| 123 | //!Action variables, accepted by \c write(). |
---|
| 124 | RV Urv; // |
---|
| 125 | public: |
---|
| 126 | //! Returns full vector of observed data |
---|
| 127 | void getdata(vec &dt); |
---|
| 128 | //! Returns data records at indeces. |
---|
| 129 | void getdata(vec &dt, ivec &indeces); |
---|
| 130 | //! Accepts action variable and schedule it for application. |
---|
| 131 | void write(vec &ut); |
---|
| 132 | //! Accepts action variables at specific indeces |
---|
| 133 | void write(vec &ut, ivec &indeces); |
---|
| 134 | /*! \brief Method that assigns random variables to the datasource. |
---|
| 135 | Typically, the datasource will be constructed without knowledge of random variables. This method will associate existing variables with RVs. |
---|
| 136 | |
---|
| 137 | (Inherited from m3k, may be deprecated soon). |
---|
| 138 | */ |
---|
| 139 | void linkrvs(RV &drv, RV &urv); |
---|
| 140 | |
---|
| 141 | //! Moves from $t$ to $t+1$, i.e. perfroms the actions and reads response of the system. |
---|
| 142 | void step(); |
---|
| 143 | }; |
---|
| 144 | |
---|
| 145 | |
---|
[2] | 146 | #endif // BM_H |
---|