[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 | |
---|
[182] | 22 | //this comes first because it is used inside emix! |
---|
| 23 | |
---|
| 24 | /*! \brief Class representing ratio of two densities |
---|
| 25 | which arise e.g. by applying the Bayes rule. |
---|
| 26 | It represents density in the form: |
---|
| 27 | \f[ |
---|
| 28 | f(rv|rvc) = \frac{f(rv,rvc)}{f(rvc)} |
---|
| 29 | \f] |
---|
| 30 | where \f$ f(rvc) = \int f(rv,rvc) d\ rv \f$. |
---|
| 31 | |
---|
| 32 | In particular this type of arise by conditioning of a mixture model. |
---|
| 33 | |
---|
| 34 | At present the only supported operation is evalcond(). |
---|
| 35 | */ |
---|
| 36 | class mratio: public mpdf { |
---|
[192] | 37 | protected: |
---|
[182] | 38 | //! Nominator in the form of mpdf |
---|
[192] | 39 | const epdf* nom; |
---|
[182] | 40 | //!Denominator in the form of epdf |
---|
[192] | 41 | epdf* den; |
---|
| 42 | //!flag for destructor |
---|
| 43 | bool destroynom; |
---|
| 44 | public: |
---|
| 45 | //!Default constructor. By default, the given epdf is not copied! |
---|
[182] | 46 | //! It is assumed that this function will be used only temporarily. |
---|
[192] | 47 | mratio ( const epdf* nom0, const RV &rv, bool copy=false ) :mpdf ( rv,RV() ) { |
---|
| 48 | if ( copy ) { |
---|
[182] | 49 | // nom = nom0->_copy_(); |
---|
[192] | 50 | destroynom=true; |
---|
[182] | 51 | } |
---|
[192] | 52 | else { |
---|
| 53 | nom = nom0; |
---|
| 54 | destroynom = false; |
---|
| 55 | } |
---|
| 56 | rvc = nom->_rv().subt ( rv ); |
---|
| 57 | it_assert_debug ( rvc.length() >0,"Makes no sense to use this object!" ); |
---|
| 58 | den = nom->marginal ( rvc ); |
---|
| 59 | }; |
---|
| 60 | double evalcond ( const vec &val, const vec &cond ) { |
---|
| 61 | return exp ( nom->evalpdflog ( concat ( val,cond ) ) - den->evalpdflog ( cond ) ); |
---|
| 62 | } |
---|
[182] | 63 | //! Object takes ownership of nom and will destroy it |
---|
[192] | 64 | void ownnom() {destroynom=true;} |
---|
[182] | 65 | //! Default destructor |
---|
[192] | 66 | ~mratio() {delete den; if ( destroynom ) {delete nom;}} |
---|
[182] | 67 | }; |
---|
| 68 | |
---|
[107] | 69 | /*! |
---|
| 70 | * \brief Mixture of epdfs |
---|
| 71 | |
---|
| 72 | Density function: |
---|
| 73 | \f[ |
---|
| 74 | f(x) = \sum_{i=1}^{n} w_{i} f_i(x), \quad \sum_{i=1}^n w_i = 1. |
---|
| 75 | \f] |
---|
| 76 | where \f$f_i(x)\f$ is any density on random variable \f$x\f$, called \a component, |
---|
| 77 | |
---|
| 78 | */ |
---|
[145] | 79 | class emix : public epdf { |
---|
[162] | 80 | protected: |
---|
| 81 | //! weights of the components |
---|
| 82 | vec w; |
---|
| 83 | //! Component (epdfs) |
---|
| 84 | Array<epdf*> Coms; |
---|
[178] | 85 | //!Flag if owning Coms |
---|
| 86 | bool destroyComs; |
---|
[162] | 87 | public: |
---|
| 88 | //!Default constructor |
---|
[181] | 89 | emix ( const RV &rv ) : epdf ( rv ) {}; |
---|
[182] | 90 | //! Set weights \c w and components \c Coms |
---|
| 91 | //!By default Coms are copied inside. \param copy can be set to false if Coms live externally. Use method ownComs() if Coms should be destroyed by the destructor. |
---|
[178] | 92 | void set_parameters ( const vec &w, const Array<epdf*> &Coms, bool copy=true ); |
---|
[107] | 93 | |
---|
[162] | 94 | vec sample() const; |
---|
| 95 | vec mean() const { |
---|
| 96 | int i; vec mu = zeros ( rv.count() ); |
---|
| 97 | for ( i = 0;i < w.length();i++ ) {mu += w ( i ) * Coms ( i )->mean(); } |
---|
| 98 | return mu; |
---|
| 99 | } |
---|
| 100 | double evalpdflog ( const vec &val ) const { |
---|
| 101 | int i; |
---|
| 102 | double sum = 0.0; |
---|
[192] | 103 | for ( i = 0;i < w.length();i++ ) {sum += w ( i ) * exp ( Coms ( i )->evalpdflog ( val ) );} |
---|
[162] | 104 | return log ( sum ); |
---|
| 105 | }; |
---|
[182] | 106 | vec evalpdflog_m ( const mat &Val ) const { |
---|
[192] | 107 | vec x=ones ( Val.cols() ); |
---|
| 108 | vec y ( Val.cols() ); |
---|
| 109 | for ( int i = 0; i < w.length(); i++ ) { |
---|
| 110 | y = w ( i ) *exp ( Coms ( i )->evalpdflog_m ( Val ) ); |
---|
| 111 | elem_mult_inplace ( y,x ); //result is in x |
---|
[182] | 112 | } |
---|
[192] | 113 | return log ( x ); |
---|
[182] | 114 | }; |
---|
[189] | 115 | mat evalpdflog_M ( const mat &Val ) const { |
---|
[192] | 116 | mat X ( w.length(), Val.cols() ); |
---|
| 117 | for ( int i = 0; i < w.length(); i++ ) { |
---|
| 118 | X.set_row ( i, w ( i ) *exp ( Coms ( i )->evalpdflog_m ( Val ) ) ); |
---|
[189] | 119 | } |
---|
| 120 | return X; |
---|
| 121 | }; |
---|
[107] | 122 | |
---|
[182] | 123 | emix* marginal ( const RV &rv ) const; |
---|
| 124 | mratio* condition ( const RV &rv ) const; //why not mratio!! |
---|
| 125 | |
---|
[107] | 126 | //Access methods |
---|
[162] | 127 | //! returns a pointer to the internal mean value. Use with Care! |
---|
| 128 | vec& _w() {return w;} |
---|
[181] | 129 | virtual ~emix() {if ( destroyComs ) {for ( int i=0;i<Coms.length();i++ ) {delete Coms ( i );}}} |
---|
[178] | 130 | //! Auxiliary function for taking ownership of the Coms() |
---|
[181] | 131 | void ownComs() {destroyComs=true;} |
---|
[107] | 132 | }; |
---|
| 133 | |
---|
[115] | 134 | /*! \brief Chain rule decomposition of epdf |
---|
| 135 | |
---|
[145] | 136 | Probability density in the form of Chain-rule decomposition: |
---|
| 137 | \[ |
---|
| 138 | f(x_1,x_2,x_3) = f(x_1|x_2,x_3)f(x_2,x_3)f(x_3) |
---|
| 139 | \] |
---|
| 140 | Note that |
---|
[115] | 141 | */ |
---|
[175] | 142 | class mprod: public compositepdf, public mpdf { |
---|
[162] | 143 | protected: |
---|
[181] | 144 | //! pointers to epdfs - shortcut to mpdfs()._epdf() |
---|
[162] | 145 | Array<epdf*> epdfs; |
---|
[192] | 146 | //! Data link for each mpdfs |
---|
| 147 | Array<datalink_m2m*> dls; |
---|
[162] | 148 | public: |
---|
[168] | 149 | /*!\brief Constructor from list of mFacs, |
---|
[165] | 150 | */ |
---|
[192] | 151 | mprod ( Array<mpdf*> mFacs ) : compositepdf ( mFacs ), mpdf ( getrv ( true ),RV() ), epdfs ( n ), dls ( n ) { |
---|
[181] | 152 | setrvc ( rv,rvc ); |
---|
[192] | 153 | // rv and rvc established = > we can link them with mpdfs |
---|
[181] | 154 | for ( int i = 0;i < n;i++ ) { |
---|
[192] | 155 | dls ( i ) = new datalink_m2m ( mpdfs ( i )->_rv(), mpdfs(i)->_rvc(), rv, rvc ); |
---|
[181] | 156 | } |
---|
| 157 | |
---|
| 158 | for ( int i=0;i<n;i++ ) { |
---|
| 159 | epdfs ( i ) =& ( mpdfs ( i )->_epdf() ); |
---|
| 160 | } |
---|
[175] | 161 | }; |
---|
[124] | 162 | |
---|
[182] | 163 | double evalcond ( const vec &val, const vec &cond ) { |
---|
[162] | 164 | int i; |
---|
| 165 | double res = 0.0; |
---|
[182] | 166 | for ( i = n - 1;i >= 0;i-- ) { |
---|
[192] | 167 | if ( mpdfs(i)->_rvc().count() >0) { |
---|
| 168 | mpdfs ( i )->condition ( dls ( i )->get_cond ( val,cond ) ); |
---|
[181] | 169 | } |
---|
[162] | 170 | // add logarithms |
---|
[192] | 171 | res += epdfs ( i )->evalpdflog ( dls ( i )->get_val ( val ) ); |
---|
[145] | 172 | } |
---|
[182] | 173 | return exp ( res ); |
---|
[162] | 174 | } |
---|
| 175 | vec samplecond ( const vec &cond, double &ll ) { |
---|
[192] | 176 | //! Ugly hack to help to discover if mpfs are not in proper order. Correct solution = check that explicitely. |
---|
| 177 | vec smp= std::numeric_limits<double>::infinity() * ones ( rv.count() ); |
---|
[165] | 178 | vec smpi; |
---|
| 179 | ll = 0; |
---|
[192] | 180 | // Hard assumption here!!! We are going backwards, to assure that samples that are needed from smp are already generated! |
---|
[162] | 181 | for ( int i = ( n - 1 );i >= 0;i-- ) { |
---|
[192] | 182 | if ( mpdfs(i)->_rvc().count() ) { |
---|
| 183 | mpdfs ( i )->condition ( dls ( i )->get_cond ( smp ,cond ) ); // smp is val here!! |
---|
[145] | 184 | } |
---|
[165] | 185 | smpi = epdfs ( i )->sample(); |
---|
[162] | 186 | // copy contribution of this pdf into smp |
---|
[192] | 187 | dls(i)->fill_val(smp, smpi); |
---|
[165] | 188 | // add ith likelihood contribution |
---|
[168] | 189 | ll+=epdfs ( i )->evalpdflog ( smpi ); |
---|
[145] | 190 | } |
---|
[162] | 191 | return smp; |
---|
| 192 | } |
---|
| 193 | mat samplecond ( const vec &cond, vec &ll, int N ) { |
---|
[168] | 194 | mat Smp ( rv.count(),N ); |
---|
| 195 | for ( int i=0;i<N;i++ ) {Smp.set_col ( i,samplecond ( cond,ll ( i ) ) );} |
---|
[162] | 196 | return Smp; |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | ~mprod() {}; |
---|
[107] | 200 | }; |
---|
| 201 | |
---|
[168] | 202 | //! Product of independent epdfs. For dependent pdfs, use mprod. |
---|
| 203 | class eprod: public epdf { |
---|
| 204 | protected: |
---|
| 205 | //! Components (epdfs) |
---|
[170] | 206 | Array<const epdf*> epdfs; |
---|
[168] | 207 | //! Array of indeces |
---|
| 208 | Array<ivec> rvinds; |
---|
| 209 | public: |
---|
[170] | 210 | eprod ( const Array<const epdf*> epdfs0 ) : epdf ( RV() ),epdfs ( epdfs0 ),rvinds ( epdfs.length() ) { |
---|
[168] | 211 | bool independent=true; |
---|
| 212 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
| 213 | independent=rv.add ( epdfs ( i )->_rv() ); |
---|
| 214 | it_assert_debug ( independent==true, "eprod:: given components are not independent ." ); |
---|
| 215 | rvinds ( i ) = ( epdfs ( i )->_rv() ).dataind ( rv ); |
---|
| 216 | } |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | vec mean() const { |
---|
| 220 | vec tmp ( rv.count() ); |
---|
| 221 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
| 222 | vec pom = epdfs ( i )->mean(); |
---|
| 223 | set_subvector ( tmp,rvinds ( i ), pom ); |
---|
| 224 | } |
---|
| 225 | return tmp; |
---|
| 226 | } |
---|
| 227 | vec sample() const { |
---|
| 228 | vec tmp ( rv.count() ); |
---|
| 229 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
| 230 | vec pom = epdfs ( i )->sample(); |
---|
| 231 | set_subvector ( tmp,rvinds ( i ), pom ); |
---|
| 232 | } |
---|
| 233 | return tmp; |
---|
| 234 | } |
---|
| 235 | double evalpdflog ( const vec &val ) const { |
---|
| 236 | double tmp=0; |
---|
| 237 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
[181] | 238 | tmp+=epdfs ( i )->evalpdflog ( val ( rvinds ( i ) ) ); |
---|
[168] | 239 | } |
---|
| 240 | return tmp; |
---|
| 241 | } |
---|
[170] | 242 | //!access function |
---|
[181] | 243 | const epdf* operator () ( int i ) const {it_assert_debug ( i<epdfs.length(),"wrong index" );return epdfs ( i );} |
---|
[168] | 244 | }; |
---|
| 245 | |
---|
| 246 | |
---|
[145] | 247 | /*! \brief Mixture of mpdfs with constant weights, all mpdfs are of equal type |
---|
[124] | 248 | |
---|
| 249 | */ |
---|
[145] | 250 | class mmix : public mpdf { |
---|
[162] | 251 | protected: |
---|
| 252 | //! Component (epdfs) |
---|
| 253 | Array<mpdf*> Coms; |
---|
| 254 | //!Internal epdf |
---|
| 255 | emix Epdf; |
---|
| 256 | public: |
---|
| 257 | //!Default constructor |
---|
| 258 | mmix ( RV &rv, RV &rvc ) : mpdf ( rv, rvc ), Epdf ( rv ) {ep = &Epdf;}; |
---|
| 259 | //! Set weights \c w and components \c R |
---|
| 260 | void set_parameters ( const vec &w, const Array<mpdf*> &Coms ) { |
---|
| 261 | Array<epdf*> Eps ( Coms.length() ); |
---|
[124] | 262 | |
---|
[162] | 263 | for ( int i = 0;i < Coms.length();i++ ) { |
---|
| 264 | Eps ( i ) = & ( Coms ( i )->_epdf() ); |
---|
| 265 | } |
---|
| 266 | Epdf.set_parameters ( w, Eps ); |
---|
| 267 | }; |
---|
[124] | 268 | |
---|
[162] | 269 | void condition ( const vec &cond ) { |
---|
| 270 | for ( int i = 0;i < Coms.length();i++ ) {Coms ( i )->condition ( cond );} |
---|
| 271 | }; |
---|
[124] | 272 | }; |
---|
[182] | 273 | |
---|
[107] | 274 | #endif //MX_H |
---|