root/bdm/stat/libBM.h @ 118

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

preklad...

  • 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 );
170        vec temp= ep->sample();
171        ll=ep->evalpdflog ( temp );return temp;};
172        //! 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.
173        virtual mat samplecond ( vec &cond, vec &ll, int N ) {
174                this->condition ( cond );
175                mat temp ( rv.count(),N ); vec smp ( rv.count() ); 
176                for ( int i=0;i<N;i++ ) {smp=ep->sample() ;temp.set_col ( i, smp );ll ( i ) =ep->evalpdflog ( smp );}
177                return temp;
178        };
179        //! Update \c ep so that it represents this mpdf conditioned on \c rvc = cond
180        virtual void condition ( const vec &cond ) {};
181
182        //! Shortcut for conditioning and evaluation of the internal epdf. In some cases,  this operation can be implemented efficiently.
183        virtual double evalcond ( const vec &dt, const vec &cond ) {this->condition ( cond );return ep->eval ( dt );};
184
185        //! Destructor for future use;
186        virtual ~mpdf() {};
187
188        //! Default constructor
189        mpdf ( const RV &rv0, const RV &rvc0 ) :rv ( rv0 ),rvc ( rvc0 ) {};
190        //! access function
191        RV _rvc() {return rvc;}
192        //!access function
193        epdf& _epdf() {return *ep;}
194};
195
196/*! \brief Unconditional mpdf, allows using epdf in the role of mpdf.
197
198WARNING: the class does not check validity of the \c ep pointer nor its existence.
199*/
200class mepdf : public mpdf {
201public:
202        //!Default constructor
203        mepdf ( const RV &rv, const RV &rvc, epdf* em ) :mpdf ( rv,rvc ) {ep=em;};
204};
205
206/*! \brief Abstract class for discrete-time sources of data.
207
208The 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.
209Moreover, for controlled systems, it is able to receive the desired control action and perform it in the next step. (Or as soon as possible).
210
211*/
212
213class DS {
214protected:
215        //!Observed variables, returned by \c getdata().
216        RV Drv;
217        //!Action variables, accepted by \c write().
218        RV Urv; //
219public:
220        //! Returns full vector of observed data
221        void getdata ( vec &dt );
222        //! Returns data records at indeces.
223        void getdata ( vec &dt, ivec &indeces );
224        //! Accepts action variable and schedule it for application.
225        void write ( vec &ut );
226        //! Accepts action variables at specific indeces
227        void write ( vec &ut, ivec &indeces );
228        /*! \brief Method that assigns random variables to the datasource.
229        Typically, the datasource will be constructed without knowledge of random variables. This method will associate existing variables with RVs.
230
231        (Inherited from m3k, may be deprecated soon).
232        */
233        void linkrvs ( RV &drv, RV &urv );
234
235        //! Moves from \f$t\f$ to \f$t+1\f$, i.e. perfroms the actions and reads response of the system.
236        void step();
237
238};
239
240/*! \brief Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities.
241
242*/
243
244class BM {
245protected:
246        //!Random variable of the posterior
247        RV rv;
248        //!Logarithm of marginalized data likelihood.
249        double ll;
250        //!  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.
251        bool evalll;
252public:
253
254        //!Default constructor
255        BM ( const RV &rv0 ) :rv ( rv0 ), ll ( 0 ),evalll ( true ) {//Fixme: test rv
256        };
257
258        /*! \brief Incremental Bayes rule
259        @param dt vector of input data
260        */
261        virtual void bayes ( const vec &dt ) = 0;
262        //! Batch Bayes rule (columns of Dt are observations)
263        void bayes ( mat Dt );
264        //! Returns a pointer to the epdf representing posterior density on parameters. Use with care!
265        virtual epdf& _epdf() =0;
266
267        //! Destructor for future use;
268        virtual ~BM() {};
269        //!access function
270        const RV& _rv() const {return rv;}
271        //!access function
272        double _ll() const {return ll;}
273};
274
275/*!
276\brief Conditional Bayesian Filter
277
278Evaluates 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.
279
280This is an interface class used to assure that certain BM has operation \c condition .
281
282*/
283
284class BMcond {
285protected:
286        //! Identificator of the conditioning variable
287        RV rvc;
288public:
289        //! Substitute \c val for \c rvc.
290        virtual void condition ( const vec &val ) =0;
291        //! Default constructor
292        BMcond ( RV &rv0 ) :rvc ( rv0 ) {};
293        //! Destructor for future use
294        virtual ~BMcond() {};
295        //! access function
296        const RV& _rvc() const {return rvc;}
297};
298
299#endif // BM_H
Note: See TracBrowser for help on using the browser.