[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; |
---|
| 84 | //! Indicate independence of its factors |
---|
| 85 | bool independent; |
---|
| 86 | // //! Indicate internal creation of mpdfs which must be destroyed |
---|
| 87 | // bool intermpdfs; |
---|
| 88 | public: |
---|
| 89 | //!Constructor from list of eFacs or list of mFacs |
---|
| 90 | mprod ( Array<mpdf*> mFacs ) : mpdf ( RV(), RV() ), n ( mFacs.length() ), epdfs ( n ), mpdfs ( mFacs ), rvinds ( n ), rvcinrv ( n ), rvcinds ( n ) { |
---|
| 91 | int i; |
---|
| 92 | // Create rv |
---|
| 93 | for ( i = 0;i < n;i++ ) { |
---|
| 94 | rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs. |
---|
| 95 | epdfs ( i ) = & ( mpdfs ( i )->_epdf() ); // add pointer to epdf |
---|
[145] | 96 | }; |
---|
[162] | 97 | // Create rvc |
---|
| 98 | for ( i = 0;i < n;i++ ) { |
---|
| 99 | rvc.add ( mpdfs ( i )->_rv().subt ( rv ) ); //add rv to common rvs. |
---|
[145] | 100 | }; |
---|
[124] | 101 | |
---|
[162] | 102 | independent = true; |
---|
| 103 | //test rvc of mpdfs and fill rvinds |
---|
| 104 | for ( i = 0;i < n;i++ ) { |
---|
| 105 | // find ith rv in common rv |
---|
| 106 | rvinds ( i ) = mpdfs ( i )->_rv().dataind ( rv ); |
---|
| 107 | // find ith rvc in common rv |
---|
| 108 | rvcinrv ( i ) = mpdfs ( i )->_rvc().dataind ( rv ); |
---|
| 109 | // find ith rvc in common rv |
---|
| 110 | rvcinds ( i ) = mpdfs ( i )->_rvc().dataind ( rvc ); |
---|
| 111 | // |
---|
| 112 | if ( rvcinds ( i ).length() >0 ) {independent = false;} |
---|
| 113 | if ( rvcinds ( i ).length() >0 ) {independent = false;} |
---|
[145] | 114 | } |
---|
[162] | 115 | }; |
---|
| 116 | |
---|
| 117 | double evalpdflog ( const vec &val ) const { |
---|
| 118 | int i; |
---|
| 119 | double res = 0.0; |
---|
| 120 | for ( i = n - 1;i > 0;i++ ) { |
---|
| 121 | if ( rvcinds ( i ).length() > 0 ) |
---|
| 122 | {mpdfs ( i )->condition ( val ( rvcinds ( i ) ) );} |
---|
| 123 | // add logarithms |
---|
| 124 | res += epdfs ( i )->evalpdflog ( val ( rvinds ( i ) ) ); |
---|
[145] | 125 | } |
---|
[162] | 126 | return res; |
---|
| 127 | } |
---|
| 128 | vec samplecond ( const vec &cond, double &ll ) { |
---|
| 129 | vec smp=zeros ( rv.count() ); |
---|
| 130 | vec condi; |
---|
| 131 | for ( int i = ( n - 1 );i >= 0;i-- ) { |
---|
| 132 | if ( rvcinds ( i ).length() > 0 ) { |
---|
| 133 | condi = zeros ( rvcinrv.length() + rvcinds.length() ); |
---|
| 134 | // copy data in condition |
---|
| 135 | set_subvector ( condi,rvcinds ( i ), cond ); |
---|
| 136 | // copy data in already generated sample |
---|
| 137 | set_subvector ( condi,rvcinrv ( i ), smp ); |
---|
| 138 | |
---|
| 139 | mpdfs ( i )->condition ( condi ); |
---|
[145] | 140 | } |
---|
[162] | 141 | // copy contribution of this pdf into smp |
---|
| 142 | set_subvector ( smp,rvinds ( i ), epdfs ( i )->sample() ); |
---|
[145] | 143 | } |
---|
[162] | 144 | return smp; |
---|
| 145 | } |
---|
| 146 | mat samplecond ( const vec &cond, vec &ll, int N ) { |
---|
| 147 | mat Smp(rv.count(),N); |
---|
| 148 | for(int i=0;i<N;i++){Smp.set_col(i,samplecond(cond,ll(i)));} |
---|
| 149 | return Smp; |
---|
| 150 | } |
---|
| 151 | // vec mean() const { |
---|
| 152 | // vec tmp ( rv.count() ); |
---|
| 153 | // if ( independent ) { |
---|
| 154 | // for ( int i=0;i<n;i++ ) { |
---|
| 155 | // vec pom = epdfs ( i )->mean(); |
---|
| 156 | // set_subvector ( tmp,rvinds ( i ), pom ); |
---|
| 157 | // } |
---|
| 158 | // } |
---|
| 159 | // else { |
---|
| 160 | // int N=50*rv.count(); |
---|
| 161 | // it_warning ( "eprod.mean() computed by sampling" ); |
---|
| 162 | // tmp = zeros ( rv.count() ); |
---|
| 163 | // for ( int i=0;i<N;i++ ) { tmp += sample();} |
---|
| 164 | // tmp /=N; |
---|
| 165 | // } |
---|
| 166 | // return tmp; |
---|
| 167 | // } |
---|
| 168 | |
---|
| 169 | ~mprod() {}; |
---|
[107] | 170 | }; |
---|
| 171 | |
---|
[145] | 172 | /*! \brief Mixture of mpdfs with constant weights, all mpdfs are of equal type |
---|
[124] | 173 | |
---|
| 174 | */ |
---|
[145] | 175 | class mmix : public mpdf { |
---|
[162] | 176 | protected: |
---|
| 177 | //! Component (epdfs) |
---|
| 178 | Array<mpdf*> Coms; |
---|
| 179 | //!Internal epdf |
---|
| 180 | emix Epdf; |
---|
| 181 | public: |
---|
| 182 | //!Default constructor |
---|
| 183 | mmix ( RV &rv, RV &rvc ) : mpdf ( rv, rvc ), Epdf ( rv ) {ep = &Epdf;}; |
---|
| 184 | //! Set weights \c w and components \c R |
---|
| 185 | void set_parameters ( const vec &w, const Array<mpdf*> &Coms ) { |
---|
| 186 | Array<epdf*> Eps ( Coms.length() ); |
---|
[124] | 187 | |
---|
[162] | 188 | for ( int i = 0;i < Coms.length();i++ ) { |
---|
| 189 | Eps ( i ) = & ( Coms ( i )->_epdf() ); |
---|
| 190 | } |
---|
| 191 | Epdf.set_parameters ( w, Eps ); |
---|
| 192 | }; |
---|
[124] | 193 | |
---|
[162] | 194 | void condition ( const vec &cond ) { |
---|
| 195 | for ( int i = 0;i < Coms.length();i++ ) {Coms ( i )->condition ( cond );} |
---|
| 196 | }; |
---|
[124] | 197 | }; |
---|
[107] | 198 | #endif //MX_H |
---|