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 ( const ivec &ind ) const; |
---|
85 | //! Select only variables at indeces ind |
---|
86 | RV operator() ( const 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(const 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 | //!access function |
---|
107 | void set_id ( int at, int id0 ) {ids ( at )=id0;}; |
---|
108 | //!access function |
---|
109 | void set_size ( int at, int size0 ) {sizes ( at )=size0; tsize=sum(sizes);}; |
---|
110 | //!access function |
---|
111 | void set_time ( int at, int time0 ) {times ( at )=time0;}; |
---|
112 | |
---|
113 | //!Assign unused ids to this rv |
---|
114 | void newids(); |
---|
115 | }; |
---|
116 | |
---|
117 | //! Concat two random variables |
---|
118 | RV concat ( const RV &rv1, const RV &rv2 ); |
---|
119 | |
---|
120 | |
---|
121 | //! Class representing function \f$f(x)\f$ of variable \f$x\f$ represented by \c rv |
---|
122 | |
---|
123 | class fnc { |
---|
124 | protected: |
---|
125 | //! Length of the output vector |
---|
126 | int dimy; |
---|
127 | public: |
---|
128 | //!default constructor |
---|
129 | fnc ( int dy ) :dimy ( dy ) {}; |
---|
130 | //! function evaluates numerical value of \f$f(x)\f$ at \f$x=\f$ \c cond |
---|
131 | virtual vec eval ( const vec &cond ) { |
---|
132 | return vec ( 0 ); |
---|
133 | }; |
---|
134 | |
---|
135 | //! access function |
---|
136 | int _dimy() const{return dimy;} |
---|
137 | |
---|
138 | //! Destructor for future use; |
---|
139 | virtual ~fnc() {}; |
---|
140 | }; |
---|
141 | |
---|
142 | |
---|
143 | //! Probability density function with numerical statistics, e.g. posterior density. |
---|
144 | |
---|
145 | class epdf { |
---|
146 | protected: |
---|
147 | //! Identified of the random variable |
---|
148 | RV rv; |
---|
149 | public: |
---|
150 | //!default constructor |
---|
151 | epdf() :rv ( ) {}; |
---|
152 | |
---|
153 | //!default constructor |
---|
154 | epdf ( const RV &rv0 ) :rv ( rv0 ) {}; |
---|
155 | |
---|
156 | // //! Returns the required moment of the epdf |
---|
157 | // virtual vec moment ( const int order = 1 ); |
---|
158 | |
---|
159 | //! Returns a sample, \f$x\f$ from density \f$epdf(rv)\f$ |
---|
160 | virtual vec sample () const =0; |
---|
161 | //! Returns N samples from density \f$epdf(rv)\f$ |
---|
162 | virtual mat sampleN ( int N ) const; |
---|
163 | //! Compute probability of argument \c val |
---|
164 | virtual double eval ( const vec &val ) const {return exp ( this->evalpdflog ( val ) );}; |
---|
165 | |
---|
166 | //! Compute log-probability of argument \c val |
---|
167 | virtual double evalpdflog ( const vec &val ) const =0; |
---|
168 | |
---|
169 | //! Compute log-probability of multiple values argument \c val |
---|
170 | virtual vec evalpdflog_m ( const mat &Val ) const { |
---|
171 | vec x ( Val.cols() ); |
---|
172 | for ( int i=0;i<Val.cols();i++ ) {x ( i ) =evalpdflog( Val.get_col(i) ) ;} |
---|
173 | return x; |
---|
174 | } |
---|
175 | |
---|
176 | //! return expected value |
---|
177 | virtual vec mean() const =0; |
---|
178 | |
---|
179 | //! Destructor for future use; |
---|
180 | virtual ~epdf() {}; |
---|
181 | //! access function, possibly dangerous! |
---|
182 | const RV& _rv() const {return rv;} |
---|
183 | //! modifier function - useful when copying epdfs |
---|
184 | void _renewrv(const RV &in_rv){rv=in_rv;} |
---|
185 | }; |
---|
186 | |
---|
187 | |
---|
188 | //! Conditional probability density, e.g. modeling some dependencies. |
---|
189 | //TODO Samplecond can be generalized |
---|
190 | |
---|
191 | class mpdf { |
---|
192 | protected: |
---|
193 | //! modeled random variable |
---|
194 | RV rv; |
---|
195 | //! random variable in condition |
---|
196 | RV rvc; |
---|
197 | //! pointer to internal epdf |
---|
198 | epdf* ep; |
---|
199 | public: |
---|
200 | |
---|
201 | //! Returns the required moment of the epdf |
---|
202 | // virtual fnc moment ( const int order = 1 ); |
---|
203 | //! 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. |
---|
204 | virtual vec samplecond (const vec &cond, double &ll ) {this->condition ( cond ); |
---|
205 | vec temp= ep->sample(); |
---|
206 | ll=ep->evalpdflog ( temp );return temp;}; |
---|
207 | //! 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. |
---|
208 | virtual mat samplecond (const vec &cond, vec &ll, int N ) { |
---|
209 | this->condition ( cond ); |
---|
210 | mat temp ( rv.count(),N ); vec smp ( rv.count() ); |
---|
211 | for ( int i=0;i<N;i++ ) {smp=ep->sample() ;temp.set_col ( i, smp );ll ( i ) =ep->evalpdflog ( smp );} |
---|
212 | return temp; |
---|
213 | }; |
---|
214 | //! Update \c ep so that it represents this mpdf conditioned on \c rvc = cond |
---|
215 | virtual void condition ( const vec &cond ) {}; |
---|
216 | |
---|
217 | //! Shortcut for conditioning and evaluation of the internal epdf. In some cases, this operation can be implemented efficiently. |
---|
218 | virtual double evalcond ( const vec &dt, const vec &cond ) {this->condition ( cond );return ep->eval ( dt );}; |
---|
219 | |
---|
220 | //! Destructor for future use; |
---|
221 | virtual ~mpdf() {}; |
---|
222 | |
---|
223 | //! Default constructor |
---|
224 | mpdf ( const RV &rv0, const RV &rvc0 ) :rv ( rv0 ),rvc ( rvc0 ) {}; |
---|
225 | //! access function |
---|
226 | RV _rvc() {return rvc;} |
---|
227 | //! access function |
---|
228 | RV _rv() {return rv;} |
---|
229 | //!access function |
---|
230 | epdf& _epdf() {return *ep;} |
---|
231 | }; |
---|
232 | |
---|
233 | /*! \brief Unconditional mpdf, allows using epdf in the role of mpdf. |
---|
234 | |
---|
235 | WARNING: the class does not check validity of the \c ep pointer nor its existence. |
---|
236 | */ |
---|
237 | class mepdf : public mpdf { |
---|
238 | public: |
---|
239 | //!Default constructor |
---|
240 | mepdf (epdf &em ) :mpdf ( em._rv(),RV() ) {ep=&em;}; |
---|
241 | }; |
---|
242 | |
---|
243 | //!\brief Abstract composition of pdfs, a base for specific classes |
---|
244 | class compositepdf{ |
---|
245 | protected: |
---|
246 | //!Number of mpdfs in the composite |
---|
247 | int n; |
---|
248 | //! Elements of composition |
---|
249 | Array<mpdf*> mpdfs; |
---|
250 | //! Indeces of rvs in common rv |
---|
251 | Array<ivec> rvsinrv; |
---|
252 | //! Indeces of rvc in common rv |
---|
253 | Array<ivec> rvcsinrv; |
---|
254 | public: |
---|
255 | compositepdf(Array<mpdf*> A0): n(A0.length()), mpdfs(A0), rvsinrv(n), rvcsinrv(n){}; |
---|
256 | RV getrv(bool checkoverlap=false); |
---|
257 | void setrvc(const RV &rv, RV &rvc); |
---|
258 | void setindices(const RV &rv); |
---|
259 | void setrvcinrv(const RV &rvc, Array<ivec> &rvcind); |
---|
260 | }; |
---|
261 | |
---|
262 | /*! \brief Abstract class for discrete-time sources of data. |
---|
263 | |
---|
264 | 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. |
---|
265 | 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). |
---|
266 | |
---|
267 | */ |
---|
268 | |
---|
269 | class DS { |
---|
270 | protected: |
---|
271 | //!Observed variables, returned by \c getdata(). |
---|
272 | RV Drv; |
---|
273 | //!Action variables, accepted by \c write(). |
---|
274 | RV Urv; // |
---|
275 | public: |
---|
276 | //! Returns full vector of observed data |
---|
277 | void getdata ( vec &dt ); |
---|
278 | //! Returns data records at indeces. |
---|
279 | void getdata ( vec &dt, ivec &indeces ); |
---|
280 | //! Accepts action variable and schedule it for application. |
---|
281 | void write ( vec &ut ); |
---|
282 | //! Accepts action variables at specific indeces |
---|
283 | void write ( vec &ut, ivec &indeces ); |
---|
284 | /*! \brief Method that assigns random variables to the datasource. |
---|
285 | Typically, the datasource will be constructed without knowledge of random variables. This method will associate existing variables with RVs. |
---|
286 | |
---|
287 | (Inherited from m3k, may be deprecated soon). |
---|
288 | */ |
---|
289 | void linkrvs ( RV &drv, RV &urv ); |
---|
290 | |
---|
291 | //! Moves from \f$t\f$ to \f$t+1\f$, i.e. perfroms the actions and reads response of the system. |
---|
292 | void step(); |
---|
293 | |
---|
294 | }; |
---|
295 | |
---|
296 | /*! \brief Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities. |
---|
297 | |
---|
298 | */ |
---|
299 | |
---|
300 | class BM { |
---|
301 | protected: |
---|
302 | //!Random variable of the posterior |
---|
303 | RV rv; |
---|
304 | //!Logarithm of marginalized data likelihood. |
---|
305 | double ll; |
---|
306 | //! 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 computational time. |
---|
307 | bool evalll; |
---|
308 | public: |
---|
309 | |
---|
310 | //!Default constructor |
---|
311 | BM ( const RV &rv0, double ll0=0,bool evalll0=true ) :rv ( rv0 ), ll ( ll0 ),evalll ( evalll0) {//Fixme: test rv |
---|
312 | }; |
---|
313 | //!Copy constructor |
---|
314 | BM (const BM &B) : rv(B.rv), ll(B.ll), evalll(B.evalll) {} |
---|
315 | |
---|
316 | /*! \brief Incremental Bayes rule |
---|
317 | @param dt vector of input data |
---|
318 | */ |
---|
319 | virtual void bayes ( const vec &dt ) = 0; |
---|
320 | //! Batch Bayes rule (columns of Dt are observations) |
---|
321 | virtual void bayesB (const mat &Dt ); |
---|
322 | //! Returns a pointer to the epdf representing posterior density on parameters. Use with care! |
---|
323 | virtual const epdf& _epdf() const =0; |
---|
324 | |
---|
325 | //! Evaluates predictive log-likelihood of the given data record |
---|
326 | //! I.e. marginal likelihood of the data with the posterior integrated out. |
---|
327 | virtual double logpred(const vec &dt)const{it_error("Not implemented");return 0.0;} |
---|
328 | //! Matrix version of logpred |
---|
329 | vec logpred_m(const mat &dt)const{vec tmp(dt.cols());for(int i=0;i<dt.cols();i++){tmp(i)=logpred(dt.get_col(i));}return tmp;} |
---|
330 | |
---|
331 | //! Destructor for future use; |
---|
332 | virtual ~BM() {}; |
---|
333 | //!access function |
---|
334 | const RV& _rv() const {return rv;} |
---|
335 | //!access function |
---|
336 | double _ll() const {return ll;} |
---|
337 | //!access function |
---|
338 | void set_evalll(bool evl0){evalll=evl0;} |
---|
339 | |
---|
340 | //! Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! |
---|
341 | //! Prototype: BM* _copy_(){BM Tmp*=new Tmp(this*); return Tmp; } |
---|
342 | virtual BM* _copy_(bool changerv=false){it_error("function _copy_ not implemented for this BM"); return NULL;}; |
---|
343 | }; |
---|
344 | |
---|
345 | /*! |
---|
346 | \brief Conditional Bayesian Filter |
---|
347 | |
---|
348 | 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. |
---|
349 | |
---|
350 | This is an interface class used to assure that certain BM has operation \c condition . |
---|
351 | |
---|
352 | */ |
---|
353 | |
---|
354 | class BMcond { |
---|
355 | protected: |
---|
356 | //! Identificator of the conditioning variable |
---|
357 | RV rvc; |
---|
358 | public: |
---|
359 | //! Substitute \c val for \c rvc. |
---|
360 | virtual void condition ( const vec &val ) =0; |
---|
361 | //! Default constructor |
---|
362 | BMcond ( RV &rv0 ) :rvc ( rv0 ) {}; |
---|
363 | //! Destructor for future use |
---|
364 | virtual ~BMcond() {}; |
---|
365 | //! access function |
---|
366 | const RV& _rvc() const {return rvc;} |
---|
367 | }; |
---|
368 | |
---|
369 | #endif // BM_H |
---|