[107] | 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 | */ |
---|
[145] | 32 | class emix : public epdf { |
---|
[162] | 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 ); |
---|
[107] | 43 | |
---|
[162] | 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 | }; |
---|
[107] | 56 | |
---|
| 57 | //Access methods |
---|
[162] | 58 | //! returns a pointer to the internal mean value. Use with Care! |
---|
| 59 | vec& _w() {return w;} |
---|
[107] | 60 | }; |
---|
| 61 | |
---|
[115] | 62 | /*! \brief Chain rule decomposition of epdf |
---|
| 63 | |
---|
[145] | 64 | Probability density in the form of Chain-rule decomposition: |
---|
| 65 | \[ |
---|
| 66 | f(x_1,x_2,x_3) = f(x_1|x_2,x_3)f(x_2,x_3)f(x_3) |
---|
| 67 | \] |
---|
| 68 | Note that |
---|
[115] | 69 | */ |
---|
[162] | 70 | class mprod: public mpdf { |
---|
| 71 | protected: |
---|
| 72 | // |
---|
| 73 | int n; |
---|
| 74 | // pointers to epdfs |
---|
| 75 | Array<epdf*> epdfs; |
---|
| 76 | Array<mpdf*> mpdfs; |
---|
| 77 | // |
---|
| 78 | //! Indeces of rvs in common rv |
---|
| 79 | Array<ivec> rvinds; |
---|
| 80 | //! Indeces of rvc in common rv |
---|
| 81 | Array<ivec> rvcinrv; |
---|
| 82 | //! Indeces of rvc in common rvc |
---|
| 83 | Array<ivec> rvcinds; |
---|
[168] | 84 | // //! Indicate independence of its factors |
---|
| 85 | // bool independent; |
---|
[162] | 86 | // //! Indicate internal creation of mpdfs which must be destroyed |
---|
| 87 | // bool intermpdfs; |
---|
| 88 | public: |
---|
[168] | 89 | /*!\brief Constructor from list of mFacs, |
---|
[165] | 90 | Additional parameter overlap is left for future use. Do not set to true for mprod. |
---|
| 91 | */ |
---|
[168] | 92 | mprod ( Array<mpdf*> mFacs, bool overlap=false ); |
---|
[124] | 93 | |
---|
[162] | 94 | double evalpdflog ( const vec &val ) const { |
---|
| 95 | int i; |
---|
| 96 | double res = 0.0; |
---|
| 97 | for ( i = n - 1;i > 0;i++ ) { |
---|
| 98 | if ( rvcinds ( i ).length() > 0 ) |
---|
| 99 | {mpdfs ( i )->condition ( val ( rvcinds ( i ) ) );} |
---|
| 100 | // add logarithms |
---|
| 101 | res += epdfs ( i )->evalpdflog ( val ( rvinds ( i ) ) ); |
---|
[145] | 102 | } |
---|
[162] | 103 | return res; |
---|
| 104 | } |
---|
| 105 | vec samplecond ( const vec &cond, double &ll ) { |
---|
| 106 | vec smp=zeros ( rv.count() ); |
---|
| 107 | vec condi; |
---|
[165] | 108 | vec smpi; |
---|
| 109 | ll = 0; |
---|
[162] | 110 | for ( int i = ( n - 1 );i >= 0;i-- ) { |
---|
| 111 | if ( rvcinds ( i ).length() > 0 ) { |
---|
| 112 | condi = zeros ( rvcinrv.length() + rvcinds.length() ); |
---|
| 113 | // copy data in condition |
---|
| 114 | set_subvector ( condi,rvcinds ( i ), cond ); |
---|
| 115 | // copy data in already generated sample |
---|
| 116 | set_subvector ( condi,rvcinrv ( i ), smp ); |
---|
| 117 | |
---|
| 118 | mpdfs ( i )->condition ( condi ); |
---|
[145] | 119 | } |
---|
[165] | 120 | smpi = epdfs ( i )->sample(); |
---|
[162] | 121 | // copy contribution of this pdf into smp |
---|
[165] | 122 | set_subvector ( smp,rvinds ( i ), smpi ); |
---|
| 123 | // add ith likelihood contribution |
---|
[168] | 124 | ll+=epdfs ( i )->evalpdflog ( smpi ); |
---|
[145] | 125 | } |
---|
[162] | 126 | return smp; |
---|
| 127 | } |
---|
| 128 | mat samplecond ( const vec &cond, vec &ll, int N ) { |
---|
[168] | 129 | mat Smp ( rv.count(),N ); |
---|
| 130 | for ( int i=0;i<N;i++ ) {Smp.set_col ( i,samplecond ( cond,ll ( i ) ) );} |
---|
[162] | 131 | return Smp; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | ~mprod() {}; |
---|
[107] | 135 | }; |
---|
| 136 | |
---|
[168] | 137 | //! Product of independent epdfs. For dependent pdfs, use mprod. |
---|
| 138 | class eprod: public epdf { |
---|
| 139 | protected: |
---|
| 140 | //! Components (epdfs) |
---|
[170] | 141 | Array<const epdf*> epdfs; |
---|
[168] | 142 | //! Array of indeces |
---|
| 143 | Array<ivec> rvinds; |
---|
| 144 | public: |
---|
[170] | 145 | eprod ( const Array<const epdf*> epdfs0 ) : epdf ( RV() ),epdfs ( epdfs0 ),rvinds ( epdfs.length() ) { |
---|
[168] | 146 | bool independent=true; |
---|
| 147 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
| 148 | independent=rv.add ( epdfs ( i )->_rv() ); |
---|
| 149 | it_assert_debug ( independent==true, "eprod:: given components are not independent ." ); |
---|
| 150 | rvinds ( i ) = ( epdfs ( i )->_rv() ).dataind ( rv ); |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | vec mean() const { |
---|
| 155 | vec tmp ( rv.count() ); |
---|
| 156 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
| 157 | vec pom = epdfs ( i )->mean(); |
---|
| 158 | set_subvector ( tmp,rvinds ( i ), pom ); |
---|
| 159 | } |
---|
| 160 | return tmp; |
---|
| 161 | } |
---|
| 162 | vec sample() const { |
---|
| 163 | vec tmp ( rv.count() ); |
---|
| 164 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
| 165 | vec pom = epdfs ( i )->sample(); |
---|
| 166 | set_subvector ( tmp,rvinds ( i ), pom ); |
---|
| 167 | } |
---|
| 168 | return tmp; |
---|
| 169 | } |
---|
| 170 | double evalpdflog ( const vec &val ) const { |
---|
| 171 | double tmp=0; |
---|
| 172 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
| 173 | tmp+=epdfs(i)->evalpdflog(val(rvinds(i))); |
---|
| 174 | } |
---|
| 175 | return tmp; |
---|
| 176 | } |
---|
[170] | 177 | //!access function |
---|
| 178 | const epdf* operator () (int i) const {it_assert_debug(i<epdfs.length(),"wrong index");return epdfs(i);} |
---|
[168] | 179 | }; |
---|
| 180 | |
---|
| 181 | |
---|
[145] | 182 | /*! \brief Mixture of mpdfs with constant weights, all mpdfs are of equal type |
---|
[124] | 183 | |
---|
| 184 | */ |
---|
[145] | 185 | class mmix : public mpdf { |
---|
[162] | 186 | protected: |
---|
| 187 | //! Component (epdfs) |
---|
| 188 | Array<mpdf*> Coms; |
---|
| 189 | //!Internal epdf |
---|
| 190 | emix Epdf; |
---|
| 191 | public: |
---|
| 192 | //!Default constructor |
---|
| 193 | mmix ( RV &rv, RV &rvc ) : mpdf ( rv, rvc ), Epdf ( rv ) {ep = &Epdf;}; |
---|
| 194 | //! Set weights \c w and components \c R |
---|
| 195 | void set_parameters ( const vec &w, const Array<mpdf*> &Coms ) { |
---|
| 196 | Array<epdf*> Eps ( Coms.length() ); |
---|
[124] | 197 | |
---|
[162] | 198 | for ( int i = 0;i < Coms.length();i++ ) { |
---|
| 199 | Eps ( i ) = & ( Coms ( i )->_epdf() ); |
---|
| 200 | } |
---|
| 201 | Epdf.set_parameters ( w, Eps ); |
---|
| 202 | }; |
---|
[124] | 203 | |
---|
[162] | 204 | void condition ( const vec &cond ) { |
---|
| 205 | for ( int i = 0;i < Coms.length();i++ ) {Coms ( i )->condition ( cond );} |
---|
| 206 | }; |
---|
[124] | 207 | }; |
---|
[107] | 208 | #endif //MX_H |
---|