- 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
    00286         void from_setting ( const Setting &set );
    00287
    00288         // TODO dodelat void to_setting( Setting &set ) const;
    00289
    00291         static void clear_all();
    00293         string show_all();
    00294
    00295 };
    00296 UIREGISTER ( RV );
    00297 SHAREDPTR ( RV );
    00298
    00300 RV concat ( const RV &rv1, const RV &rv2 );
    00301
    00303 class fnc : public root {
    00304 protected:
    00306         int dimy;
    00307 public:
    00309         fnc() {};
    00311         virtual vec eval ( const vec &cond ) {
    00312                 return vec ( 0 );
    00313         };
    00314
    00316         virtual void condition ( const vec &val ) {};
    00317
    00319         int dimension() const {
    00320                 return dimy;
    00321         }
    00322 };
    00323
    00324 class mpdf;
    00325
    00327
    00328 class epdf : public root {
    00329 protected:
    00331         int dim;
    00333         RV rv;
    00334
    00335 public:
    00347         epdf() : dim ( 0 ), rv() {};
    00348         epdf ( const epdf &e ) : dim ( e.dim ), rv ( e.rv ) {};
    00349         epdf ( const RV &rv0 ) : dim ( rv0._dsize() ) {
    00350                 set_rv ( rv0 );
    00351         };
    00352         void set_parameters ( int dim0 ) {
    00353                 dim = dim0;
    00354         }
    00356
    00359
    00361         virtual vec sample() const {
    00362                 bdm_error ( "not implemented" );
    00363                 return vec();
    00364         }
    00365
    00367         virtual mat sample_m ( int N ) const;
    00368
    00371         virtual double evallog ( const vec &val ) const {
    00372                 bdm_error ( "not implemented" );
    00373                 return 0.0;
    00374         }
    00375
    00377         virtual vec evallog_m ( const mat &Val ) const;
    00378
    00380         virtual vec evallog_m ( const Array<vec> &Avec ) const;
    00381
    00383         virtual shared_ptr<mpdf> condition ( const RV &rv ) const;
    00384
    00386         virtual shared_ptr<epdf> marginal ( const RV &rv ) const;
    00387
    00389         virtual vec mean() const {
    00390                 bdm_error ( "not implemneted" );
    00391                 return vec();
    00392         }
    00393
    00395         virtual vec variance() const {
    00396                 bdm_error ( "not implemneted" );
    00397                 return vec();
    00398         }
    00399
    00401         virtual void qbounds ( vec &lb, vec &ub, double percentage = 0.95 ) const {
    00402                 vec mea = mean();
    00403                 vec std = sqrt ( variance() );
    00404                 lb = mea - 2 * std;
    00405                 ub = mea + 2 * std;
    00406         };
    00408
    00414
    00416         void set_rv ( const RV &rv0 ) {
    00417                 rv = rv0;
    00418         }
    00419
    00421         bool isnamed() const {
    00422                 bool b = ( dim == rv._dsize() );
    00423                 return b;
    00424         }
    00425
    00427         const RV& _rv() const {
    00428                 bdm_assert_debug ( isnamed(), "" );
    00429                 return rv;
    00430         }
    00432
    00435
    00437         int dimension() const {
    00438                 return dim;
    00439         }
    00447         void from_setting ( const Setting &set ) {
    00448                 shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional );
    00449                 if ( r ) {
    00450                         set_rv ( *r );
    00451                 }
    00452         }
    00453
    00454 };
    00455 SHAREDPTR ( epdf );
    00456
    00458 class mpdf : public root {
    00459 protected:
    00461         int dimc;
    00463         RV rvc;
    00464 private:
    00466         epdf* ep;
    00467
    00468 protected:
    00470         void set_ep ( epdf &iepdf ) {
    00471                 ep = &iepdf;
    00472         }
    00474         void set_ep ( epdf *iepdfp ) {
    00475                 ep = iepdfp;
    00476         }
    00477
    00478 public:
    00481
    00482         mpdf() : dimc ( 0 ), rvc(), ep ( NULL ) { }
    00483
    00484         mpdf ( const mpdf &m ) : dimc ( m.dimc ), rvc ( m.rvc ), ep ( m.ep ) { }
    00486
    00489
    00491         virtual vec samplecond ( const vec &cond ) {
    00492                 bdm_error ( "Not implemented" );
    00493                 return vec();
    00494         }
    00495
    00497         virtual mat samplecond_m ( const vec &cond, int N );
    00498
    00500         virtual double evallogcond ( const vec &dt, const vec &cond ) {
    00501                 bdm_error ( "Not implemented" );
    00502                 return 0.0;
    00503         }
    00504
    00506         virtual vec evallogcond_m ( const mat &Dt, const vec &cond ) {
    00507                 vec v ( Dt.cols() );
    00508                 for ( int i = 0; i < Dt.cols(); i++ ) {
    00509                         v ( i ) = evallogcond ( Dt.get_col ( i ), cond );
    00510                 }
    00511                 return v;
    00512         }
    00513
    00515         virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ) {
    00516                 bdm_error ( "Not implemented" );
    00517                 return vec();
    00518         }
    00519
    00522
    00523         const RV& _rv() const {
    00524                 return ep->_rv();
    00525         }
    00526         const RV& _rvc() const {
    00527                 return rvc;
    00528         }
    00529
    00530         int dimension() const {
    00531                 return ep->dimension();
    00532         }
    00533         int dimensionc() {
    00534                 return dimc;
    00535         }
    00536
    00546         void from_setting ( const Setting &set );
    00548
    00551         void set_rvc ( const RV &rvc0 ) {
    00552                 rvc = rvc0;
    00553         }
    00554         void set_rv ( const RV &rv0 ) {
    00555                 ep->set_rv ( rv0 );
    00556         }
    00557         bool isnamed() {
    00558                 return ( ep->isnamed() ) && ( dimc == rvc._dsize() );
    00559         }
    00561 };
    00562 SHAREDPTR ( mpdf );
    00563
    00565 template <class EPDF>
    00566 class mpdf_internal: public mpdf {
    00567 protected :
    00569         EPDF iepdf;
    00570 public:
    00572         mpdf_internal() : mpdf(), iepdf() {
    00573                 set_ep ( iepdf );
    00574         }
    00575
    00578         virtual void condition ( const vec &cond ) {
    00579                 bdm_error ( "Not implemented" );
    00580         }
    00581
    00583         EPDF& e() {
    00584                 return iepdf;
    00585         }
    00586
    00588         vec samplecond ( const vec &cond );
    00590         double evallogcond ( const vec &val, const vec &cond );
    00592         virtual vec evallogcond_m ( const mat &Dt, const vec &cond );
    00594         virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond );
    00596         virtual mat samplecond_m ( const vec &cond, int N );
    00597 };
    00598
    00624 class datalink {
    00625 protected:
    00627         int downsize;
    00628
    00630         int upsize;
    00631
    00633         ivec v2v_up;
    00634
    00635 public:
    00637         datalink() : downsize ( 0 ), upsize ( 0 ) { }
    00638
    00640         datalink ( const RV &rv, const RV &rv_up ) {
    00641                 set_connection ( rv, rv_up );
    00642         }
    00643
    00645         void set_connection ( const RV &rv, const RV &rv_up );
    00646
    00648         void set_connection ( int ds, int us, const ivec &upind );
    00649
    00651         vec pushdown ( const vec &val_up ) {
    00652                 vec tmp ( downsize );
    00653                 filldown ( val_up, tmp );
    00654                 return tmp;
    00655         }
    00657         void filldown ( const vec &val_up, vec &val_down ) {
    00658                 bdm_assert_debug ( upsize == val_up.length(), "Wrong val_up" );
    00659                 val_down = val_up ( v2v_up );
    00660         }
    00662         void pushup ( vec &val_up, const vec &val ) {
    00663                 bdm_assert_debug ( downsize == val.length(), "Wrong val" );
    00664                 bdm_assert_debug ( upsize == val_up.length(), "Wrong val_up" );
    00665                 set_subvector ( val_up, v2v_up, val );
    00666         }
    00668         int _upsize() {
    00669                 return upsize;
    00670         }
    00672         int _downsize() {
    00673                 return downsize;
    00674         }
    00675 };
    00676
    00679 class datalink_part : public datalink {
    00680 protected:
    00682         ivec v2v_down;
    00683 public:
    00684         void set_connection ( const RV &rv, const RV &rv_up );
    00686         void filldown ( const vec &val_up, vec &val_down ) {
    00687                 set_subvector ( val_down, v2v_down, val_up ( v2v_up ) );
    00688         }
    00689 };
    00690
    00695 class datalink_buffered: public datalink_part {
    00696 protected:
    00698         vec history;
    00700         RV Hrv;
    00702         ivec h2v_down;
    00704         ivec h2v_hist;
    00706         ivec v2h_up;
    00707 public:
    00708
    00709         datalink_buffered() : datalink_part(), history ( 0 ), h2v_down ( 0 ), h2v_hist ( 0 ) {};
    00711         void step ( const vec &val_up ) {
    00712                 if ( v2h_up.length() > 0 ) {
    00713                         history.shift_right ( 0, v2h_up.length() );
    00714                   history.set_subvector ( 0, val_up(v2h_up) );
    00715                 }
    00716         }
    00718         vec pushdown ( const vec &val_up ) {
    00719                 vec tmp ( downsize );
    00720                 filldown ( val_up, tmp );
    00721                 return tmp;
    00722         }
    00723
    00724         void filldown ( const vec &val_up, vec &val_down ) {
    00725                 bdm_assert_debug ( val_down.length() >= downsize, "short val_down" );
    00726
    00727                 set_subvector ( val_down, v2v_down, val_up ( v2v_up ) ); // copy direct values
    00728                 set_subvector ( val_down, h2v_down, history ( h2v_hist ) ); // copy delayed values
    00729         }
    00730
    00731         void set_connection ( const RV &rv, const RV &rv_up ) {
    00732                 // create link between up and down
    00733                 datalink_part::set_connection ( rv, rv_up );
    00734
    00735                 // create rvs of history
    00736                 // we can store only what we get in rv_up - everything else is removed
    00737                 ivec valid_ids = rv.findself_ids ( rv_up );
    00738                 RV rv_hist = rv.subselect ( find ( valid_ids >= 0 ) ); // select only rvs that are in rv_up
    00739           RV rv_hist0 =rv_hist.remove_time();  // these RVs will form history at time =0
    00740           v2h_up = rv_hist0.dataind(rv_up); // indeces of elements of rv_up to be copied
    00741           // now we need to know what is needed from Up
    00742                 rv_hist = rv_hist.expand_delayes(); // full regressor - including time 0
    00743           Hrv=rv_hist.subt(rv_hist0);   // remove time 0
    00744                 history = zeros ( Hrv._dsize() );
    00745
    00746                 Hrv.dataind ( rv, h2v_hist, h2v_down );
    00747
    00748                 downsize = v2v_down.length() + h2v_down.length();
    00749                 upsize = v2v_up.length();
    00750         }
    00752         void set_history(const RV& rv1, const vec &hist0){
    00753                 bdm_assert(rv1._dsize()==hist0.length(),"hist is not compatible with given rv1");
    00754                 ivec ind_H;
    00755                 ivec ind_h0;
    00756                 Hrv.dataind(rv1, ind_H, ind_h0); // find indeces of rv in
    00757                 set_subvector(history, ind_H, hist0(ind_h0)); // copy given hist to appropriate places
    00758         }
    00759 };
    00760
    00762 class datalink_2to1_buffered {
    00763 protected:
    00764         datalink_buffered dl1;
    00765         datalink_buffered dl2;
    00766 public:
    00768         void set_connection ( const RV &rv, const RV &rv_up1, const RV &rv_up2 ) {
    00769                 dl1.set_connection ( rv, rv_up1 );
    00770                 dl2.set_connection ( rv, rv_up2 );
    00771         }
    00772         void filldown ( const vec &val1, const vec &val2, vec &val_down ) {
    00773                 bdm_assert_debug ( val_down.length() >= dl1._downsize() + dl2._downsize(), "short val_down" );
    00774                 dl1.filldown ( val1, val_down );
    00775                 dl2.filldown ( val2, val_down );
    00776         }
    00777         void step ( const vec &dt, const vec &ut ) {
    00778                 dl1.step ( dt );
    00779                 dl2.step ( ut );
    00780         }
    00781 };
    00782
    00783
    00784
    00786 class datalink_m2e: public datalink {
    00787 protected:
    00789         int condsize;
    00790
    00792         ivec v2c_up;
    00793
    00795         ivec v2c_lo;
    00796
    00797 public:
    00799         datalink_m2e() : condsize ( 0 ) { }
    00800
    00802         void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up );
    00803
    00805         vec get_cond ( const vec &val_up );
    00806
    00808         void pushup_cond ( vec &val_up, const vec &val, const vec &cond );
    00809 };
    00810
    00813 class datalink_m2m: public datalink_m2e {
    00814 protected:
    00816         ivec c2c_up;
    00818         ivec c2c_lo;
    00819
    00820 public:
    00822         datalink_m2m() {};
    00824         void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up ) {
    00825                 datalink_m2e::set_connection ( rv, rvc, rv_up );
    00826                 //establish c2c connection
    00827                 rvc.dataind ( rvc_up, c2c_lo, c2c_up );
    00828                 bdm_assert_debug ( c2c_lo.length() + v2c_lo.length() == condsize, "cond is not fully given" );
    00829         }
    00830
    00832         vec get_cond ( const vec &val_up, const vec &cond_up ) {
    00833                 vec tmp ( condsize );
    00834                 set_subvector ( tmp, v2c_lo, val_up ( v2c_up ) );
    00835                 set_subvector ( tmp, c2c_lo, cond_up ( c2c_up ) );
    00836                 return tmp;
    00837         }
    00839
    00840 };
    00841
    00847 class logger : public root {
    00848 protected:
    00850         Array<RV> entries;
    00852         Array<string> names;
    00853 public:
    00855         logger() : entries ( 0 ), names ( 0 ) {}
    00856
    00859         virtual int add ( const RV &rv, string prefix = "" ) {
    00860                 int id;
    00861                 if ( rv._dsize() > 0 ) {
    00862                         id = entries.length();
    00863                         names = concat ( names, prefix ); // diff
    00864                         entries.set_length ( id + 1, true );
    00865                         entries ( id ) = rv;
    00866                 } else {
    00867                         id = -1;
    00868                 }
    00869                 return id; // identifier of the last entry
    00870         }
    00871
    00873         virtual void logit ( int id, const vec &v ) {
    00874                 bdm_error ( "Not implemented" );
    00875         };
    00877         virtual void logit ( int id, const double &d ) {
    00878                 bdm_error ( "Not implemented" );
    00879         };
    00880
    00882         virtual void step() {
    00883                 bdm_error ( "Not implemneted" );
    00884         };
    00885
    00887         virtual void finalize() {};
    00888
    00890         virtual void init() {};
    00891
    00892 };
    00893
    00897 class mepdf : public mpdf {
    00899         shared_ptr<epdf> iepdf;
    00900 public:
    00902         mepdf() { }
    00904         mepdf ( shared_ptr<epdf> em ) {
    00905                 iepdf = em;
    00906                 set_ep ( *iepdf.get() );
    00907                 dimc = 0;
    00908         }
    00909
    00911         vec samplecond ( const vec &cond ) {
    00912                 return iepdf->sample();
    00913         }
    00914         double evallogcond ( const vec &val, const vec &cond ) {
    00915                 return iepdf->evallog ( val );
    00916         }
    00917
    00925         void from_setting ( const Setting &set );
    00926 };
    00927 UIREGISTER ( mepdf );
    00928 SHAREDPTR ( mepdf );
    00929
    00931 RV get_composite_rv ( const Array<shared_ptr<mpdf> > &mpdfs, bool checkoverlap = false );
    00932
    00948 class DS : public root {
    00949 protected:
    00951         int dtsize;
    00953         int utsize;
    00955                 int ytsize;
    00957         RV Drv;
    00959         RV Urv; //
    00961                 RV Yrv; //
    00963         int L_dt, L_ut;
    00964 public:
    00966                 DS() : Drv(), Urv(),Yrv() {};
    00967
    00969         virtual int max_length() {return std::numeric_limits< int >::max();}
    00971         virtual void getdata ( vec &dt ) {
    00972                 bdm_error ( "abstract class" );
    00973         }
    00975         virtual void getdata ( vec &dt, const ivec &indeces ) {
    00976                 bdm_error ( "abstract class" );
    00977         }
    00978
    00980         virtual void write ( vec &ut ) {
    00981                 bdm_error ( "abstract class" );
    00982         }
    00983
    00985         virtual void write ( vec &ut, const ivec &indeces ) {
    00986                 bdm_error ( "abstract class" );
    00987         }
    00988
    00990         virtual void step()
    00991         {
    00992                 bdm_error ( "abstract class" );
    00993         }
    00994
    00996         virtual void log_add ( logger &L ) {
    00997                 bdm_assert ( dtsize == Drv._dsize(), "invalid DS: dtsize (" + num2str ( dtsize ) + ") different from Drv " + num2str ( Drv._dsize() ) );
    00998                 bdm_assert ( utsize == Urv._dsize(), "invalid DS: utsize (" + num2str ( utsize ) + ") different from Urv " + num2str ( Urv._dsize() ) );
    00999
    01000                 L_dt = L.add ( Drv, "" );
    01001                 L_ut = L.add ( Urv, "" );
    01002         }
    01004         virtual void logit ( logger &L ) {
    01005                 vec tmp ( Drv._dsize() + Urv._dsize() );
    01006                 getdata ( tmp );
    01007                 // d is first in getdata
    01008                 L.logit ( L_dt, tmp.left ( Drv._dsize() ) );
    01009                 // u follows after d in getdata
    01010                 L.logit ( L_ut, tmp.mid ( Drv._dsize(), Urv._dsize() ) );
    01011         }
    01013         virtual const RV& _drv() const {
    01014                 //              return concat (Drv, Urv);// why!!!
    01015                 return Drv;// why!!!
    01016         }
    01018         const RV& _urv() const {
    01019                 return Urv;
    01020         }
    01022                 const RV& _yrv() const {
    01023                         return Yrv;
    01024                 }
    01026                 virtual void set_drv (const  RV &yrv, const RV &urv) {
    01027                     Yrv = yrv;
    01028                         Drv = concat(yrv,urv);
    01029                 Urv = urv;
    01030         }
    01031 };
    01032
    01054 class BM : public root {
    01055 protected:
    01057         RV drv;
    01059         double ll;
    01061         bool evalll;
    01062 public:
    01065
    01066         BM() : ll ( 0 ), evalll ( true ), LIDs ( 4 ), LFlags ( 4 ) {
    01067                 LIDs = -1;/*empty IDs*/
    01068                 LFlags = 0;
    01069                 LFlags ( 0 ) = 1;  /*log only mean*/
    01070         };
    01071         BM ( const BM &B ) :  drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {}
    01074         virtual BM* _copy_() const {
    01075                 return NULL;
    01076         };
    01078
    01081
    01085         virtual void bayes ( const vec &dt ) = 0;
    01087         virtual void bayesB ( const mat &Dt );
    01090         virtual double logpred ( const vec &dt ) const {
    01091                 bdm_error ( "Not implemented" );
    01092                 return 0.0;
    01093         }
    01094
    01096         vec logpred_m ( const mat &dt ) const {
    01097                 vec tmp ( dt.cols() );
    01098                 for ( int i = 0; i < dt.cols(); i++ ) {
    01099                         tmp ( i ) = logpred ( dt.get_col ( i ) );
    01100                 }
    01101                 return tmp;
    01102         }
    01103
    01105         virtual epdf* epredictor() const {
    01106                 bdm_error ( "Not implemented" );
    01107                 return NULL;
    01108         };
    01110         virtual mpdf* predictor() const {
    01111                 bdm_error ( "Not implemented" );
    01112                 return NULL;
    01113         };
    01115
    01120
    01122         RV rvc;
    01124         const RV& _rvc() const {
    01125                 return rvc;
    01126         }
    01127
    01129         virtual void condition ( const vec &val ) {
    01130                 bdm_error ( "Not implemented!" );
    01131         }
    01132
    01134
    01135
    01138
    01139         const RV& _drv() const {
    01140                 return drv;
    01141         }
    01142         void set_drv ( const RV &rv ) {
    01143                 drv = rv;
    01144         }
    01145         void set_rv ( const RV &rv ) {
    01146                 const_cast<epdf&> ( posterior() ).set_rv ( rv );
    01147         }
    01148         double _ll() const {
    01149                 return ll;
    01150         }
    01151         void set_evalll ( bool evl0 ) {
    01152                 evalll = evl0;
    01153         }
    01154         virtual const epdf& posterior() const = 0;
    01156
    01159
    01161         virtual void set_options ( const string &opt ) {
    01162                 LFlags ( 0 ) = 1;
    01163                 if ( opt.find ( "logbounds" ) != string::npos ) {
    01164                         LFlags ( 1 ) = 1;
    01165                         LFlags ( 2 ) = 1;
    01166                 }
    01167                 if ( opt.find ( "logll" ) != string::npos ) {
    01168                         LFlags ( 3 ) = 1;
    01169                 }
    01170         }
    01172         ivec LIDs;
    01173
    01175         ivec LFlags;
    01177         virtual void log_add ( logger &L, const string &name = "" ) {
    01178                 // internal
    01179                 RV r;
    01180                 if ( posterior().isnamed() ) {
    01181                         r = posterior()._rv();
    01182                 } else {
    01183                         r = RV ( "est", posterior().dimension() );
    01184                 };
    01185
    01186                 // Add mean value
    01187                 if ( LFlags ( 0 ) ) LIDs ( 0 ) = L.add ( r, name + "mean_" );
    01188                 if ( LFlags ( 1 ) ) LIDs ( 1 ) = L.add ( r, name + "lb_" );
    01189                 if ( LFlags ( 2 ) ) LIDs ( 2 ) = L.add ( r, name + "ub_" );
    01190                 if ( LFlags ( 3 ) ) LIDs ( 3 ) = L.add ( RV ( "ll", 1 ), name );    //TODO: "local" RV
    01191         }
    01192         virtual void logit ( logger &L ) {
    01193                 L.logit ( LIDs ( 0 ), posterior().mean() );
    01194                 if ( LFlags ( 1 ) || LFlags ( 2 ) ) {  //if one of them is off, its LID==-1 and will not be stored
    01195                         vec ub, lb;
    01196                         posterior().qbounds ( lb, ub );
    01197                         L.logit ( LIDs ( 1 ), lb );
    01198                         L.logit ( LIDs ( 2 ), ub );
    01199                 }
    01200                 if ( LFlags ( 3 ) ) L.logit ( LIDs ( 3 ), ll );
    01201         }
    01203         void from_setting ( const Setting &set ) {
    01204                 shared_ptr<RV> r = UI::build<RV> ( set, "drv", UI::optional );
    01205                 if ( r ) {
    01206                         set_drv ( *r );
    01207                 }
    01208                 string opt;
    01209                 if ( !UI::get ( opt, set, "options", UI::optional ) ) {
    01210                         set_options ( opt );
    01211                 }
    01212         }
    01213
    01214 };
    01215
    01216 typedef Array<shared_ptr<epdf> > epdf_array;
    01217
    01218 typedef Array<shared_ptr<mpdf> > mpdf_array;
    01219
    01220 template<class EPDF>
    01221 vec mpdf_internal<EPDF>::samplecond ( const vec &cond ) {
    01222         condition ( cond );
    01223         vec temp = iepdf.sample();
    01224         return temp;
    01225 }
    01226
    01227 template<class EPDF>
    01228 mat mpdf_internal<EPDF>::samplecond_m ( const vec &cond, int N ) {
    01229         condition ( cond );
    01230         mat temp ( dimension(), N );
    01231         vec smp ( dimension() );
    01232         for ( int i = 0; i < N; i++ ) {
    01233                 smp = iepdf.sample();
    01234                 temp.set_col ( i, smp );
    01235         }
    01236
    01237         return temp;
    01238 }
    01239
    01240 template<class EPDF>
    01241 double mpdf_internal<EPDF>::evallogcond ( const vec &dt, const vec &cond ) {
    01242         double tmp;
    01243         condition ( cond );
    01244         tmp = iepdf.evallog ( dt );
    01245         return tmp;
    01246 }
    01247
    01248 template<class EPDF>
    01249 vec mpdf_internal<EPDF>::evallogcond_m ( const mat &Dt, const vec &cond ) {
    01250         condition ( cond );
    01251         return iepdf.evallog_m ( Dt );
    01252 }
    01253
    01254 template<class EPDF>
    01255 vec mpdf_internal<EPDF>::evallogcond_m ( const Array<vec> &Dt, const vec &cond ) {
    01256         condition ( cond );
    01257         return iepdf.evallog_m ( Dt );
    01258 }
    01259
    01260 }; //namespace
    01261 #endif // BDMBASE_H