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