1 | /*! |
---|
2 | \file |
---|
3 | \brief Probability distributions for Mixtures of pdfs |
---|
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 MX_H |
---|
14 | #define MX_H |
---|
15 | |
---|
16 | #include "libBM.h" |
---|
17 | #include "libEF.h" |
---|
18 | //#include <std> |
---|
19 | |
---|
20 | using namespace itpp; |
---|
21 | |
---|
22 | /*! |
---|
23 | * \brief Mixture of epdfs |
---|
24 | |
---|
25 | Density function: |
---|
26 | \f[ |
---|
27 | f(x) = \sum_{i=1}^{n} w_{i} f_i(x), \quad \sum_{i=1}^n w_i = 1. |
---|
28 | \f] |
---|
29 | where \f$f_i(x)\f$ is any density on random variable \f$x\f$, called \a component, |
---|
30 | |
---|
31 | */ |
---|
32 | class emix : public epdf { |
---|
33 | protected: |
---|
34 | //! weights of the components |
---|
35 | vec w; |
---|
36 | //! Component (epdfs) |
---|
37 | Array<epdf*> Coms; |
---|
38 | public: |
---|
39 | //!Default constructor |
---|
40 | emix ( RV &rv ) : epdf ( rv ) {}; |
---|
41 | //! Set weights \c w and components \c R |
---|
42 | void set_parameters ( const vec &w, const Array<epdf*> &Coms ); |
---|
43 | |
---|
44 | vec sample() const; |
---|
45 | vec mean() const { |
---|
46 | int i; vec mu=zeros ( rv.count() ); |
---|
47 | for ( i=0;i<w.length();i++ ) {mu+=w ( i ) *Coms ( i )->mean(); } |
---|
48 | return mu; |
---|
49 | } |
---|
50 | double evalpdflog ( const vec &val ) const { |
---|
51 | int i; |
---|
52 | double sum=0.0; |
---|
53 | for ( i=0;i<w.length();i++ ) {sum+=w ( i ) *Coms ( i )->evalpdflog ( val );} |
---|
54 | return log ( sum ); |
---|
55 | }; |
---|
56 | |
---|
57 | //Access methods |
---|
58 | //! returns a pointer to the internal mean value. Use with Care! |
---|
59 | vec& _w() {return w;} |
---|
60 | }; |
---|
61 | |
---|
62 | /*! \brief Chain rule decomposition of epdf |
---|
63 | |
---|
64 | |
---|
65 | */ |
---|
66 | class eprod: public epdf { |
---|
67 | protected: |
---|
68 | Array<epdf*> epdfs; |
---|
69 | Array<mpdf*> mpdfs; |
---|
70 | public: |
---|
71 | |
---|
72 | |
---|
73 | }; |
---|
74 | |
---|
75 | /*! \brief Mixture of mpdfs with constant weights |
---|
76 | |
---|
77 | */ |
---|
78 | class mmix : public mpdf { |
---|
79 | protected: |
---|
80 | //! Component (epdfs) |
---|
81 | Array<mpdf*> Coms; |
---|
82 | //!Internal epdf |
---|
83 | emix Epdf; |
---|
84 | public: |
---|
85 | //!Default constructor |
---|
86 | mmix ( RV &rv, RV &rvc ) : mpdf ( rv, rvc ), Epdf ( rv ) {ep=&Epdf;}; |
---|
87 | //! Set weights \c w and components \c R |
---|
88 | void set_parameters ( const vec &w, const Array<mpdf*> &Coms ) { |
---|
89 | Array<epdf*> Eps ( Coms.length()); |
---|
90 | |
---|
91 | for ( int i=0;i<Coms.length();i++ ) {mpdf* Ci=Coms(i); |
---|
92 | Eps ( i ) =& ( Coms ( i )->_epdf() );} |
---|
93 | Epdf.set_parameters ( w,Eps ); |
---|
94 | }; |
---|
95 | |
---|
96 | void condition ( const vec &cond ) { |
---|
97 | for ( int i=0;i<Coms.length();i++ ) {Coms ( i )->condition ( cond );} |
---|
98 | }; |
---|
99 | }; |
---|
100 | #endif //MX_H |
---|