Changeset 192 for bdm/stat

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

Location:
bdm/stat
Files:
5 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/emix.cpp

    r183 r192  
    1111        if ( copy ) { 
    1212                Coms.set_length(Coms0.length()); 
    13                 for ( i=0;i<w.length();i++ ) {*Coms ( i ) =*Coms0 ( i );} 
     13                for ( i=0;i<w.length();i++ ) {it_error("Not imp..."); 
     14                        *Coms ( i ) =*Coms0 ( i );} 
    1415                destroyComs=true; 
    1516        } 
  • 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 ); 
  • bdm/stat/libBM.cpp

    r182 r192  
    234234} 
    235235 
    236 void compositepdf::setindices(const RV &rv){ 
    237         for (int i = 0;i < n;i++ ) { 
    238                 // find ith rv in common rv 
    239                 rvsinrv ( i ) = mpdfs ( i )->_rv().dataind ( rv ); 
    240                 // establish link between rv and rvc 
    241                 rv.dataind(mpdfs(i)->_rvc(), irv_rvcs(i), irvcs_rv(i)); 
    242         } 
    243 } 
    244  
    245236void BM::bayesB(const mat &Data){ 
    246237        for(int t=0;t<Data.cols();t++){bayes(Data.get_col(t));} 
  • bdm/stat/libBM.h

    r190 r192  
    247247}; 
    248248 
    249 //!DataLink is a connection between epdf and its superordinate (Up) 
     249//!DataLink is a connection between an epdf and its superordinate epdf (Up) 
    250250//! It is assumed that my val is fully present in "Up" 
    251 class datalink { 
     251class datalink_e2e { 
    252252protected: 
    253253        //! Remember how long val should be 
     
    257257        //! val-to-val link, indeces of the upper val 
    258258        ivec v2v_up; 
    259         public: 
     259public: 
    260260        //! Constructor 
    261         datalink ( const RV &rv, const RV &rv_up ) : 
    262                 valsize(rv.count()), valupsize(rv_up.count()), v2v_up ( rv.dataind ( rv_up ) )  {it_assert_debug(v2v_up.length()==valsize,"rv is not fully in rv_up");} 
     261        datalink_e2e ( const RV &rv, const RV &rv_up ) : 
     262                        valsize ( rv.count() ), valupsize ( rv_up.count() ), v2v_up ( rv.dataind ( rv_up ) )  { 
     263                it_assert_debug ( v2v_up.length() ==valsize,"rv is not fully in rv_up" ); 
     264        } 
    263265        //! Get val for myself from val of "Up" 
    264         vec get_val ( const vec &val_up ) {it_assert_debug(valupsize==val_up.length(),"Wrong val_up"); return get_vec ( val_up,v2v_up );} 
     266        vec get_val ( const vec &val_up ) {it_assert_debug ( valupsize==val_up.length(),"Wrong val_up" ); return get_vec ( val_up,v2v_up );} 
    265267        //! Fill val of "Up" by my pieces 
    266         void fill_val ( vec &val_up, const vec &val ) {it_assert_debug(valsize==val.length(),"Wrong val");it_assert_debug(valupsize==val_up.length(),"Wrong val_up");set_subvector ( val_up, v2v_up, val );} 
    267 }; 
    268  
    269 //!DataLink is a connection between mpdf and its superordinate (Up) 
    270 //! This class links  
    271 class datalink_mpdf: public datalink { 
    272 protected: 
     268        void fill_val ( vec &val_up, const vec &val ) { 
     269                it_assert_debug ( valsize==val.length(),"Wrong val" ); 
     270                it_assert_debug ( valupsize==val_up.length(),"Wrong val_up" ); 
     271                set_subvector ( val_up, v2v_up, val ); 
     272        } 
     273}; 
     274 
     275//! data link between 
     276class datalink_m2e: public datalink_e2e { 
     277protected: 
     278        //! Remember how long cond should be 
     279        int condsize; 
    273280        //!upper_val-to-local_cond link, indeces of the upper val 
    274281        ivec v2c_up; 
    275282        //!upper_val-to-local_cond link, ideces of the local cond 
    276283        ivec v2c_lo; 
     284 
     285public: 
     286        //! Constructor 
     287        datalink_m2e ( const RV &rv,  const RV &rvc, const RV &rv_up ) : 
     288                        datalink_e2e ( rv,rv_up ), condsize ( rvc.count() ) { 
     289                //establish v2c connection 
     290                rvc.dataind ( rv_up, v2c_lo, v2c_up ); 
     291        } 
     292        //!Construct condition 
     293        vec get_cond ( const vec &val_up ) { 
     294                vec tmp ( condsize ); 
     295                set_subvector ( tmp,v2c_lo,val_up ( v2c_up ) ); 
     296                return tmp; 
     297        } 
     298}; 
     299//!DataLink is a connection between mpdf and its superordinate (Up) 
     300//! This class links 
     301class datalink_m2m: public datalink_m2e { 
     302protected: 
    277303        //!cond-to-cond link, indeces of the upper cond 
    278304        ivec c2c_up; 
    279305        //!cond-to-cond link, indeces of the local cond 
    280306        ivec c2c_lo; 
    281         //! size of cond 
    282         int csize; 
    283307public: 
    284308        //! Constructor 
    285         datalink_mpdf ( const mpdf &Me, const mpdf &Up ) : 
    286                         datalink ( Me._rv(),Up._rv() ) { 
    287                 //establish v2c connection 
    288                 Me._rvc().dataind ( Up._rv(), v2c_lo, v2c_up ); 
     309        datalink_m2m ( const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up ) : 
     310                        datalink_m2e ( rv, rvc, rv_up) { 
    289311                //establish c2c connection 
    290                 Me._rvc().dataind ( Up._rvc(), c2c_lo, c2c_up ); 
    291                 csize = Me._rvc().count(); 
     312                rvc.dataind ( rvc_up, c2c_lo, c2c_up ); 
     313                it_assert_debug(c2c_lo.length()+v2c_lo.length()==condsize, "cond is not fully given"); 
    292314        } 
    293315        //! Get cond for myself from val and cond of "Up" 
    294316        vec get_cond ( const vec &val_up, const vec &cond_up ) { 
    295                 vec tmp(csize); 
    296                 set_subvector(tmp,v2c_lo,val_up(v2c_up)); 
    297                 set_subvector(tmp,c2c_lo,cond_up(c2c_up)); 
     317                vec tmp ( condsize ); 
     318                set_subvector ( tmp,v2c_lo,val_up ( v2c_up ) ); 
     319                set_subvector ( tmp,c2c_lo,cond_up ( c2c_up ) ); 
    298320                return tmp; 
    299321        } 
    300         //! Fill  
     322        //! Fill 
    301323 
    302324}; 
     
    304326/*! \brief Unconditional mpdf, allows using epdf in the role of mpdf. 
    305327 
    306 WARNING: the class does not check validity of the \c ep pointer nor its existence. 
    307328*/ 
    308329class mepdf : public mpdf { 
     
    314335 
    315336//!\brief Abstract composition of pdfs, a base for specific classes 
     337//!this abstract class is common to epdf and mpdf 
    316338class compositepdf { 
    317339protected: 
     
    320342        //! Elements of composition 
    321343        Array<mpdf*> mpdfs; 
    322         //! Indeces of rvs in common rv 
    323         Array<ivec> rvsinrv; 
    324         //! Indeces of rvc in common rv 
    325         Array<ivec> irvcs_rv; 
    326         //! Indeces of common rv in rvc 
    327         Array<ivec> irv_rvcs; 
    328 public: 
    329         compositepdf ( Array<mpdf*> A0 ) : n ( A0.length() ), mpdfs ( A0 ), rvsinrv ( n ), irvcs_rv ( n ),irv_rvcs ( n ) {}; 
     344public: 
     345        compositepdf ( Array<mpdf*> A0 ) : n ( A0.length() ), mpdfs ( A0 ) {}; 
    330346        //! find common rv, flag \param checkoverlap modifies whether overlaps are acceptable 
    331347        RV getrv ( bool checkoverlap=false ); 
    332348        //! common rvc of all mpdfs is written to rvc 
    333349        void setrvc ( const RV &rv, RV &rvc ); 
    334         //! fill all rv*inrv* according to 
    335         void setindices ( const RV &rv ); 
    336350}; 
    337351 
  • bdm/stat/libEF.h

    r185 r192  
    380380        //! Set \c A and \c R 
    381381        void set_parameters ( const  mat &A, const vec &mu0, const sq_T &R ); 
    382         //!Generate one sample of the posterior 
    383         vec samplecond (const vec &cond, double &lik ); 
    384         //!Generate matrix of samples of the posterior 
    385         mat samplecond (const vec &cond, vec &lik, int n ); 
     382//      //!Generate one sample of the posterior 
     383//      vec samplecond (const vec &cond, double &lik ); 
     384//      //!Generate matrix of samples of the posterior 
     385//      mat samplecond (const vec &cond, vec &lik, int n ); 
    386386        //! Set value of \c rvc . Result of this operation is stored in \c epdf use function \c _ep to access it. 
    387387        void condition (const vec &cond ); 
     
    567567} 
    568568 
    569 template<class sq_T> 
    570 vec mlnorm<sq_T>::samplecond (const  vec &cond, double &lik ) { 
    571         this->condition ( cond ); 
    572         vec smp = epdf.sample(); 
    573         lik = epdf.eval ( smp ); 
    574         return smp; 
    575 } 
    576  
    577 template<class sq_T> 
    578 mat mlnorm<sq_T>::samplecond (const vec &cond, vec &lik, int n ) { 
    579         int i; 
    580         int dim = rv.count(); 
    581         mat Smp ( dim,n ); 
    582         vec smp ( dim ); 
    583         this->condition ( cond ); 
    584  
    585         for ( i=0; i<n; i++ ) { 
    586                 smp = epdf.sample(); 
    587                 lik ( i ) = epdf.eval ( smp ); 
    588                 Smp.set_col ( i ,smp ); 
    589         } 
    590  
    591         return Smp; 
    592 } 
     569// template<class sq_T> 
     570// vec mlnorm<sq_T>::samplecond (const  vec &cond, double &lik ) { 
     571//      this->condition ( cond ); 
     572//      vec smp = epdf.sample(); 
     573//      lik = epdf.eval ( smp ); 
     574//      return smp; 
     575// } 
     576 
     577// template<class sq_T> 
     578// mat mlnorm<sq_T>::samplecond (const vec &cond, vec &lik, int n ) { 
     579//      int i; 
     580//      int dim = rv.count(); 
     581//      mat Smp ( dim,n ); 
     582//      vec smp ( dim ); 
     583//      this->condition ( cond ); 
     584//  
     585//      for ( i=0; i<n; i++ ) { 
     586//              smp = epdf.sample(); 
     587//              lik ( i ) = epdf.eval ( smp ); 
     588//              Smp.set_col ( i ,smp ); 
     589//      } 
     590//  
     591//      return Smp; 
     592// } 
    593593 
    594594template<class sq_T>