root/library/bdm/estim/mixtures.h @ 1004

Revision 1004, 4.6 kB (checked in by smidl, 14 years ago)

mixtures do not have n

  • Property svn:eol-style set to native
Line 
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
13#ifndef MIXTURES_H
14#define MIXTURES_H
15
16
17#include "../math/functions.h"
18#include "../stat/exp_family.h"
19#include "../stat/emix.h"
20#include "arx.h"
21
22namespace bdm {
23
24//! enum switch for internal approximation used in MixEF
25enum MixEF_METHOD { EM = 0, QB = 1};
26
27/*!
28* \brief Mixture of Exponential Family Densities
29
30An approximate estimation method for models with latent discrete variable, such as
31mixture models of the following kind:
32\f[
33f(y_t|\psi_t, \Theta) = \sum_{i=1}^{n} w_i f(y_t|\psi_t, \theta_i)
34\f]
35where  \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$.
36
37The 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.
38
39This 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.
40
41TODO: Extend BM to use rvc.
42*/
43class MixEF: public BMEF {
44protected:
45        //! Models for Components of \f$\theta_i\f$
46        Array<BMEF*> Coms;
47        //! Statistics for weights
48        multiBM weights;
49        //aux
50        friend class eprod_mix;
51        //!Posterior on component parameters
52        class eprod_mix: public eprod_base {
53                protected:
54                const MixEF &mix; // pointer to parent.n
55                public:
56                eprod_mix(const MixEF &m):mix(m){}
57                const epdf* factor(int i) const {return (i==(mix.Coms.length()-1)) ? &mix.weights.posterior() : &mix.Coms(i)->posterior();}
58                const int no_factors()const {return mix.Coms.length()+1;}
59        } est;
60        ////!indices of component rvc in common rvc
61
62        //! Flag for a method that is used in the inference
63        MixEF_METHOD method;
64
65public:
66        //! Full constructor
67        MixEF ( const Array<BMEF*> &Coms0, const vec &alpha0 ) :
68                        BMEF ( ), Coms ( Coms0.length() ),
69                        weights (), est(*this), method ( QB ) {
70                for ( int i = 0; i < Coms0.length(); i++ ) {
71                        Coms ( i ) = ( BMEF* ) Coms0 ( i )->_copy();
72                }
73                weights.set_parameters(alpha0);
74                weights.validate();
75        }
76
77        //! Constructor of empty mixture
78        MixEF () :
79                        BMEF ( ), Coms ( 0 ),
80                        weights (), est(*this), method ( QB ) {
81        }
82        //! Copy constructor
83        MixEF ( const MixEF &M2 ) : BMEF ( ),  Coms ( M2.Coms.length() ),
84                        weights ( M2.weights ), est(*this), method ( M2.method ) {
85                for ( int i = 0; i < M2.Coms.length(); i++ ) {
86                        Coms ( i ) = (BMEF*) M2.Coms ( i )->_copy();
87                }
88        }
89
90        //! Initializing the mixture by a random pick of centroids from data
91        //! \param Com0 Initial component - necessary to determine its type.
92        //! \param Data Data on which the initialization will be done
93        //! \param c Initial number of components, default=5
94        void init ( BMEF* Com0, const mat &Data, const int c = 5 );
95        //Destructor
96        //! Recursive EM-like algorithm (QB-variant), see Karny et. al, 2006
97        void bayes ( const vec &yt, const vec &cond );
98        //! EM algorithm
99        void bayes ( const mat &yt, const vec &cond );
100        //! batch weighted Bayes rule
101        void bayes_batch ( const mat &yt, const mat &cond, const vec &wData );
102        double logpred ( const vec &yt ) const;
103        //! return correctly typed posterior (covariant return)
104        const eprod_mix& posterior() const {
105                return est;
106        }
107
108        emix* epredictor(const vec &cond=vec()) const;
109        //! Flatten the density as if it was not estimated from the data
110        void flatten ( const BMEF* M2 );
111        //! Access function
112        BMEF* _Coms ( int i ) {
113                return Coms ( i );
114        }
115
116        //!Set which method is to be used
117        void set_method ( MixEF_METHOD M ) {
118                method = M;
119        }
120
121void to_setting ( Setting &set ) const {
122        BMEF::to_setting( set );
123        UI::save ( Coms, set, "Coms" );
124        UI::save ( &weights, set, "weights" );
125}
126/*! \brief reads data from setting
127*/
128void from_setting (const  Setting &set ) {
129        BMEF::from_setting( set );
130        UI::get ( Coms, set, "Coms" );
131        UI::get ( weights, set, "weights" );
132}
133};
134UIREGISTER ( MixEF );
135
136class ARXprod: public ProdBMBase {
137        Array<shared_ptr<ARX> > arxs;
138        public:
139                ARX* bm(int i){return arxs(i).get();}
140                int no_bms() {return arxs.length();}
141};
142UIREGISTER(ARXprod);
143
144}
145#endif // MIXTURES_H
Note: See TracBrowser for help on using the browser.