[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 | } |
---|
[550] | 93 | |
---|
| 94 | private: |
---|
| 95 | // not implemented |
---|
| 96 | mratio ( const mratio & ); |
---|
| 97 | mratio &operator=( const mratio & ); |
---|
[182] | 98 | }; |
---|
| 99 | |
---|
[107] | 100 | /*! |
---|
| 101 | * \brief Mixture of epdfs |
---|
| 102 | |
---|
| 103 | Density function: |
---|
| 104 | \f[ |
---|
| 105 | f(x) = \sum_{i=1}^{n} w_{i} f_i(x), \quad \sum_{i=1}^n w_i = 1. |
---|
| 106 | \f] |
---|
| 107 | where \f$f_i(x)\f$ is any density on random variable \f$x\f$, called \a component, |
---|
| 108 | |
---|
| 109 | */ |
---|
[145] | 110 | class emix : public epdf { |
---|
[162] | 111 | protected: |
---|
| 112 | //! weights of the components |
---|
| 113 | vec w; |
---|
| 114 | //! Component (epdfs) |
---|
[504] | 115 | Array<shared_ptr<epdf> > Coms; |
---|
| 116 | |
---|
[162] | 117 | public: |
---|
| 118 | //!Default constructor |
---|
[286] | 119 | emix ( ) : epdf ( ) {}; |
---|
[182] | 120 | //! Set weights \c w and components \c Coms |
---|
[224] | 121 | //!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] | 122 | void set_parameters ( const vec &w, const Array<shared_ptr<epdf> > &Coms ); |
---|
[107] | 123 | |
---|
[162] | 124 | vec sample() const; |
---|
| 125 | vec mean() const { |
---|
[477] | 126 | int i; |
---|
| 127 | vec mu = zeros ( dim ); |
---|
| 128 | for ( i = 0; i < w.length(); i++ ) { |
---|
| 129 | mu += w ( i ) * Coms ( i )->mean(); |
---|
| 130 | } |
---|
[162] | 131 | return mu; |
---|
| 132 | } |
---|
[229] | 133 | vec variance() const { |
---|
| 134 | //non-central moment |
---|
[286] | 135 | vec mom2 = zeros ( dim ); |
---|
[477] | 136 | for ( int i = 0; i < w.length(); i++ ) { |
---|
| 137 | mom2 += w ( i ) * ( Coms ( i )->variance() + pow ( Coms ( i )->mean(), 2 ) ); |
---|
| 138 | } |
---|
[229] | 139 | //central moment |
---|
[477] | 140 | return mom2 - pow ( mean(), 2 ); |
---|
[229] | 141 | } |
---|
[211] | 142 | double evallog ( const vec &val ) const { |
---|
[162] | 143 | int i; |
---|
| 144 | double sum = 0.0; |
---|
[477] | 145 | for ( i = 0; i < w.length(); i++ ) { |
---|
| 146 | sum += w ( i ) * exp ( Coms ( i )->evallog ( val ) ); |
---|
| 147 | } |
---|
| 148 | if ( sum == 0.0 ) { |
---|
| 149 | sum = std::numeric_limits<double>::epsilon(); |
---|
| 150 | } |
---|
| 151 | double tmp = log ( sum ); |
---|
| 152 | it_assert_debug ( std::isfinite ( tmp ), "Infinite" ); |
---|
[214] | 153 | return tmp; |
---|
[162] | 154 | }; |
---|
[211] | 155 | vec evallog_m ( const mat &Val ) const { |
---|
[477] | 156 | vec x = zeros ( Val.cols() ); |
---|
[192] | 157 | for ( int i = 0; i < w.length(); i++ ) { |
---|
[477] | 158 | x += w ( i ) * exp ( Coms ( i )->evallog_m ( Val ) ); |
---|
[182] | 159 | } |
---|
[192] | 160 | return log ( x ); |
---|
[182] | 161 | }; |
---|
[214] | 162 | //! Auxiliary function that returns pdflog for each component |
---|
[211] | 163 | mat evallog_M ( const mat &Val ) const { |
---|
[192] | 164 | mat X ( w.length(), Val.cols() ); |
---|
| 165 | for ( int i = 0; i < w.length(); i++ ) { |
---|
[211] | 166 | X.set_row ( i, w ( i ) *exp ( Coms ( i )->evallog_m ( Val ) ) ); |
---|
[189] | 167 | } |
---|
| 168 | return X; |
---|
| 169 | }; |
---|
[107] | 170 | |
---|
[504] | 171 | shared_ptr<epdf> marginal ( const RV &rv ) const; |
---|
[536] | 172 | //! Update already existing marginal density \c target |
---|
[504] | 173 | void marginal ( const RV &rv, emix &target ) const; |
---|
| 174 | shared_ptr<mpdf> condition ( const RV &rv ) const; |
---|
[182] | 175 | |
---|
[107] | 176 | //Access methods |
---|
[162] | 177 | //! returns a pointer to the internal mean value. Use with Care! |
---|
[477] | 178 | vec& _w() { |
---|
| 179 | return w; |
---|
| 180 | } |
---|
[204] | 181 | |
---|
[193] | 182 | //!access function |
---|
[504] | 183 | shared_ptr<epdf> _Coms ( int i ) { |
---|
[477] | 184 | return Coms ( i ); |
---|
[286] | 185 | } |
---|
[504] | 186 | |
---|
[477] | 187 | void set_rv ( const RV &rv ) { |
---|
| 188 | epdf::set_rv ( rv ); |
---|
| 189 | for ( int i = 0; i < Coms.length(); i++ ) { |
---|
| 190 | Coms ( i )->set_rv ( rv ); |
---|
| 191 | } |
---|
| 192 | } |
---|
[107] | 193 | }; |
---|
[529] | 194 | SHAREDPTR( emix ); |
---|
[107] | 195 | |
---|
[333] | 196 | /*! |
---|
| 197 | * \brief Mixture of egiws |
---|
| 198 | |
---|
| 199 | */ |
---|
| 200 | class egiwmix : public egiw { |
---|
| 201 | protected: |
---|
| 202 | //! weights of the components |
---|
| 203 | vec w; |
---|
| 204 | //! Component (epdfs) |
---|
| 205 | Array<egiw*> Coms; |
---|
| 206 | //!Flag if owning Coms |
---|
| 207 | bool destroyComs; |
---|
| 208 | public: |
---|
| 209 | //!Default constructor |
---|
| 210 | egiwmix ( ) : egiw ( ) {}; |
---|
| 211 | |
---|
| 212 | //! Set weights \c w and components \c Coms |
---|
| 213 | //!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] | 214 | void set_parameters ( const vec &w, const Array<egiw*> &Coms, bool copy = false ); |
---|
[333] | 215 | |
---|
| 216 | //!return expected value |
---|
| 217 | vec mean() const; |
---|
| 218 | |
---|
| 219 | //!return a sample from the density |
---|
| 220 | vec sample() const; |
---|
| 221 | |
---|
| 222 | //!return the expected variance |
---|
[477] | 223 | vec variance() const; |
---|
[333] | 224 | |
---|
| 225 | // TODO!!! Defined to follow ANSI and/or for future development |
---|
| 226 | void mean_mat ( mat &M, mat&R ) const {}; |
---|
[477] | 227 | double evallog_nn ( const vec &val ) const { |
---|
| 228 | return 0; |
---|
| 229 | }; |
---|
| 230 | double lognc () const { |
---|
| 231 | return 0; |
---|
[504] | 232 | } |
---|
[333] | 233 | |
---|
[504] | 234 | shared_ptr<epdf> marginal ( const RV &rv ) const; |
---|
| 235 | void marginal ( const RV &rv, emix &target ) const; |
---|
| 236 | |
---|
[333] | 237 | //Access methods |
---|
| 238 | //! returns a pointer to the internal mean value. Use with Care! |
---|
[477] | 239 | vec& _w() { |
---|
| 240 | return w; |
---|
| 241 | } |
---|
| 242 | virtual ~egiwmix() { |
---|
| 243 | if ( destroyComs ) { |
---|
| 244 | for ( int i = 0; i < Coms.length(); i++ ) { |
---|
| 245 | delete Coms ( i ); |
---|
| 246 | } |
---|
| 247 | } |
---|
| 248 | } |
---|
[333] | 249 | //! Auxiliary function for taking ownership of the Coms() |
---|
[477] | 250 | void ownComs() { |
---|
| 251 | destroyComs = true; |
---|
| 252 | } |
---|
[333] | 253 | |
---|
| 254 | //!access function |
---|
[477] | 255 | egiw* _Coms ( int i ) { |
---|
| 256 | return Coms ( i ); |
---|
| 257 | } |
---|
[333] | 258 | |
---|
[477] | 259 | void set_rv ( const RV &rv ) { |
---|
| 260 | egiw::set_rv ( rv ); |
---|
| 261 | for ( int i = 0; i < Coms.length(); i++ ) { |
---|
| 262 | Coms ( i )->set_rv ( rv ); |
---|
| 263 | } |
---|
[333] | 264 | } |
---|
| 265 | |
---|
| 266 | //! Approximation of a GiW mix by a single GiW pdf |
---|
| 267 | egiw* approx(); |
---|
| 268 | }; |
---|
| 269 | |
---|
[115] | 270 | /*! \brief Chain rule decomposition of epdf |
---|
| 271 | |
---|
[145] | 272 | Probability density in the form of Chain-rule decomposition: |
---|
| 273 | \[ |
---|
| 274 | f(x_1,x_2,x_3) = f(x_1|x_2,x_3)f(x_2,x_3)f(x_3) |
---|
| 275 | \] |
---|
| 276 | Note that |
---|
[115] | 277 | */ |
---|
[507] | 278 | class mprod: public mpdf { |
---|
| 279 | private: |
---|
| 280 | Array<shared_ptr<mpdf> > mpdfs; |
---|
| 281 | |
---|
[192] | 282 | //! Data link for each mpdfs |
---|
[546] | 283 | Array<shared_ptr<datalink_m2m> > dls; |
---|
[461] | 284 | |
---|
[546] | 285 | protected: |
---|
[487] | 286 | //! dummy epdf used only as storage for RV and dim |
---|
| 287 | epdf iepdf; |
---|
[461] | 288 | |
---|
[162] | 289 | public: |
---|
[507] | 290 | //! \brief Default constructor |
---|
| 291 | mprod() { } |
---|
| 292 | |
---|
| 293 | /*!\brief Constructor from list of mFacs |
---|
[165] | 294 | */ |
---|
[507] | 295 | mprod ( const Array<shared_ptr<mpdf> > &mFacs ) { |
---|
[477] | 296 | set_elements ( mFacs ); |
---|
[461] | 297 | } |
---|
[536] | 298 | //! Set internal \c mpdfs from given values |
---|
[507] | 299 | void set_elements (const Array<shared_ptr<mpdf> > &mFacs ); |
---|
[477] | 300 | |
---|
[211] | 301 | double evallogcond ( const vec &val, const vec &cond ) { |
---|
[162] | 302 | int i; |
---|
[270] | 303 | double res = 0.0; |
---|
[477] | 304 | for ( i = mpdfs.length() - 1; i >= 0; i-- ) { |
---|
[193] | 305 | /* if ( mpdfs(i)->_rvc().count() >0) { |
---|
| 306 | mpdfs ( i )->condition ( dls ( i )->get_cond ( val,cond ) ); |
---|
| 307 | } |
---|
| 308 | // add logarithms |
---|
[270] | 309 | res += epdfs ( i )->evallog ( dls ( i )->pushdown ( val ) );*/ |
---|
| 310 | res += mpdfs ( i )->evallogcond ( |
---|
| 311 | dls ( i )->pushdown ( val ), |
---|
[204] | 312 | dls ( i )->get_cond ( val, cond ) |
---|
| 313 | ); |
---|
[145] | 314 | } |
---|
[193] | 315 | return res; |
---|
[162] | 316 | } |
---|
[477] | 317 | vec evallogcond_m ( const mat &Dt, const vec &cond ) { |
---|
| 318 | vec tmp ( Dt.cols() ); |
---|
| 319 | for ( int i = 0; i < Dt.cols(); i++ ) { |
---|
| 320 | tmp ( i ) = evallogcond ( Dt.get_col ( i ), cond ); |
---|
[395] | 321 | } |
---|
| 322 | return tmp; |
---|
| 323 | }; |
---|
[477] | 324 | vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ) { |
---|
| 325 | vec tmp ( Dt.length() ); |
---|
| 326 | for ( int i = 0; i < Dt.length(); i++ ) { |
---|
| 327 | tmp ( i ) = evallogcond ( Dt ( i ), cond ); |
---|
[395] | 328 | } |
---|
[477] | 329 | return tmp; |
---|
[395] | 330 | }; |
---|
| 331 | |
---|
| 332 | |
---|
[270] | 333 | //TODO smarter... |
---|
| 334 | vec samplecond ( const vec &cond ) { |
---|
[477] | 335 | //! Ugly hack to help to discover if mpfs are not in proper order. Correct solution = check that explicitely. |
---|
[487] | 336 | vec smp = std::numeric_limits<double>::infinity() * ones ( dimension() ); |
---|
[165] | 337 | vec smpi; |
---|
[192] | 338 | // Hard assumption here!!! We are going backwards, to assure that samples that are needed from smp are already generated! |
---|
[477] | 339 | for ( int i = ( mpdfs.length() - 1 ); i >= 0; i-- ) { |
---|
[487] | 340 | // generate contribution of this mpdf |
---|
| 341 | smpi = mpdfs(i)->samplecond(dls ( i )->get_cond ( smp , cond )); |
---|
[162] | 342 | // copy contribution of this pdf into smp |
---|
[270] | 343 | dls ( i )->pushup ( smp, smpi ); |
---|
[145] | 344 | } |
---|
[162] | 345 | return smp; |
---|
| 346 | } |
---|
| 347 | |
---|
[395] | 348 | //! Load from structure with elements: |
---|
| 349 | //! \code |
---|
| 350 | //! { class='mprod'; |
---|
| 351 | //! mpdfs = (..., ...); // list of mpdfs in the order of chain rule |
---|
| 352 | //! } |
---|
| 353 | //! \endcode |
---|
| 354 | //!@} |
---|
[477] | 355 | void from_setting ( const Setting &set ) { |
---|
[527] | 356 | Array<shared_ptr<mpdf> > atmp; //temporary Array |
---|
[507] | 357 | UI::get ( atmp, set, "mpdfs", UI::compulsory ); |
---|
[527] | 358 | set_elements ( atmp ); |
---|
[395] | 359 | } |
---|
[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 |
---|