root/bdm/stat/emix.h @ 178

Revision 178, 5.4 kB (checked in by smidl, 16 years ago)

Changes in basic structures! new methods

  • 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;
38        //!Flag if owning Coms
39        bool destroyComs;
40public:
41        //!Default constructor
42        emix (const RV &rv ) : epdf ( rv ) {};
43        //! Set weights \c w and components \c Coms , Coms are not copied!
44        void set_parameters ( const vec &w, const Array<epdf*> &Coms, bool copy=true );
45
46        vec sample() const;
47        vec mean() const {
48                int i; vec mu = zeros ( rv.count() );
49                for ( i = 0;i < w.length();i++ ) {mu += w ( i ) * Coms ( i )->mean(); }
50                return mu;
51        }
52        double evalpdflog ( const vec &val ) const {
53                int i;
54                double sum = 0.0;
55                for ( i = 0;i < w.length();i++ ) {sum += w ( i ) * Coms ( i )->evalpdflog ( val );}
56                return log ( sum );
57        };
58
59//Access methods
60        //! returns a pointer to the internal mean value. Use with Care!
61        vec& _w() {return w;}
62        virtual ~emix(){if (destroyComs){for(int i=0;i<Coms.length();i++){delete Coms(i);}}}
63        //! Auxiliary function for taking ownership of the Coms()
64        void ownComs(){destroyComs=true;}
65};
66
67/*! \brief Chain rule decomposition of epdf
68
69Probability density in the form of Chain-rule decomposition:
70\[
71f(x_1,x_2,x_3) = f(x_1|x_2,x_3)f(x_2,x_3)f(x_3)
72\]
73Note that
74*/
75class mprod: public compositepdf, public mpdf {
76protected:
77        // pointers to epdfs
78        Array<epdf*> epdfs;
79        //! Indeces of rvc in common rvc
80        Array<ivec> rvcinds;
81public:
82        /*!\brief Constructor from list of mFacs,
83        Additional parameter overlap is left for future use. Do not set to true for mprod.
84        */
85        mprod ( Array<mpdf*> mFacs): compositepdf(mFacs), mpdf(getrv(true),RV()), epdfs(n), rvcinds(n)
86        {       setrvc(rv,rvc);
87                setrvcinrv(rvc,rvcinds);
88                setindices(rv); 
89        for(int i=0;i<n;i++){epdfs(i)=&(mpdfs(i)->_epdf());}
90        };
91
92        double evalpdflog ( const vec &val ) const {
93                int i;
94                double res = 0.0;
95                for ( i = n - 1;i > 0;i++ ) {
96                        if ( rvcinds ( i ).length() > 0 )
97                                {mpdfs ( i )->condition ( val ( rvcinds ( i ) ) );}
98                        // add logarithms
99                        res += epdfs ( i )->evalpdflog ( val ( rvsinrv ( i ) ) );
100                }
101                return res;
102        }
103        vec samplecond ( const vec &cond, double &ll ) {
104                vec smp=zeros ( rv.count() );
105                vec condi;
106                vec smpi;
107                ll = 0;
108                for ( int i = ( n - 1 );i >= 0;i-- ) {
109                        if (( rvcinds ( i ).length() > 0 )||( rvcsinrv ( i ).length() > 0 )) {
110                                condi = zeros ( rvcsinrv(i).length() + rvcinds(i).length() );
111                                // copy data in condition
112                                set_subvector ( condi,rvcinds ( i ), cond );
113                                // copy data in already generated sample
114                                set_subvector ( condi,rvinrvcs ( i ), get_vec(smp,rvcsinrv(i)) );
115
116                                mpdfs ( i )->condition ( condi );
117                        }
118                        smpi = epdfs ( i )->sample();
119                        // copy contribution of this pdf into smp
120                        set_subvector ( smp,rvsinrv ( i ), smpi );
121                        // add ith likelihood contribution
122                        ll+=epdfs ( i )->evalpdflog ( smpi );
123                }
124                return smp;
125        }
126        mat samplecond ( const vec &cond, vec &ll, int N ) {
127                mat Smp ( rv.count(),N );
128                for ( int i=0;i<N;i++ ) {Smp.set_col ( i,samplecond ( cond,ll ( i ) ) );}
129                return Smp;
130        }
131
132        ~mprod() {};
133};
134
135//! Product of independent epdfs. For dependent pdfs, use mprod.
136class eprod: public epdf {
137protected:
138        //! Components (epdfs)
139        Array<const epdf*> epdfs;
140        //! Array of indeces
141        Array<ivec> rvinds;
142public:
143        eprod ( const Array<const epdf*> epdfs0 ) : epdf ( RV() ),epdfs ( epdfs0 ),rvinds ( epdfs.length() ) {
144                bool independent=true;
145                for ( int i=0;i<epdfs.length();i++ ) {
146                        independent=rv.add ( epdfs ( i )->_rv() );
147                        it_assert_debug ( independent==true, "eprod:: given components are not independent ." );
148                        rvinds ( i ) = ( epdfs ( i )->_rv() ).dataind ( rv );
149                }
150        }
151
152        vec mean() const {
153                vec tmp ( rv.count() );
154                for ( int i=0;i<epdfs.length();i++ ) {
155                        vec pom = epdfs ( i )->mean();
156                        set_subvector ( tmp,rvinds ( i ), pom );
157                }
158                return tmp;
159        }
160        vec sample() const {
161                vec tmp ( rv.count() );
162                for ( int i=0;i<epdfs.length();i++ ) {
163                        vec pom = epdfs ( i )->sample();
164                        set_subvector ( tmp,rvinds ( i ), pom );
165                }
166                return tmp;
167        }
168        double evalpdflog ( const vec &val ) const {
169                double tmp=0;
170                for ( int i=0;i<epdfs.length();i++ ) {
171                        tmp+=epdfs(i)->evalpdflog(val(rvinds(i)));
172                }
173                return tmp;
174        }
175        //!access function
176        const epdf* operator () (int i) const {it_assert_debug(i<epdfs.length(),"wrong index");return epdfs(i);}
177};
178
179
180/*! \brief Mixture of mpdfs with constant weights, all mpdfs are of equal type
181
182*/
183class mmix : public mpdf {
184protected:
185        //! Component (epdfs)
186        Array<mpdf*> Coms;
187        //!Internal epdf
188        emix Epdf;
189public:
190        //!Default constructor
191        mmix ( RV &rv, RV &rvc ) : mpdf ( rv, rvc ), Epdf ( rv ) {ep = &Epdf;};
192        //! Set weights \c w and components \c R
193        void set_parameters ( const vec &w, const Array<mpdf*> &Coms ) {
194                Array<epdf*> Eps ( Coms.length() );
195
196                for ( int i = 0;i < Coms.length();i++ ) {
197                        Eps ( i ) = & ( Coms ( i )->_epdf() );
198                }
199                Epdf.set_parameters ( w, Eps );
200        };
201
202        void condition ( const vec &cond ) {
203                for ( int i = 0;i < Coms.length();i++ ) {Coms ( i )->condition ( cond );}
204        };
205};
206#endif //MX_H
Note: See TracBrowser for help on using the browser.