[176] | 1 | /*! |
---|
| 2 | \file |
---|
| 3 | \brief Bayesian Filtering for mixtures of exponential family (EF) members |
---|
| 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 MIXTURES_H |
---|
| 14 | #define MIXTURES_H |
---|
[176] | 15 | |
---|
[262] | 16 | |
---|
[384] | 17 | #include "../math/functions.h" |
---|
| 18 | #include "../stat/exp_family.h" |
---|
[394] | 19 | #include "../stat/emix.h" |
---|
[176] | 20 | |
---|
[286] | 21 | namespace bdm { |
---|
[176] | 22 | |
---|
[536] | 23 | //! enum switch for internal approximation used in MixEF |
---|
[189] | 24 | enum MixEF_METHOD { EM = 0, QB = 1}; |
---|
| 25 | |
---|
[176] | 26 | /*! |
---|
| 27 | * \brief Mixture of Exponential Family Densities |
---|
| 28 | |
---|
| 29 | An approximate estimation method for models with latent discrete variable, such as |
---|
| 30 | mixture models of the following kind: |
---|
| 31 | \f[ |
---|
| 32 | f(y_t|\psi_t, \Theta) = \sum_{i=1}^{n} w_i f(y_t|\psi_t, \theta_i) |
---|
| 33 | \f] |
---|
| 34 | where \f$\psi\f$ is a known function of past outputs, \f$w=[w_1,\ldots,w_n]\f$ are component weights, and component parameters \f$\theta_i\f$ are assumed to be mutually independent. \f$\Theta\f$ is an aggregation af all component parameters and weights, i.e. \f$\Theta = [\theta_1,\ldots,\theta_n,w]\f$. |
---|
| 35 | |
---|
| 36 | The characteristic feature of this model is that if the exact values of the latent variable were known, estimation of the parameters can be handled by a single model. For example, for the case of mixture models, posterior density for each component parameters would be a BayesianModel from Exponential Family. |
---|
| 37 | |
---|
[180] | 38 | This class uses EM-style type algorithms for estimation of its parameters. Under this simplification, the posterior density is a product of exponential family members, hence under EM-style approximate estimation this class itself belongs to the exponential family. |
---|
[176] | 39 | |
---|
| 40 | TODO: Extend BM to use rvc. |
---|
| 41 | */ |
---|
[197] | 42 | class MixEF: public BMEF { |
---|
[176] | 43 | protected: |
---|
| 44 | //!Number of components |
---|
| 45 | int n; |
---|
| 46 | //! Models for Components of \f$\theta_i\f$ |
---|
| 47 | Array<BMEF*> Coms; |
---|
| 48 | //! Statistics for weights |
---|
| 49 | multiBM weights; |
---|
| 50 | //!Posterior on component parameters |
---|
| 51 | eprod* est; |
---|
| 52 | ////!Indeces of component rvc in common rvc |
---|
[286] | 53 | |
---|
[189] | 54 | //! Flag for a method that is used in the inference |
---|
| 55 | MixEF_METHOD method; |
---|
[286] | 56 | |
---|
[176] | 57 | //! Auxiliary function for use in constructors |
---|
| 58 | void build_est() { |
---|
[286] | 59 | est = new eprod; |
---|
[477] | 60 | if ( n > 0 ) { |
---|
| 61 | Array<const epdf*> epdfs ( n + 1 ); |
---|
| 62 | for ( int i = 0; i < Coms.length(); i++ ) { |
---|
| 63 | epdfs ( i ) = & ( Coms ( i )->posterior() ); |
---|
[286] | 64 | } |
---|
| 65 | // last in the product is the weight |
---|
[477] | 66 | epdfs ( n ) = & ( weights.posterior() ); |
---|
[286] | 67 | est->set_parameters ( epdfs, false ); |
---|
[176] | 68 | } |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | public: |
---|
| 72 | //! Full constructor |
---|
| 73 | MixEF ( const Array<BMEF*> &Coms0, const vec &alpha0 ) : |
---|
[286] | 74 | BMEF ( ), n ( Coms0.length() ), Coms ( n ), |
---|
| 75 | weights (), method ( QB ) { |
---|
[477] | 76 | for ( int i = 0; i < n; i++ ) { |
---|
| 77 | Coms ( i ) = ( BMEF* ) Coms0 ( i )->_copy_(); |
---|
| 78 | } |
---|
[176] | 79 | build_est(); |
---|
[565] | 80 | } |
---|
| 81 | |
---|
[197] | 82 | //! Constructor of empty mixture |
---|
[176] | 83 | MixEF () : |
---|
[270] | 84 | BMEF ( ), n ( 0 ), Coms ( 0 ), |
---|
[477] | 85 | weights (), method ( QB ) { |
---|
| 86 | build_est(); |
---|
| 87 | } |
---|
[197] | 88 | //! Copy constructor |
---|
[286] | 89 | MixEF ( const MixEF &M2 ) : BMEF ( ), n ( M2.n ), Coms ( n ), |
---|
| 90 | weights ( M2.weights ), method ( M2.method ) { |
---|
[477] | 91 | for ( int i = 0; i < n; i++ ) { |
---|
| 92 | Coms ( i ) = M2.Coms ( i )->_copy_(); |
---|
| 93 | } |
---|
[286] | 94 | build_est(); |
---|
| 95 | } |
---|
[565] | 96 | |
---|
[176] | 97 | //! Initializing the mixture by a random pick of centroids from data |
---|
| 98 | //! \param Com0 Initial component - necessary to determine its type. |
---|
| 99 | //! \param Data Data on which the initialization will be done |
---|
| 100 | //! \param c Initial number of components, default=5 |
---|
[477] | 101 | void init ( BMEF* Com0, const mat &Data, int c = 5 ); |
---|
[176] | 102 | //Destructor |
---|
| 103 | ~MixEF() { |
---|
| 104 | delete est; |
---|
[477] | 105 | for ( int i = 0; i < n; i++ ) { |
---|
| 106 | delete Coms ( i ); |
---|
| 107 | } |
---|
[176] | 108 | } |
---|
| 109 | //! Recursive EM-like algorithm (QB-variant), see Karny et. al, 2006 |
---|
| 110 | void bayes ( const vec &dt ); |
---|
| 111 | //! EM algorithm |
---|
| 112 | void bayes ( const mat &dt ); |
---|
[660] | 113 | //! batch weighted Bayes rule |
---|
[189] | 114 | void bayesB ( const mat &dt, const vec &wData ); |
---|
[176] | 115 | double logpred ( const vec &dt ) const; |
---|
[660] | 116 | //! return correctly typed posterior (covariant return) |
---|
| 117 | const eprod& posterior() const { |
---|
[477] | 118 | return *est; |
---|
| 119 | } |
---|
[270] | 120 | emix* epredictor() const; |
---|
[180] | 121 | //! Flatten the density as if it was not estimated from the data |
---|
[286] | 122 | void flatten ( const BMEF* M2 ); |
---|
[193] | 123 | //! Access function |
---|
[477] | 124 | BMEF* _Coms ( int i ) { |
---|
| 125 | return Coms ( i ); |
---|
| 126 | } |
---|
[286] | 127 | |
---|
[189] | 128 | //!Set which method is to be used |
---|
[477] | 129 | void set_method ( MixEF_METHOD M ) { |
---|
| 130 | method = M; |
---|
| 131 | } |
---|
[176] | 132 | }; |
---|
| 133 | |
---|
[254] | 134 | } |
---|
[384] | 135 | #endif // MIXTURES_H |
---|
[176] | 136 | |
---|