root/bdm/stat/libBM.h @ 28

Revision 28, 5.1 kB (checked in by smidl, 16 years ago)

prelozitelna verze

  • 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*/
26class RV {
27        //! size = sum of sizes
28        int size;
29        //! len = number of individual rvs
30        int len;
31        ivec ids;
32        ivec sizes;
33        ivec times;
34        ivec obs;
35        Array<std::string> names;
36
37private:
38        void init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times, ivec in_obs );
39public:
40        //! Full constructor which is called by the others
41        RV ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times, ivec in_obs );
42        //! default constructor
43        RV ( ivec ids );
44        //! Empty constructor will be set later
45        RV ();
46       
47        //! Printing output e.g. for debugging.
48        friend std::ostream &operator<< ( std::ostream &os, const RV &rv );
49
50        //! Return length (number of scalars) of the RV.
51        int count() const {return size;} ;
52        //TODO why not inline and later??
53       
54        //! Find indexes of another rv in self
55        ivec find(RV rv2);
56        //! Add (concat) another variable to the current one
57        RV add(RV rv2);
58        //! Subtract  another variable from the current one
59        RV subt(RV rv2);
60        //! Select only variables at indeces ind
61        RV subselect(ivec ind);
62        //! Select only variables at indeces ind
63        RV operator()(ivec ind);
64        //! Generate new \c RV with \c time shifted by delta.
65        void t(int delta);
66        //! generate a list of indeces, i.e. which
67        ivec indexlist();
68};
69
70
71
72
73//! Class representing function $f(x)$ of variable $x$ represented by \c rv
74class fnc {
75protected:
76        int dimy;
77public: 
78        //! function evaluates numerical value of $f(x)$ at $x=cond$
79        virtual vec eval(const vec &cond)
80        {
81                return vec(0);
82        }; //Fixme: virtual?
83
84        //! access function
85        int _dimy()const{return dimy;}
86
87                //! Destructor for future use;
88                virtual ~fnc(){};
89};
90
91
92//! Probability density function with numerical statistics, e.g. posterior density.
93class epdf {
94        RV rv;
95public:
96        //!default constructor
97        epdf():rv(ivec(0)){};
98        //! Returns the required moment of the epdf
99//      virtual vec moment ( const int order = 1 );
100        //! Returns a sample from the density, \f$x \sim epdf(rv)\f$
101        virtual vec sample ()=0;
102        //! Compute probability of argument \c val
103        virtual double eval(const vec &val){return 0.0;};
104        //! Compute log-probability of argument \c val
105        virtual double evalpdflog(const vec &val){return 0.0;};
106
107                //! Destructor for future use;
108                virtual ~epdf(){};
109};
110
111//! Conditional probability density, e.g. modeling some dependencies.
112class mpdf {
113        //! modeled random variable
114        RV rv;
115        //! random variable in condition
116        RV rvc;
117public:
118
119        //! Returns the required moment of the epdf
120//      virtual fnc moment ( const int order = 1 );
121        //! Returns a sample from the density conditioned on \c cond, $x \sim epdf(rv|cond)$
122        virtual vec samplecond (vec &cond, double lik){return vec(0);};
123        virtual void condition (vec &cond){};
124
125                //! Destructor for future use;
126                virtual ~mpdf(){};
127};
128
129/*! \brief Abstract class for discrete-time sources of data.
130
131The 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.
132Moreover, for controlled systems, it is able to receive the desired control action and perform it in the next step. (Or as soon as possible).
133
134*/
135class DS {
136protected:
137        //!Observed variables, returned by \c getdata().
138        RV Drv; 
139        //!Action variables, accepted by \c write().
140        RV Urv; //
141public:
142        //! Returns full vector of observed data
143        void getdata(vec &dt);
144        //! Returns data records at indeces.
145        void getdata(vec &dt, ivec &indeces);
146        //! Accepts action variable and schedule it for application.
147        void write(vec &ut);
148        //! Accepts action variables at specific indeces       
149        void write(vec &ut, ivec &indeces);
150        /*! \brief Method that assigns random variables to the datasource.
151        Typically, the datasource will be constructed without knowledge of random variables. This method will associate existing variables with RVs.
152       
153        (Inherited from m3k, may be deprecated soon).
154        */
155        void linkrvs(RV &drv, RV &urv);
156       
157        //! Moves from $t$ to $t+1$, i.e. perfroms the actions and reads response of the system.
158        void step();
159               
160};
161
162/*! \brief Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities.
163       
164*/
165class BM {
166public:
167        //!Logarithm of marginalized data likelihood.
168        double ll;
169        //!  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.
170        bool evalll;
171
172        //!Default constructor
173        BM():ll(0),evalll(true){};
174       
175        /*! \brief Incremental Bayes rule
176        @param dt vector of input data
177        */
178        virtual void bayes ( const vec &dt) = 0;
179        //! Batch Bayes rule (columns of Dt are observations)
180        void bayes ( mat Dt );
181        //! Returns a pointer to the epdf representing posterior density on parameters. Use with care!
182        virtual epdf* _epdf()=0;
183
184                //! Destructor for future use;
185                virtual ~BM(){};
186};
187
188#endif // BM_H
Note: See TracBrowser for help on using the browser.