root/bdm/stat/emix.h @ 165

Revision 165, 4.4 kB (checked in by smidl, 16 years ago)

Switch from eprod to mprod, merger devel

  • Property svn:eol-style set to native
Line 
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
13#ifndef MX_H
14#define MX_H
15
16#include "libBM.h"
17#include "libEF.h"
18//#include <std>
19
20using namespace itpp;
21
22/*!
23* \brief Mixture of epdfs
24
25Density function:
26\f[
27f(x) = \sum_{i=1}^{n} w_{i} f_i(x), \quad \sum_{i=1}^n w_i = 1.
28\f]
29where \f$f_i(x)\f$ is any density on random variable \f$x\f$, called \a component,
30
31*/
32class emix : public epdf {
33protected:
34        //! weights of the components
35        vec w;
36        //! Component (epdfs)
37        Array<epdf*> Coms;
38public:
39        //!Default constructor
40        emix ( RV &rv ) : epdf ( rv ) {};
41        //! Set weights \c w and components \c R
42        void set_parameters ( const vec &w, const Array<epdf*> &Coms );
43
44        vec sample() const;
45        vec mean() const {
46                int i; vec mu = zeros ( rv.count() );
47                for ( i = 0;i < w.length();i++ ) {mu += w ( i ) * Coms ( i )->mean(); }
48                return mu;
49        }
50        double evalpdflog ( const vec &val ) const {
51                int i;
52                double sum = 0.0;
53                for ( i = 0;i < w.length();i++ ) {sum += w ( i ) * Coms ( i )->evalpdflog ( val );}
54                return log ( sum );
55        };
56
57//Access methods
58        //! returns a pointer to the internal mean value. Use with Care!
59        vec& _w() {return w;}
60};
61
62/*! \brief Chain rule decomposition of epdf
63
64Probability density in the form of Chain-rule decomposition:
65\[
66f(x_1,x_2,x_3) = f(x_1|x_2,x_3)f(x_2,x_3)f(x_3)
67\]
68Note that
69*/
70class mprod: public mpdf {
71protected:
72        //
73        int n;
74        // pointers to epdfs
75        Array<epdf*> epdfs;
76        Array<mpdf*> mpdfs;
77        //
78        //! Indeces of rvs in common rv
79        Array<ivec> rvinds;
80        //! Indeces of rvc in common rv
81        Array<ivec> rvcinrv;
82        //! Indeces of rvc in common rvc
83        Array<ivec> rvcinds;
84        //! Indicate independence of its factors
85        bool independent;
86//      //! Indicate internal creation of mpdfs which must be destroyed
87//      bool intermpdfs;
88public:
89        /*!\brief Constructor from list of mFacs,
90        Additional parameter overlap is left for future use. Do not set to true for mprod.
91        */
92        mprod ( Array<mpdf*> mFacs, bool overlap=false ); 
93
94        double evalpdflog ( const vec &val ) const {
95                int i;
96                double res = 0.0;
97                for ( i = n - 1;i > 0;i++ ) {
98                        if ( rvcinds ( i ).length() > 0 )
99                                {mpdfs ( i )->condition ( val ( rvcinds ( i ) ) );}
100                        // add logarithms
101                        res += epdfs ( i )->evalpdflog ( val ( rvinds ( i ) ) );
102                }
103                return res;
104        }
105        vec samplecond ( const vec &cond, double &ll ) {
106                vec smp=zeros ( rv.count() );
107                vec condi;
108                vec smpi;
109                ll = 0;
110                for ( int i = ( n - 1 );i >= 0;i-- ) {
111                        if ( rvcinds ( i ).length() > 0 ) {
112                                condi = zeros ( rvcinrv.length() + rvcinds.length() );
113                                // copy data in condition
114                                set_subvector ( condi,rvcinds ( i ), cond );
115                                // copy data in already generated sample
116                                set_subvector ( condi,rvcinrv ( i ), smp );
117
118                                mpdfs ( i )->condition ( condi );
119                        }
120                        smpi = epdfs ( i )->sample();
121                        // copy contribution of this pdf into smp
122                        set_subvector ( smp,rvinds ( i ), smpi );
123                        // add ith likelihood contribution
124                        ll+=epdfs(i)->evalpdflog(smpi);
125                }
126                return smp;
127        }
128        mat samplecond ( const vec &cond, vec &ll, int N ) {
129                mat Smp(rv.count(),N);
130                for(int i=0;i<N;i++){Smp.set_col(i,samplecond(cond,ll(i)));}
131                return Smp;
132        }
133//      vec mean() const {
134//              vec tmp ( rv.count() );
135//              if ( independent ) {
136//                      for ( int i=0;i<n;i++ ) {
137//                              vec pom = epdfs ( i )->mean();
138//                              set_subvector ( tmp,rvinds ( i ), pom );
139//                      }
140//              }
141//              else {
142//                      int N=50*rv.count();
143//                      it_warning ( "eprod.mean() computed by sampling" );
144//                      tmp = zeros ( rv.count() );
145//                      for ( int i=0;i<N;i++ ) { tmp += sample();}
146//                      tmp /=N;
147//              }
148//              return tmp;
149//      }
150
151        ~mprod() {};
152};
153
154/*! \brief Mixture of mpdfs with constant weights, all mpdfs are of equal type
155
156*/
157class mmix : public mpdf {
158protected:
159        //! Component (epdfs)
160        Array<mpdf*> Coms;
161        //!Internal epdf
162        emix Epdf;
163public:
164        //!Default constructor
165        mmix ( RV &rv, RV &rvc ) : mpdf ( rv, rvc ), Epdf ( rv ) {ep = &Epdf;};
166        //! Set weights \c w and components \c R
167        void set_parameters ( const vec &w, const Array<mpdf*> &Coms ) {
168                Array<epdf*> Eps ( Coms.length() );
169
170                for ( int i = 0;i < Coms.length();i++ ) {
171                        Eps ( i ) = & ( Coms ( i )->_epdf() );
172                }
173                Epdf.set_parameters ( w, Eps );
174        };
175
176        void condition ( const vec &cond ) {
177                for ( int i = 0;i < Coms.length();i++ ) {Coms ( i )->condition ( cond );}
178        };
179};
180#endif //MX_H
Note: See TracBrowser for help on using the browser.