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 | |
---|
19 | using namespace itpp; |
---|
20 | |
---|
21 | /*! |
---|
22 | * \brief Class representing variables, most often random variables |
---|
23 | |
---|
24 | * More?... |
---|
25 | */ |
---|
26 | class RV { |
---|
27 | int size,len; |
---|
28 | ivec ids; |
---|
29 | ivec sizes; |
---|
30 | ivec times; |
---|
31 | ivec obs; |
---|
32 | Array<std::string> names; |
---|
33 | |
---|
34 | private: |
---|
35 | void init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times, ivec in_obs ); |
---|
36 | public: |
---|
37 | //! Full constructor which is called by the others |
---|
38 | RV ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times, ivec in_obs ); |
---|
39 | //! default constructor |
---|
40 | RV ( ivec ids ); |
---|
41 | //! Empty constructor will be set later |
---|
42 | RV (); |
---|
43 | |
---|
44 | //! Printing output e.g. for debugging. |
---|
45 | friend std::ostream &operator<< ( std::ostream &os, const RV &rv ); |
---|
46 | |
---|
47 | //! Return length (number of scalars) of the RV. |
---|
48 | int count() {return size;} |
---|
49 | //TODO why not inline and later?? |
---|
50 | |
---|
51 | //! Find indexes of another rv in self |
---|
52 | ivec find(RV rv2); |
---|
53 | //! Add (concat) another variable to the current one |
---|
54 | RV add(RV rv2); |
---|
55 | //! Subtract another variable from the current one |
---|
56 | RV subt(RV rv2); |
---|
57 | //! Select only variables at indeces ind |
---|
58 | RV subselect(ivec ind); |
---|
59 | //! Select only variables at indeces ind |
---|
60 | RV operator()(ivec ind); |
---|
61 | //! Generate new \c RV with \c time shifted by delta. |
---|
62 | void t(int delta); |
---|
63 | }; |
---|
64 | |
---|
65 | |
---|
66 | |
---|
67 | |
---|
68 | //! Class representing function of variables |
---|
69 | class fnc { |
---|
70 | RV rv; |
---|
71 | }; |
---|
72 | |
---|
73 | //! Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities. |
---|
74 | class BM { |
---|
75 | public: |
---|
76 | //!Logarithm of marginalized data likelihood. |
---|
77 | double ll; |
---|
78 | |
---|
79 | /*! \brief Incremental Bayes rule |
---|
80 | @param dt vector of input data |
---|
81 | @param evall If true, the filter will compute likelihood of the data record and store it in \c ll |
---|
82 | */ |
---|
83 | virtual void bayes ( const vec &dt, bool evall=true ) = 0; |
---|
84 | //! Batch Bayes rule (columns of Dt are observations) |
---|
85 | void bayes ( mat Dt ); |
---|
86 | }; |
---|
87 | |
---|
88 | //! Probability density function with numerical statistics, e.g. posterior density. |
---|
89 | class epdf { |
---|
90 | RV rv; |
---|
91 | public: |
---|
92 | //! Returns the required moment of the epdf |
---|
93 | // virtual vec moment ( const int order = 1 ); |
---|
94 | //! Returns a sample from the density, $x \sim epdf(rv)$ |
---|
95 | virtual vec sample (){}; |
---|
96 | virtual double eval(const vec &val){}; |
---|
97 | }; |
---|
98 | |
---|
99 | //! Conditional probability density, e.g. modeling some dependencies. |
---|
100 | class mpdf { |
---|
101 | //! modeled random variable |
---|
102 | RV rv; |
---|
103 | //! random variable in condition |
---|
104 | RV rvc; |
---|
105 | public: |
---|
106 | |
---|
107 | //! Returns the required moment of the epdf |
---|
108 | // virtual fnc moment ( const int order = 1 ); |
---|
109 | //! Returns a sample from the density conditioned on \c cond, $x \sim epdf(rv|cond)$ |
---|
110 | virtual vec samplecond (vec &cond, double lik){}; |
---|
111 | virtual void condition (vec &cond){}; |
---|
112 | }; |
---|
113 | |
---|
114 | |
---|
115 | |
---|
116 | #endif // BM_H |
---|