- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

# 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 namespace bdm{
    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 ( ), dl ( rv,rvc,nom0->_rv() ) {
    00050                 ep->set_rv(_rv());
    00051                 set_rvc(nom0->_rv().subt ( rv ) );
    00052                 if ( copy ) {
    00053 //                      nom = nom0->_copy_();
    00054                         it_error ( "todo" );
    00055                         destroynom=true;
    00056                 }
    00057                 else {
    00058                         nom = nom0;
    00059                         destroynom = false;
    00060                 }
    00061                 it_assert_debug ( rvc.length() >0,"Makes no sense to use this object!" );
    00062                 den = nom->marginal ( rvc );
    00063         };
    00064         double evallogcond ( const vec &val, const vec &cond ) {
    00065                 double tmp;
    00066                 vec nom_val ( ep->dimension() + dimc );
    00067                 dl.pushup_cond ( nom_val,val,cond );
    00068                 tmp = exp ( nom->evallog ( nom_val ) - den->evallog ( cond ) );
    00069                 it_assert_debug(std::isfinite(tmp),"Infinite value");
    00070                 return tmp;
    00071         }
    00073         void ownnom() {destroynom=true;}
    00075         ~mratio() {delete den; if ( destroynom ) {delete nom;}}
    00076 };
    00077
    00088 class emix : public epdf {
    00089 protected:
    00091         vec w;
    00093         Array<epdf*> Coms;
    00095         bool destroyComs;
    00096 public:
    00098         emix (  ) : epdf ( ) {};
    00101         void set_parameters ( const vec &w, const Array<epdf*> &Coms, bool copy=true );
    00102
    00103         vec sample() const;
    00104         vec mean() const {
    00105                 int i; vec mu = zeros ( dim);
    00106                 for ( i = 0;i < w.length();i++ ) {mu += w ( i ) * Coms ( i )->mean(); }
    00107                 return mu;
    00108         }
    00109         vec variance() const {
    00110                 //non-central moment
    00111                 vec mom2 = zeros(dim);
    00112                 for ( int i = 0;i < w.length();i++ ) {mom2 += w ( i ) * pow(Coms ( i )->mean(),2); }
    00113                 //central moment
    00114                 return mom2-pow(mean(),2);
    00115         }
    00116         double evallog ( const vec &val ) const {
    00117                 int i;
    00118                 double sum = 0.0;
    00119                 for ( i = 0;i < w.length();i++ ) {sum += w ( i ) * exp ( Coms ( i )->evallog ( val ) );}
    00120                 if (sum==0.0){sum=std::numeric_limits<double>::epsilon();}
    00121                 double tmp=log ( sum );
    00122                 it_assert_debug(std::isfinite(tmp),"Infinite");
    00123                 return tmp;
    00124         };
    00125         vec evallog_m ( const mat &Val ) const {
    00126                 vec x=zeros ( Val.cols() );
    00127                 for ( int i = 0; i < w.length(); i++ ) {
    00128                         x+= w ( i ) *exp ( Coms ( i )->evallog_m ( Val ) );
    00129                 }
    00130                 return log ( x );
    00131         };
    00133         mat evallog_M ( const mat &Val ) const {
    00134                 mat X ( w.length(), Val.cols() );
    00135                 for ( int i = 0; i < w.length(); i++ ) {
    00136                         X.set_row ( i, w ( i ) *exp ( Coms ( i )->evallog_m ( Val ) ) );
    00137                 }
    00138                 return X;
    00139         };
    00140
    00141         emix* marginal ( const RV &rv ) const;
    00142         mratio* condition ( const RV &rv ) const; //why not mratio!!
    00143
    00144 //Access methods
    00146         vec& _w() {return w;}
    00147         virtual ~emix() {if ( destroyComs ) {for ( int i=0;i<Coms.length();i++ ) {delete Coms ( i );}}}
    00149         void ownComs() {destroyComs=true;}
    00150
    00152         epdf* _Coms ( int i ) {return Coms ( i );}
    00153 };
    00154
    00163 class mprod: public compositepdf, public mpdf {
    00164 protected:
    00166         Array<epdf*> epdfs;
    00168         Array<datalink_m2m*> dls;
    00169 public:
    00172         mprod ( Array<mpdf*> mFacs ) : compositepdf ( mFacs ), mpdf (), epdfs ( n ), dls ( n ) {
    00173 //              setrvc ( ep->_rv(),_rvc() );
    00174                 // rv and rvc established = > we can link them with mpdfs
    00175                 for ( int i = 0;i < n;i++ ) {
    00176                         dls ( i ) = new datalink_m2m ( mpdfs ( i )->_rv(), mpdfs ( i )->_rvc(), _rv(), _rvc() );
    00177                 }
    00178
    00179                 for ( int i=0;i<n;i++ ) {
    00180                         epdfs ( i ) =& ( mpdfs ( i )->_epdf() );
    00181                 }
    00182         };
    00183
    00184         double evallogcond ( const vec &val, const vec &cond ) {
    00185                 int i;
    00186                 double res = 1.0;
    00187                 for ( i = n - 1;i >= 0;i-- ) {
    00188                         /*                      if ( mpdfs(i)->_rvc().count() >0) {
    00189                                                         mpdfs ( i )->condition ( dls ( i )->get_cond ( val,cond ) );
    00190                                                 }
    00191                                                 // add logarithms
    00192                                                 res += epdfs ( i )->evallog ( dls ( i )->pushdown ( val ) );*/
    00193                         res *= mpdfs ( i )->evallogcond (
    00194                                    dls ( i )->pushdown ( val ),
    00195                                    dls ( i )->get_cond ( val, cond )
    00196                                );
    00197                 }
    00198                 return res;
    00199         }
    00200         vec samplecond ( const vec &cond, double &ll ) {
    00202                 vec smp= std::numeric_limits<double>::infinity() * ones ( ep->dimension() );
    00203                 vec smpi;
    00204                 ll = 0;
    00205                 // Hard assumption here!!! We are going backwards, to assure that samples that are needed from smp are already generated!
    00206                 for ( int i = ( n - 1 );i >= 0;i-- ) {
    00207                         if ( mpdfs ( i )->dimensionc() ) {
    00208                                 mpdfs ( i )->condition ( dls ( i )->get_cond ( smp ,cond ) ); // smp is val here!!
    00209                         }
    00210                         smpi = epdfs ( i )->sample();
    00211                         // copy contribution of this pdf into smp
    00212                         dls ( i )->pushup ( smp, smpi );
    00213                         // add ith likelihood contribution
    00214                         ll+=epdfs ( i )->evallog ( smpi );
    00215                 }
    00216                 return smp;
    00217         }
    00218         mat samplecond ( const vec &cond, vec &ll, int N ) {
    00219                 mat Smp ( dimension(),N );
    00220                 for ( int i=0;i<N;i++ ) {Smp.set_col ( i,samplecond ( cond,ll ( i ) ) );}
    00221                 return Smp;
    00222         }
    00223
    00224         ~mprod() {};
    00225 };
    00226
    00228 class eprod: public epdf {
    00229 protected:
    00231         Array<const epdf*> epdfs;
    00233         Array<datalink*> dls;
    00234 public:
    00235         eprod ( const Array<const epdf*> epdfs0 ) : epdf (  ),epdfs ( epdfs0 ),dls ( epdfs.length() ) {
    00236                 bool independent=true;
    00237                 for ( int i=0;i<epdfs.length();i++ ) {
    00238                         independent=rv.add ( epdfs ( i )->_rv() );
    00239                         it_assert_debug ( independent==true, "eprod:: given components are not independent ." );
    00240                 }
    00241                 for ( int i=0;i<epdfs.length();i++ ) {
    00242                         dls ( i ) = new datalink ( epdfs ( i )->_rv() , rv );
    00243                 }
    00244         }
    00245
    00246         vec mean() const {
    00247                 vec tmp ( dim );
    00248                 for ( int i=0;i<epdfs.length();i++ ) {
    00249                         vec pom = epdfs ( i )->mean();
    00250                         dls ( i )->pushup ( tmp, pom );
    00251                 }
    00252                 return tmp;
    00253         }
    00254         vec variance() const {
    00255                 vec tmp ( dim ); //second moment
    00256                 for ( int i=0;i<epdfs.length();i++ ) {
    00257                         vec pom = epdfs ( i )->mean();
    00258                         dls ( i )->pushup ( tmp, pow(pom,2) );
    00259                 }
    00260                 return tmp-pow(mean(),2);
    00261         }
    00262         vec sample() const {
    00263                 vec tmp ( dim );
    00264                 for ( int i=0;i<epdfs.length();i++ ) {
    00265                         vec pom = epdfs ( i )->sample();
    00266                         dls ( i )->pushup ( tmp, pom );
    00267                 }
    00268                 return tmp;
    00269         }
    00270         double evallog ( const vec &val ) const {
    00271                 double tmp=0;
    00272                 for ( int i=0;i<epdfs.length();i++ ) {
    00273                         tmp+=epdfs ( i )->evallog ( dls ( i )->pushdown ( val ) );
    00274                 }
    00275                 it_assert_debug(std::isfinite(tmp),"Infinite");
    00276                 return tmp;
    00277         }
    00279         const epdf* operator () ( int i ) const {it_assert_debug ( i<epdfs.length(),"wrong index" );return epdfs ( i );}
    00280
    00282         ~eprod() {for ( int i=0;i<epdfs.length();i++ ) {delete dls ( i );}}
    00283 };
    00284
    00285
    00289 class mmix : public mpdf {
    00290 protected:
    00292         Array<mpdf*> Coms;
    00294         emix Epdf;
    00295 public:
    00297         mmix ( ) : mpdf ( ), Epdf () {ep = &Epdf;};
    00299         void set_parameters ( const vec &w, const Array<mpdf*> &Coms ) {
    00300                 Array<epdf*> Eps ( Coms.length() );
    00301
    00302                 for ( int i = 0;i < Coms.length();i++ ) {
    00303                         Eps ( i ) = & ( Coms ( i )->_epdf() );
    00304                 }
    00305                 Epdf.set_parameters ( w, Eps );
    00306         };
    00307
    00308         void condition ( const vec &cond ) {
    00309                 for ( int i = 0;i < Coms.length();i++ ) {Coms ( i )->condition ( cond );}
    00310         };
    00311 };
    00312
    00313 }
    00314 #endif //MX_H