- Main Page
- Related Pages
- Classes
- Files

- File List
- File Members

# bdmbase.h

Go to the documentation of this file.

    00001
    00013 #ifndef BDMBASE_H
    00014 #define BDMBASE_H
    00015
    00016 #include <map>
    00017
    00018 #include "../itpp_ext.h"
    00019 #include "../bdmroot.h"
    00020 #include "../shared_ptr.h"
    00021 #include "user_info.h"
    00022
    00023 using namespace libconfig;
    00024 using namespace itpp;
    00025 using namespace std;
    00026
    00027 namespace bdm {
    00029 class str {
    00030 public:
    00032         ivec ids;
    00034         ivec times;
    00036         str ( ivec ids0, ivec times0 ) : ids ( ids0 ), times ( times0 ) {
    00037                 bdm_assert ( times0.length() == ids0.length(), "Incompatible input" );
    00038         };
    00039 };
    00040
    00079 class RV : public root {
    00080 private:
    00081         typedef std::map<string, int> str2int_map;
    00082
    00084         static ivec SIZES;
    00086         static Array<string> NAMES;
    00087
    00089         const static int BUFFER_STEP;
    00090
    00092         static str2int_map MAP;
    00093
    00094 public:
    00095
    00096 protected:
    00098         int dsize;
    00100         int len;
    00102         ivec ids;
    00104         ivec times;
    00105
    00106 private:
    00107         enum enum_dummy {dummy};
    00108
    00110         void init ( const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times );
    00111         int init ( const string &name, int size );
    00114         explicit RV ( const ivec &ids0, enum_dummy dum ) : dsize ( 0 ), len ( ids0.length() ), ids ( ids0 ), times ( zeros_i ( ids0.length() ) ) {
    00115                 dsize = countsize();
    00116         }
    00117 public:
    00120
    00122         RV ( const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times ) {
    00123                 init ( in_names, in_sizes, in_times );
    00124         }
    00125
    00127         RV ( const Array<std::string> &in_names, const ivec &in_sizes ) {
    00128                 init ( in_names, in_sizes, zeros_i ( in_names.length() ) );
    00129         }
    00130
    00132         RV ( const Array<std::string> &in_names ) {
    00133                 init ( in_names, ones_i ( in_names.length() ), zeros_i ( in_names.length() ) );
    00134         }
    00135
    00137         RV() : dsize ( 0 ), len ( 0 ), ids ( 0 ), times ( 0 ) {}
    00138
    00140         RV ( string name, int sz, int tm = 0 );
    00141
    00142         // compiler-generated copy constructor is used
    00144
    00147
    00149         friend std::ostream &operator<< ( std::ostream &os, const RV &rv );
    00150
    00151         string to_string() {ostringstream o; o << this; return o.str();}
    00152
    00154         int _dsize() const {
    00155                 return dsize;
    00156         }
    00157
    00159         const ivec& _ids() const {
    00160                 return ids;
    00161         }
    00162
    00164         int countsize() const;
    00166         ivec cumsizes() const;
    00168         int length() const {
    00169                 return len;
    00170         }
    00171         int id ( int at ) const {
    00172                 return ids ( at );
    00173         }
    00174         int size ( int at ) const {
    00175                 return SIZES ( ids ( at ) );
    00176         }
    00177         int time ( int at ) const {
    00178                 return times ( at );
    00179         }
    00180         std::string name ( int at ) const {
    00181                 return NAMES ( ids ( at ) );
    00182         }
    00184         std::string scalarname ( int scalat ) const {
    00185                 bdm_assert(scalat<dsize,"Wrong input index");
    00186                 int id=0;
    00187                 int scalid=0;
    00188                 while (scalid+SIZES(ids(id))<=scalat)  { scalid+=SIZES(ids(id)); id++;};
    00189                 //now id is the id of variable of interest
    00190                 if (size(id)==1)
    00191                         return  NAMES ( ids ( id ) );
    00192                 else
    00193                         return  NAMES ( ids ( id ) )+ "_" + num2str(scalat-scalid);
    00194
    00195         }
    00196         void set_time ( int at, int time0 ) {
    00197                 times ( at ) = time0;
    00198         }
    00200
    00203
    00205         ivec findself ( const RV &rv2 ) const;
    00207         ivec findself_ids ( const RV &rv2 ) const;
    00209         bool equal ( const RV &rv2 ) const;
    00211         bool add ( const RV &rv2 );
    00213         RV subt ( const RV &rv2 ) const;
    00215         RV subselect ( const ivec &ind ) const;
    00216
    00218         RV operator() ( const ivec &ind ) const {
    00219                 return subselect ( ind );
    00220         }
    00221
    00223         RV operator() ( int di1, int di2 ) const;
    00224
    00226         void t_plus ( int delta );
    00228
    00231         RV remove_time() const {
    00232                 return RV ( unique ( ids ), dummy );
    00233         }
    00235         RV copy_t(int dt) const {
    00236                 RV tmp=*this;
    00237                 tmp.t_plus(dt);
    00238                 return tmp;
    00239         }
    00241         RV expand_delayes() const {
    00242                 RV rvt = this->remove_time(); //rv at t=0
    00243                 RV tmp = rvt;
    00244                 int td = mint();
    00245                 for ( int i = -1; i >= td; i-- ) {
    00246                         rvt.t_plus ( -1 );
    00247                         tmp.add ( rvt ); //shift u1
    00248                 }
    00249                 return tmp;
    00250         }
    00252
    00255
    00257         str tostr() const;
    00260         ivec dataind ( const RV &crv ) const;
    00262         ivec dataind_part ( const RV &crv ) const;
    00265         void dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const;
    00267         int mint() const {
    00268                         return times.length()>0 ? min (times) : 0;
    00269         }
    00271
    00284         void from_setting ( const Setting &set );
    00285
    00286         // TODO dodelat void to_setting( Setting &set ) const;
    00287
    00289         static void clear_all();
    00291         string show_all();
    00292
    00293 };
    00294 UIREGISTER ( RV );
    00295 SHAREDPTR ( RV );
    00296
    00298 RV concat ( const RV &rv1, const RV &rv2 );
    00299
    00301 class fnc : public root {
    00302 protected:
    00304         int dimy;
    00305 public:
    00307         fnc() {};
    00309         virtual vec eval ( const vec &cond ) {
    00310                 return vec ( 0 );
    00311         };
    00312
    00314         virtual void condition ( const vec &val ) {};
    00315
    00317         int dimension() const {
    00318                 return dimy;
    00319         }
    00320 };
    00321
    00322 class mpdf;
    00323
    00325
    00326 class epdf : public root {
    00327 protected:
    00329         int dim;
    00331         RV rv;
    00332
    00333 public:
    00345         epdf() : dim ( 0 ), rv() {};
    00346         epdf ( const epdf &e ) : dim ( e.dim ), rv ( e.rv ) {};
    00347         epdf ( const RV &rv0 ) : dim ( rv0._dsize() ) {
    00348                 set_rv ( rv0 );
    00349         };
    00350         void set_parameters ( int dim0 ) {
    00351                 dim = dim0;
    00352         }
    00354
    00357
    00359         virtual vec sample() const {
    00360                 bdm_error ( "not implemented" );
    00361                 return vec();
    00362         }
    00363
    00365         virtual mat sample_m ( int N ) const;
    00366
    00369         virtual double evallog ( const vec &val ) const {
    00370                 bdm_error ( "not implemented" );
    00371                 return 0.0;
    00372         }
    00373
    00375         virtual vec evallog_m ( const mat &Val ) const;
    00376
    00378         virtual vec evallog_m ( const Array<vec> &Avec ) const;
    00379
    00381         virtual shared_ptr<mpdf> condition ( const RV &rv ) const;
    00382
    00384         virtual shared_ptr<epdf> marginal ( const RV &rv ) const;
    00385
    00387         virtual vec mean() const {
    00388                 bdm_error ( "not implemneted" );
    00389                 return vec();
    00390         }
    00391
    00393         virtual vec variance() const {
    00394                 bdm_error ( "not implemneted" );
    00395                 return vec();
    00396         }
    00397
    00399         virtual void qbounds ( vec &lb, vec &ub, double percentage = 0.95 ) const {
    00400                 vec mea = mean();
    00401                 vec std = sqrt ( variance() );
    00402                 lb = mea - 2 * std;
    00403                 ub = mea + 2 * std;
    00404         };
    00406
    00412
    00414         void set_rv ( const RV &rv0 ) {
    00415                 rv = rv0;
    00416         }
    00417
    00419         bool isnamed() const {
    00420                 bool b = ( dim == rv._dsize() );
    00421                 return b;
    00422         }
    00423
    00425         const RV& _rv() const {
    00426                 //bdm_assert_debug ( isnamed(), "" );
    00427                 return rv;
    00428         }
    00430
    00433
    00435         int dimension() const {
    00436                 return dim;
    00437         }
    00445         void from_setting ( const Setting &set ) {
    00446                 shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional );
    00447                 if ( r ) {
    00448                         set_rv ( *r );
    00449                 }
    00450         }
    00451
    00452 };
    00453 SHAREDPTR ( epdf );
    00454
    00456 class mpdf : public root {
    00457 protected:
    00459         int dimc;
    00461         RV rvc;
    00462 private:
    00464         epdf* ep;
    00465
    00466 protected:
    00468         void set_ep ( epdf &iepdf ) {
    00469                 ep = &iepdf;
    00470         }
    00472         void set_ep ( epdf *iepdfp ) {
    00473                 ep = iepdfp;
    00474         }
    00475
    00476 public:
    00479
    00480         mpdf() : dimc ( 0 ), rvc(), ep ( NULL ) { }
    00481
    00482         mpdf ( const mpdf &m ) : dimc ( m.dimc ), rvc ( m.rvc ), ep ( m.ep ) { }
    00484
    00487
    00489         virtual vec samplecond ( const vec &cond ) {
    00490                 bdm_error ( "Not implemented" );
    00491                 return vec();
    00492         }
    00493
    00495         virtual mat samplecond_m ( const vec &cond, int N );
    00496
    00498         virtual double evallogcond ( const vec &dt, const vec &cond ) {
    00499                 bdm_error ( "Not implemented" );
    00500                 return 0.0;
    00501         }
    00502
    00504         virtual vec evallogcond_m ( const mat &Dt, const vec &cond ) {
    00505                 vec v ( Dt.cols() );
    00506                 for ( int i = 0; i < Dt.cols(); i++ ) {
    00507                         v ( i ) = evallogcond ( Dt.get_col ( i ), cond );
    00508                 }
    00509                 return v;
    00510         }
    00511
    00513         virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ) {
    00514                 bdm_error ( "Not implemented" );
    00515                 return vec();
    00516         }
    00517
    00520
    00521         const RV& _rv() const {
    00522                 return ep->_rv();
    00523         }
    00524         const RV& _rvc() const {
    00525                 return rvc;
    00526         }
    00527
    00528         int dimension() const {
    00529                 return ep->dimension();
    00530         }
    00531         int dimensionc() {
    00532                 return dimc;
    00533         }
    00534
    00544         void from_setting ( const Setting &set );
    00546
    00549         void set_rvc ( const RV &rvc0 ) {
    00550                 rvc = rvc0;
    00551         }
    00552         void set_rv ( const RV &rv0 ) {
    00553                 ep->set_rv ( rv0 );
    00554         }
    00555         bool isnamed() {
    00556                 return ( ep->isnamed() ) && ( dimc == rvc._dsize() );
    00557         }
    00559 };
    00560 SHAREDPTR ( mpdf );
    00561
    00563 template <class EPDF>
    00564 class mpdf_internal: public mpdf {
    00565 protected :
    00567         EPDF iepdf;
    00568 public:
    00570         mpdf_internal() : mpdf(), iepdf() {
    00571                 set_ep ( iepdf );
    00572         }
    00573
    00576         virtual void condition ( const vec &cond ) {
    00577                 bdm_error ( "Not implemented" );
    00578         }
    00579
    00581         EPDF& e() {
    00582                 return iepdf;
    00583         }
    00584
    00586         vec samplecond ( const vec &cond );
    00588         double evallogcond ( const vec &val, const vec &cond );
    00590         virtual vec evallogcond_m ( const mat &Dt, const vec &cond );
    00592         virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond );
    00594         virtual mat samplecond_m ( const vec &cond, int N );
    00595 };
    00596
    00622 class datalink {
    00623 protected:
    00625         int downsize;
    00626
    00628         int upsize;
    00629
    00631         ivec v2v_up;
    00632
    00633 public:
    00635         datalink() : downsize ( 0 ), upsize ( 0 ) { }
    00636
    00638         datalink ( const RV &rv, const RV &rv_up ) {
    00639                 set_connection ( rv, rv_up );
    00640         }
    00641
    00643         virtual void set_connection ( const RV &rv, const RV &rv_up );
    00644
    00646         virtual void set_connection ( int ds, int us, const ivec &upind );
    00647
    00649         vec pushdown ( const vec &val_up ) {
    00650                 vec tmp ( downsize );
    00651                 filldown ( val_up, tmp );
    00652                 return tmp;
    00653         }
    00655         virtual void filldown ( const vec &val_up, vec &val_down ) {
    00656                 bdm_assert_debug ( upsize == val_up.length(), "Wrong val_up" );
    00657                 val_down = val_up ( v2v_up );
    00658         }
    00660         virtual void pushup ( vec &val_up, const vec &val ) {
    00661                 bdm_assert_debug ( downsize == val.length(), "Wrong val" );
    00662                 bdm_assert_debug ( upsize == val_up.length(), "Wrong val_up" );
    00663                 set_subvector ( val_up, v2v_up, val );
    00664         }
    00666         int _upsize() {
    00667                 return upsize;
    00668         }
    00670         int _downsize() {
    00671                 return downsize;
    00672         }
    00673 };
    00674
    00677 class datalink_part : public datalink {
    00678 protected:
    00680         ivec v2v_down;
    00681 public:
    00682         void set_connection ( const RV &rv, const RV &rv_up );
    00684         void filldown ( const vec &val_up, vec &val_down ) {
    00685                 set_subvector ( val_down, v2v_down, val_up ( v2v_up ) );
    00686         }
    00687 };
    00688
    00693 class datalink_buffered: public datalink_part {
    00694 protected:
    00696         vec history;
    00698         RV Hrv;
    00700         ivec h2v_down;
    00702         ivec h2v_hist;
    00704         ivec v2h_up;
    00705 public:
    00706
    00707         datalink_buffered() : datalink_part(), history ( 0 ), h2v_down ( 0 ), h2v_hist ( 0 ) {};
    00709         void step ( const vec &val_up ) {
    00710                 if ( v2h_up.length() > 0 ) {
    00711                         history.shift_right ( 0, v2h_up.length() );
    00712                   history.set_subvector ( 0, val_up(v2h_up) );
    00713                 }
    00714         }
    00716         vec pushdown ( const vec &val_up ) {
    00717                 vec tmp ( downsize );
    00718                 filldown ( val_up, tmp );
    00719                 return tmp;
    00720         }
    00721
    00722         void filldown ( const vec &val_up, vec &val_down ) {
    00723                 bdm_assert_debug ( val_down.length() >= downsize, "short val_down" );
    00724
    00725                 set_subvector ( val_down, v2v_down, val_up ( v2v_up ) ); // copy direct values
    00726                 set_subvector ( val_down, h2v_down, history ( h2v_hist ) ); // copy delayed values
    00727         }
    00728
    00729         void set_connection ( const RV &rv, const RV &rv_up ) {
    00730                 // create link between up and down
    00731                 datalink_part::set_connection ( rv, rv_up );
    00732
    00733                 // create rvs of history
    00734                 // we can store only what we get in rv_up - everything else is removed
    00735                 ivec valid_ids = rv.findself_ids ( rv_up );
    00736                 RV rv_hist = rv.subselect ( find ( valid_ids >= 0 ) ); // select only rvs that are in rv_up
    00737                 RV rv_hist0 =rv_hist.remove_time();  // these RVs will form history at time =0
    00738                 // now we need to know what is needed from Up
    00739                 rv_hist = rv_hist.expand_delayes(); // full regressor - including time 0
    00740                 Hrv=rv_hist.subt(rv_hist0);   // remove time 0
    00741                 history = zeros ( Hrv._dsize() );
    00742
    00743                 // decide if we need to copy val to history
    00744                 if (Hrv._dsize() >0) {
    00745                         v2h_up = rv_hist0.dataind(rv_up); // indeces of elements of rv_up to be copied
    00746                 } // else v2h_up is empty
    00747
    00748                 Hrv.dataind ( rv, h2v_hist, h2v_down );
    00749
    00750                 downsize = v2v_down.length() + h2v_down.length();
    00751                 upsize = v2v_up.length();
    00752         }
    00754         void set_history(const RV& rv1, const vec &hist0){
    00755                 bdm_assert(rv1._dsize()==hist0.length(),"hist is not compatible with given rv1");
    00756                 ivec ind_H;
    00757                 ivec ind_h0;
    00758                 Hrv.dataind(rv1, ind_H, ind_h0); // find indeces of rv in
    00759                 set_subvector(history, ind_H, hist0(ind_h0)); // copy given hist to appropriate places
    00760         }
    00761 };
    00762
    00764 class datalink_2to1_buffered {
    00765 protected:
    00767         datalink_buffered dl1;
    00769         datalink_buffered dl2;
    00770 public:
    00772         void set_connection ( const RV &rv, const RV &rv_up1, const RV &rv_up2 ) {
    00773                 dl1.set_connection ( rv, rv_up1 );
    00774                 dl2.set_connection ( rv, rv_up2 );
    00775         }
    00777         void filldown ( const vec &val1, const vec &val2, vec &val_down ) {
    00778                 bdm_assert_debug ( val_down.length() >= dl1._downsize() + dl2._downsize(), "short val_down" );
    00779                 dl1.filldown ( val1, val_down );
    00780                 dl2.filldown ( val2, val_down );
    00781         }
    00783         void step ( const vec &dt, const vec &ut ) {
    00784                 dl1.step ( dt );
    00785                 dl2.step ( ut );
    00786         }
    00787 };
    00788
    00789
    00790
    00792 class datalink_m2e: public datalink {
    00793 protected:
    00795         int condsize;
    00796
    00798         ivec v2c_up;
    00799
    00801         ivec v2c_lo;
    00802
    00803 public:
    00805         datalink_m2e() : condsize ( 0 ) { }
    00806
    00808         void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up );
    00809
    00811         vec get_cond ( const vec &val_up );
    00812
    00814         void pushup_cond ( vec &val_up, const vec &val, const vec &cond );
    00815 };
    00816
    00819 class datalink_m2m: public datalink_m2e {
    00820 protected:
    00822         ivec c2c_up;
    00824         ivec c2c_lo;
    00825
    00826 public:
    00828         datalink_m2m() {};
    00830         void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up ) {
    00831                 datalink_m2e::set_connection ( rv, rvc, rv_up );
    00832                 //establish c2c connection
    00833                 rvc.dataind ( rvc_up, c2c_lo, c2c_up );
    00834                 bdm_assert_debug ( c2c_lo.length() + v2c_lo.length() == condsize, "cond is not fully given" );
    00835         }
    00836
    00838         vec get_cond ( const vec &val_up, const vec &cond_up ) {
    00839                 vec tmp ( condsize );
    00840                 set_subvector ( tmp, v2c_lo, val_up ( v2c_up ) );
    00841                 set_subvector ( tmp, c2c_lo, cond_up ( c2c_up ) );
    00842                 return tmp;
    00843         }
    00845
    00846 };
    00847
    00853 class logger : public root {
    00854 protected:
    00856         Array<RV> entries;
    00858         Array<string> names;
    00859 public:
    00861         logger() : entries ( 0 ), names ( 0 ) {}
    00862
    00865         virtual int add ( const RV &rv, string prefix = "" ) {
    00866                 int id;
    00867                 if ( rv._dsize() > 0 ) {
    00868                         id = entries.length();
    00869                         names = concat ( names, prefix ); // diff
    00870                         entries.set_length ( id + 1, true );
    00871                         entries ( id ) = rv;
    00872                 } else {
    00873                         id = -1;
    00874                 }
    00875                 return id; // identifier of the last entry
    00876         }
    00877
    00879         virtual void logit ( int id, const vec &v ) {
    00880                 bdm_error ( "Not implemented" );
    00881         };
    00883         virtual void logit ( int id, const double &d ) {
    00884                 bdm_error ( "Not implemented" );
    00885         };
    00886
    00888         virtual void step() {
    00889                 bdm_error ( "Not implemneted" );
    00890         };
    00891
    00893         virtual void finalize() {};
    00894
    00896         virtual void init() {};
    00897
    00898 };
    00899
    00903 class mepdf : public mpdf {
    00905         shared_ptr<epdf> iepdf;
    00906 public:
    00908         mepdf() { }
    00910         mepdf ( shared_ptr<epdf> em ) {
    00911                 iepdf = em;
    00912                 set_ep ( *iepdf.get() );
    00913                 dimc = 0;
    00914         }
    00915
    00917         vec samplecond ( const vec &cond ) {
    00918                 return iepdf->sample();
    00919         }
    00920         double evallogcond ( const vec &val, const vec &cond ) {
    00921                 return iepdf->evallog ( val );
    00922         }
    00923
    00931         void from_setting ( const Setting &set );
    00932 };
    00933 UIREGISTER ( mepdf );
    00934 SHAREDPTR ( mepdf );
    00935
    00937 RV get_composite_rv ( const Array<shared_ptr<mpdf> > &mpdfs, bool checkoverlap = false );
    00938
    00954 class DS : public root {
    00955 protected:
    00957         int dtsize;
    00959         int utsize;
    00961                 int ytsize;
    00963         RV Drv;
    00965         RV Urv; //
    00967                 RV Yrv; //
    00969         ivec LIDs;
    00970 public:
    00972                 DS() : Drv(), Urv(),Yrv(), LIDs(2) {};
    00973
    00975         virtual int max_length() {return std::numeric_limits< int >::max();}
    00977         virtual void getdata ( vec &dt ) {
    00978                 bdm_error ( "abstract class" );
    00979         }
    00981         virtual void getdata ( vec &dt, const ivec &indeces ) {
    00982                 bdm_error ( "abstract class" );
    00983         }
    00984
    00986         virtual void write (const vec &ut ) {
    00987                 bdm_error ( "abstract class" );
    00988         }
    00989
    00991         virtual void write (const vec &ut, const ivec &indeces ) {
    00992                 bdm_error ( "abstract class" );
    00993         }
    00994
    00996         virtual void step()
    00997         {
    00998                 bdm_error ( "abstract class" );
    00999         }
    01000
    01002         virtual void log_add ( logger &L ) {
    01003                 bdm_assert ( dtsize == Drv._dsize(), "invalid DS: dtsize (" + num2str ( dtsize ) + ") different from Drv " + num2str ( Drv._dsize() ) );
    01004                 bdm_assert ( utsize == Urv._dsize(), "invalid DS: utsize (" + num2str ( utsize ) + ") different from Urv " + num2str ( Urv._dsize() ) );
    01005
    01006                 LIDs(0) = L.add ( Drv, "" );
    01007                 LIDs(1) = L.add ( Urv, "" );
    01008         }
    01010         virtual void logit ( logger &L ) {
    01011                 vec tmp ( Drv._dsize() + Urv._dsize() );
    01012                 getdata ( tmp );
    01013                 // d is first in getdata
    01014                 L.logit ( LIDs(0), tmp.left ( Drv._dsize() ) );
    01015                 // u follows after d in getdata
    01016                 L.logit ( LIDs(1), tmp.mid ( Drv._dsize(), Urv._dsize() ) );
    01017         }
    01019         virtual const RV& _drv() const {
    01020                 //              return concat (Drv, Urv);// why!!!
    01021                 return Drv;// why!!!
    01022         }
    01024         const RV& _urv() const {
    01025                 return Urv;
    01026         }
    01028                 const RV& _yrv() const {
    01029                         return Yrv;
    01030                 }
    01032                 virtual void set_drv (const  RV &yrv, const RV &urv) {
    01033                     Yrv = yrv;
    01034                         Drv = concat(yrv,urv);
    01035                 Urv = urv;
    01036         }
    01037 };
    01038
    01060 class BM : public root {
    01061 protected:
    01063         RV drv;
    01065         double ll;
    01067         bool evalll;
    01068 public:
    01071
    01072         BM() : ll ( 0 ), evalll ( true ), LIDs ( 4 ), LFlags ( 4 ) {
    01073                 LIDs = -1;/*empty IDs*/
    01074                 LFlags = 0;
    01075                 LFlags ( 0 ) = 1;  /*log only mean*/
    01076         };
    01077         BM ( const BM &B ) :  drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {}
    01080         virtual BM* _copy_() const {
    01081                 return NULL;
    01082         };
    01084
    01087
    01091         virtual void bayes ( const vec &dt ) = 0;
    01093         virtual void bayesB ( const mat &Dt );
    01096         virtual double logpred ( const vec &dt ) const {
    01097                 bdm_error ( "Not implemented" );
    01098                 return 0.0;
    01099         }
    01100
    01102         vec logpred_m ( const mat &dt ) const {
    01103                 vec tmp ( dt.cols() );
    01104                 for ( int i = 0; i < dt.cols(); i++ ) {
    01105                         tmp ( i ) = logpred ( dt.get_col ( i ) );
    01106                 }
    01107                 return tmp;
    01108         }
    01109
    01111         virtual epdf* epredictor() const {
    01112                 bdm_error ( "Not implemented" );
    01113                 return NULL;
    01114         };
    01116         virtual mpdf* predictor() const {
    01117                 bdm_error ( "Not implemented" );
    01118                 return NULL;
    01119         };
    01121
    01126
    01128         RV rvc;
    01130         const RV& _rvc() const {
    01131                 return rvc;
    01132         }
    01133
    01135         virtual void condition ( const vec &val ) {
    01136                 bdm_error ( "Not implemented!" );
    01137         }
    01138
    01140
    01141
    01145         const RV& _drv() const {
    01146                 return drv;
    01147         }
    01149         void set_drv ( const RV &rv ) {
    01150                 drv = rv;
    01151         }
    01153         void set_rv ( const RV &rv ) {
    01154                 const_cast<epdf&> ( posterior() ).set_rv ( rv );
    01155         }
    01157         double _ll() const {
    01158                 return ll;
    01159         }
    01161         void set_evalll ( bool evl0 ) {
    01162                 evalll = evl0;
    01163         }
    01165         virtual const epdf& posterior() const = 0;
    01167
    01170
    01172         virtual void set_options ( const string &opt ) {
    01173                 LFlags ( 0 ) = 1;
    01174                 if ( opt.find ( "logbounds" ) != string::npos ) {
    01175                         LFlags ( 1 ) = 1;
    01176                         LFlags ( 2 ) = 1;
    01177                 }
    01178                 if ( opt.find ( "logll" ) != string::npos ) {
    01179                         LFlags ( 3 ) = 1;
    01180                 }
    01181         }
    01183         ivec LIDs;
    01184
    01186         ivec LFlags;
    01188         virtual void log_add ( logger &L, const string &name = "" ) {
    01189                 // internal
    01190                 RV r;
    01191                 if ( posterior().isnamed() ) {
    01192                         r = posterior()._rv();
    01193                 } else {
    01194                         r = RV ( "est", posterior().dimension() );
    01195                 };
    01196
    01197                 // Add mean value
    01198                 if ( LFlags ( 0 ) ) LIDs ( 0 ) = L.add ( r, name + "mean_" );
    01199                 if ( LFlags ( 1 ) ) LIDs ( 1 ) = L.add ( r, name + "lb_" );
    01200                 if ( LFlags ( 2 ) ) LIDs ( 2 ) = L.add ( r, name + "ub_" );
    01201                 if ( LFlags ( 3 ) ) LIDs ( 3 ) = L.add ( RV ( "ll", 1 ), name );    //TODO: "local" RV
    01202         }
    01204         virtual void logit ( logger &L ) {
    01205                 L.logit ( LIDs ( 0 ), posterior().mean() );
    01206                 if ( LFlags ( 1 ) || LFlags ( 2 ) ) {  //if one of them is off, its LID==-1 and will not be stored
    01207                         vec ub, lb;
    01208                         posterior().qbounds ( lb, ub );
    01209                         L.logit ( LIDs ( 1 ), lb );
    01210                         L.logit ( LIDs ( 2 ), ub );
    01211                 }
    01212                 if ( LFlags ( 3 ) ) L.logit ( LIDs ( 3 ), ll );
    01213         }
    01215         void from_setting ( const Setting &set ) {
    01216                 shared_ptr<RV> r = UI::build<RV> ( set, "drv", UI::optional );
    01217                 if ( r ) {
    01218                         set_drv ( *r );
    01219                 }
    01220                 string opt;
    01221                 if ( !UI::get ( opt, set, "options", UI::optional ) ) {
    01222                         set_options ( opt );
    01223                 }
    01224         }
    01225
    01226 };
    01228 typedef Array<shared_ptr<epdf> > epdf_array;
    01230 typedef Array<shared_ptr<mpdf> > mpdf_array;
    01231
    01232 template<class EPDF>
    01233 vec mpdf_internal<EPDF>::samplecond ( const vec &cond ) {
    01234         condition ( cond );
    01235         vec temp = iepdf.sample();
    01236         return temp;
    01237 }
    01238
    01239 template<class EPDF>
    01240 mat mpdf_internal<EPDF>::samplecond_m ( const vec &cond, int N ) {
    01241         condition ( cond );
    01242         mat temp ( dimension(), N );
    01243         vec smp ( dimension() );
    01244         for ( int i = 0; i < N; i++ ) {
    01245                 smp = iepdf.sample();
    01246                 temp.set_col ( i, smp );
    01247         }
    01248
    01249         return temp;
    01250 }
    01251
    01252 template<class EPDF>
    01253 double mpdf_internal<EPDF>::evallogcond ( const vec &dt, const vec &cond ) {
    01254         double tmp;
    01255         condition ( cond );
    01256         tmp = iepdf.evallog ( dt );
    01257         return tmp;
    01258 }
    01259
    01260 template<class EPDF>
    01261 vec mpdf_internal<EPDF>::evallogcond_m ( const mat &Dt, const vec &cond ) {
    01262         condition ( cond );
    01263         return iepdf.evallog_m ( Dt );
    01264 }
    01265
    01266 template<class EPDF>
    01267 vec mpdf_internal<EPDF>::evallogcond_m ( const Array<vec> &Dt, const vec &cond ) {
    01268         condition ( cond );
    01269         return iepdf.evallog_m ( Dt );
    01270 }
    01271
    01272 }; //namespace
    01273 #endif // BDMBASE_H