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

Revision 943, 4.3 kB (checked in by smidl, 14 years ago)

syntax of epredictor

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