root/bdm/stat/libBM.h @ 51

Revision 51, 7.3 kB (checked in by smidl, 16 years ago)

oprava EKF + debug vypisove fce

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