root/bdm/stat/libBM.h @ 32

Revision 32, 6.7 kB (checked in by smidl, 16 years ago)

test KF : estimation of R in KF is not possible! Likelihood of y_t is growing when R -> 0

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