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

Revision 1064, 7.0 kB (checked in by mido, 14 years ago)

astyle applied all over the library

  • 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
41Two methods are provided:
42 - QB where the probability of being from one component is computed using predictors,
43 - EM where the data is assigned to component with the highest likelihood (winner takes all)
44
45These methods are stored in attribute options:
46 - method: ["EM","QB"] estimation method as mentioned above, QB is default
47 - max_niter: maximum of iterations in bayes_batch()
48
49*/
50class MixEF: public BMEF {
51protected:
52    //! Models for Components of \f$\theta_i\f$
53    Array<BMEF*> Coms;
54    //! Statistics for weights
55    multiBM weights;
56    //aux
57    friend class eprod_mix;
58    //!Posterior on component parameters
59    class eprod_mix: public eprod_base {
60    protected:
61        const MixEF &mix; // pointer to parent.n
62    public:
63        eprod_mix(const MixEF &m):mix(m) {}
64        const epdf* factor(int i) const {
65            return (i==(mix.Coms.length()-1)) ? &mix.weights.posterior() : &mix.Coms(i)->posterior();
66        }
67        const int no_factors()const {
68            return mix.Coms.length()+1;
69        }
70    } est;
71    ////!indices of component rvc in common rvc
72
73    class MixEF_options: public root {
74    public:
75        //! Flag for a method that is used in the inference
76        MixEF_METHOD method;
77
78        //! maximum number of iterations
79        int max_niter;
80
81        MixEF_options():method(QB),max_niter(10) {};
82
83        //! Settings for MixEF
84        /*!
85        \code
86        method = "EM";               % or QB (default)
87        max_iter = 10;               % maximum number of iterations
88        \endcode
89        */
90        void from_setting(const Setting &set) {
91            string meth;
92            UI::get(meth,set,"method",UI::optional);
93            if (meth=="EM") {
94                method=EM;
95            }
96            max_niter =10;
97            UI::get(max_niter,set,"max_niter",UI::optional);
98        };
99        void to_setting(Setting &set)const {
100            string meth=(method==EM ? "EM" : "QB");
101            UI::save(meth,set,"method");
102            UI::save(max_niter,set,"max_niter");
103        };
104    };
105
106    MixEF_options options;
107public:
108    //! Full constructor
109    MixEF ( const Array<BMEF*> &Coms0, const vec &alpha0 ) :
110        BMEF ( ), Coms ( Coms0.length() ),
111        weights (), est(*this), options() {
112        for ( int i = 0; i < Coms0.length(); i++ ) {
113            Coms ( i ) = ( BMEF* ) Coms0 ( i )->_copy();
114        }
115        weights.set_parameters(alpha0);
116        weights.validate();
117    }
118
119    //! Constructor of empty mixture
120    MixEF () :
121        BMEF ( ), Coms ( 0 ),
122        weights (), est(*this), options() {
123    }
124    //! Copy constructor
125    MixEF ( const MixEF &M2 ) : BMEF ( ),  Coms ( M2.Coms.length() ),
126        weights ( M2.weights ), est(*this), options(M2.options) {
127        for ( int i = 0; i < M2.Coms.length(); i++ ) {
128            Coms ( i ) = (BMEF*) M2.Coms ( i )->_copy();
129        }
130    }
131
132    //! Initializing the mixture by a random pick of centroids from data
133    //! \param Com0 Initial component - necessary to determine its type.
134    //! \param Data Data on which the initialization will be done
135    //! \param c Initial number of components, default=5
136    //! when the number of data records (columns of Data) is equal to the number of requested components, each data is used, otherwise, they are picked randomly.
137    void init ( BMEF* Com0, const mat &Data, const int c = 5 );
138    //Destructor
139    //! Recursive EM-like algorithm (QB-variant), see Karny et. al, 2006
140    void bayes ( const vec &yt, const vec &cond );
141    //! batch weighted Bayes rule
142    double bayes_batch_weighted ( const mat &yt, const mat &cond, const vec &wData );
143    double bayes_batch ( const mat &yt, const mat &cond) {
144        return bayes_batch_weighted(yt,cond,ones(yt.cols()));
145    };
146    double logpred ( const vec &yt, const vec &cond ) const;
147    //! return correctly typed posterior (covariant return)
148    const eprod_mix& posterior() const {
149        return est;
150    }
151
152    emix* epredictor(const vec &cond=vec()) const;
153    //! Flatten the density as if it was not estimated from the data
154    void flatten ( const BMEF* M2, double weight );
155    //! Access function
156    BMEF* _Coms ( int i ) {
157        return Coms ( i );
158    }
159
160    //!Set which method is to be used
161    void set_method ( MixEF_METHOD M ) {
162        options.method = M;
163    }
164
165    void to_setting ( Setting &set ) const {
166        BMEF::to_setting( set );
167        UI::save ( Coms, set, "Coms" );
168        UI::save ( &weights, set, "weights" );
169        UI::save (options, set, "options");
170    }
171    /*! \brief reads data from setting
172    \code
173    Coms = {struct('class',"BMEF..."),...};           % Components - Bayesian models (BM)
174    weights = [0.2, 0.3,...];                         % weights
175    options.method = "EM" or "QB";                    % methods of computing bayes
176    options.max_iter = 10;                            % maximum number of iterations in bayes_batch
177    \endcode
178    */
179    void from_setting (const  Setting &set ) {
180        BMEF::from_setting( set );
181        UI::get ( Coms, set, "Coms" );
182        UI::get ( weights, set, "weights" );
183        UI::get (options, set, "options",UI::optional);
184    }
185};
186UIREGISTER ( MixEF );
187
188class ARXprod: public ProdBMBase {
189    Array<shared_ptr<ARX> > arxs;
190public:
191    ARX* bm(int i) {
192        return arxs(i).get();
193    }
194    int no_bms() {
195        return arxs.length();
196    }
197};
198UIREGISTER(ARXprod);
199
200}
201#endif // MIXTURES_H
Note: See TracBrowser for help on using the browser.