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 | //! Structure of RV (used internally) |
---|
22 | class str{ |
---|
23 | public: |
---|
24 | ivec ids; |
---|
25 | ivec times; |
---|
26 | str(ivec ids0, ivec times0):ids(ids0),times(times0){ |
---|
27 | it_assert_debug(times0.length()==ids0.length(),"Incompatible input"); |
---|
28 | }; |
---|
29 | }; |
---|
30 | |
---|
31 | /*! |
---|
32 | * \brief Class representing variables, most often random variables |
---|
33 | |
---|
34 | * More?... |
---|
35 | */ |
---|
36 | |
---|
37 | class RV { |
---|
38 | protected: |
---|
39 | //! size = sum of sizes |
---|
40 | int tsize; |
---|
41 | //! len = number of individual rvs |
---|
42 | int len; |
---|
43 | //! Vector of unique IDs |
---|
44 | ivec ids; |
---|
45 | //! Vector of sizes |
---|
46 | ivec sizes; |
---|
47 | //! Vector of shifts from current time |
---|
48 | ivec times; |
---|
49 | //! Array of names |
---|
50 | Array<std::string> names; |
---|
51 | |
---|
52 | private: |
---|
53 | //! auxiliary function used in constructor |
---|
54 | void init (ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times ); |
---|
55 | public: |
---|
56 | //! Full constructor |
---|
57 | RV ( Array<std::string> in_names, ivec in_sizes, ivec in_times ); |
---|
58 | //! Constructor with times=0 |
---|
59 | RV ( Array<std::string> in_names, ivec in_sizes ); |
---|
60 | //! Constructor with sizes=1, times=0 |
---|
61 | RV ( Array<std::string> in_names ); |
---|
62 | //! Constructor of empty RV |
---|
63 | RV (); |
---|
64 | |
---|
65 | //! Printing output e.g. for debugging. |
---|
66 | friend std::ostream &operator<< ( std::ostream &os, const RV &rv ); |
---|
67 | |
---|
68 | //! Return number of scalars in the RV. |
---|
69 | int count() const {return tsize;} ; |
---|
70 | //! Return length (number of entries) of the RV. |
---|
71 | int length() const {return len;} ; |
---|
72 | |
---|
73 | //TODO why not inline and later?? |
---|
74 | |
---|
75 | //! Find indexes of self in another rv, \return ivec of the same size as self. |
---|
76 | ivec findself (const RV &rv2 ) const; |
---|
77 | //! Compare if \c rv2 is identical to this \c RV |
---|
78 | bool equal (const RV &rv2 ) const; |
---|
79 | //! Add (concat) another variable to the current one, \return true if all rv2 were added, false if rv2 is in conflict |
---|
80 | bool add ( const RV &rv2 ); |
---|
81 | //! Subtract another variable from the current one |
---|
82 | RV subt ( const RV rv2 ) const; |
---|
83 | //! Select only variables at indeces ind |
---|
84 | RV subselect ( ivec ind ) const; |
---|
85 | //! Select only variables at indeces ind |
---|
86 | RV operator() ( ivec ind ) const; |
---|
87 | //! Shift \c time shifted by delta. |
---|
88 | void t ( int delta ); |
---|
89 | //! generate \c str from rv, by expanding sizes |
---|
90 | str tostr() const; |
---|
91 | //! generate indeces into \param crv data vector that form data vector of self. |
---|
92 | ivec dataind(RV crv) const; |
---|
93 | |
---|
94 | //!access function |
---|
95 | Array<std::string>& _names() {return names;}; |
---|
96 | |
---|
97 | //!access function |
---|
98 | int id ( int at ) {return ids ( at );}; |
---|
99 | //!access function |
---|
100 | int size ( int at ) {return sizes ( at );}; |
---|
101 | //!access function |
---|
102 | int time ( int at ) {return times ( at );}; |
---|
103 | //!access function |
---|
104 | std::string name ( int at ) {return names ( at );}; |
---|
105 | }; |
---|
106 | |
---|
107 | //! Concat two random variables |
---|
108 | RV concat ( const RV &rv1, const RV &rv2 ); |
---|
109 | |
---|
110 | |
---|
111 | //! Class representing function \f$f(x)\f$ of variable \f$x\f$ represented by \c rv |
---|
112 | |
---|
113 | class fnc { |
---|
114 | protected: |
---|
115 | //! Length of the output vector |
---|
116 | int dimy; |
---|
117 | public: |
---|
118 | //!default constructor |
---|
119 | fnc ( int dy ) :dimy ( dy ) {}; |
---|
120 | //! function evaluates numerical value of \f$f(x)\f$ at \f$x=\f$ \c cond |
---|
121 | virtual vec eval ( const vec &cond ) { |
---|
122 | return vec ( 0 ); |
---|
123 | }; |
---|
124 | |
---|
125 | //! access function |
---|
126 | int _dimy() const{return dimy;} |
---|
127 | |
---|
128 | //! Destructor for future use; |
---|
129 | virtual ~fnc() {}; |
---|
130 | }; |
---|
131 | |
---|
132 | |
---|
133 | //! Probability density function with numerical statistics, e.g. posterior density. |
---|
134 | |
---|
135 | class epdf { |
---|
136 | protected: |
---|
137 | //! Identified of the random variable |
---|
138 | RV rv; |
---|
139 | public: |
---|
140 | //!default constructor |
---|
141 | epdf() :rv ( ) {}; |
---|
142 | |
---|
143 | //!default constructor |
---|
144 | epdf ( const RV &rv0 ) :rv ( rv0 ) {}; |
---|
145 | |
---|
146 | //! Returns the required moment of the epdf |
---|
147 | // virtual vec moment ( const int order = 1 ); |
---|
148 | //! Returns a sample, \f$x\f$ from density \f$epdf(rv)\f$ |
---|
149 | virtual vec sample () const =0; |
---|
150 | //! Returns N samples from density \f$epdf(rv)\f$ |
---|
151 | virtual mat sampleN ( int N ) const; |
---|
152 | //! Compute probability of argument \c val |
---|
153 | virtual double eval ( const vec &val ) const {return exp ( this->evalpdflog ( val ) );}; |
---|
154 | |
---|
155 | //! Compute log-probability of argument \c val |
---|
156 | virtual double evalpdflog ( const vec &val ) const =0; |
---|
157 | |
---|
158 | //! return expected value |
---|
159 | virtual vec mean() const =0; |
---|
160 | |
---|
161 | //! Destructor for future use; |
---|
162 | virtual ~epdf() {}; |
---|
163 | //! access function, possibly dangerous! |
---|
164 | RV& _rv() {return rv;} |
---|
165 | }; |
---|
166 | |
---|
167 | |
---|
168 | //! Conditional probability density, e.g. modeling some dependencies. |
---|
169 | //TODO Samplecond can be generalized |
---|
170 | |
---|
171 | class mpdf { |
---|
172 | protected: |
---|
173 | //! modeled random variable |
---|
174 | RV rv; |
---|
175 | //! random variable in condition |
---|
176 | RV rvc; |
---|
177 | //! pointer to internal epdf |
---|
178 | epdf* ep; |
---|
179 | public: |
---|
180 | |
---|
181 | //! Returns the required moment of the epdf |
---|
182 | // virtual fnc moment ( const int order = 1 ); |
---|
183 | //! 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 \param ll is a return value of log-likelihood of the sample. |
---|
184 | virtual vec samplecond (const vec &cond, double &ll ) {this->condition ( cond ); |
---|
185 | vec temp= ep->sample(); |
---|
186 | ll=ep->evalpdflog ( temp );return temp;}; |
---|
187 | //! 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 \param ll is a return value of log-likelihood of the sample. |
---|
188 | virtual mat samplecond (const vec &cond, vec &ll, int N ) { |
---|
189 | this->condition ( cond ); |
---|
190 | mat temp ( rv.count(),N ); vec smp ( rv.count() ); |
---|
191 | for ( int i=0;i<N;i++ ) {smp=ep->sample() ;temp.set_col ( i, smp );ll ( i ) =ep->evalpdflog ( smp );} |
---|
192 | return temp; |
---|
193 | }; |
---|
194 | //! Update \c ep so that it represents this mpdf conditioned on \c rvc = cond |
---|
195 | virtual void condition ( const vec &cond ) {}; |
---|
196 | |
---|
197 | //! Shortcut for conditioning and evaluation of the internal epdf. In some cases, this operation can be implemented efficiently. |
---|
198 | virtual double evalcond ( const vec &dt, const vec &cond ) {this->condition ( cond );return ep->eval ( dt );}; |
---|
199 | |
---|
200 | //! Destructor for future use; |
---|
201 | virtual ~mpdf() {}; |
---|
202 | |
---|
203 | //! Default constructor |
---|
204 | mpdf ( const RV &rv0, const RV &rvc0 ) :rv ( rv0 ),rvc ( rvc0 ) {}; |
---|
205 | //! access function |
---|
206 | RV _rvc() {return rvc;} |
---|
207 | //! access function |
---|
208 | RV _rv() {return rv;} |
---|
209 | //!access function |
---|
210 | epdf& _epdf() {return *ep;} |
---|
211 | }; |
---|
212 | |
---|
213 | /*! \brief Unconditional mpdf, allows using epdf in the role of mpdf. |
---|
214 | |
---|
215 | WARNING: the class does not check validity of the \c ep pointer nor its existence. |
---|
216 | */ |
---|
217 | class mepdf : public mpdf { |
---|
218 | public: |
---|
219 | //!Default constructor |
---|
220 | mepdf (epdf &em ) :mpdf ( em._rv(),RV() ) {ep=&em;}; |
---|
221 | }; |
---|
222 | |
---|
223 | /*! \brief Abstract class for discrete-time sources of data. |
---|
224 | |
---|
225 | 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. |
---|
226 | 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). |
---|
227 | |
---|
228 | */ |
---|
229 | |
---|
230 | class DS { |
---|
231 | protected: |
---|
232 | //!Observed variables, returned by \c getdata(). |
---|
233 | RV Drv; |
---|
234 | //!Action variables, accepted by \c write(). |
---|
235 | RV Urv; // |
---|
236 | public: |
---|
237 | //! Returns full vector of observed data |
---|
238 | void getdata ( vec &dt ); |
---|
239 | //! Returns data records at indeces. |
---|
240 | void getdata ( vec &dt, ivec &indeces ); |
---|
241 | //! Accepts action variable and schedule it for application. |
---|
242 | void write ( vec &ut ); |
---|
243 | //! Accepts action variables at specific indeces |
---|
244 | void write ( vec &ut, ivec &indeces ); |
---|
245 | /*! \brief Method that assigns random variables to the datasource. |
---|
246 | Typically, the datasource will be constructed without knowledge of random variables. This method will associate existing variables with RVs. |
---|
247 | |
---|
248 | (Inherited from m3k, may be deprecated soon). |
---|
249 | */ |
---|
250 | void linkrvs ( RV &drv, RV &urv ); |
---|
251 | |
---|
252 | //! Moves from \f$t\f$ to \f$t+1\f$, i.e. perfroms the actions and reads response of the system. |
---|
253 | void step(); |
---|
254 | |
---|
255 | }; |
---|
256 | |
---|
257 | /*! \brief Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities. |
---|
258 | |
---|
259 | */ |
---|
260 | |
---|
261 | class BM { |
---|
262 | protected: |
---|
263 | //!Random variable of the posterior |
---|
264 | RV rv; |
---|
265 | //!Logarithm of marginalized data likelihood. |
---|
266 | double ll; |
---|
267 | //! 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. |
---|
268 | bool evalll; |
---|
269 | public: |
---|
270 | |
---|
271 | //!Default constructor |
---|
272 | BM ( const RV &rv0 ) :rv ( rv0 ), ll ( 0 ),evalll ( true ) {//Fixme: test rv |
---|
273 | }; |
---|
274 | |
---|
275 | /*! \brief Incremental Bayes rule |
---|
276 | @param dt vector of input data |
---|
277 | */ |
---|
278 | virtual void bayes ( const vec &dt ) = 0; |
---|
279 | //! Batch Bayes rule (columns of Dt are observations) |
---|
280 | void bayes ( mat Dt ); |
---|
281 | //! Returns a pointer to the epdf representing posterior density on parameters. Use with care! |
---|
282 | virtual epdf& _epdf() =0; |
---|
283 | |
---|
284 | //! Destructor for future use; |
---|
285 | virtual ~BM() {}; |
---|
286 | //!access function |
---|
287 | const RV& _rv() const {return rv;} |
---|
288 | //!access function |
---|
289 | double _ll() const {return ll;} |
---|
290 | }; |
---|
291 | |
---|
292 | /*! |
---|
293 | \brief Conditional Bayesian Filter |
---|
294 | |
---|
295 | Evaluates conditional filtering density \f$f(rv|rvc,data)\f$ for a given \c rvc which is specified in each step by calling function \c condition. |
---|
296 | |
---|
297 | This is an interface class used to assure that certain BM has operation \c condition . |
---|
298 | |
---|
299 | */ |
---|
300 | |
---|
301 | class BMcond { |
---|
302 | protected: |
---|
303 | //! Identificator of the conditioning variable |
---|
304 | RV rvc; |
---|
305 | public: |
---|
306 | //! Substitute \c val for \c rvc. |
---|
307 | virtual void condition ( const vec &val ) =0; |
---|
308 | //! Default constructor |
---|
309 | BMcond ( RV &rv0 ) :rvc ( rv0 ) {}; |
---|
310 | //! Destructor for future use |
---|
311 | virtual ~BMcond() {}; |
---|
312 | //! access function |
---|
313 | const RV& _rvc() const {return rvc;} |
---|
314 | }; |
---|
315 | |
---|
316 | #endif // BM_H |
---|