root/bdm/stat/emix.h @ 178

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

Changes in basic structures! new methods

  • Property svn:eol-style set to native
RevLine 
[107]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*/
[145]32class emix : public epdf {
[162]33protected:
34        //! weights of the components
35        vec w;
36        //! Component (epdfs)
37        Array<epdf*> Coms;
[178]38        //!Flag if owning Coms
39        bool destroyComs;
[162]40public:
41        //!Default constructor
[178]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 );
[107]45
[162]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        };
[107]58
59//Access methods
[162]60        //! returns a pointer to the internal mean value. Use with Care!
61        vec& _w() {return w;}
[178]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;}
[107]65};
66
[115]67/*! \brief Chain rule decomposition of epdf
68
[145]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
[115]74*/
[175]75class mprod: public compositepdf, public mpdf {
[162]76protected:
77        // pointers to epdfs
78        Array<epdf*> epdfs;
79        //! Indeces of rvc in common rvc
80        Array<ivec> rvcinds;
81public:
[168]82        /*!\brief Constructor from list of mFacs,
[165]83        Additional parameter overlap is left for future use. Do not set to true for mprod.
84        */
[176]85        mprod ( Array<mpdf*> mFacs): compositepdf(mFacs), mpdf(getrv(true),RV()), epdfs(n), rvcinds(n)
[175]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        };
[124]91
[162]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
[175]99                        res += epdfs ( i )->evalpdflog ( val ( rvsinrv ( i ) ) );
[145]100                }
[162]101                return res;
102        }
103        vec samplecond ( const vec &cond, double &ll ) {
104                vec smp=zeros ( rv.count() );
105                vec condi;
[165]106                vec smpi;
107                ll = 0;
[162]108                for ( int i = ( n - 1 );i >= 0;i-- ) {
[178]109                        if (( rvcinds ( i ).length() > 0 )||( rvcsinrv ( i ).length() > 0 )) {
110                                condi = zeros ( rvcsinrv(i).length() + rvcinds(i).length() );
[162]111                                // copy data in condition
112                                set_subvector ( condi,rvcinds ( i ), cond );
113                                // copy data in already generated sample
[178]114                                set_subvector ( condi,rvinrvcs ( i ), get_vec(smp,rvcsinrv(i)) );
[162]115
116                                mpdfs ( i )->condition ( condi );
[145]117                        }
[165]118                        smpi = epdfs ( i )->sample();
[162]119                        // copy contribution of this pdf into smp
[175]120                        set_subvector ( smp,rvsinrv ( i ), smpi );
[165]121                        // add ith likelihood contribution
[168]122                        ll+=epdfs ( i )->evalpdflog ( smpi );
[145]123                }
[162]124                return smp;
125        }
126        mat samplecond ( const vec &cond, vec &ll, int N ) {
[168]127                mat Smp ( rv.count(),N );
128                for ( int i=0;i<N;i++ ) {Smp.set_col ( i,samplecond ( cond,ll ( i ) ) );}
[162]129                return Smp;
130        }
131
132        ~mprod() {};
[107]133};
134
[168]135//! Product of independent epdfs. For dependent pdfs, use mprod.
136class eprod: public epdf {
137protected:
138        //! Components (epdfs)
[170]139        Array<const epdf*> epdfs;
[168]140        //! Array of indeces
141        Array<ivec> rvinds;
142public:
[170]143        eprod ( const Array<const epdf*> epdfs0 ) : epdf ( RV() ),epdfs ( epdfs0 ),rvinds ( epdfs.length() ) {
[168]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        }
[170]175        //!access function
176        const epdf* operator () (int i) const {it_assert_debug(i<epdfs.length(),"wrong index");return epdfs(i);}
[168]177};
178
179
[145]180/*! \brief Mixture of mpdfs with constant weights, all mpdfs are of equal type
[124]181
182*/
[145]183class mmix : public mpdf {
[162]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() );
[124]195
[162]196                for ( int i = 0;i < Coms.length();i++ ) {
197                        Eps ( i ) = & ( Coms ( i )->_epdf() );
198                }
199                Epdf.set_parameters ( w, Eps );
200        };
[124]201
[162]202        void condition ( const vec &cond ) {
203                for ( int i = 0;i < Coms.length();i++ ) {Coms ( i )->condition ( cond );}
204        };
[124]205};
[107]206#endif //MX_H
Note: See TracBrowser for help on using the browser.