root/bdm/stat/libBM.h @ 62

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

nova simulace s EKFfixed a novy EKF na plnych maticich

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