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