[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 | |
---|
[333] | 16 | #define LOG2 0.69314718055995 |
---|
| 17 | |
---|
[107] | 18 | #include "libEF.h" |
---|
| 19 | //#include <std> |
---|
| 20 | |
---|
[286] | 21 | namespace bdm { |
---|
[107] | 22 | |
---|
[182] | 23 | //this comes first because it is used inside emix! |
---|
| 24 | |
---|
| 25 | /*! \brief Class representing ratio of two densities |
---|
| 26 | which arise e.g. by applying the Bayes rule. |
---|
| 27 | It represents density in the form: |
---|
| 28 | \f[ |
---|
| 29 | f(rv|rvc) = \frac{f(rv,rvc)}{f(rvc)} |
---|
| 30 | \f] |
---|
| 31 | where \f$ f(rvc) = \int f(rv,rvc) d\ rv \f$. |
---|
| 32 | |
---|
| 33 | In particular this type of arise by conditioning of a mixture model. |
---|
| 34 | |
---|
[211] | 35 | At present the only supported operation is evallogcond(). |
---|
[182] | 36 | */ |
---|
| 37 | class mratio: public mpdf { |
---|
[192] | 38 | protected: |
---|
[182] | 39 | //! Nominator in the form of mpdf |
---|
[192] | 40 | const epdf* nom; |
---|
[182] | 41 | //!Denominator in the form of epdf |
---|
[192] | 42 | epdf* den; |
---|
| 43 | //!flag for destructor |
---|
| 44 | bool destroynom; |
---|
[193] | 45 | //!datalink between conditional and nom |
---|
| 46 | datalink_m2e dl; |
---|
[192] | 47 | public: |
---|
| 48 | //!Default constructor. By default, the given epdf is not copied! |
---|
[182] | 49 | //! It is assumed that this function will be used only temporarily. |
---|
[286] | 50 | mratio ( const epdf* nom0, const RV &rv, bool copy=false ) :mpdf ( ), dl ( ) { |
---|
| 51 | // adjust rv and rvc |
---|
| 52 | rvc = nom0->_rv().subt ( rv ); |
---|
| 53 | dimc = rvc._dsize(); |
---|
| 54 | ep = new epdf; |
---|
| 55 | ep->set_parameters(rv._dsize()); |
---|
| 56 | ep->set_rv(rv); |
---|
| 57 | |
---|
| 58 | //prepare data structures |
---|
| 59 | if ( copy ) {it_error ( "todo" ); destroynom=true; } |
---|
| 60 | else { nom = nom0; destroynom = false; } |
---|
| 61 | it_assert_debug ( rvc.length() >0,"Makes no sense to use this object!" ); |
---|
| 62 | |
---|
| 63 | // build denominator |
---|
[192] | 64 | den = nom->marginal ( rvc ); |
---|
[286] | 65 | dl.set_connection(rv,rvc,nom0->_rv()); |
---|
[192] | 66 | }; |
---|
[211] | 67 | double evallogcond ( const vec &val, const vec &cond ) { |
---|
[214] | 68 | double tmp; |
---|
[270] | 69 | vec nom_val ( ep->dimension() + dimc ); |
---|
| 70 | dl.pushup_cond ( nom_val,val,cond ); |
---|
[214] | 71 | tmp = exp ( nom->evallog ( nom_val ) - den->evallog ( cond ) ); |
---|
[286] | 72 | it_assert_debug ( std::isfinite ( tmp ),"Infinite value" ); |
---|
[214] | 73 | return tmp; |
---|
[192] | 74 | } |
---|
[182] | 75 | //! Object takes ownership of nom and will destroy it |
---|
[192] | 76 | void ownnom() {destroynom=true;} |
---|
[182] | 77 | //! Default destructor |
---|
[192] | 78 | ~mratio() {delete den; if ( destroynom ) {delete nom;}} |
---|
[182] | 79 | }; |
---|
| 80 | |
---|
[107] | 81 | /*! |
---|
| 82 | * \brief Mixture of epdfs |
---|
| 83 | |
---|
| 84 | Density function: |
---|
| 85 | \f[ |
---|
| 86 | f(x) = \sum_{i=1}^{n} w_{i} f_i(x), \quad \sum_{i=1}^n w_i = 1. |
---|
| 87 | \f] |
---|
| 88 | where \f$f_i(x)\f$ is any density on random variable \f$x\f$, called \a component, |
---|
| 89 | |
---|
| 90 | */ |
---|
[145] | 91 | class emix : public epdf { |
---|
[162] | 92 | protected: |
---|
| 93 | //! weights of the components |
---|
| 94 | vec w; |
---|
| 95 | //! Component (epdfs) |
---|
| 96 | Array<epdf*> Coms; |
---|
[178] | 97 | //!Flag if owning Coms |
---|
| 98 | bool destroyComs; |
---|
[162] | 99 | public: |
---|
| 100 | //!Default constructor |
---|
[286] | 101 | emix ( ) : epdf ( ) {}; |
---|
[182] | 102 | //! Set weights \c w and components \c Coms |
---|
[224] | 103 | //!By default Coms are copied inside. Parameter \c copy can be set to false if Coms live externally. Use method ownComs() if Coms should be destroyed by the destructor. |
---|
[270] | 104 | void set_parameters ( const vec &w, const Array<epdf*> &Coms, bool copy=false ); |
---|
[107] | 105 | |
---|
[162] | 106 | vec sample() const; |
---|
| 107 | vec mean() const { |
---|
[286] | 108 | int i; vec mu = zeros ( dim ); |
---|
[162] | 109 | for ( i = 0;i < w.length();i++ ) {mu += w ( i ) * Coms ( i )->mean(); } |
---|
| 110 | return mu; |
---|
| 111 | } |
---|
[229] | 112 | vec variance() const { |
---|
| 113 | //non-central moment |
---|
[286] | 114 | vec mom2 = zeros ( dim ); |
---|
[310] | 115 | for ( int i = 0;i < w.length();i++ ) {mom2 += w ( i ) * (Coms(i)->variance() + pow ( Coms ( i )->mean(),2 )); } |
---|
[229] | 116 | //central moment |
---|
[286] | 117 | return mom2-pow ( mean(),2 ); |
---|
[229] | 118 | } |
---|
[211] | 119 | double evallog ( const vec &val ) const { |
---|
[162] | 120 | int i; |
---|
| 121 | double sum = 0.0; |
---|
[211] | 122 | for ( i = 0;i < w.length();i++ ) {sum += w ( i ) * exp ( Coms ( i )->evallog ( val ) );} |
---|
[286] | 123 | if ( sum==0.0 ) {sum=std::numeric_limits<double>::epsilon();} |
---|
[214] | 124 | double tmp=log ( sum ); |
---|
[286] | 125 | it_assert_debug ( std::isfinite ( tmp ),"Infinite" ); |
---|
[214] | 126 | return tmp; |
---|
[162] | 127 | }; |
---|
[211] | 128 | vec evallog_m ( const mat &Val ) const { |
---|
[193] | 129 | vec x=zeros ( Val.cols() ); |
---|
[192] | 130 | for ( int i = 0; i < w.length(); i++ ) { |
---|
[211] | 131 | x+= w ( i ) *exp ( Coms ( i )->evallog_m ( Val ) ); |
---|
[182] | 132 | } |
---|
[192] | 133 | return log ( x ); |
---|
[182] | 134 | }; |
---|
[214] | 135 | //! Auxiliary function that returns pdflog for each component |
---|
[211] | 136 | mat evallog_M ( const mat &Val ) const { |
---|
[192] | 137 | mat X ( w.length(), Val.cols() ); |
---|
| 138 | for ( int i = 0; i < w.length(); i++ ) { |
---|
[211] | 139 | X.set_row ( i, w ( i ) *exp ( Coms ( i )->evallog_m ( Val ) ) ); |
---|
[189] | 140 | } |
---|
| 141 | return X; |
---|
| 142 | }; |
---|
[107] | 143 | |
---|
[182] | 144 | emix* marginal ( const RV &rv ) const; |
---|
| 145 | mratio* condition ( const RV &rv ) const; //why not mratio!! |
---|
| 146 | |
---|
[107] | 147 | //Access methods |
---|
[162] | 148 | //! returns a pointer to the internal mean value. Use with Care! |
---|
| 149 | vec& _w() {return w;} |
---|
[181] | 150 | virtual ~emix() {if ( destroyComs ) {for ( int i=0;i<Coms.length();i++ ) {delete Coms ( i );}}} |
---|
[178] | 151 | //! Auxiliary function for taking ownership of the Coms() |
---|
[181] | 152 | void ownComs() {destroyComs=true;} |
---|
[204] | 153 | |
---|
[193] | 154 | //!access function |
---|
[204] | 155 | epdf* _Coms ( int i ) {return Coms ( i );} |
---|
[286] | 156 | void set_rv(const RV &rv){ |
---|
| 157 | epdf::set_rv(rv); |
---|
| 158 | for(int i=0;i<Coms.length();i++){Coms(i)->set_rv(rv);} |
---|
| 159 | } |
---|
[107] | 160 | }; |
---|
| 161 | |
---|
[333] | 162 | |
---|
| 163 | /*! |
---|
| 164 | * \brief Mixture of egiws |
---|
| 165 | |
---|
| 166 | */ |
---|
| 167 | class egiwmix : public egiw { |
---|
| 168 | protected: |
---|
| 169 | //! weights of the components |
---|
| 170 | vec w; |
---|
| 171 | //! Component (epdfs) |
---|
| 172 | Array<egiw*> Coms; |
---|
| 173 | //!Flag if owning Coms |
---|
| 174 | bool destroyComs; |
---|
| 175 | public: |
---|
| 176 | //!Default constructor |
---|
| 177 | egiwmix ( ) : egiw ( ) {}; |
---|
| 178 | |
---|
| 179 | //! Set weights \c w and components \c Coms |
---|
| 180 | //!By default Coms are copied inside. Parameter \c copy can be set to false if Coms live externally. Use method ownComs() if Coms should be destroyed by the destructor. |
---|
| 181 | void set_parameters ( const vec &w, const Array<egiw*> &Coms, bool copy=false ); |
---|
| 182 | |
---|
| 183 | //!return expected value |
---|
| 184 | vec mean() const; |
---|
| 185 | |
---|
| 186 | //!return a sample from the density |
---|
| 187 | vec sample() const; |
---|
| 188 | |
---|
| 189 | //!return the expected variance |
---|
| 190 | vec variance() const; |
---|
| 191 | |
---|
| 192 | // TODO!!! Defined to follow ANSI and/or for future development |
---|
| 193 | void mean_mat ( mat &M, mat&R ) const {}; |
---|
| 194 | double evallog_nn ( const vec &val ) const {return 0;}; |
---|
| 195 | double lognc () const {return 0;}; |
---|
| 196 | emix* marginal ( const RV &rv ) const; |
---|
| 197 | |
---|
| 198 | //Access methods |
---|
| 199 | //! returns a pointer to the internal mean value. Use with Care! |
---|
| 200 | vec& _w() {return w;} |
---|
| 201 | virtual ~egiwmix() {if ( destroyComs ) {for ( int i=0;i<Coms.length();i++ ) {delete Coms ( i );}}} |
---|
| 202 | //! Auxiliary function for taking ownership of the Coms() |
---|
| 203 | void ownComs() {destroyComs=true;} |
---|
| 204 | |
---|
| 205 | //!access function |
---|
| 206 | egiw* _Coms ( int i ) {return Coms ( i );} |
---|
| 207 | |
---|
| 208 | void set_rv(const RV &rv){ |
---|
| 209 | egiw::set_rv(rv); |
---|
| 210 | for(int i=0;i<Coms.length();i++){Coms(i)->set_rv(rv);} |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | //! Approximation of a GiW mix by a single GiW pdf |
---|
| 214 | egiw* approx(); |
---|
| 215 | }; |
---|
| 216 | |
---|
[115] | 217 | /*! \brief Chain rule decomposition of epdf |
---|
| 218 | |
---|
[145] | 219 | Probability density in the form of Chain-rule decomposition: |
---|
| 220 | \[ |
---|
| 221 | f(x_1,x_2,x_3) = f(x_1|x_2,x_3)f(x_2,x_3)f(x_3) |
---|
| 222 | \] |
---|
| 223 | Note that |
---|
[115] | 224 | */ |
---|
[175] | 225 | class mprod: public compositepdf, public mpdf { |
---|
[162] | 226 | protected: |
---|
[271] | 227 | //! pointers to epdfs - shortcut to mpdfs().posterior() |
---|
[162] | 228 | Array<epdf*> epdfs; |
---|
[192] | 229 | //! Data link for each mpdfs |
---|
| 230 | Array<datalink_m2m*> dls; |
---|
[270] | 231 | //! dummy ep |
---|
| 232 | epdf dummy; |
---|
[162] | 233 | public: |
---|
[168] | 234 | /*!\brief Constructor from list of mFacs, |
---|
[165] | 235 | */ |
---|
[378] | 236 | mprod (){}; |
---|
| 237 | mprod (Array<mpdf*> mFacs ){set_elements( mFacs );}; |
---|
| 238 | void set_elements(Array<mpdf*> mFacs ) { |
---|
| 239 | |
---|
| 240 | set_elements(mFacs); |
---|
| 241 | |
---|
[270] | 242 | ep=&dummy; |
---|
[286] | 243 | RV rv=getrv ( true ); |
---|
| 244 | set_rv ( rv );dummy.set_parameters ( rv._dsize() ); |
---|
[270] | 245 | setrvc ( ep->_rv(),rvc ); |
---|
[192] | 246 | // rv and rvc established = > we can link them with mpdfs |
---|
[378] | 247 | for ( int i = 0;i < mpdfs.length(); i++ ) { |
---|
[286] | 248 | dls ( i ) = new datalink_m2m; |
---|
| 249 | dls(i)->set_connection( mpdfs ( i )->_rv(), mpdfs ( i )->_rvc(), _rv(), _rvc() ); |
---|
[181] | 250 | } |
---|
| 251 | |
---|
[378] | 252 | for ( int i=0; i<mpdfs.length(); i++ ) { |
---|
[181] | 253 | epdfs ( i ) =& ( mpdfs ( i )->_epdf() ); |
---|
| 254 | } |
---|
[175] | 255 | }; |
---|
[124] | 256 | |
---|
[211] | 257 | double evallogcond ( const vec &val, const vec &cond ) { |
---|
[162] | 258 | int i; |
---|
[270] | 259 | double res = 0.0; |
---|
[378] | 260 | for ( i = mpdfs.length() - 1;i >= 0;i-- ) { |
---|
[193] | 261 | /* if ( mpdfs(i)->_rvc().count() >0) { |
---|
| 262 | mpdfs ( i )->condition ( dls ( i )->get_cond ( val,cond ) ); |
---|
| 263 | } |
---|
| 264 | // add logarithms |
---|
[270] | 265 | res += epdfs ( i )->evallog ( dls ( i )->pushdown ( val ) );*/ |
---|
| 266 | res += mpdfs ( i )->evallogcond ( |
---|
| 267 | dls ( i )->pushdown ( val ), |
---|
[204] | 268 | dls ( i )->get_cond ( val, cond ) |
---|
| 269 | ); |
---|
[145] | 270 | } |
---|
[193] | 271 | return res; |
---|
[162] | 272 | } |
---|
[270] | 273 | //TODO smarter... |
---|
| 274 | vec samplecond ( const vec &cond ) { |
---|
[192] | 275 | //! Ugly hack to help to discover if mpfs are not in proper order. Correct solution = check that explicitely. |
---|
[270] | 276 | vec smp= std::numeric_limits<double>::infinity() * ones ( ep->dimension() ); |
---|
[165] | 277 | vec smpi; |
---|
[192] | 278 | // Hard assumption here!!! We are going backwards, to assure that samples that are needed from smp are already generated! |
---|
[378] | 279 | for ( int i = ( mpdfs.length() - 1 );i >= 0;i-- ) { |
---|
[270] | 280 | if ( mpdfs ( i )->dimensionc() ) { |
---|
[192] | 281 | mpdfs ( i )->condition ( dls ( i )->get_cond ( smp ,cond ) ); // smp is val here!! |
---|
[145] | 282 | } |
---|
[165] | 283 | smpi = epdfs ( i )->sample(); |
---|
[162] | 284 | // copy contribution of this pdf into smp |
---|
[270] | 285 | dls ( i )->pushup ( smp, smpi ); |
---|
[165] | 286 | // add ith likelihood contribution |
---|
[145] | 287 | } |
---|
[162] | 288 | return smp; |
---|
| 289 | } |
---|
[270] | 290 | mat samplecond ( const vec &cond, int N ) { |
---|
| 291 | mat Smp ( dimension(),N ); |
---|
| 292 | for ( int i=0;i<N;i++ ) {Smp.set_col ( i,samplecond ( cond ) );} |
---|
[162] | 293 | return Smp; |
---|
| 294 | } |
---|
| 295 | |
---|
| 296 | ~mprod() {}; |
---|
[107] | 297 | }; |
---|
| 298 | |
---|
[168] | 299 | //! Product of independent epdfs. For dependent pdfs, use mprod. |
---|
| 300 | class eprod: public epdf { |
---|
| 301 | protected: |
---|
| 302 | //! Components (epdfs) |
---|
[170] | 303 | Array<const epdf*> epdfs; |
---|
[168] | 304 | //! Array of indeces |
---|
[270] | 305 | Array<datalink*> dls; |
---|
[168] | 306 | public: |
---|
[286] | 307 | eprod () : epdfs ( 0 ),dls ( 0 ) {}; |
---|
| 308 | void set_parameters ( const Array<const epdf*> &epdfs0, bool named=true ) { |
---|
| 309 | epdfs=epdfs0;//.set_length ( epdfs0.length() ); |
---|
| 310 | dls.set_length ( epdfs.length() ); |
---|
| 311 | |
---|
[168] | 312 | bool independent=true; |
---|
[286] | 313 | if ( named ) { |
---|
| 314 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
| 315 | independent=rv.add ( epdfs ( i )->_rv() ); |
---|
| 316 | it_assert_debug ( independent==true, "eprod:: given components are not independent." ); |
---|
| 317 | } |
---|
| 318 | dim=rv._dsize(); |
---|
[168] | 319 | } |
---|
[286] | 320 | else { |
---|
| 321 | dim =0; for ( int i=0;i<epdfs.length();i++ ) { |
---|
| 322 | dim+=epdfs ( i )->dimension(); |
---|
| 323 | } |
---|
[204] | 324 | } |
---|
[286] | 325 | // |
---|
| 326 | int cumdim=0; |
---|
| 327 | int dimi=0; |
---|
| 328 | int i; |
---|
| 329 | for ( i=0;i<epdfs.length();i++ ) { |
---|
| 330 | dls ( i ) = new datalink; |
---|
| 331 | if ( named ) {dls ( i )->set_connection ( epdfs ( i )->_rv() , rv );} |
---|
| 332 | else { |
---|
| 333 | dimi = epdfs ( i )->dimension(); |
---|
| 334 | dls ( i )->set_connection ( dimi, dim, linspace ( cumdim,cumdim+dimi-1 ) ); |
---|
| 335 | cumdim+=dimi; |
---|
| 336 | } |
---|
| 337 | } |
---|
[168] | 338 | } |
---|
| 339 | |
---|
| 340 | vec mean() const { |
---|
[270] | 341 | vec tmp ( dim ); |
---|
[168] | 342 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
| 343 | vec pom = epdfs ( i )->mean(); |
---|
[270] | 344 | dls ( i )->pushup ( tmp, pom ); |
---|
[168] | 345 | } |
---|
| 346 | return tmp; |
---|
| 347 | } |
---|
[229] | 348 | vec variance() const { |
---|
[270] | 349 | vec tmp ( dim ); //second moment |
---|
[229] | 350 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
| 351 | vec pom = epdfs ( i )->mean(); |
---|
[286] | 352 | dls ( i )->pushup ( tmp, pow ( pom,2 ) ); |
---|
[229] | 353 | } |
---|
[286] | 354 | return tmp-pow ( mean(),2 ); |
---|
[229] | 355 | } |
---|
[168] | 356 | vec sample() const { |
---|
[270] | 357 | vec tmp ( dim ); |
---|
[168] | 358 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
| 359 | vec pom = epdfs ( i )->sample(); |
---|
[270] | 360 | dls ( i )->pushup ( tmp, pom ); |
---|
[168] | 361 | } |
---|
| 362 | return tmp; |
---|
| 363 | } |
---|
[211] | 364 | double evallog ( const vec &val ) const { |
---|
[168] | 365 | double tmp=0; |
---|
| 366 | for ( int i=0;i<epdfs.length();i++ ) { |
---|
[270] | 367 | tmp+=epdfs ( i )->evallog ( dls ( i )->pushdown ( val ) ); |
---|
[168] | 368 | } |
---|
[286] | 369 | it_assert_debug ( std::isfinite ( tmp ),"Infinite" ); |
---|
[168] | 370 | return tmp; |
---|
| 371 | } |
---|
[170] | 372 | //!access function |
---|
[181] | 373 | const epdf* operator () ( int i ) const {it_assert_debug ( i<epdfs.length(),"wrong index" );return epdfs ( i );} |
---|
[204] | 374 | |
---|
[193] | 375 | //!Destructor |
---|
[204] | 376 | ~eprod() {for ( int i=0;i<epdfs.length();i++ ) {delete dls ( i );}} |
---|
[168] | 377 | }; |
---|
| 378 | |
---|
| 379 | |
---|
[145] | 380 | /*! \brief Mixture of mpdfs with constant weights, all mpdfs are of equal type |
---|
[124] | 381 | |
---|
| 382 | */ |
---|
[145] | 383 | class mmix : public mpdf { |
---|
[162] | 384 | protected: |
---|
| 385 | //! Component (epdfs) |
---|
| 386 | Array<mpdf*> Coms; |
---|
| 387 | //!Internal epdf |
---|
| 388 | emix Epdf; |
---|
| 389 | public: |
---|
| 390 | //!Default constructor |
---|
[270] | 391 | mmix ( ) : mpdf ( ), Epdf () {ep = &Epdf;}; |
---|
[162] | 392 | //! Set weights \c w and components \c R |
---|
| 393 | void set_parameters ( const vec &w, const Array<mpdf*> &Coms ) { |
---|
| 394 | Array<epdf*> Eps ( Coms.length() ); |
---|
[124] | 395 | |
---|
[162] | 396 | for ( int i = 0;i < Coms.length();i++ ) { |
---|
| 397 | Eps ( i ) = & ( Coms ( i )->_epdf() ); |
---|
| 398 | } |
---|
| 399 | Epdf.set_parameters ( w, Eps ); |
---|
| 400 | }; |
---|
[124] | 401 | |
---|
[162] | 402 | void condition ( const vec &cond ) { |
---|
| 403 | for ( int i = 0;i < Coms.length();i++ ) {Coms ( i )->condition ( cond );} |
---|
| 404 | }; |
---|
[124] | 405 | }; |
---|
[182] | 406 | |
---|
[254] | 407 | } |
---|
[107] | 408 | #endif //MX_H |
---|