root/bdm/stat/libBM.h @ 27

Revision 27, 4.6 kB (checked in by mido, 16 years ago)

par preklepu

  • 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
88//! Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities.
89class BM {
90public:
91        //!Logarithm of marginalized data likelihood.
92        double ll;
93
94        //!Default constructor
95        BM(){ll=0;};
96       
97        /*! \brief Incremental Bayes rule
98        @param dt vector of input data
99        @param evall If true, the filter will compute likelihood of the data record and store it in \c ll
100        */
101        virtual void bayes ( const vec &dt, bool evall=true ) = 0;
102        //! Batch Bayes rule (columns of Dt are observations)
103        void bayes ( mat Dt );
104};
105
106//! Probability density function with numerical statistics, e.g. posterior density.
107class epdf {
108        RV rv;
109public:
110        //! Returns the required moment of the epdf
111//      virtual vec moment ( const int order = 1 );
112        //! Returns a sample from the density, \f$x \sim epdf(rv)\f$
113        virtual vec sample ()=0;
114        //! Compute probability of argument \c val
115        virtual double eval(const vec &val)
116        {
117                return 0.0;
118        };
119};
120
121//! Conditional probability density, e.g. modeling some dependencies.
122class mpdf {
123        //! modeled random variable
124        RV rv;
125        //! random variable in condition
126        RV rvc;
127public:
128
129        //! Returns the required moment of the epdf
130//      virtual fnc moment ( const int order = 1 );
131        //! Returns a sample from the density conditioned on \c cond, $x \sim epdf(rv|cond)$
132        virtual vec samplecond (vec &cond, double lik){};
133        virtual void condition (vec &cond){};
134};
135
136/*! \brief Abstract class for discrete-time sources of data.
137
138The 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.
139Moreover, for controlled systems, it is able to receive the desired control action and perform it in the next step. (Or as soon as possible).
140
141*/
142class DS {
143protected:
144        //!Observed variables, returned by \c getdata().
145        RV Drv; 
146        //!Action variables, accepted by \c write().
147        RV Urv; //
148public:
149        //! Returns full vector of observed data
150        void getdata(vec &dt);
151        //! Returns data records at indeces.
152        void getdata(vec &dt, ivec &indeces);
153        //! Accepts action variable and schedule it for application.
154        void write(vec &ut);
155        //! Accepts action variables at specific indeces       
156        void write(vec &ut, ivec &indeces);
157        /*! \brief Method that assigns random variables to the datasource.
158        Typically, the datasource will be constructed without knowledge of random variables. This method will associate existing variables with RVs.
159       
160        (Inherited from m3k, may be deprecated soon).
161        */
162        void linkrvs(RV &drv, RV &urv);
163       
164        //! Moves from $t$ to $t+1$, i.e. perfroms the actions and reads response of the system.
165        void step();
166};
167
168
169#endif // BM_H
Note: See TracBrowser for help on using the browser.