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 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 length(); |
---|
49 | //! Find indexes of another rv in self |
---|
50 | ivec rvfind(RV rv2); |
---|
51 | //! Add (concat) another variable to the current one |
---|
52 | RV rvadd(RV rv2); |
---|
53 | //! Subtract another variable from the current one |
---|
54 | RV rvsubt(RV rv2); |
---|
55 | //! Select only variables at indeces ind |
---|
56 | RV rvsubselect(ivec ind); |
---|
57 | //! Select only variables at indeces ind |
---|
58 | RV operator()(ivec ind); |
---|
59 | //! Generate new \c RV with \c time shifted by delta. |
---|
60 | void t(int delta); |
---|
61 | }; |
---|
62 | |
---|
63 | |
---|
64 | |
---|
65 | |
---|
66 | //! Class representing function of variables |
---|
67 | class fnc { |
---|
68 | RV rv; |
---|
69 | }; |
---|
70 | |
---|
71 | //! Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities. |
---|
72 | class BM { |
---|
73 | public: |
---|
74 | //!Logarithm of marginalized data likelihood. |
---|
75 | double ll; |
---|
76 | |
---|
77 | /*! \brief Incremental Bayes rule |
---|
78 | @param dt vector of input data |
---|
79 | @param evall If true, the filter will compute likelihood of the data record and store it in \c ll |
---|
80 | */ |
---|
81 | virtual void bayes ( const vec &dt, bool evall=true ) = 0; |
---|
82 | //! Batch Bayes rule (columns of Dt are observations) |
---|
83 | void bayes ( mat Dt ); |
---|
84 | }; |
---|
85 | |
---|
86 | //! Probability density function with numerical statistics, e.g. posterior density. |
---|
87 | class epdf { |
---|
88 | RV rv; |
---|
89 | public: |
---|
90 | //! Returns the required moment of the epdf |
---|
91 | // virtual vec moment ( const int order = 1 ); |
---|
92 | //! Returns a sample from the density, $x \sim epdf(rv)$ |
---|
93 | virtual vec sample (){}; |
---|
94 | virtual double eval(const vec &val){}; |
---|
95 | }; |
---|
96 | |
---|
97 | //! Conditional probability density, e.g. modeling some dependencies. |
---|
98 | class mpdf { |
---|
99 | //! modeled random variable |
---|
100 | RV rv; |
---|
101 | //! random variable in condition |
---|
102 | RV rvc; |
---|
103 | public: |
---|
104 | |
---|
105 | //! Returns the required moment of the epdf |
---|
106 | // virtual fnc moment ( const int order = 1 ); |
---|
107 | //! Returns a sample from the density conditioned on \c cond, $x \sim epdf(rv|cond)$ |
---|
108 | virtual vec samplecond (vec &cond, double lik){}; |
---|
109 | virtual void condition (vec &cond){}; |
---|
110 | }; |
---|
111 | |
---|
112 | #endif // BM_H |
---|