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