Changeset 192 for bdm/stat/emix.h

Show
Ignore:
Timestamp:
10/22/08 10:46:40 (16 years ago)
Author:
smidl
Message:

modification of datalinks and switch mprod and merger to use them

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/emix.h

    r189 r192  
    3535 */ 
    3636class mratio: public mpdf { 
    37         protected: 
     37protected: 
    3838        //! Nominator in the form of mpdf 
    39                 const epdf* nom; 
     39        const epdf* nom; 
    4040        //!Denominator in the form of epdf 
    41                 epdf* den; 
    42         //!flag for destructor  
    43                 bool destroynom; 
    44         public: 
    45         //!Default constructor. By default, the given epdf is not copied!  
     41        epdf* den; 
     42        //!flag for destructor 
     43        bool destroynom; 
     44public: 
     45        //!Default constructor. By default, the given epdf is not copied! 
    4646        //! It is assumed that this function will be used only temporarily. 
    47                 mratio(const epdf* nom0, const RV &rv, bool copy=false):mpdf(rv,RV()){ 
    48                         if (copy){ 
     47        mratio ( const epdf* nom0, const RV &rv, bool copy=false ) :mpdf ( rv,RV() ) { 
     48                if ( copy ) { 
    4949//                      nom = nom0->_copy_(); 
    50                                 destroynom=true; 
    51                         } else { 
    52                                 nom = nom0; 
    53                                 destroynom = false; 
    54                         } 
    55                         rvc = nom->_rv().subt(rv); 
    56                         it_assert_debug(rvc.length()>0,"Makes no sense to use this object!"); 
    57                         den = nom->marginal(rvc); 
    58                 }; 
    59                 double evalcond(const vec &val, const vec &cond) { 
    60                         return exp(nom->evalpdflog(concat(val,cond)) - den->evalpdflog(cond)); 
    61                 } 
     50                        destroynom=true; 
     51                } 
     52                else { 
     53                        nom = nom0; 
     54                        destroynom = false; 
     55                } 
     56                rvc = nom->_rv().subt ( rv ); 
     57                it_assert_debug ( rvc.length() >0,"Makes no sense to use this object!" ); 
     58                den = nom->marginal ( rvc ); 
     59        }; 
     60        double evalcond ( const vec &val, const vec &cond ) { 
     61                return exp ( nom->evalpdflog ( concat ( val,cond ) ) - den->evalpdflog ( cond ) ); 
     62        } 
    6263        //! Object takes ownership of nom and will destroy it 
    63                 void ownnom(){destroynom=true;} 
     64        void ownnom() {destroynom=true;} 
    6465        //! Default destructor 
    65                 ~mratio(){delete den; if (destroynom) {delete nom;}} 
     66        ~mratio() {delete den; if ( destroynom ) {delete nom;}} 
    6667}; 
    6768 
     
    100101                int i; 
    101102                double sum = 0.0; 
    102                 for ( i = 0;i < w.length();i++ ) {sum += w ( i ) * exp(Coms ( i )->evalpdflog ( val ));} 
     103                for ( i = 0;i < w.length();i++ ) {sum += w ( i ) * exp ( Coms ( i )->evalpdflog ( val ) );} 
    103104                return log ( sum ); 
    104105        }; 
    105106        vec evalpdflog_m ( const mat &Val ) const { 
    106                 vec x=ones(Val.cols()); 
    107                 vec y(Val.cols()); 
    108                 for (int i = 0; i < w.length(); i++ ) { 
    109                         y = w ( i )*exp(Coms ( i )->evalpdflog_m ( Val ) ); 
    110                         elem_mult_inplace(y,x); //result is in x 
    111                 } 
    112                 return log(x); 
     107                vec x=ones ( Val.cols() ); 
     108                vec y ( Val.cols() ); 
     109                for ( int i = 0; i < w.length(); i++ ) { 
     110                        y = w ( i ) *exp ( Coms ( i )->evalpdflog_m ( Val ) ); 
     111                        elem_mult_inplace ( y,x ); //result is in x 
     112                } 
     113                return log ( x ); 
    113114        }; 
    114115        mat evalpdflog_M ( const mat &Val ) const { 
    115                 mat X(w.length(), Val.cols()); 
    116                 for (int i = 0; i < w.length(); i++ ) { 
    117                         X.set_row(i, w ( i )*exp(Coms ( i )->evalpdflog_m ( Val ) )); 
     116                mat X ( w.length(), Val.cols() ); 
     117                for ( int i = 0; i < w.length(); i++ ) { 
     118                        X.set_row ( i, w ( i ) *exp ( Coms ( i )->evalpdflog_m ( Val ) ) ); 
    118119                } 
    119120                return X; 
     
    143144        //! pointers to epdfs - shortcut to mpdfs()._epdf() 
    144145        Array<epdf*> epdfs; 
    145         //! Indeces of rvc in common rvc 
    146         Array<ivec> irvcs_rvc; 
    147         //! Indeces of common rvc in rvcs 
    148         Array<ivec> irvc_rvcs; 
     146        //! Data link for each mpdfs 
     147        Array<datalink_m2m*> dls; 
    149148public: 
    150149        /*!\brief Constructor from list of mFacs, 
    151150        */ 
    152         mprod ( Array<mpdf*> mFacs ) : compositepdf ( mFacs ), mpdf ( getrv ( true ),RV() ), epdfs ( n ), irvcs_rvc ( n ), irvc_rvcs ( n ) { 
     151        mprod ( Array<mpdf*> mFacs ) : compositepdf ( mFacs ), mpdf ( getrv ( true ),RV() ), epdfs ( n ), dls ( n ) { 
    153152                setrvc ( rv,rvc ); 
    154                 setindices ( rv ); 
     153                // rv and rvc established = > we can link them with mpdfs 
    155154                for ( int i = 0;i < n;i++ ) { 
    156                         // establish link between rvc and rvcs 
    157                         rvc.dataind ( mpdfs ( i )->_rvc(), irvc_rvcs ( i ), irvcs_rvc ( i ) ); 
     155                        dls ( i ) = new datalink_m2m ( mpdfs ( i )->_rv(), mpdfs(i)->_rvc(), rv, rvc ); 
    158156                } 
    159157 
     
    166164                int i; 
    167165                double res = 0.0; 
    168                 vec condi; 
    169166                for ( i = n - 1;i >= 0;i-- ) { 
    170                         if ( ( irvcs_rvc ( i ).length() > 0 ) || ( irvcs_rv ( i ).length() > 0 ) ) { 
    171                                 condi = zeros ( irvcs_rvc ( i ).length() +irvcs_rv ( i ).length() ); 
    172                                 //copy part of the rvc into condi 
    173                                 set_subvector ( condi, irvcs_rvc ( i ), get_vec ( cond,irvc_rvcs ( i ) ) ); 
    174                                 //copy part of the rv into condi 
    175                                 set_subvector ( condi, irvcs_rv ( i ), get_vec ( val,irv_rvcs ( i ) ) ); 
    176                                 mpdfs ( i )->condition ( condi ); 
     167                        if ( mpdfs(i)->_rvc().count() >0) { 
     168                                mpdfs ( i )->condition ( dls ( i )->get_cond ( val,cond ) ); 
    177169                        } 
    178170                        // add logarithms 
    179                         res += epdfs ( i )->evalpdflog ( val ( rvsinrv ( i ) ) ); 
     171                        res += epdfs ( i )->evalpdflog ( dls ( i )->get_val ( val ) ); 
    180172                } 
    181173                return exp ( res ); 
    182174        } 
    183175        vec samplecond ( const vec &cond, double &ll ) { 
    184                 vec smp=zeros ( rv.count() ); 
    185                 vec condi; 
     176                //! Ugly hack to help to discover if mpfs are not in proper order. Correct solution = check that explicitely. 
     177                vec smp= std::numeric_limits<double>::infinity() * ones ( rv.count() ); 
    186178                vec smpi; 
    187179                ll = 0; 
     180                // Hard assumption here!!! We are going backwards, to assure that samples that are needed from smp are already generated! 
    188181                for ( int i = ( n - 1 );i >= 0;i-- ) { 
    189                         if ( ( irvcs_rvc ( i ).length() > 0 ) || ( irvcs_rv ( i ).length() > 0 ) ) { 
    190                                 condi = zeros ( irvcs_rv ( i ).length() + irvcs_rvc ( i ).length() ); 
    191                                 // copy data in condition 
    192                                 set_subvector ( condi,irvcs_rvc ( i ), get_vec ( cond, irvc_rvcs ( i ) ) ); 
    193                                 // copy data in already generated sample 
    194                                 set_subvector ( condi,irvcs_rv ( i ), get_vec ( smp,irv_rvcs ( i ) ) ); 
    195  
    196                                 mpdfs ( i )->condition ( condi ); 
     182                        if ( mpdfs(i)->_rvc().count() ) { 
     183                                mpdfs ( i )->condition ( dls ( i )->get_cond ( smp ,cond ) ); // smp is val here!! 
    197184                        } 
    198185                        smpi = epdfs ( i )->sample(); 
    199186                        // copy contribution of this pdf into smp 
    200                         set_subvector ( smp,rvsinrv ( i ), smpi ); 
     187                        dls(i)->fill_val(smp, smpi); 
    201188                        // add ith likelihood contribution 
    202189                        ll+=epdfs ( i )->evalpdflog ( smpi );