- Main Page
- Related Pages
- Classes
- Files

# work/git/mixpp/bdm/stat/emix.h

Go to the documentation of this file.

    00001
    00013 #ifndef MX_H
    00014 #define MX_H
    00015
    00016 #include "libBM.h"
    00017 #include "libEF.h"
    00018 //#include <std>
    00019
    00020 using namespace itpp;
    00021
    00032 class emix : public epdf {
    00033 protected:
    00035         vec w;
    00037         Array<epdf*> Coms;
    00038 public:
    00040         emix ( RV &rv ) : epdf ( rv ) {};
    00042         void set_parameters ( const vec &w, const Array<epdf*> &Coms );
    00043
    00044         vec sample() const;
    00045         vec mean() const {
    00046                 int i; vec mu = zeros ( rv.count() );
    00047                 for ( i = 0;i < w.length();i++ ) {mu += w ( i ) * Coms ( i )->mean(); }
    00048                 return mu;
    00049         }
    00050         double evalpdflog ( const vec &val ) const {
    00051                 int i;
    00052                 double sum = 0.0;
    00053                 for ( i = 0;i < w.length();i++ ) {sum += w ( i ) * Coms ( i )->evalpdflog ( val );}
    00054                 return log ( sum );
    00055         };
    00056
    00057 //Access methods
    00059         vec& _w() {return w;}
    00060 };
    00061
    00070 class mprod: public compositepdf, public mpdf {
    00071 protected:
    00072         // pointers to epdfs
    00073         Array<epdf*> epdfs;
    00075         Array<ivec> rvcinds;
    00076 public:
    00080         mprod ( Array<mpdf*> mFacs): compositepdf(mFacs), mpdf(getrv(true),RV()), epdfs(n), rvcinds(n)
    00081         {       setrvc(rv,rvc);
    00082                 setrvcinrv(rvc,rvcinds);
    00083                 setindices(rv);
    00084         for(int i=0;i<n;i++){epdfs(i)=&(mpdfs(i)->_epdf());}
    00085         };
    00086
    00087         double evalpdflog ( const vec &val ) const {
    00088                 int i;
    00089                 double res = 0.0;
    00090                 for ( i = n - 1;i > 0;i++ ) {
    00091                         if ( rvcinds ( i ).length() > 0 )
    00092                                 {mpdfs ( i )->condition ( val ( rvcinds ( i ) ) );}
    00093                         // add logarithms
    00094                         res += epdfs ( i )->evalpdflog ( val ( rvsinrv ( i ) ) );
    00095                 }
    00096                 return res;
    00097         }
    00098         vec samplecond ( const vec &cond, double &ll ) {
    00099                 vec smp=zeros ( rv.count() );
    00100                 vec condi;
    00101                 vec smpi;
    00102                 ll = 0;
    00103                 for ( int i = ( n - 1 );i >= 0;i-- ) {
    00104                         if ( rvcinds ( i ).length() > 0 ) {
    00105                                 condi = zeros ( rvcsinrv.length() + rvcinds.length() );
    00106                                 // copy data in condition
    00107                                 set_subvector ( condi,rvcinds ( i ), cond );
    00108                                 // copy data in already generated sample
    00109                                 set_subvector ( condi,rvcsinrv ( i ), smp );
    00110
    00111                                 mpdfs ( i )->condition ( condi );
    00112                         }
    00113                         smpi = epdfs ( i )->sample();
    00114                         // copy contribution of this pdf into smp
    00115                         set_subvector ( smp,rvsinrv ( i ), smpi );
    00116                         // add ith likelihood contribution
    00117                         ll+=epdfs ( i )->evalpdflog ( smpi );
    00118                 }
    00119                 return smp;
    00120         }
    00121         mat samplecond ( const vec &cond, vec &ll, int N ) {
    00122                 mat Smp ( rv.count(),N );
    00123                 for ( int i=0;i<N;i++ ) {Smp.set_col ( i,samplecond ( cond,ll ( i ) ) );}
    00124                 return Smp;
    00125         }
    00126
    00127         ~mprod() {};
    00128 };
    00129
    00131 class eprod: public epdf {
    00132 protected:
    00134         Array<const epdf*> epdfs;
    00136         Array<ivec> rvinds;
    00137 public:
    00138         eprod ( const Array<const epdf*> epdfs0 ) : epdf ( RV() ),epdfs ( epdfs0 ),rvinds ( epdfs.length() ) {
    00139                 bool independent=true;
    00140                 for ( int i=0;i<epdfs.length();i++ ) {
    00141                         independent=rv.add ( epdfs ( i )->_rv() );
    00142                         it_assert_debug ( independent==true, "eprod:: given components are not independent ." );
    00143                         rvinds ( i ) = ( epdfs ( i )->_rv() ).dataind ( rv );
    00144                 }
    00145         }
    00146
    00147         vec mean() const {
    00148                 vec tmp ( rv.count() );
    00149                 for ( int i=0;i<epdfs.length();i++ ) {
    00150                         vec pom = epdfs ( i )->mean();
    00151                         set_subvector ( tmp,rvinds ( i ), pom );
    00152                 }
    00153                 return tmp;
    00154         }
    00155         vec sample() const {
    00156                 vec tmp ( rv.count() );
    00157                 for ( int i=0;i<epdfs.length();i++ ) {
    00158                         vec pom = epdfs ( i )->sample();
    00159                         set_subvector ( tmp,rvinds ( i ), pom );
    00160                 }
    00161                 return tmp;
    00162         }
    00163         double evalpdflog ( const vec &val ) const {
    00164                 double tmp=0;
    00165                 for ( int i=0;i<epdfs.length();i++ ) {
    00166                         tmp+=epdfs(i)->evalpdflog(val(rvinds(i)));
    00167                 }
    00168                 return tmp;
    00169         }
    00171         const epdf* operator () (int i) const {it_assert_debug(i<epdfs.length(),"wrong index");return epdfs(i);}
    00172 };
    00173
    00174
    00178 class mmix : public mpdf {
    00179 protected:
    00181         Array<mpdf*> Coms;
    00183         emix Epdf;
    00184 public:
    00186         mmix ( RV &rv, RV &rvc ) : mpdf ( rv, rvc ), Epdf ( rv ) {ep = &Epdf;};
    00188         void set_parameters ( const vec &w, const Array<mpdf*> &Coms ) {
    00189                 Array<epdf*> Eps ( Coms.length() );
    00190
    00191                 for ( int i = 0;i < Coms.length();i++ ) {
    00192                         Eps ( i ) = & ( Coms ( i )->_epdf() );
    00193                 }
    00194                 Epdf.set_parameters ( w, Eps );
    00195         };
    00196
    00197         void condition ( const vec &cond ) {
    00198                 for ( int i = 0;i < Coms.length();i++ ) {Coms ( i )->condition ( cond );}
    00199         };
    00200 };
    00201 #endif //MX_H