- 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_debug ( 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         }
    00183         void set_time ( int at, int time0 ) {
    00184                 times ( at ) = time0;
    00185         }
    00187
    00190
    00192         ivec findself ( const RV &rv2 ) const;
    00194         ivec findself_ids ( const RV &rv2 ) const;
    00196         bool equal ( const RV &rv2 ) const;
    00198         bool add ( const RV &rv2 );
    00200         RV subt ( const RV &rv2 ) const;
    00202         RV subselect ( const ivec &ind ) const;
    00203
    00205         RV operator() ( const ivec &ind ) const {
    00206                 return subselect ( ind );
    00207         }
    00208
    00210         RV operator() ( int di1, int di2 ) const;
    00211
    00213         void t_plus ( int delta );
    00215
    00218         RV remove_time() const {
    00219                 return RV ( unique ( ids ), dummy );
    00220         }
    00222         RV copy_t(int dt) const {
    00223                 RV tmp=*this;
    00224                 tmp.t_plus(dt);
    00225                 return tmp;
    00226         }
    00228         RV expand_delayes() const {
    00229                 RV rvt = this->remove_time(); //rv at t=0
    00230                 RV tmp = rvt;
    00231                 int td = mint();
    00232                 for ( int i = -1; i >= td; i-- ) {
    00233                         rvt.t_plus ( -1 );
    00234                         tmp.add ( rvt ); //shift u1
    00235                 }
    00236                 return tmp;
    00237         }
    00239
    00242
    00244         str tostr() const;
    00247         ivec dataind ( const RV &crv ) const;
    00249         ivec dataind_part ( const RV &crv ) const;
    00252         void dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const;
    00254         int mint() const {
    00255                         return times.length()>0 ? min (times) : 0;
    00256         }
    00258
    00273         void from_setting ( const Setting &set );
    00274
    00275         // TODO dodelat void to_setting( Setting &set ) const;
    00276
    00278         static void clear_all();
    00280         string show_all();
    00281
    00282 };
    00283 UIREGISTER ( RV );
    00284 SHAREDPTR ( RV );
    00285
    00287 RV concat ( const RV &rv1, const RV &rv2 );
    00288
    00290 class fnc : public root {
    00291 protected:
    00293         int dimy;
    00294 public:
    00296         fnc() {};
    00298         virtual vec eval ( const vec &cond ) {
    00299                 return vec ( 0 );
    00300         };
    00301
    00303         virtual void condition ( const vec &val ) {};
    00304
    00306         int dimension() const {
    00307                 return dimy;
    00308         }
    00309 };
    00310
    00311 class mpdf;
    00312
    00314
    00315 class epdf : public root {
    00316 protected:
    00318         int dim;
    00320         RV rv;
    00321
    00322 public:
    00334         epdf() : dim ( 0 ), rv() {};
    00335         epdf ( const epdf &e ) : dim ( e.dim ), rv ( e.rv ) {};
    00336         epdf ( const RV &rv0 ) : dim ( rv0._dsize() ) {
    00337                 set_rv ( rv0 );
    00338         };
    00339         void set_parameters ( int dim0 ) {
    00340                 dim = dim0;
    00341         }
    00343
    00346
    00348         virtual vec sample() const {
    00349                 bdm_error ( "not implemented" );
    00350                 return vec();
    00351         }
    00352
    00354         virtual mat sample_m ( int N ) const;
    00355
    00358         virtual double evallog ( const vec &val ) const {
    00359                 bdm_error ( "not implemented" );
    00360                 return 0.0;
    00361         }
    00362
    00364         virtual vec evallog_m ( const mat &Val ) const;
    00365
    00367         virtual vec evallog_m ( const Array<vec> &Avec ) const;
    00368
    00370         virtual shared_ptr<mpdf> condition ( const RV &rv ) const;
    00371
    00373         virtual shared_ptr<epdf> marginal ( const RV &rv ) const;
    00374
    00376         virtual vec mean() const {
    00377                 bdm_error ( "not implemneted" );
    00378                 return vec();
    00379         }
    00380
    00382         virtual vec variance() const {
    00383                 bdm_error ( "not implemneted" );
    00384                 return vec();
    00385         }
    00386
    00388         virtual void qbounds ( vec &lb, vec &ub, double percentage = 0.95 ) const {
    00389                 vec mea = mean();
    00390                 vec std = sqrt ( variance() );
    00391                 lb = mea - 2 * std;
    00392                 ub = mea + 2 * std;
    00393         };
    00395
    00401
    00403         void set_rv ( const RV &rv0 ) {
    00404                 rv = rv0;
    00405         }
    00406
    00408         bool isnamed() const {
    00409                 bool b = ( dim == rv._dsize() );
    00410                 return b;
    00411         }
    00412
    00414         const RV& _rv() const {
    00415                 bdm_assert_debug ( isnamed(), "" );
    00416                 return rv;
    00417         }
    00419
    00422
    00424         int dimension() const {
    00425                 return dim;
    00426         }
    00434         void from_setting ( const Setting &set ) {
    00435                 shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional );
    00436                 if ( r ) {
    00437                         set_rv ( *r );
    00438                 }
    00439         }
    00440
    00441 };
    00442 SHAREDPTR ( epdf );
    00443
    00445 class mpdf : public root {
    00446 protected:
    00448         int dimc;
    00450         RV rvc;
    00451 private:
    00453         epdf* ep;
    00454
    00455 protected:
    00457         void set_ep ( epdf &iepdf ) {
    00458                 ep = &iepdf;
    00459         }
    00461         void set_ep ( epdf *iepdfp ) {
    00462                 ep = iepdfp;
    00463         }
    00464
    00465 public:
    00468
    00469         mpdf() : dimc ( 0 ), rvc(), ep ( NULL ) { }
    00470
    00471         mpdf ( const mpdf &m ) : dimc ( m.dimc ), rvc ( m.rvc ), ep ( m.ep ) { }
    00473
    00476
    00478         virtual vec samplecond ( const vec &cond ) {
    00479                 bdm_error ( "Not implemented" );
    00480                 return vec();
    00481         }
    00482
    00484         virtual mat samplecond_m ( const vec &cond, int N );
    00485
    00487         virtual double evallogcond ( const vec &dt, const vec &cond ) {
    00488                 bdm_error ( "Not implemented" );
    00489                 return 0.0;
    00490         }
    00491
    00493         virtual vec evallogcond_m ( const mat &Dt, const vec &cond ) {
    00494                 vec v ( Dt.cols() );
    00495                 for ( int i = 0; i < Dt.cols(); i++ ) {
    00496                         v ( i ) = evallogcond ( Dt.get_col ( i ), cond );
    00497                 }
    00498                 return v;
    00499         }
    00500
    00502         virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ) {
    00503                 bdm_error ( "Not implemented" );
    00504                 return vec();
    00505         }
    00506
    00509
    00510         const RV& _rv() const {
    00511                 return ep->_rv();
    00512         }
    00513         const RV& _rvc() const {
    00514                 return rvc;
    00515         }
    00516
    00517         int dimension() const {
    00518                 return ep->dimension();
    00519         }
    00520         int dimensionc() {
    00521                 return dimc;
    00522         }
    00523
    00533         void from_setting ( const Setting &set );
    00535
    00538         void set_rvc ( const RV &rvc0 ) {
    00539                 rvc = rvc0;
    00540         }
    00541         void set_rv ( const RV &rv0 ) {
    00542                 ep->set_rv ( rv0 );
    00543         }
    00544         bool isnamed() {
    00545                 return ( ep->isnamed() ) && ( dimc == rvc._dsize() );
    00546         }
    00548 };
    00549 SHAREDPTR ( mpdf );
    00550
    00552 template <class EPDF>
    00553 class mpdf_internal: public mpdf {
    00554 protected :
    00556         EPDF iepdf;
    00557 public:
    00559         mpdf_internal() : mpdf(), iepdf() {
    00560                 set_ep ( iepdf );
    00561         }
    00562
    00565         virtual void condition ( const vec &cond ) {
    00566                 bdm_error ( "Not implemented" );
    00567         }
    00568
    00570         EPDF& e() {
    00571                 return iepdf;
    00572         }
    00573
    00575         vec samplecond ( const vec &cond );
    00577         double evallogcond ( const vec &val, const vec &cond );
    00579         virtual vec evallogcond_m ( const mat &Dt, const vec &cond );
    00581         virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond );
    00583         virtual mat samplecond_m ( const vec &cond, int N );
    00584 };
    00585
    00611 class datalink {
    00612 protected:
    00614         int downsize;
    00615
    00617         int upsize;
    00618
    00620         ivec v2v_up;
    00621
    00622 public:
    00624         datalink() : downsize ( 0 ), upsize ( 0 ) { }
    00625
    00627         datalink ( const RV &rv, const RV &rv_up ) {
    00628                 set_connection ( rv, rv_up );
    00629         }
    00630
    00632         void set_connection ( const RV &rv, const RV &rv_up );
    00633
    00635         void set_connection ( int ds, int us, const ivec &upind );
    00636
    00638         vec pushdown ( const vec &val_up ) {
    00639                 vec tmp ( downsize );
    00640                 filldown ( val_up, tmp );
    00641                 return tmp;
    00642         }
    00644         void filldown ( const vec &val_up, vec &val_down ) {
    00645                 bdm_assert_debug ( upsize == val_up.length(), "Wrong val_up" );
    00646                 val_down = val_up ( v2v_up );
    00647         }
    00649         void pushup ( vec &val_up, const vec &val ) {
    00650                 bdm_assert_debug ( downsize == val.length(), "Wrong val" );
    00651                 bdm_assert_debug ( upsize == val_up.length(), "Wrong val_up" );
    00652                 set_subvector ( val_up, v2v_up, val );
    00653         }
    00655         int _upsize() {
    00656                 return upsize;
    00657         }
    00659         int _downsize() {
    00660                 return downsize;
    00661         }
    00662 };
    00663
    00666 class datalink_part : public datalink {
    00667 protected:
    00669         ivec v2v_down;
    00670 public:
    00671         void set_connection ( const RV &rv, const RV &rv_up );
    00673         void filldown ( const vec &val_up, vec &val_down ) {
    00674                 set_subvector ( val_down, v2v_down, val_up ( v2v_up ) );
    00675         }
    00676 };
    00677
    00682 class datalink_buffered: public datalink_part {
    00683 protected:
    00685         vec history;
    00687         RV Hrv;
    00689         ivec h2v_down;
    00691         ivec h2v_hist;
    00693         ivec v2h_up;
    00694 public:
    00695
    00696         datalink_buffered() : datalink_part(), history ( 0 ), h2v_down ( 0 ), h2v_hist ( 0 ) {};
    00698         void step ( const vec &val_up ) {
    00699                 if ( Hrv._dsize() > 0 ) {
    00700                         history.shift_right ( 0, Hrv._dsize() );
    00701                   history.set_subvector ( 0, val_up(v2h_up) ); // write U after Drv
    00702                 }
    00703         }
    00705         vec pushdown ( const vec &val_up ) {
    00706                 vec tmp ( downsize );
    00707                 filldown ( val_up, tmp );
    00708                 return tmp;
    00709         }
    00710
    00711         void filldown ( const vec &val_up, vec &val_down ) {
    00712                 bdm_assert_debug ( val_down.length() >= downsize, "short val_down" );
    00713
    00714                 set_subvector ( val_down, v2v_down, val_up ( v2v_up ) ); // copy direct values
    00715                 set_subvector ( val_down, h2v_down, history ( h2v_hist ) ); // copy delayed values
    00716         }
    00717
    00718         void set_connection ( const RV &rv, const RV &rv_up ) {
    00719                 // create link between up and down
    00720                 datalink_part::set_connection ( rv, rv_up );
    00721
    00722                 // create rvs of history
    00723                 // we can store only what we get in rv_up - everything else is removed
    00724                 ivec valid_ids = rv.findself_ids ( rv_up );
    00725                 RV rv_hist = rv.subselect ( find ( valid_ids >= 0 ) ); // select only rvs that are in rv_up
    00726           RV rv_hist0 =rv_hist.remove_time();  // these RVs will form history at time =0
    00727           v2h_up = rv_hist0.dataind(rv_up); // indeces of elements of rv_up to be copied
    00728           // now we need to know what is needed from Up
    00729                 rv_hist = rv_hist.expand_delayes(); // full regressor - including time 0
    00730           Hrv=rv_hist.subt(rv_hist0);   // remove time 0
    00731                 history = zeros ( Hrv._dsize() );
    00732
    00733                 Hrv.dataind ( rv, h2v_hist, h2v_down );
    00734
    00735                 downsize = v2v_down.length() + h2v_down.length();
    00736                 upsize = v2v_up.length();
    00737         }
    00738 };
    00739
    00741 class datalink_2to1_buffered {
    00742 protected:
    00743         datalink_buffered dl1;
    00744         datalink_buffered dl2;
    00745 public:
    00747         void set_connection ( const RV &rv, const RV &rv_up1, const RV &rv_up2 ) {
    00748                 dl1.set_connection ( rv, rv_up1 );
    00749                 dl2.set_connection ( rv, rv_up2 );
    00750         }
    00751         void filldown ( const vec &val1, const vec &val2, vec &val_down ) {
    00752                 bdm_assert_debug ( val_down.length() >= dl1._downsize() + dl2._downsize(), "short val_down" );
    00753                 dl1.filldown ( val1, val_down );
    00754                 dl2.filldown ( val2, val_down );
    00755         }
    00756         void step ( const vec &dt, const vec &ut ) {
    00757                 dl1.step ( dt );
    00758                 dl2.step ( ut );
    00759         }
    00760 };
    00761
    00762
    00763
    00765 class datalink_m2e: public datalink {
    00766 protected:
    00768         int condsize;
    00769
    00771         ivec v2c_up;
    00772
    00774         ivec v2c_lo;
    00775
    00776 public:
    00778         datalink_m2e() : condsize ( 0 ) { }
    00779
    00781         void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up );
    00782
    00784         vec get_cond ( const vec &val_up );
    00785
    00787         void pushup_cond ( vec &val_up, const vec &val, const vec &cond );
    00788 };
    00789
    00792 class datalink_m2m: public datalink_m2e {
    00793 protected:
    00795         ivec c2c_up;
    00797         ivec c2c_lo;
    00798
    00799 public:
    00801         datalink_m2m() {};
    00803         void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up ) {
    00804                 datalink_m2e::set_connection ( rv, rvc, rv_up );
    00805                 //establish c2c connection
    00806                 rvc.dataind ( rvc_up, c2c_lo, c2c_up );
    00807                 bdm_assert_debug ( c2c_lo.length() + v2c_lo.length() == condsize, "cond is not fully given" );
    00808         }
    00809
    00811         vec get_cond ( const vec &val_up, const vec &cond_up ) {
    00812                 vec tmp ( condsize );
    00813                 set_subvector ( tmp, v2c_lo, val_up ( v2c_up ) );
    00814                 set_subvector ( tmp, c2c_lo, cond_up ( c2c_up ) );
    00815                 return tmp;
    00816         }
    00818
    00819 };
    00820
    00826 class logger : public root {
    00827 protected:
    00829         Array<RV> entries;
    00831         Array<string> names;
    00832 public:
    00834         logger() : entries ( 0 ), names ( 0 ) {}
    00835
    00838         virtual int add ( const RV &rv, string prefix = "" ) {
    00839                 int id;
    00840                 if ( rv._dsize() > 0 ) {
    00841                         id = entries.length();
    00842                         names = concat ( names, prefix ); // diff
    00843                         entries.set_length ( id + 1, true );
    00844                         entries ( id ) = rv;
    00845                 } else {
    00846                         id = -1;
    00847                 }
    00848                 return id; // identifier of the last entry
    00849         }
    00850
    00852         virtual void logit ( int id, const vec &v ) {
    00853                 bdm_error ( "Not implemented" );
    00854         };
    00856         virtual void logit ( int id, const double &d ) {
    00857                 bdm_error ( "Not implemented" );
    00858         };
    00859
    00861         virtual void step() {
    00862                 bdm_error ( "Not implemneted" );
    00863         };
    00864
    00866         virtual void finalize() {};
    00867
    00869         virtual void init() {};
    00870
    00871 };
    00872
    00876 class mepdf : public mpdf {
    00878         shared_ptr<epdf> iepdf;
    00879 public:
    00881         mepdf() { }
    00883         mepdf ( shared_ptr<epdf> em ) {
    00884                 iepdf = em;
    00885                 set_ep ( *iepdf.get() );
    00886                 dimc = 0;
    00887         }
    00888
    00890         vec samplecond ( const vec &cond ) {
    00891                 return iepdf->sample();
    00892         }
    00893         double evallogcond ( const vec &val, const vec &cond ) {
    00894                 return iepdf->evallog ( val );
    00895         }
    00896
    00904         void from_setting ( const Setting &set );
    00905 };
    00906 UIREGISTER ( mepdf );
    00907 SHAREDPTR ( mepdf );
    00908
    00910 RV get_composite_rv ( const Array<shared_ptr<mpdf> > &mpdfs, bool checkoverlap = false );
    00911
    00927 class DS : public root {
    00928 protected:
    00930         int dtsize;
    00932         int utsize;
    00934                 int ytsize;
    00936         RV Drv;
    00938         RV Urv; //
    00940                 RV Yrv; //
    00942         int L_dt, L_ut;
    00943 public:
    00945                 DS() : Drv(), Urv(),Yrv() {};
    00946
    00948         virtual void getdata ( vec &dt ) {
    00949                 bdm_error ( "abstract class" );
    00950         }
    00951
    00953         virtual void getdata ( vec &dt, const ivec &indeces ) {
    00954                 bdm_error ( "abstract class" );
    00955         }
    00956
    00958         virtual void write ( vec &ut ) {
    00959                 bdm_error ( "abstract class" );
    00960         }
    00961
    00963         virtual void write ( vec &ut, const ivec &indeces ) {
    00964                 bdm_error ( "abstract class" );
    00965         }
    00966
    00968         virtual void step() = 0;
    00969
    00971         virtual void log_add ( logger &L ) {
    00972                 bdm_assert_debug ( dtsize == Drv._dsize(), "invalid DS: dtsize (" + num2str ( dtsize ) + ") different from Drv " + num2str ( Drv._dsize() ) );
    00973                 bdm_assert_debug ( utsize == Urv._dsize(), "invalid DS: utsize (" + num2str ( utsize ) + ") different from Urv " + num2str ( Urv._dsize() ) );
    00974
    00975                 L_dt = L.add ( Drv, "" );
    00976                 L_ut = L.add ( Urv, "" );
    00977         }
    00979         virtual void logit ( logger &L ) {
    00980                 vec tmp ( Drv._dsize() + Urv._dsize() );
    00981                 getdata ( tmp );
    00982                 // d is first in getdata
    00983                 L.logit ( L_dt, tmp.left ( Drv._dsize() ) );
    00984                 // u follows after d in getdata
    00985                 L.logit ( L_ut, tmp.mid ( Drv._dsize(), Urv._dsize() ) );
    00986         }
    00988         virtual const RV& _drv() const {
    00989                 //              return concat (Drv, Urv);// why!!!
    00990                 return Drv;// why!!!
    00991         }
    00993         const RV& _urv() const {
    00994                 return Urv;
    00995         }
    00997                 const RV& _yrv() const {
    00998                         return Yrv;
    00999                 }
    01001                 virtual void set_drv (const  RV &yrv, const RV &urv) {
    01002                     Yrv = yrv;
    01003                         Drv = concat(yrv,urv);
    01004                 Urv = urv;
    01005         }
    01006 };
    01007
    01029 class BM : public root {
    01030 protected:
    01032         RV drv;
    01034         double ll;
    01036         bool evalll;
    01037 public:
    01040
    01041         BM() : ll ( 0 ), evalll ( true ), LIDs ( 4 ), LFlags ( 4 ) {
    01042                 LIDs = -1;/*empty IDs*/
    01043                 LFlags = 0;
    01044                 LFlags ( 0 ) = 1;  /*log only mean*/
    01045         };
    01046         BM ( const BM &B ) :  drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {}
    01049         virtual BM* _copy_() const {
    01050                 return NULL;
    01051         };
    01053
    01056
    01060         virtual void bayes ( const vec &dt ) = 0;
    01062         virtual void bayesB ( const mat &Dt );
    01065         virtual double logpred ( const vec &dt ) const {
    01066                 bdm_error ( "Not implemented" );
    01067                 return 0.0;
    01068         }
    01069
    01071         vec logpred_m ( const mat &dt ) const {
    01072                 vec tmp ( dt.cols() );
    01073                 for ( int i = 0; i < dt.cols(); i++ ) {
    01074                         tmp ( i ) = logpred ( dt.get_col ( i ) );
    01075                 }
    01076                 return tmp;
    01077         }
    01078
    01080         virtual epdf* epredictor() const {
    01081                 bdm_error ( "Not implemented" );
    01082                 return NULL;
    01083         };
    01085         virtual mpdf* predictor() const {
    01086                 bdm_error ( "Not implemented" );
    01087                 return NULL;
    01088         };
    01090
    01095
    01097         RV rvc;
    01099         const RV& _rvc() const {
    01100                 return rvc;
    01101         }
    01102
    01104         virtual void condition ( const vec &val ) {
    01105                 bdm_error ( "Not implemented!" );
    01106         }
    01107
    01109
    01110
    01113
    01114         const RV& _drv() const {
    01115                 return drv;
    01116         }
    01117         void set_drv ( const RV &rv ) {
    01118                 drv = rv;
    01119         }
    01120         void set_rv ( const RV &rv ) {
    01121                 const_cast<epdf&> ( posterior() ).set_rv ( rv );
    01122         }
    01123         double _ll() const {
    01124                 return ll;
    01125         }
    01126         void set_evalll ( bool evl0 ) {
    01127                 evalll = evl0;
    01128         }
    01129         virtual const epdf& posterior() const = 0;
    01131
    01134
    01136         virtual void set_options ( const string &opt ) {
    01137                 LFlags ( 0 ) = 1;
    01138                 if ( opt.find ( "logbounds" ) != string::npos ) {
    01139                         LFlags ( 1 ) = 1;
    01140                         LFlags ( 2 ) = 1;
    01141                 }
    01142                 if ( opt.find ( "logll" ) != string::npos ) {
    01143                         LFlags ( 3 ) = 1;
    01144                 }
    01145         }
    01147         ivec LIDs;
    01148
    01150         ivec LFlags;
    01152         virtual void log_add ( logger &L, const string &name = "" ) {
    01153                 // internal
    01154                 RV r;
    01155                 if ( posterior().isnamed() ) {
    01156                         r = posterior()._rv();
    01157                 } else {
    01158                         r = RV ( "est", posterior().dimension() );
    01159                 };
    01160
    01161                 // Add mean value
    01162                 if ( LFlags ( 0 ) ) LIDs ( 0 ) = L.add ( r, name + "mean_" );
    01163                 if ( LFlags ( 1 ) ) LIDs ( 1 ) = L.add ( r, name + "lb_" );
    01164                 if ( LFlags ( 2 ) ) LIDs ( 2 ) = L.add ( r, name + "ub_" );
    01165                 if ( LFlags ( 3 ) ) LIDs ( 3 ) = L.add ( RV ( "ll", 1 ), name );    //TODO: "local" RV
    01166         }
    01167         virtual void logit ( logger &L ) {
    01168                 L.logit ( LIDs ( 0 ), posterior().mean() );
    01169                 if ( LFlags ( 1 ) || LFlags ( 2 ) ) {  //if one of them is off, its LID==-1 and will not be stored
    01170                         vec ub, lb;
    01171                         posterior().qbounds ( lb, ub );
    01172                         L.logit ( LIDs ( 1 ), lb );
    01173                         L.logit ( LIDs ( 2 ), ub );
    01174                 }
    01175                 if ( LFlags ( 3 ) ) L.logit ( LIDs ( 3 ), ll );
    01176         }
    01178         void from_setting ( const Setting &set ) {
    01179                 shared_ptr<RV> r = UI::build<RV> ( set, "drv", UI::optional );
    01180                 if ( r ) {
    01181                         set_drv ( *r );
    01182                 }
    01183                 string opt;
    01184                 if ( !UI::get ( opt, set, "options", UI::optional ) ) {
    01185                         set_options ( opt );
    01186                 }
    01187         }
    01188
    01189 };
    01190
    01191 typedef Array<shared_ptr<epdf> > epdf_array;
    01192
    01193 typedef Array<shared_ptr<mpdf> > mpdf_array;
    01194
    01195 template<class EPDF>
    01196 vec mpdf_internal<EPDF>::samplecond ( const vec &cond ) {
    01197         condition ( cond );
    01198         vec temp = iepdf.sample();
    01199         return temp;
    01200 }
    01201
    01202 template<class EPDF>
    01203 mat mpdf_internal<EPDF>::samplecond_m ( const vec &cond, int N ) {
    01204         condition ( cond );
    01205         mat temp ( dimension(), N );
    01206         vec smp ( dimension() );
    01207         for ( int i = 0; i < N; i++ ) {
    01208                 smp = iepdf.sample();
    01209                 temp.set_col ( i, smp );
    01210         }
    01211
    01212         return temp;
    01213 }
    01214
    01215 template<class EPDF>
    01216 double mpdf_internal<EPDF>::evallogcond ( const vec &dt, const vec &cond ) {
    01217         double tmp;
    01218         condition ( cond );
    01219         tmp = iepdf.evallog ( dt );
    01220         return tmp;
    01221 }
    01222
    01223 template<class EPDF>
    01224 vec mpdf_internal<EPDF>::evallogcond_m ( const mat &Dt, const vec &cond ) {
    01225         condition ( cond );
    01226         return iepdf.evallog_m ( Dt );
    01227 }
    01228
    01229 template<class EPDF>
    01230 vec mpdf_internal<EPDF>::evallogcond_m ( const Array<vec> &Dt, const vec &cond ) {
    01231         condition ( cond );
    01232         return iepdf.evallog_m ( Dt );
    01233 }
    01234
    01235 }; //namespace
    01236 #endif // BDMBASE_H