root/bdm/stat/libBM.h @ 115

Revision 115, 8.6 kB (checked in by smidl, 16 years ago)

zmena mmix_triv na mepdf

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