root/bdm/stat/libBM.h @ 85

Revision 85, 7.7 kB (checked in by smidl, 16 years ago)

compilation and documantation fixes

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