Changeset 192

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:
8 modified

Legend:

Unmodified
Added
Removed
  • bdm/estim/merger.cpp

    r190 r192  
    3333        vec &w = eSmp._w(); //aux 
    3434 
    35         mat Smp_ex =ones ( rv.count() +1,Ns ); // Extended samples for the ARX model - the last row is ones  
     35        mat Smp_ex =ones ( rv.count() +1,Ns ); // Extended samples for the ARX model - the last row is ones 
    3636        for ( int i=0;i<Ns;i++ ) {      set_col_part ( Smp_ex,i,Smp ( i ) );} 
    3737 
     
    6060                //Re-Initialize Mixture model 
    6161                Mix.init ( &A0, Smp_ex, Nc ); 
    62                 Mix.bayesB ( Smp_ex, ones(Ns));//w*Ns ); 
    63                 Mpred = Mix.predictor(rv); // Allocation => must be deleted at the end!! 
    64          
     62                Mix.bayesB ( Smp_ex, ones ( Ns ) );//w*Ns ); 
     63                Mpred = Mix.predictor ( rv ); // Allocation => must be deleted at the end!! 
     64 
    6565                // Generate new samples 
    6666                eSmp.set_samples ( Mpred ); 
     
    7979                        //======== Same RVs =========== 
    8080                        //Split according to dependency in rvs 
    81                         if ( rvsinrv ( i ).length() ==rv.count() ) { 
     81                        if ( mpdfs ( i )->_rv().count() ==rv.count() ) { 
    8282                                // no need for conditioning or marginalization 
    8383                                for ( int j=0;j<Ns; j++ ) { // Smp is Array<> => for cycle 
     
    8686                        } 
    8787                        else { 
    88                                 vec smpk; 
    8988                                // compute likelihood of marginal on the conditional variable 
    9089                                if ( mpdfs ( i )->_rvc().count() >0 ) { 
    9190                                        // Make marginal on rvc_i 
    9291                                        epdf* tmp_marg = Mpred->marginal ( mpdfs ( i )->_rvc() ); 
    93                                         for(int k=0;k<Ns;k++){ 
    94                                         lw_src(k) += tmp_marg->evalpdflog ( get_vec(Smp(i), irv_rvcs(i) ) );} 
     92                                        //compute vector of lw_src 
     93                                        for ( int k=0;k<Ns;k++ ) { 
     94                                                lw_src ( k ) += tmp_marg->evalpdflog ( dls ( i )->get_val ( Smp ( i ) ) ); 
     95                                        } 
    9596                                        delete tmp_marg; 
    9697                                } 
    9798                                // Compute likelihood of the missing variable 
    98                                 if ( rv.count() > mpdfs ( i )->_rv().count() + mpdfs ( i )->_rvc().count() ) { 
    99                                         // There are variales unknown to mpdfs 
    100                                         RV z = ( rv.subt ( mpdfs ( i )->_rv() ) ).subt ( mpdfs ( i )->_rvc() ); 
    101                                         mpdf* tmp_cond = Mpred->condition ( z );  
    102                                         // Indeces of rest rv in Smp 
    103                                         ivec zinrv=z.dataind ( rv ) ; 
    104                                         // Indeces of rest rvc in Smp 
    105                                         ivec zinrvc=tmp_cond->_rvc().dataind ( rv ); 
     99                                if ( rv.count() > ( mpdfs ( i )->_rv().count() + mpdfs ( i )->_rvc().count() ) ) { 
     100                                        // There are variales unknown to mpdfs(i) : rvzs 
     101                                        mpdf* tmp_cond = Mpred->condition ( rvzs ( i ) ); 
    106102                                        // Compute likelihood 
    107103                                        for ( int k= 0; k<Ns; k++ ) { 
    108                                                 smpk=Smp( k ); 
    109                                                 lw_src ( k ) += log( tmp_cond->evalcond ( get_vec ( smpk,zinrv ), get_vec ( smpk,zinrvc ) )); 
     104                                                lw_src ( k ) += log ( 
     105                                                                    tmp_cond->evalcond ( 
     106                                                                        zdls ( i )->get_val ( Smp ( k ) ), 
     107                                                                        zdls ( i )->get_cond ( Smp ( k ) ) ) ); 
    110108                                        } 
    111109                                        delete tmp_cond; 
     
    113111                                // Compute likelihood of the partial source 
    114112                                for ( int k= 0; k<Ns; k++ ) { 
    115                                         smpk=Smp( k ); 
    116                                         mpdfs ( i )->condition ( get_vec ( smpk,irv_rvcs(i) ) ); 
    117                                         lw_src ( k ) += mpdfs ( i )->_epdf().evalpdflog ( get_vec ( smpk, rvsinrv ( i ) ) ); 
     113                                        mpdfs ( i )->condition ( dls ( i )->get_cond ( Smp(k) ) ); 
     114                                        lw_src ( k ) += mpdfs ( i )->_epdf().evalpdflog ( dls ( i )->get_val ( Smp(k) ) ); 
    118115                                } 
    119116                        } 
     
    128125 
    129126                w = lognorm_merge ( lW ); //merge 
    130                  
     127 
    131128                sprintf ( str,"w%d",niter ); 
    132129                dbg << Name ( str ) << w; 
     
    150147                // ==== stopping rule === 
    151148                niter++; 
    152                 converged = ( niter>20); 
     149                converged = ( niter>20 ); 
    153150        } 
    154151 
  • bdm/estim/merger.h

    r190 r192  
    3030        //!Internal mixture of EF models 
    3131        MixEF Mix; 
     32        //! Data link for each mpdf in mpdfs 
     33        Array<datalink_m2e*> dls; 
     34        //! Array of rvs that are not modelled by mpdfs at all (aux) 
     35        Array<RV> rvzs; 
     36        //! Data Links of rv0 mpdfs - these will be conditioned the (rv,rvc) of mpdfs 
     37        Array<datalink_m2e*> zdls;  
     38         
    3239        //!Number of samples used in approximation 
    3340        int Ns; 
     
    4047        merger ( const Array<mpdf*> &S ) : 
    4148                        compositepdf ( S ), epdf ( getrv ( false ) ), 
    42                         Mix ( Array<BMEF*> ( 0 ),vec ( 0 ) ) 
    43         {setindices(rv); beta=2.0; Ns=100; Nc=10; Mix.set_method(EM);} 
    44         //! Set internal parameters used in approximation 
     49                        Mix ( Array<BMEF*> ( 0 ),vec ( 0 ) ), dls(n), rvzs(n), zdls(n) { 
     50                                RV ztmp; 
     51                for ( int i=0;i<n;i++ ) { 
     52                        //Establich connection between mpdfs and merger 
     53                        dls ( i ) = new datalink_m2e ( mpdfs ( i )->_rv(), mpdfs(i)->_rvc(), rv ); 
     54                        // find out what is missing in each mpdf 
     55                        ztmp= mpdfs(i)->_rv(); 
     56                        ztmp.add(mpdfs(i)->_rvc()); 
     57                        rvzs(i)=rv.subt(ztmp); 
     58                        zdls(i) = new datalink_m2e (rvzs(i), ztmp, rv ) ; 
     59                }; 
     60                //Set Default values of parameters 
     61                beta=2.0; 
     62                Ns=100; 
     63                Nc=10; 
     64                Mix.set_method ( EM ); 
     65        } 
     66//! Set internal parameters used in approximation 
    4567        void set_parameters ( double beta0, int Ns0, int Nc0 ) {        beta=beta0;Ns=Ns0;Nc=Nc0;} 
    46         //!Initialize the proposal density. This function must be called before merge()! 
    47         void init() { 
     68//!Initialize the proposal density. This function must be called before merge()! 
     69        void init() { ////////////// NOT FINISHED 
    4870                Array<vec> Smps ( n ); 
    4971                //Gibbs sampling 
    5072                for ( int i=0;i<n;i++ ) {Smps ( i ) =zeros ( 0 );} 
    5173        } 
    52         //!Create a mixture density using known proposal 
     74//!Create a mixture density using known proposal 
    5375        void merge ( const epdf* g0 ); 
    54         //!Create a mixture density, make sure to call init() before the first call 
     76//!Create a mixture density, make sure to call init() before the first call 
    5577        void merge () {merge ( & ( Mix._epdf() ) );}; 
    5678 
    57         //! Merge log-likelihood values 
     79//! Merge log-likelihood values 
    5880        vec lognorm_merge ( mat &lW ); 
    59         //! sample from merged density 
    60         //! weight w is a 
    61         vec sample ( )const { return Mix._epdf().sample();} 
    62         double evalpdflog ( const vec &dt ) const{  
    63                 vec dtf=ones(dt.length()+1);     
    64                 dtf.set_subvector(0,dt);  
    65                 return Mix.logpred ( dtf );} 
    66         vec mean()const {return Mix._epdf().mean();} 
    67         //! for future use 
    68         virtual ~merger() {}; 
    69          
    70         //! Access function 
     81//! sample from merged density 
     82//! weight w is a 
     83        vec sample ( ) const { return Mix._epdf().sample();} 
     84        double evalpdflog ( const vec &dt ) const { 
     85                vec dtf=ones ( dt.length() +1 ); 
     86                dtf.set_subvector ( 0,dt ); 
     87                return Mix.logpred ( dtf ); 
     88        } 
     89        vec mean() const {return Mix._epdf().mean();} 
     90//! for future use 
     91        virtual ~merger() { 
     92                for (int i=0; i<n; i++){ 
     93                        delete dls(i); 
     94                        delete zdls(i);                  
     95                } 
     96        }; 
     97 
     98//! Access function 
    7199        MixEF& _Mix() {return Mix;} 
    72100}; 
  • 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> 
  • tests/datalink_test.cpp

    r190 r192  
    1010using std::endl; 
    1111 
    12 int main() 
    13 { 
    14         RV a = RV ( "{a }","2"); 
    15         RV b = RV ( "{b }"); 
    16         RV c = RV ( "{c }"); 
    17          
    18         mpdf M1(concat(a,b),c); 
    19         mpdf M2(a, concat(b,c)); 
    20          
    21         datalink_mpdf dl(M2,M1); 
    22          
    23         vec val("1 1.5 2"); 
    24         vec cond("3"); 
    25          
     12int main() { 
     13        RV a = RV ( "{a }","2" ); 
     14        RV b = RV ( "{b }" ); 
     15        RV c = RV ( "{c }" ); 
     16 
     17        mpdf M1 ( concat ( a,b ),c ); 
     18        mpdf M2 ( a, concat ( b,c ) ); 
     19 
     20        datalink_m2m dl ( a,M2._rvc(),M1._rv(), M1._rvc() ); 
     21 
     22        vec val ( "1 1.5 2" ); 
     23        vec cond ( "3" ); 
     24 
    2625        cout << "val: " << val  << endl; 
    2726        cout << "cond: " << cond  << endl; 
    28          
    29         cout << "lo val: " << dl.get_val(val) <<endl; 
    30         cout << "lo cond: " << dl.get_cond(val,cond) <<endl; 
    31          
     27 
     28        cout << "lo val: " << dl.get_val ( val ) <<endl; 
     29        cout << "lo cond: " << dl.get_cond ( val,cond ) <<endl; 
     30 
    3231        //getchar(); 
    3332        return 0;