[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 | */ |
---|
[32] | 26 | |
---|
[2] | 27 | class RV { |
---|
[32] | 28 | protected: |
---|
[19] | 29 | //! size = sum of sizes |
---|
| 30 | int size; |
---|
| 31 | //! len = number of individual rvs |
---|
| 32 | int len; |
---|
[5] | 33 | ivec ids; |
---|
| 34 | ivec sizes; |
---|
| 35 | ivec times; |
---|
| 36 | ivec obs; |
---|
| 37 | Array<std::string> names; |
---|
[2] | 38 | |
---|
[5] | 39 | private: |
---|
[32] | 40 | void init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times); |
---|
[2] | 41 | public: |
---|
[5] | 42 | //! Full constructor which is called by the others |
---|
[32] | 43 | RV ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times); |
---|
[5] | 44 | //! default constructor |
---|
| 45 | RV ( ivec ids ); |
---|
[8] | 46 | //! Empty constructor will be set later |
---|
| 47 | RV (); |
---|
[32] | 48 | |
---|
[5] | 49 | //! Printing output e.g. for debugging. |
---|
| 50 | friend std::ostream &operator<< ( std::ostream &os, const RV &rv ); |
---|
| 51 | |
---|
[8] | 52 | //! Return length (number of scalars) of the RV. |
---|
[19] | 53 | int count() const {return size;} ; |
---|
[32] | 54 | |
---|
[12] | 55 | //TODO why not inline and later?? |
---|
[32] | 56 | |
---|
[5] | 57 | //! Find indexes of another rv in self |
---|
[32] | 58 | ivec find ( RV rv2 ); |
---|
[5] | 59 | //! Add (concat) another variable to the current one |
---|
[32] | 60 | void add (const RV &rv2 ); |
---|
| 61 | //! Add (concat) another variable to the current one |
---|
| 62 | friend RV concat (const RV &rv1, const RV &rv2 ); |
---|
[5] | 63 | //! Subtract another variable from the current one |
---|
[32] | 64 | RV subt ( RV rv2 ); |
---|
[5] | 65 | //! Select only variables at indeces ind |
---|
[32] | 66 | RV subselect ( ivec ind ); |
---|
[5] | 67 | //! Select only variables at indeces ind |
---|
[32] | 68 | RV operator() ( ivec ind ); |
---|
[8] | 69 | //! Generate new \c RV with \c time shifted by delta. |
---|
[32] | 70 | void t ( int delta ); |
---|
| 71 | //! generate a list of indeces, i.e. which |
---|
[19] | 72 | ivec indexlist(); |
---|
[2] | 73 | }; |
---|
| 74 | |
---|
| 75 | |
---|
[32] | 76 | //! Class representing function $f(x)$ of variable $x$ represented by \c rv |
---|
[2] | 77 | |
---|
| 78 | class fnc { |
---|
[22] | 79 | protected: |
---|
| 80 | int dimy; |
---|
[32] | 81 | public: |
---|
[19] | 82 | //! function evaluates numerical value of $f(x)$ at $x=cond$ |
---|
[32] | 83 | virtual vec eval ( const vec &cond ) { |
---|
| 84 | return vec ( 0 ); |
---|
[27] | 85 | }; //Fixme: virtual? |
---|
| 86 | |
---|
[22] | 87 | //! access function |
---|
[32] | 88 | int _dimy() const{return dimy;} |
---|
[28] | 89 | |
---|
[32] | 90 | //! Destructor for future use; |
---|
| 91 | virtual ~fnc() {}; |
---|
[2] | 92 | }; |
---|
| 93 | |
---|
[7] | 94 | |
---|
[4] | 95 | //! Probability density function with numerical statistics, e.g. posterior density. |
---|
[32] | 96 | |
---|
[2] | 97 | class epdf { |
---|
[32] | 98 | protected: |
---|
[5] | 99 | RV rv; |
---|
[2] | 100 | public: |
---|
[28] | 101 | //!default constructor |
---|
[32] | 102 | epdf() :rv ( ivec ( 0 ) ) {}; |
---|
| 103 | |
---|
| 104 | //!default constructor |
---|
| 105 | epdf ( const RV &rv0 ) :rv ( rv0 ) {}; |
---|
| 106 | |
---|
[5] | 107 | //! Returns the required moment of the epdf |
---|
[8] | 108 | // virtual vec moment ( const int order = 1 ); |
---|
[22] | 109 | //! Returns a sample from the density, \f$x \sim epdf(rv)\f$ |
---|
[32] | 110 | virtual vec sample () const =0; |
---|
[22] | 111 | //! Compute probability of argument \c val |
---|
[32] | 112 | virtual double eval ( const vec &val ) const {return exp(this->evalpdflog(val));}; |
---|
| 113 | |
---|
[28] | 114 | //! Compute log-probability of argument \c val |
---|
[32] | 115 | virtual double evalpdflog ( const vec &val ) const =0; |
---|
[28] | 116 | |
---|
[32] | 117 | //! return expected value |
---|
| 118 | virtual vec mean() const =0; |
---|
| 119 | |
---|
| 120 | //! Destructor for future use; |
---|
| 121 | virtual ~epdf() {}; |
---|
| 122 | //! access function |
---|
| 123 | RV _rv() const {return rv;} |
---|
[2] | 124 | }; |
---|
| 125 | |
---|
[32] | 126 | |
---|
[5] | 127 | //! Conditional probability density, e.g. modeling some dependencies. |
---|
[32] | 128 | //TODO Samplecond can be generalized |
---|
| 129 | |
---|
[2] | 130 | class mpdf { |
---|
[32] | 131 | protected: |
---|
[5] | 132 | //! modeled random variable |
---|
| 133 | RV rv; |
---|
| 134 | //! random variable in condition |
---|
| 135 | RV rvc; |
---|
[32] | 136 | //! pointer to internal epdf |
---|
| 137 | epdf* ep; |
---|
[2] | 138 | public: |
---|
| 139 | |
---|
[5] | 140 | //! Returns the required moment of the epdf |
---|
[8] | 141 | // virtual fnc moment ( const int order = 1 ); |
---|
[32] | 142 | //! Returns a sample from the density conditioned on \c cond, $x \sim epdf(rv|cond)$. \param ll is a return value of log-likelihood of the sample. |
---|
| 143 | virtual vec samplecond ( vec &cond, double &ll ) {this->condition(cond);vec temp= ep->sample();ll=ep->evalpdflog(temp);return temp;}; |
---|
[28] | 144 | |
---|
[32] | 145 | virtual void condition ( const vec &cond ) {}; |
---|
| 146 | |
---|
| 147 | virtual double evalcond (const vec &dt, const vec &cond ) {this->condition(cond);return ep->eval(dt);}; |
---|
| 148 | |
---|
| 149 | //! Destructor for future use; |
---|
| 150 | virtual ~mpdf() {}; |
---|
| 151 | |
---|
| 152 | //! Default constructor |
---|
| 153 | mpdf ( const RV &rv0, const RV &rvc0 ) :rv ( rv0 ),rvc ( rvc0 ) {}; |
---|
| 154 | //! access function |
---|
| 155 | RV _rvc(){return rvc;} |
---|
| 156 | //!access function |
---|
| 157 | epdf& _epdf(){return *ep;} |
---|
[2] | 158 | }; |
---|
| 159 | |
---|
[18] | 160 | /*! \brief Abstract class for discrete-time sources of data. |
---|
[12] | 161 | |
---|
[18] | 162 | 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. |
---|
| 163 | 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] | 164 | |
---|
[18] | 165 | */ |
---|
[32] | 166 | |
---|
[18] | 167 | class DS { |
---|
[19] | 168 | protected: |
---|
[18] | 169 | //!Observed variables, returned by \c getdata(). |
---|
[32] | 170 | RV Drv; |
---|
[18] | 171 | //!Action variables, accepted by \c write(). |
---|
| 172 | RV Urv; // |
---|
| 173 | public: |
---|
[32] | 174 | //! Returns full vector of observed data |
---|
| 175 | void getdata ( vec &dt ); |
---|
[18] | 176 | //! Returns data records at indeces. |
---|
[32] | 177 | void getdata ( vec &dt, ivec &indeces ); |
---|
[18] | 178 | //! Accepts action variable and schedule it for application. |
---|
[32] | 179 | void write ( vec &ut ); |
---|
| 180 | //! Accepts action variables at specific indeces |
---|
| 181 | void write ( vec &ut, ivec &indeces ); |
---|
| 182 | /*! \brief Method that assigns random variables to the datasource. |
---|
[18] | 183 | Typically, the datasource will be constructed without knowledge of random variables. This method will associate existing variables with RVs. |
---|
[32] | 184 | |
---|
[18] | 185 | (Inherited from m3k, may be deprecated soon). |
---|
| 186 | */ |
---|
[32] | 187 | void linkrvs ( RV &drv, RV &urv ); |
---|
| 188 | |
---|
| 189 | //! Moves from $t$ to $t+1$, i.e. perfroms the actions and reads response of the system. |
---|
[18] | 190 | void step(); |
---|
[32] | 191 | |
---|
[18] | 192 | }; |
---|
| 193 | |
---|
[28] | 194 | /*! \brief Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities. |
---|
[32] | 195 | |
---|
[28] | 196 | */ |
---|
[32] | 197 | |
---|
[28] | 198 | class BM { |
---|
[32] | 199 | protected: |
---|
| 200 | //!Random variable of the posterior |
---|
| 201 | RV rv; |
---|
[28] | 202 | //!Logarithm of marginalized data likelihood. |
---|
[32] | 203 | double ll; |
---|
[28] | 204 | //! 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 time. |
---|
| 205 | bool evalll; |
---|
[32] | 206 | public: |
---|
[18] | 207 | |
---|
[28] | 208 | //!Default constructor |
---|
[32] | 209 | BM(const RV &rv0) :rv(rv0), ll ( 0 ),evalll ( true ) {//Fixme: test rv |
---|
| 210 | }; |
---|
| 211 | |
---|
[28] | 212 | /*! \brief Incremental Bayes rule |
---|
| 213 | @param dt vector of input data |
---|
| 214 | */ |
---|
[32] | 215 | virtual void bayes ( const vec &dt ) = 0; |
---|
[28] | 216 | //! Batch Bayes rule (columns of Dt are observations) |
---|
| 217 | void bayes ( mat Dt ); |
---|
| 218 | //! Returns a pointer to the epdf representing posterior density on parameters. Use with care! |
---|
[32] | 219 | virtual epdf& _epdf()=0; |
---|
[28] | 220 | |
---|
[32] | 221 | //! Destructor for future use; |
---|
| 222 | virtual ~BM() {}; |
---|
| 223 | //!access function |
---|
| 224 | const RV& _rv() const {return rv;} |
---|
| 225 | //!access function |
---|
| 226 | double _ll() const {return ll;} |
---|
[28] | 227 | }; |
---|
| 228 | |
---|
[32] | 229 | /*! |
---|
| 230 | \brief Conditional Bayesian Filter |
---|
| 231 | |
---|
| 232 | Evaluates conditional filtering density $f(rv|rvc,data)$ for a given \c rvc which is specified in each step by calling function \c condition. |
---|
| 233 | |
---|
| 234 | This is an interface class used to assure that certain BM has operation \c condition . |
---|
| 235 | |
---|
| 236 | */ |
---|
| 237 | |
---|
| 238 | class BMcond { |
---|
| 239 | protected: |
---|
| 240 | RV rvc; |
---|
| 241 | public: |
---|
| 242 | //! Substitute \c val for \c rvc. |
---|
| 243 | virtual void condition ( const vec &val ) =0; |
---|
| 244 | BMcond(RV &rv0):rvc(rv0){}; |
---|
| 245 | virtual ~BMcond(){}; |
---|
| 246 | const RV& _rvc() const {return rvc;} |
---|
| 247 | }; |
---|
| 248 | |
---|
[2] | 249 | #endif // BM_H |
---|