- 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 mpdf {
    00071 protected:
    00072         //
    00073         int n;
    00074         // pointers to epdfs
    00075         Array<epdf*> epdfs;
    00076         Array<mpdf*> mpdfs;
    00077         //
    00079         Array<ivec> rvinds;
    00081         Array<ivec> rvcinrv;
    00083         Array<ivec> rvcinds;
    00085         bool independent;
    00086 //      //! Indicate internal creation of mpdfs which must be destroyed
    00087 //      bool intermpdfs;
    00088 public:
    00090         mprod ( Array<mpdf*> mFacs ) : mpdf ( RV(), RV() ), n ( mFacs.length() ), epdfs ( n ), mpdfs ( mFacs ), rvinds ( n ), rvcinrv ( n ), rvcinds ( n ) {
    00091                 int i;
    00092                 // Create rv
    00093                 for ( i = 0;i < n;i++ ) {
    00094                         rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs.
    00095                         epdfs ( i ) = & ( mpdfs ( i )->_epdf() ); // add pointer to epdf
    00096                 };
    00097                 // Create rvc
    00098                 for ( i = 0;i < n;i++ ) {
    00099                         rvc.add ( mpdfs ( i )->_rv().subt ( rv ) ); //add rv to common rvs.
    00100                 };
    00101
    00102                 independent = true;
    00103                 //test rvc of mpdfs and fill rvinds
    00104                 for ( i = 0;i < n;i++ ) {
    00105                         // find ith rv in common rv
    00106                         rvinds ( i ) = mpdfs ( i )->_rv().dataind ( rv );
    00107                         // find ith rvc in common rv
    00108                         rvcinrv ( i ) = mpdfs ( i )->_rvc().dataind ( rv );
    00109                         // find ith rvc in common rv
    00110                         rvcinds ( i ) = mpdfs ( i )->_rvc().dataind ( rvc );
    00111                         //
    00112                         if ( rvcinds ( i ).length() >0 ) {independent = false;}
    00113                         if ( rvcinds ( i ).length() >0 ) {independent = false;}
    00114                 }
    00115         };
    00116
    00117         double evalpdflog ( const vec &val ) const {
    00118                 int i;
    00119                 double res = 0.0;
    00120                 for ( i = n - 1;i > 0;i++ ) {
    00121                         if ( rvcinds ( i ).length() > 0 )
    00122                                 {mpdfs ( i )->condition ( val ( rvcinds ( i ) ) );}
    00123                         // add logarithms
    00124                         res += epdfs ( i )->evalpdflog ( val ( rvinds ( i ) ) );
    00125                 }
    00126                 return res;
    00127         }
    00128         vec samplecond ( const vec &cond, vec &ll ) const {
    00129                 vec smp=zeros ( rv.count() );
    00130                 vec condi;
    00131                 for ( int i = ( n - 1 );i >= 0;i-- ) {
    00132                         if ( rvcinds ( i ).length() > 0 ) {
    00133                                 condi = zeros ( rvcinrv.length() + rvcinds.length() );
    00134                                 // copy data in condition
    00135                                 set_subvector ( condi,rvcinds ( i ), cond );
    00136                                 // copy data in already generated sample
    00137                                 set_subvector ( condi,rvcinrv ( i ), smp );
    00138
    00139                                 mpdfs ( i )->condition ( condi );
    00140                         }
    00141                         // copy contribution of this pdf into smp
    00142                         set_subvector ( smp,rvinds ( i ), epdfs ( i )->sample() );
    00143                 }
    00144                 return smp;
    00145         }
    00146 //      vec mean() const {
    00147 //              vec tmp ( rv.count() );
    00148 //              if ( independent ) {
    00149 //                      for ( int i=0;i<n;i++ ) {
    00150 //                              vec pom = epdfs ( i )->mean();
    00151 //                              set_subvector ( tmp,rvinds ( i ), pom );
    00152 //                      }
    00153 //              }
    00154 //              else {
    00155 //                      int N=50*rv.count();
    00156 //                      it_warning ( "eprod.mean() computed by sampling" );
    00157 //                      tmp = zeros ( rv.count() );
    00158 //                      for ( int i=0;i<N;i++ ) { tmp += sample();}
    00159 //                      tmp /=N;
    00160 //              }
    00161 //              return tmp;
    00162 //      }
    00163
    00164         ~mprod() {};
    00165 };
    00166
    00170 class mmix : public mpdf {
    00171 protected:
    00173         Array<mpdf*> Coms;
    00175         emix Epdf;
    00176 public:
    00178         mmix ( RV &rv, RV &rvc ) : mpdf ( rv, rvc ), Epdf ( rv ) {ep = &Epdf;};
    00180         void set_parameters ( const vec &w, const Array<mpdf*> &Coms ) {
    00181                 Array<epdf*> Eps ( Coms.length() );
    00182
    00183                 for ( int i = 0;i < Coms.length();i++ ) {
    00184                         Eps ( i ) = & ( Coms ( i )->_epdf() );
    00185                 }
    00186                 Epdf.set_parameters ( w, Eps );
    00187         };
    00188
    00189         void condition ( const vec &cond ) {
    00190                 for ( int i = 0;i < Coms.length();i++ ) {Coms ( i )->condition ( cond );}
    00191         };
    00192 };
    00193 #endif //MX_H