| 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 | { |
|---|
| 34 | protected: |
|---|
| 35 | //! weights of the components |
|---|
| 36 | vec w; |
|---|
| 37 | //! Component (epdfs) |
|---|
| 38 | Array<epdf*> Coms; |
|---|
| 39 | public: |
|---|
| 40 | //!Default constructor |
|---|
| 41 | emix ( RV &rv ) : epdf ( rv ) {}; |
|---|
| 42 | //! Set weights \c w and components \c R |
|---|
| 43 | void set_parameters ( const vec &w, const Array<epdf*> &Coms ); |
|---|
| 44 | |
|---|
| 45 | vec sample() const; |
|---|
| 46 | vec mean() const |
|---|
| 47 | { |
|---|
| 48 | int i; vec mu=zeros ( rv.count() ); |
|---|
| 49 | for ( i=0;i<w.length();i++ ) {mu+=w ( i ) *Coms ( i )->mean(); } |
|---|
| 50 | return mu; |
|---|
| 51 | } |
|---|
| 52 | double evalpdflog ( const vec &val ) const |
|---|
| 53 | { |
|---|
| 54 | int i; |
|---|
| 55 | double sum=0.0; |
|---|
| 56 | for ( i=0;i<w.length();i++ ) {sum+=w ( i ) *Coms ( i )->evalpdflog ( val );} |
|---|
| 57 | return log ( sum ); |
|---|
| 58 | }; |
|---|
| 59 | |
|---|
| 60 | //Access methods |
|---|
| 61 | //! returns a pointer to the internal mean value. Use with Care! |
|---|
| 62 | vec& _w() {return w;} |
|---|
| 63 | }; |
|---|
| 64 | |
|---|
| 65 | /*! \brief Chain rule decomposition of epdf |
|---|
| 66 | |
|---|
| 67 | |
|---|
| 68 | */ |
|---|
| 69 | class eprod: public epdf |
|---|
| 70 | { |
|---|
| 71 | protected: |
|---|
| 72 | Array<epdf*> epdfs; |
|---|
| 73 | Array<mpdf*> mpdfs; |
|---|
| 74 | public: |
|---|
| 75 | |
|---|
| 76 | |
|---|
| 77 | }; |
|---|
| 78 | |
|---|
| 79 | /*! \brief Mixture of mpdfs with constant weights |
|---|
| 80 | |
|---|
| 81 | */ |
|---|
| 82 | class mmix : public mpdf |
|---|
| 83 | { |
|---|
| 84 | protected: |
|---|
| 85 | //! Component (epdfs) |
|---|
| 86 | Array<mpdf*> Coms; |
|---|
| 87 | //!Internal epdf |
|---|
| 88 | emix Epdf; |
|---|
| 89 | public: |
|---|
| 90 | //!Default constructor |
|---|
| 91 | mmix ( RV &rv, RV &rvc ) : mpdf ( rv, rvc ), Epdf ( rv ) {ep=&Epdf;}; |
|---|
| 92 | //! Set weights \c w and components \c R |
|---|
| 93 | void set_parameters ( const vec &w, const Array<mpdf*> &Coms ) |
|---|
| 94 | { |
|---|
| 95 | Array<epdf*> Eps ( Coms.length() ); |
|---|
| 96 | |
|---|
| 97 | for ( int i=0;i<Coms.length();i++ ) |
|---|
| 98 | { |
|---|
| 99 | Eps ( i ) =& ( Coms ( i )->_epdf() ); |
|---|
| 100 | } |
|---|
| 101 | Epdf.set_parameters ( w,Eps ); |
|---|
| 102 | }; |
|---|
| 103 | |
|---|
| 104 | void condition ( const vec &cond ) |
|---|
| 105 | { |
|---|
| 106 | for ( int i=0;i<Coms.length();i++ ) {Coms ( i )->condition ( cond );} |
|---|
| 107 | }; |
|---|
| 108 | }; |
|---|
| 109 | #endif //MX_H |
|---|