- Main Page
- Related Pages
- Modules
- 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
    00022 //this comes first because it is used inside emix!
    00023
    00036 class mratio: public mpdf {
    00037 protected:
    00039         const epdf* nom;
    00041         epdf* den;
    00043         bool destroynom;
    00045         datalink_m2e dl;
    00046 public:
    00049         mratio ( const epdf* nom0, const RV &rv, bool copy=false ) :mpdf ( rv,nom0->_rv().subt ( rv ) ), dl ( rv,rvc,nom0->_rv() ) {
    00050                 if ( copy ) {
    00051 //                      nom = nom0->_copy_();
    00052                         it_error ( "todo" );
    00053                         destroynom=true;
    00054                 }
    00055                 else {
    00056                         nom = nom0;
    00057                         destroynom = false;
    00058                 }
    00059                 it_assert_debug ( rvc.length() >0,"Makes no sense to use this object!" );
    00060                 den = nom->marginal ( rvc );
    00061         };
    00062         double evallogcond ( const vec &val, const vec &cond ) {
    00063                 double tmp;
    00064                 vec nom_val ( rv.count() +rvc.count() );
    00065                 dl.fill_val_cond ( nom_val,val,cond );
    00066                 tmp = exp ( nom->evallog ( nom_val ) - den->evallog ( cond ) );
    00067                 it_assert_debug(std::isfinite(tmp),"Infinite value");
    00068                 return tmp;
    00069         }
    00071         void ownnom() {destroynom=true;}
    00073         ~mratio() {delete den; if ( destroynom ) {delete nom;}}
    00074 };
    00075
    00086 class emix : public epdf {
    00087 protected:
    00089         vec w;
    00091         Array<epdf*> Coms;
    00093         bool destroyComs;
    00094 public:
    00096         emix ( const RV &rv ) : epdf ( rv ) {};
    00099         void set_parameters ( const vec &w, const Array<epdf*> &Coms, bool copy=true );
    00100
    00101         vec sample() const;
    00102         vec mean() const {
    00103                 int i; vec mu = zeros ( rv.count() );
    00104                 for ( i = 0;i < w.length();i++ ) {mu += w ( i ) * Coms ( i )->mean(); }
    00105                 return mu;
    00106         }
    00107         double evallog ( const vec &val ) const {
    00108                 int i;
    00109                 double sum = 0.0;
    00110                 for ( i = 0;i < w.length();i++ ) {sum += w ( i ) * exp ( Coms ( i )->evallog ( val ) );}
    00111                 if (sum==0.0){sum=std::numeric_limits<double>::epsilon();}
    00112                 double tmp=log ( sum );
    00113                 it_assert_debug(std::isfinite(tmp),"Infinite");
    00114                 return tmp;
    00115         };
    00116         vec evallog_m ( const mat &Val ) const {
    00117                 vec x=zeros ( Val.cols() );
    00118                 for ( int i = 0; i < w.length(); i++ ) {
    00119                         x+= w ( i ) *exp ( Coms ( i )->evallog_m ( Val ) );
    00120                 }
    00121                 return log ( x );
    00122         };
    00124         mat evallog_M ( const mat &Val ) const {
    00125                 mat X ( w.length(), Val.cols() );
    00126                 for ( int i = 0; i < w.length(); i++ ) {
    00127                         X.set_row ( i, w ( i ) *exp ( Coms ( i )->evallog_m ( Val ) ) );
    00128                 }
    00129                 return X;
    00130         };
    00131
    00132         emix* marginal ( const RV &rv ) const;
    00133         mratio* condition ( const RV &rv ) const; //why not mratio!!
    00134
    00135 //Access methods
    00137         vec& _w() {return w;}
    00138         virtual ~emix() {if ( destroyComs ) {for ( int i=0;i<Coms.length();i++ ) {delete Coms ( i );}}}
    00140         void ownComs() {destroyComs=true;}
    00141
    00143         epdf* _Coms ( int i ) {return Coms ( i );}
    00144 };
    00145
    00154 class mprod: public compositepdf, public mpdf {
    00155 protected:
    00157         Array<epdf*> epdfs;
    00159         Array<datalink_m2m*> dls;
    00160 public:
    00163         mprod ( Array<mpdf*> mFacs ) : compositepdf ( mFacs ), mpdf ( getrv ( true ),RV() ), epdfs ( n ), dls ( n ) {
    00164                 setrvc ( rv,rvc );
    00165                 // rv and rvc established = > we can link them with mpdfs
    00166                 for ( int i = 0;i < n;i++ ) {
    00167                         dls ( i ) = new datalink_m2m ( mpdfs ( i )->_rv(), mpdfs ( i )->_rvc(), rv, rvc );
    00168                 }
    00169
    00170                 for ( int i=0;i<n;i++ ) {
    00171                         epdfs ( i ) =& ( mpdfs ( i )->_epdf() );
    00172                 }
    00173         };
    00174
    00175         double evallogcond ( const vec &val, const vec &cond ) {
    00176                 int i;
    00177                 double res = 1.0;
    00178                 for ( i = n - 1;i >= 0;i-- ) {
    00179                         /*                      if ( mpdfs(i)->_rvc().count() >0) {
    00180                                                         mpdfs ( i )->condition ( dls ( i )->get_cond ( val,cond ) );
    00181                                                 }
    00182                                                 // add logarithms
    00183                                                 res += epdfs ( i )->evallog ( dls ( i )->get_val ( val ) );*/
    00184                         res *= mpdfs ( i )->evallogcond (
    00185                                    dls ( i )->get_val ( val ),
    00186                                    dls ( i )->get_cond ( val, cond )
    00187                                );
    00188                 }
    00189                 return res;
    00190         }
    00191         vec samplecond ( const vec &cond, double &ll ) {
    00193                 vec smp= std::numeric_limits<double>::infinity() * ones ( rv.count() );
    00194                 vec smpi;
    00195                 ll = 0;
    00196                 // Hard assumption here!!! We are going backwards, to assure that samples that are needed from smp are already generated!
    00197                 for ( int i = ( n - 1 );i >= 0;i-- ) {
    00198                         if ( mpdfs ( i )->_rvc().count() ) {
    00199                                 mpdfs ( i )->condition ( dls ( i )->get_cond ( smp ,cond ) ); // smp is val here!!
    00200                         }
    00201                         smpi = epdfs ( i )->sample();
    00202                         // copy contribution of this pdf into smp
    00203                         dls ( i )->fill_val ( smp, smpi );
    00204                         // add ith likelihood contribution
    00205                         ll+=epdfs ( i )->evallog ( smpi );
    00206                 }
    00207                 return smp;
    00208         }
    00209         mat samplecond ( const vec &cond, vec &ll, int N ) {
    00210                 mat Smp ( rv.count(),N );
    00211                 for ( int i=0;i<N;i++ ) {Smp.set_col ( i,samplecond ( cond,ll ( i ) ) );}
    00212                 return Smp;
    00213         }
    00214
    00215         ~mprod() {};
    00216 };
    00217
    00219 class eprod: public epdf {
    00220 protected:
    00222         Array<const epdf*> epdfs;
    00224         Array<datalink_e2e*> dls;
    00225 public:
    00226         eprod ( const Array<const epdf*> epdfs0 ) : epdf ( RV() ),epdfs ( epdfs0 ),dls ( epdfs.length() ) {
    00227                 bool independent=true;
    00228                 for ( int i=0;i<epdfs.length();i++ ) {
    00229                         independent=rv.add ( epdfs ( i )->_rv() );
    00230                         it_assert_debug ( independent==true, "eprod:: given components are not independent ." );
    00231                 }
    00232                 for ( int i=0;i<epdfs.length();i++ ) {
    00233                         dls ( i ) = new datalink_e2e ( epdfs ( i )->_rv() , rv );
    00234                 }
    00235         }
    00236
    00237         vec mean() const {
    00238                 vec tmp ( rv.count() );
    00239                 for ( int i=0;i<epdfs.length();i++ ) {
    00240                         vec pom = epdfs ( i )->mean();
    00241                         dls ( i )->fill_val ( tmp, pom );
    00242                 }
    00243                 return tmp;
    00244         }
    00245         vec sample() const {
    00246                 vec tmp ( rv.count() );
    00247                 for ( int i=0;i<epdfs.length();i++ ) {
    00248                         vec pom = epdfs ( i )->sample();
    00249                         dls ( i )->fill_val ( tmp, pom );
    00250                 }
    00251                 return tmp;
    00252         }
    00253         double evallog ( const vec &val ) const {
    00254                 double tmp=0;
    00255                 for ( int i=0;i<epdfs.length();i++ ) {
    00256                         tmp+=epdfs ( i )->evallog ( dls ( i )->get_val ( val ) );
    00257                 }
    00258                 it_assert_debug(std::isfinite(tmp),"Infinite");
    00259                 return tmp;
    00260         }
    00262         const epdf* operator () ( int i ) const {it_assert_debug ( i<epdfs.length(),"wrong index" );return epdfs ( i );}
    00263
    00265         ~eprod() {for ( int i=0;i<epdfs.length();i++ ) {delete dls ( i );}}
    00266 };
    00267
    00268
    00272 class mmix : public mpdf {
    00273 protected:
    00275         Array<mpdf*> Coms;
    00277         emix Epdf;
    00278 public:
    00280         mmix ( RV &rv, RV &rvc ) : mpdf ( rv, rvc ), Epdf ( rv ) {ep = &Epdf;};
    00282         void set_parameters ( const vec &w, const Array<mpdf*> &Coms ) {
    00283                 Array<epdf*> Eps ( Coms.length() );
    00284
    00285                 for ( int i = 0;i < Coms.length();i++ ) {
    00286                         Eps ( i ) = & ( Coms ( i )->_epdf() );
    00287                 }
    00288                 Epdf.set_parameters ( w, Eps );
    00289         };
    00290
    00291         void condition ( const vec &cond ) {
    00292                 for ( int i = 0;i < Coms.length();i++ ) {Coms ( i )->condition ( cond );}
    00293         };
    00294 };
    00295
    00296 #endif //MX_H