root/bdm/stat/libBM.h @ 19

Revision 19, 4.4 kB (checked in by smidl, 16 years ago)

Switch to CMake

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