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