[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 | |
---|
[394] | 13 | #ifndef EMIX_H |
---|
| 14 | #define EMIX_H |
---|
[107] | 15 | |
---|
[477] | 16 | #define LOG2 0.69314718055995 |
---|
[333] | 17 | |
---|
[461] | 18 | #include "../shared_ptr.h" |
---|
[384] | 19 | #include "exp_family.h" |
---|
[107] | 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; |
---|
[504] | 41 | |
---|
[182] | 42 | //!Denominator in the form of epdf |
---|
[504] | 43 | shared_ptr<epdf> den; |
---|
| 44 | |
---|
[192] | 45 | //!flag for destructor |
---|
| 46 | bool destroynom; |
---|
[193] | 47 | //!datalink between conditional and nom |
---|
| 48 | datalink_m2e dl; |
---|
[487] | 49 | //! dummy epdf that stores only rv and dim |
---|
| 50 | epdf iepdf; |
---|
[192] | 51 | public: |
---|
| 52 | //!Default constructor. By default, the given epdf is not copied! |
---|
[182] | 53 | //! It is assumed that this function will be used only temporarily. |
---|
[487] | 54 | mratio ( const epdf* nom0, const RV &rv, bool copy = false ) : mpdf ( ), dl ( ),iepdf() { |
---|
[286] | 55 | // adjust rv and rvc |
---|
| 56 | rvc = nom0->_rv().subt ( rv ); |
---|
| 57 | dimc = rvc._dsize(); |
---|
[487] | 58 | set_ep ( iepdf ); |
---|
| 59 | iepdf.set_parameters ( rv._dsize() ); |
---|
| 60 | iepdf.set_rv ( rv ); |
---|
[477] | 61 | |
---|
[286] | 62 | //prepare data structures |
---|
[477] | 63 | if ( copy ) { |
---|
| 64 | it_error ( "todo" ); |
---|
| 65 | destroynom = true; |
---|
| 66 | } else { |
---|
| 67 | nom = nom0; |
---|
| 68 | destroynom = false; |
---|
| 69 | } |
---|
| 70 | it_assert_debug ( rvc.length() > 0, "Makes no sense to use this object!" ); |
---|
| 71 | |
---|
[286] | 72 | // build denominator |
---|
[192] | 73 | den = nom->marginal ( rvc ); |
---|
[477] | 74 | dl.set_connection ( rv, rvc, nom0->_rv() ); |
---|
[192] | 75 | }; |
---|
[211] | 76 | double evallogcond ( const vec &val, const vec &cond ) { |
---|
[214] | 77 | double tmp; |
---|
[487] | 78 | vec nom_val ( dimension() + dimc ); |
---|
[477] | 79 | dl.pushup_cond ( nom_val, val, cond ); |
---|
[214] | 80 | tmp = exp ( nom->evallog ( nom_val ) - den->evallog ( cond ) ); |
---|
| 81 | return tmp; |
---|
[192] | 82 | } |
---|
[182] | 83 | //! Object takes ownership of nom and will destroy it |
---|
[477] | 84 | void ownnom() { |
---|
| 85 | destroynom = true; |
---|
| 86 | } |
---|
[182] | 87 | //! Default destructor |
---|
[477] | 88 | ~mratio() { |
---|
| 89 | if ( destroynom ) { |
---|
| 90 | delete nom; |
---|
| 91 | } |
---|
| 92 | } |
---|
[182] | 93 | }; |
---|
| 94 | |
---|
[107] | 95 | /*! |
---|
| 96 | * \brief Mixture of epdfs |
---|
| 97 | |
---|
| 98 | Density function: |
---|
| 99 | \f[ |
---|
| 100 | f(x) = \sum_{i=1}^{n} w_{i} f_i(x), \quad \sum_{i=1}^n w_i = 1. |
---|
| 101 | \f] |
---|
| 102 | where \f$f_i(x)\f$ is any density on random variable \f$x\f$, called \a component, |
---|
| 103 | |
---|
| 104 | */ |
---|
[145] | 105 | class emix : public epdf { |
---|
[162] | 106 | protected: |
---|
| 107 | //! weights of the components |
---|
| 108 | vec w; |
---|
| 109 | //! Component (epdfs) |
---|
[504] | 110 | Array<shared_ptr<epdf> > Coms; |
---|
| 111 | |
---|
[162] | 112 | public: |
---|
| 113 | //!Default constructor |
---|
[286] | 114 | emix ( ) : epdf ( ) {}; |
---|
[182] | 115 | //! Set weights \c w and components \c Coms |
---|
[224] | 116 | //!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. |
---|
[504] | 117 | void set_parameters ( const vec &w, const Array<shared_ptr<epdf> > &Coms ); |
---|
[107] | 118 | |
---|
[162] | 119 | vec sample() const; |
---|
| 120 | vec mean() const { |
---|
[477] | 121 | int i; |
---|
| 122 | vec mu = zeros ( dim ); |
---|
| 123 | for ( i = 0; i < w.length(); i++ ) { |
---|
| 124 | mu += w ( i ) * Coms ( i )->mean(); |
---|
| 125 | } |
---|
[162] | 126 | return mu; |
---|
| 127 | } |
---|
[229] | 128 | vec variance() const { |
---|
| 129 | //non-central moment |
---|
[286] | 130 | vec mom2 = zeros ( dim ); |
---|
[477] | 131 | for ( int i = 0; i < w.length(); i++ ) { |
---|
| 132 | mom2 += w ( i ) * ( Coms ( i )->variance() + pow ( Coms ( i )->mean(), 2 ) ); |
---|
| 133 | } |
---|
[229] | 134 | //central moment |
---|
[477] | 135 | return mom2 - pow ( mean(), 2 ); |
---|
[229] | 136 | } |
---|
[211] | 137 | double evallog ( const vec &val ) const { |
---|
[162] | 138 | int i; |
---|
| 139 | double sum = 0.0; |
---|
[477] | 140 | for ( i = 0; i < w.length(); i++ ) { |
---|
| 141 | sum += w ( i ) * exp ( Coms ( i )->evallog ( val ) ); |
---|
| 142 | } |
---|
| 143 | if ( sum == 0.0 ) { |
---|
| 144 | sum = std::numeric_limits<double>::epsilon(); |
---|
| 145 | } |
---|
| 146 | double tmp = log ( sum ); |
---|
| 147 | it_assert_debug ( std::isfinite ( tmp ), "Infinite" ); |
---|
[214] | 148 | return tmp; |
---|
[162] | 149 | }; |
---|
[211] | 150 | vec evallog_m ( const mat &Val ) const { |
---|
[477] | 151 | vec x = zeros ( Val.cols() ); |
---|
[192] | 152 | for ( int i = 0; i < w.length(); i++ ) { |
---|
[477] | 153 | x += w ( i ) * exp ( Coms ( i )->evallog_m ( Val ) ); |
---|
[182] | 154 | } |
---|
[192] | 155 | return log ( x ); |
---|
[182] | 156 | }; |
---|
[214] | 157 | //! Auxiliary function that returns pdflog for each component |
---|
[211] | 158 | mat evallog_M ( const mat &Val ) const { |
---|
[192] | 159 | mat X ( w.length(), Val.cols() ); |
---|
| 160 | for ( int i = 0; i < w.length(); i++ ) { |
---|
[211] | 161 | X.set_row ( i, w ( i ) *exp ( Coms ( i )->evallog_m ( Val ) ) ); |
---|
[189] | 162 | } |
---|
| 163 | return X; |
---|
| 164 | }; |
---|
[107] | 165 | |
---|
[504] | 166 | shared_ptr<epdf> marginal ( const RV &rv ) const; |
---|
[536] | 167 | //! Update already existing marginal density \c target |
---|
[504] | 168 | void marginal ( const RV &rv, emix &target ) const; |
---|
| 169 | shared_ptr<mpdf> condition ( const RV &rv ) const; |
---|
[182] | 170 | |
---|
[107] | 171 | //Access methods |
---|
[162] | 172 | //! returns a pointer to the internal mean value. Use with Care! |
---|
[477] | 173 | vec& _w() { |
---|
| 174 | return w; |
---|
| 175 | } |
---|
[204] | 176 | |
---|
[193] | 177 | //!access function |
---|
[504] | 178 | shared_ptr<epdf> _Coms ( int i ) { |
---|
[477] | 179 | return Coms ( i ); |
---|
[286] | 180 | } |
---|
[504] | 181 | |
---|
[477] | 182 | void set_rv ( const RV &rv ) { |
---|
| 183 | epdf::set_rv ( rv ); |
---|
| 184 | for ( int i = 0; i < Coms.length(); i++ ) { |
---|
| 185 | Coms ( i )->set_rv ( rv ); |
---|
| 186 | } |
---|
| 187 | } |
---|
[107] | 188 | }; |
---|
[529] | 189 | SHAREDPTR( emix ); |
---|
[107] | 190 | |
---|
[333] | 191 | /*! |
---|
| 192 | * \brief Mixture of egiws |
---|
| 193 | |
---|
| 194 | */ |
---|
| 195 | class egiwmix : public egiw { |
---|
| 196 | protected: |
---|
| 197 | //! weights of the components |
---|
| 198 | vec w; |
---|
| 199 | //! Component (epdfs) |
---|
| 200 | Array<egiw*> Coms; |
---|
| 201 | //!Flag if owning Coms |
---|
| 202 | bool destroyComs; |
---|
| 203 | public: |
---|
| 204 | //!Default constructor |
---|
| 205 | egiwmix ( ) : egiw ( ) {}; |
---|
| 206 | |
---|
| 207 | //! Set weights \c w and components \c Coms |
---|
| 208 | //!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. |
---|
[477] | 209 | void set_parameters ( const vec &w, const Array<egiw*> &Coms, bool copy = false ); |
---|
[333] | 210 | |
---|
| 211 | //!return expected value |
---|
| 212 | vec mean() const; |
---|
| 213 | |
---|
| 214 | //!return a sample from the density |
---|
| 215 | vec sample() const; |
---|
| 216 | |
---|
| 217 | //!return the expected variance |
---|
[477] | 218 | vec variance() const; |
---|
[333] | 219 | |
---|
| 220 | // TODO!!! Defined to follow ANSI and/or for future development |
---|
| 221 | void mean_mat ( mat &M, mat&R ) const {}; |
---|
[477] | 222 | double evallog_nn ( const vec &val ) const { |
---|
| 223 | return 0; |
---|
| 224 | }; |
---|
| 225 | double lognc () const { |
---|
| 226 | return 0; |
---|
[504] | 227 | } |
---|
[333] | 228 | |
---|
[504] | 229 | shared_ptr<epdf> marginal ( const RV &rv ) const; |
---|
| 230 | void marginal ( const RV &rv, emix &target ) const; |
---|
| 231 | |
---|
[333] | 232 | //Access methods |
---|
| 233 | //! returns a pointer to the internal mean value. Use with Care! |
---|
[477] | 234 | vec& _w() { |
---|
| 235 | return w; |
---|
| 236 | } |
---|
| 237 | virtual ~egiwmix() { |
---|
| 238 | if ( destroyComs ) { |
---|
| 239 | for ( int i = 0; i < Coms.length(); i++ ) { |
---|
| 240 | delete Coms ( i ); |
---|
| 241 | } |
---|
| 242 | } |
---|
| 243 | } |
---|
[333] | 244 | //! Auxiliary function for taking ownership of the Coms() |
---|
[477] | 245 | void ownComs() { |
---|
| 246 | destroyComs = true; |
---|
| 247 | } |
---|
[333] | 248 | |
---|
| 249 | //!access function |
---|
[477] | 250 | egiw* _Coms ( int i ) { |
---|
| 251 | return Coms ( i ); |
---|
| 252 | } |
---|
[333] | 253 | |
---|
[477] | 254 | void set_rv ( const RV &rv ) { |
---|
| 255 | egiw::set_rv ( rv ); |
---|
| 256 | for ( int i = 0; i < Coms.length(); i++ ) { |
---|
| 257 | Coms ( i )->set_rv ( rv ); |
---|
| 258 | } |
---|
[333] | 259 | } |
---|
| 260 | |
---|
| 261 | //! Approximation of a GiW mix by a single GiW pdf |
---|
| 262 | egiw* approx(); |
---|
| 263 | }; |
---|
| 264 | |
---|
[115] | 265 | /*! \brief Chain rule decomposition of epdf |
---|
| 266 | |
---|
[145] | 267 | Probability density in the form of Chain-rule decomposition: |
---|
| 268 | \[ |
---|
| 269 | f(x_1,x_2,x_3) = f(x_1|x_2,x_3)f(x_2,x_3)f(x_3) |
---|
| 270 | \] |
---|
| 271 | Note that |
---|
[115] | 272 | */ |
---|
[507] | 273 | class mprod: public mpdf { |
---|
| 274 | private: |
---|
| 275 | Array<shared_ptr<mpdf> > mpdfs; |
---|
| 276 | |
---|
[192] | 277 | //! Data link for each mpdfs |
---|
[546] | 278 | Array<shared_ptr<datalink_m2m> > dls; |
---|
[461] | 279 | |
---|
[546] | 280 | protected: |
---|
[487] | 281 | //! dummy epdf used only as storage for RV and dim |
---|
| 282 | epdf iepdf; |
---|
[461] | 283 | |
---|
[162] | 284 | public: |
---|
[507] | 285 | //! \brief Default constructor |
---|
| 286 | mprod() { } |
---|
| 287 | |
---|
| 288 | /*!\brief Constructor from list of mFacs |
---|
[165] | 289 | */ |
---|
[507] | 290 | mprod ( const Array<shared_ptr<mpdf> > &mFacs ) { |
---|
[477] | 291 | set_elements ( mFacs ); |
---|
[461] | 292 | } |
---|
[536] | 293 | //! Set internal \c mpdfs from given values |
---|
[507] | 294 | void set_elements (const Array<shared_ptr<mpdf> > &mFacs ); |
---|
[477] | 295 | |
---|
[211] | 296 | double evallogcond ( const vec &val, const vec &cond ) { |
---|
[162] | 297 | int i; |
---|
[270] | 298 | double res = 0.0; |
---|
[477] | 299 | for ( i = mpdfs.length() - 1; i >= 0; i-- ) { |
---|
[193] | 300 | /* if ( mpdfs(i)->_rvc().count() >0) { |
---|
| 301 | mpdfs ( i )->condition ( dls ( i )->get_cond ( val,cond ) ); |
---|
| 302 | } |
---|
| 303 | // add logarithms |
---|
[270] | 304 | res += epdfs ( i )->evallog ( dls ( i )->pushdown ( val ) );*/ |
---|
| 305 | res += mpdfs ( i )->evallogcond ( |
---|
| 306 | dls ( i )->pushdown ( val ), |
---|
[204] | 307 | dls ( i )->get_cond ( val, cond ) |
---|
| 308 | ); |
---|
[145] | 309 | } |
---|
[193] | 310 | return res; |
---|
[162] | 311 | } |
---|
[477] | 312 | vec evallogcond_m ( const mat &Dt, const vec &cond ) { |
---|
| 313 | vec tmp ( Dt.cols() ); |
---|
| 314 | for ( int i = 0; i < Dt.cols(); i++ ) { |
---|
| 315 | tmp ( i ) = evallogcond ( Dt.get_col ( i ), cond ); |
---|
[395] | 316 | } |
---|
| 317 | return tmp; |
---|
| 318 | }; |
---|
[477] | 319 | vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ) { |
---|
| 320 | vec tmp ( Dt.length() ); |
---|
| 321 | for ( int i = 0; i < Dt.length(); i++ ) { |
---|
| 322 | tmp ( i ) = evallogcond ( Dt ( i ), cond ); |
---|
[395] | 323 | } |
---|
[477] | 324 | return tmp; |
---|
[395] | 325 | }; |
---|
| 326 | |
---|
| 327 | |
---|
[270] | 328 | //TODO smarter... |
---|
| 329 | vec samplecond ( const vec &cond ) { |
---|
[477] | 330 | //! Ugly hack to help to discover if mpfs are not in proper order. Correct solution = check that explicitely. |
---|
[487] | 331 | vec smp = std::numeric_limits<double>::infinity() * ones ( dimension() ); |
---|
[165] | 332 | vec smpi; |
---|
[192] | 333 | // Hard assumption here!!! We are going backwards, to assure that samples that are needed from smp are already generated! |
---|
[477] | 334 | for ( int i = ( mpdfs.length() - 1 ); i >= 0; i-- ) { |
---|
[487] | 335 | // generate contribution of this mpdf |
---|
| 336 | smpi = mpdfs(i)->samplecond(dls ( i )->get_cond ( smp , cond )); |
---|
[162] | 337 | // copy contribution of this pdf into smp |
---|
[270] | 338 | dls ( i )->pushup ( smp, smpi ); |
---|
[145] | 339 | } |
---|
[162] | 340 | return smp; |
---|
| 341 | } |
---|
| 342 | |
---|
[395] | 343 | //! Load from structure with elements: |
---|
| 344 | //! \code |
---|
| 345 | //! { class='mprod'; |
---|
| 346 | //! mpdfs = (..., ...); // list of mpdfs in the order of chain rule |
---|
| 347 | //! } |
---|
| 348 | //! \endcode |
---|
| 349 | //!@} |
---|
[477] | 350 | void from_setting ( const Setting &set ) { |
---|
[527] | 351 | Array<shared_ptr<mpdf> > atmp; //temporary Array |
---|
[507] | 352 | UI::get ( atmp, set, "mpdfs", UI::compulsory ); |
---|
[527] | 353 | set_elements ( atmp ); |
---|
[395] | 354 | } |
---|
[477] | 355 | |
---|
[549] | 356 | private: |
---|
| 357 | // not implemented |
---|
| 358 | mprod ( const mprod & ); |
---|
| 359 | mprod &operator=( const mprod & ); |
---|
[107] | 360 | }; |
---|
[477] | 361 | UIREGISTER ( mprod ); |
---|
[529] | 362 | SHAREDPTR ( mprod ); |
---|
[107] | 363 | |
---|
[168] | 364 | //! Product of independent epdfs. For dependent pdfs, use mprod. |
---|
| 365 | class eprod: public epdf { |
---|
| 366 | protected: |
---|
| 367 | //! Components (epdfs) |
---|
[170] | 368 | Array<const epdf*> epdfs; |
---|
[168] | 369 | //! Array of indeces |
---|
[270] | 370 | Array<datalink*> dls; |
---|
[168] | 371 | public: |
---|
[536] | 372 | //! Default constructor |
---|
[477] | 373 | eprod () : epdfs ( 0 ), dls ( 0 ) {}; |
---|
[536] | 374 | //! Set internal |
---|
[477] | 375 | void set_parameters ( const Array<const epdf*> &epdfs0, bool named = true ) { |
---|
| 376 | epdfs = epdfs0;//.set_length ( epdfs0.length() ); |
---|
[286] | 377 | dls.set_length ( epdfs.length() ); |
---|
| 378 | |
---|
[477] | 379 | bool independent = true; |
---|
[286] | 380 | if ( named ) { |
---|
[477] | 381 | for ( int i = 0; i < epdfs.length(); i++ ) { |
---|
| 382 | independent = rv.add ( epdfs ( i )->_rv() ); |
---|
| 383 | it_assert_debug ( independent == true, "eprod:: given components are not independent." ); |
---|
[286] | 384 | } |
---|
[477] | 385 | dim = rv._dsize(); |
---|
| 386 | } else { |
---|
| 387 | dim = 0; |
---|
| 388 | for ( int i = 0; i < epdfs.length(); i++ ) { |
---|
| 389 | dim += epdfs ( i )->dimension(); |
---|
[286] | 390 | } |
---|
[204] | 391 | } |
---|
[286] | 392 | // |
---|
[477] | 393 | int cumdim = 0; |
---|
| 394 | int dimi = 0; |
---|
[286] | 395 | int i; |
---|
[477] | 396 | for ( i = 0; i < epdfs.length(); i++ ) { |
---|
[286] | 397 | dls ( i ) = new datalink; |
---|
[477] | 398 | if ( named ) { |
---|
| 399 | dls ( i )->set_connection ( epdfs ( i )->_rv() , rv ); |
---|
| 400 | } else { |
---|
[286] | 401 | dimi = epdfs ( i )->dimension(); |
---|
[477] | 402 | dls ( i )->set_connection ( dimi, dim, linspace ( cumdim, cumdim + dimi - 1 ) ); |
---|
| 403 | cumdim += dimi; |
---|
[286] | 404 | } |
---|
| 405 | } |
---|
[168] | 406 | } |
---|
| 407 | |
---|
| 408 | vec mean() const { |
---|
[270] | 409 | vec tmp ( dim ); |
---|
[477] | 410 | for ( int i = 0; i < epdfs.length(); i++ ) { |
---|
[168] | 411 | vec pom = epdfs ( i )->mean(); |
---|
[270] | 412 | dls ( i )->pushup ( tmp, pom ); |
---|
[168] | 413 | } |
---|
| 414 | return tmp; |
---|
| 415 | } |
---|
[229] | 416 | vec variance() const { |
---|
[270] | 417 | vec tmp ( dim ); //second moment |
---|
[477] | 418 | for ( int i = 0; i < epdfs.length(); i++ ) { |
---|
[229] | 419 | vec pom = epdfs ( i )->mean(); |
---|
[477] | 420 | dls ( i )->pushup ( tmp, pow ( pom, 2 ) ); |
---|
[229] | 421 | } |
---|
[477] | 422 | return tmp - pow ( mean(), 2 ); |
---|
[229] | 423 | } |
---|
[168] | 424 | vec sample() const { |
---|
[270] | 425 | vec tmp ( dim ); |
---|
[477] | 426 | for ( int i = 0; i < epdfs.length(); i++ ) { |
---|
[168] | 427 | vec pom = epdfs ( i )->sample(); |
---|
[270] | 428 | dls ( i )->pushup ( tmp, pom ); |
---|
[168] | 429 | } |
---|
| 430 | return tmp; |
---|
| 431 | } |
---|
[211] | 432 | double evallog ( const vec &val ) const { |
---|
[477] | 433 | double tmp = 0; |
---|
| 434 | for ( int i = 0; i < epdfs.length(); i++ ) { |
---|
| 435 | tmp += epdfs ( i )->evallog ( dls ( i )->pushdown ( val ) ); |
---|
[168] | 436 | } |
---|
[477] | 437 | it_assert_debug ( std::isfinite ( tmp ), "Infinite" ); |
---|
[168] | 438 | return tmp; |
---|
| 439 | } |
---|
[170] | 440 | //!access function |
---|
[477] | 441 | const epdf* operator () ( int i ) const { |
---|
| 442 | it_assert_debug ( i < epdfs.length(), "wrong index" ); |
---|
| 443 | return epdfs ( i ); |
---|
| 444 | } |
---|
[204] | 445 | |
---|
[193] | 446 | //!Destructor |
---|
[477] | 447 | ~eprod() { |
---|
| 448 | for ( int i = 0; i < epdfs.length(); i++ ) { |
---|
| 449 | delete dls ( i ); |
---|
| 450 | } |
---|
| 451 | } |
---|
[168] | 452 | }; |
---|
| 453 | |
---|
| 454 | |
---|
[488] | 455 | /*! \brief Mixture of mpdfs with constant weights, all mpdfs are of equal RV and RVC |
---|
[124] | 456 | |
---|
| 457 | */ |
---|
[488] | 458 | class mmix : public mpdf { |
---|
| 459 | protected: |
---|
| 460 | //! Component (mpdfs) |
---|
| 461 | Array<shared_ptr<mpdf> > Coms; |
---|
| 462 | //!weights of the components |
---|
| 463 | vec w; |
---|
| 464 | //! dummy epdfs |
---|
| 465 | epdf dummy_epdf; |
---|
| 466 | public: |
---|
| 467 | //!Default constructor |
---|
| 468 | mmix() : Coms(0), dummy_epdf() { set_ep(dummy_epdf); } |
---|
[461] | 469 | |
---|
[488] | 470 | //! Set weights \c w and components \c R |
---|
| 471 | void set_parameters ( const vec &w0, const Array<shared_ptr<mpdf> > &Coms0 ) { |
---|
[536] | 472 | //!\todo check if all components are OK |
---|
[488] | 473 | Coms = Coms0; |
---|
| 474 | w=w0; |
---|
[503] | 475 | |
---|
[513] | 476 | if (Coms0.length()>0){ |
---|
| 477 | set_rv(Coms(0)->_rv()); |
---|
[515] | 478 | dummy_epdf.set_parameters(Coms(0)->_rv()._dsize()); |
---|
[513] | 479 | set_rvc(Coms(0)->_rvc()); |
---|
| 480 | dimc = rvc._dsize(); |
---|
| 481 | } |
---|
[488] | 482 | } |
---|
| 483 | double evallogcond (const vec &dt, const vec &cond) { |
---|
| 484 | double ll=0.0; |
---|
| 485 | for (int i=0;i<Coms.length();i++){ |
---|
| 486 | ll+=Coms(i)->evallogcond(dt,cond); |
---|
| 487 | } |
---|
| 488 | return ll; |
---|
| 489 | } |
---|
| 490 | |
---|
| 491 | vec samplecond (const vec &cond); |
---|
| 492 | |
---|
| 493 | }; |
---|
| 494 | |
---|
[254] | 495 | } |
---|
[107] | 496 | #endif //MX_H |
---|