00001
00013 #ifndef BM_H
00014 #define BM_H
00015
00016
00017 #include "../itpp_ext.h"
00018 #include "../libconfig/libconfig.h++"
00019 #include <map>
00020
00021 using namespace libconfig;
00022
00023 using namespace itpp;
00024 using namespace std;
00025
00026 namespace bdm {
00027
00029
00030 class bdmroot {
00031 public:
00033 virtual ~bdmroot()
00034 {
00035 }
00036
00038 virtual string ToString()
00039 {
00040 return "";
00041 }
00042
00044 virtual void from_setting( const Setting &root )
00045 {
00046 }
00047
00049 virtual void to_setting( Setting &root )
00050 {
00051 }
00052 };
00053
00054 typedef std::map<string, int> RVmap;
00055 extern ivec RV_SIZES;
00056 extern Array<string> RV_NAMES;
00057
00059 class str {
00060 public:
00062 ivec ids;
00064 ivec times;
00066 str ( ivec ids0, ivec times0 ) :ids ( ids0 ),times ( times0 ) {
00067 it_assert_debug ( times0.length() ==ids0.length(),"Incompatible input" );
00068 };
00069 };
00070
00109 class RV :public bdmroot {
00110 protected:
00112 int dsize;
00114 int len;
00116 ivec ids;
00118 ivec times;
00119
00120 private:
00122 void init ( Array<std::string> in_names, ivec in_sizes, ivec in_times );
00123 int init ( const string &name, int size );
00124 public:
00127
00129 RV ( Array<std::string> in_names, ivec in_sizes, ivec in_times ) {init ( in_names,in_sizes,in_times );};
00131 RV ( Array<std::string> in_names, ivec in_sizes ) {init ( in_names,in_sizes,zeros_i ( in_names.length() ) );};
00133 RV ( Array<std::string> in_names ) {init ( in_names,ones_i ( in_names.length() ),zeros_i ( in_names.length() ) );}
00135 RV () :dsize ( 0 ),len ( 0 ),ids ( 0 ),times ( 0 ) {};
00137 RV ( string name, int sz, int tm=0 );
00139
00142
00144 friend std::ostream &operator<< ( std::ostream &os, const RV &rv );
00145 int _dsize() const {return dsize;} ;
00147 int countsize() const;
00148 ivec cumsizes() const;
00149 int length() const {return len;} ;
00150 int id ( int at ) const{return ids ( at );};
00151 int size ( int at ) const {return RV_SIZES ( ids ( at ) );};
00152 int time ( int at ) const{return times ( at );};
00153 std::string name ( int at ) const {return RV_NAMES ( ids ( at ) );};
00154 void set_time ( int at, int time0 ) {times ( at ) =time0;};
00156
00157
00158
00161
00163 ivec findself ( const RV &rv2 ) const;
00165 bool equal ( const RV &rv2 ) const;
00167 bool add ( const RV &rv2 );
00169 RV subt ( const RV &rv2 ) const;
00171 RV subselect ( const ivec &ind ) const;
00173 RV operator() ( const ivec &ind ) const {return subselect ( ind );};
00175 RV operator() ( int di1, int di2 ) const {
00176 ivec sz=cumsizes();
00177 int i1=0;
00178 while ( sz ( i1 ) <di1 ) i1++;
00179 int i2=i1;
00180 while ( sz ( i2 ) <di2 ) i2++;
00181 return subselect ( linspace ( i1,i2 ) );
00182 };
00184 void t ( int delta );
00186
00189
00191 str tostr() const;
00194 ivec dataind ( const RV &crv ) const;
00197 void dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const;
00199 int mint () const {return min ( times );};
00201
00202 };
00203
00204
00206 RV concat ( const RV &rv1, const RV &rv2 );
00207
00209 extern RV RV0;
00210
00212
00213 class fnc :public bdmroot {
00214 protected:
00216 int dimy;
00217 public:
00219 fnc ( ) {};
00221 virtual vec eval ( const vec &cond ) {
00222 return vec ( 0 );
00223 };
00224
00226 virtual void condition ( const vec &val ) {};
00227
00229 int dimension() const{return dimy;}
00230 };
00231
00232 class mpdf;
00233
00235
00236 class epdf :public bdmroot {
00237 protected:
00239 int dim;
00241 RV rv;
00242
00243 public:
00255 epdf() :dim ( 0 ),rv ( ) {};
00256 epdf ( const epdf &e ) :dim ( e.dim ),rv ( e.rv ) {};
00257 epdf ( const RV &rv0 ) {set_rv ( rv0 );};
00258 void set_parameters ( int dim0 ) {dim=dim0;}
00260
00263
00265 virtual vec sample () const {it_error ( "not implemneted" );return vec ( 0 );};
00267 virtual mat sample_m ( int N ) const;
00269 virtual double evallog ( const vec &val ) const {it_error ( "not implemneted" );return 0.0;};
00271 virtual vec evallog_m ( const mat &Val ) const {
00272 vec x ( Val.cols() );
00273 for ( int i=0;i<Val.cols();i++ ) {x ( i ) =evallog ( Val.get_col ( i ) ) ;}
00274 return x;
00275 }
00277 virtual mpdf* condition ( const RV &rv ) const {it_warning ( "Not implemented" ); return NULL;}
00279 virtual epdf* marginal ( const RV &rv ) const {it_warning ( "Not implemented" ); return NULL;}
00281 virtual vec mean() const {it_error ( "not implemneted" );return vec ( 0 );};
00283 virtual vec variance() const {it_error ( "not implemneted" );return vec ( 0 );};
00285 virtual void qbounds ( vec &lb, vec &ub, double percentage=0.95 ) const {
00286 vec mea=mean(); vec std=sqrt ( variance() );
00287 lb = mea-2*std; ub=mea+2*std;
00288 };
00290
00296
00298 void set_rv ( const RV &rv0 ) {rv = rv0; }
00300 bool isnamed() const {bool b= ( dim==rv._dsize() );return b;}
00302 const RV& _rv() const {it_assert_debug ( isnamed(),"" ); return rv;}
00304
00307
00309 int dimension() const {return dim;}
00311
00312 };
00313
00314
00316
00317
00318 class mpdf : public bdmroot {
00319 protected:
00321 int dimc;
00323 RV rvc;
00325 epdf* ep;
00326 public:
00329
00330 mpdf ( ) :dimc ( 0 ),rvc ( ) {};
00332 mpdf ( const mpdf &m ) :dimc ( m.dimc ),rvc ( m.rvc ) {};
00334
00337
00339 virtual vec samplecond ( const vec &cond ) {
00340 this->condition ( cond );
00341 vec temp= ep->sample();
00342 return temp;
00343 };
00345 virtual mat samplecond_m ( const vec &cond, int N ) {
00346 this->condition ( cond );
00347 mat temp ( ep->dimension(),N ); vec smp ( ep->dimension() );
00348 for ( int i=0;i<N;i++ ) {smp=ep->sample() ;temp.set_col ( i, smp );}
00349 return temp;
00350 };
00352 virtual void condition ( const vec &cond ) {it_error ( "Not implemented" );};
00353
00355 virtual double evallogcond ( const vec &dt, const vec &cond ) {
00356 double tmp; this->condition ( cond );tmp = ep->evallog ( dt ); it_assert_debug ( std::isfinite ( tmp ),"Infinite value" ); return tmp;
00357 };
00358
00360 virtual vec evallogcond_m ( const mat &Dt, const vec &cond ) {this->condition ( cond );return ep->evallog_m ( Dt );};
00361
00364
00365 RV _rv() {return ep->_rv();}
00366 RV _rvc() {it_assert_debug ( isnamed(),"" ); return rvc;}
00367 int dimension() {return ep->dimension();}
00368 int dimensionc() {return dimc;}
00369 epdf& _epdf() {return *ep;}
00370 epdf* _e() {return ep;}
00372
00375 void set_rvc ( const RV &rvc0 ) {rvc=rvc0;}
00376 void set_rv ( const RV &rv0 ) {ep->set_rv ( rv0 );}
00377 bool isnamed() {return ( ep->isnamed() ) && ( dimc==rvc._dsize() );}
00379 };
00380
00406 class datalink {
00407 protected:
00409 int downsize;
00411 int upsize;
00413 ivec v2v_up;
00414 public:
00416 datalink () {};
00417 datalink ( const RV &rv, const RV &rv_up ) {set_connection ( rv,rv_up );};
00419 void set_connection ( const RV &rv, const RV &rv_up ) {
00420 downsize = rv._dsize();
00421 upsize = rv_up._dsize();
00422 v2v_up= ( rv.dataind ( rv_up ) );
00423
00424 it_assert_debug ( v2v_up.length() ==downsize,"rv is not fully in rv_up" );
00425 }
00427 void set_connection ( int ds, int us, const ivec &upind ) {
00428 downsize = ds;
00429 upsize = us;
00430 v2v_up= upind;
00431
00432 it_assert_debug ( v2v_up.length() ==downsize,"rv is not fully in rv_up" );
00433 }
00435 vec pushdown ( const vec &val_up ) {
00436 it_assert_debug ( upsize==val_up.length(),"Wrong val_up" );
00437 return get_vec ( val_up,v2v_up );
00438 }
00440 void pushup ( vec &val_up, const vec &val ) {
00441 it_assert_debug ( downsize==val.length(),"Wrong val" );
00442 it_assert_debug ( upsize==val_up.length(),"Wrong val_up" );
00443 set_subvector ( val_up, v2v_up, val );
00444 }
00445 };
00446
00448 class datalink_m2e: public datalink {
00449 protected:
00451 int condsize;
00453 ivec v2c_up;
00455 ivec v2c_lo;
00456
00457 public:
00458 datalink_m2e() {};
00460 void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up ) {
00461 datalink::set_connection ( rv,rv_up );
00462 condsize= rvc._dsize();
00463
00464 rvc.dataind ( rv_up, v2c_lo, v2c_up );
00465 }
00467 vec get_cond ( const vec &val_up ) {
00468 vec tmp ( condsize );
00469 set_subvector ( tmp,v2c_lo,val_up ( v2c_up ) );
00470 return tmp;
00471 }
00472 void pushup_cond ( vec &val_up, const vec &val, const vec &cond ) {
00473 it_assert_debug ( downsize==val.length(),"Wrong val" );
00474 it_assert_debug ( upsize==val_up.length(),"Wrong val_up" );
00475 set_subvector ( val_up, v2v_up, val );
00476 set_subvector ( val_up, v2c_up, cond );
00477 }
00478 };
00481 class datalink_m2m: public datalink_m2e {
00482 protected:
00484 ivec c2c_up;
00486 ivec c2c_lo;
00487 public:
00489 datalink_m2m() {};
00490 void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up ) {
00491 datalink_m2e::set_connection ( rv, rvc, rv_up );
00492
00493 rvc.dataind ( rvc_up, c2c_lo, c2c_up );
00494 it_assert_debug ( c2c_lo.length() +v2c_lo.length() ==condsize, "cond is not fully given" );
00495 }
00497 vec get_cond ( const vec &val_up, const vec &cond_up ) {
00498 vec tmp ( condsize );
00499 set_subvector ( tmp,v2c_lo,val_up ( v2c_up ) );
00500 set_subvector ( tmp,c2c_lo,cond_up ( c2c_up ) );
00501 return tmp;
00502 }
00504
00505 };
00506
00512 class logger : public bdmroot {
00513 protected:
00515 Array<RV> entries;
00517 Array<string> names;
00518 public:
00520 logger ( ) : entries ( 0 ),names ( 0 ) {}
00521
00524 virtual int add ( const RV &rv, string prefix="" ) {
00525 int id;
00526 if ( rv._dsize() >0 ) {
00527 id=entries.length();
00528 names=concat ( names, prefix);
00529 entries.set_length ( id+1,true );
00530 entries ( id ) = rv;
00531 }
00532 else { id =-1;}
00533 return id;
00534 }
00535
00537 virtual void logit ( int id, const vec &v ) =0;
00539 virtual void logit ( int id, const double &d ) =0;
00540
00542 virtual void step() =0;
00543
00545 virtual void finalize() {};
00546
00548 virtual void init() {};
00549
00550 };
00551
00555 class mepdf : public mpdf {
00556 public:
00558 mepdf ( epdf* em ) :mpdf ( ) {ep= em ;};
00559 mepdf (const epdf* em ) :mpdf ( ) {ep=const_cast<epdf*>( em );};
00560 void condition ( const vec &cond ) {}
00561 };
00562
00565 class compositepdf {
00566 protected:
00568 int n;
00570 Array<mpdf*> mpdfs;
00571 public:
00572 compositepdf ( Array<mpdf*> A0 ) : n ( A0.length() ), mpdfs ( A0 ) {};
00574 RV getrv ( bool checkoverlap=false );
00576 void setrvc ( const RV &rv, RV &rvc );
00577 };
00578
00586 class DS : public bdmroot {
00587 protected:
00588 int dtsize;
00589 int utsize;
00591 RV Drv;
00593 RV Urv;
00595 int L_dt, L_ut;
00596 public:
00598 DS() :Drv ( ),Urv ( ) {};
00600 virtual void getdata ( vec &dt ) {it_error ( "abstract class" );};
00602 virtual void getdata ( vec &dt, const ivec &indeces ) {it_error ( "abstract class" );};
00604 virtual void write ( vec &ut ) {it_error ( "abstract class" );};
00606 virtual void write ( vec &ut, const ivec &indeces ) {it_error ( "abstract class" );};
00607
00609 virtual void step() =0;
00610
00612 virtual void log_add ( logger &L ) {
00613 it_assert_debug ( dtsize==Drv._dsize(),"" );
00614 it_assert_debug ( utsize==Urv._dsize(),"" );
00615
00616 L_dt=L.add ( Drv,"" );
00617 L_ut=L.add ( Urv,"" );
00618 }
00620 virtual void logit ( logger &L ) {
00621 vec tmp ( Drv._dsize() +Urv._dsize() );
00622 getdata ( tmp );
00623
00624 L.logit ( L_dt,tmp.left ( Drv._dsize() ) );
00625
00626 L.logit ( L_ut,tmp.mid ( Drv._dsize(), Urv._dsize() ) );
00627 }
00629 virtual RV _drv() const {return concat ( Drv,Urv );}
00631 const RV& _urv() const {return Urv;}
00633 virtual void set_drv (const RV &drv, const RV &urv) { Drv=drv;Urv=urv;}
00634 };
00635
00657 class BM :public bdmroot {
00658 protected:
00660 RV drv;
00662 double ll;
00664 bool evalll;
00665 public:
00668
00669 BM () :ll ( 0 ),evalll ( true ), LIDs ( 4 ), LFlags(4) {
00670 LIDs=-1; LFlags=0; LFlags(0)=1;};
00671 BM ( const BM &B ) : drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {}
00674 virtual BM* _copy_ () const {return NULL;};
00676
00679
00683 virtual void bayes ( const vec &dt ) = 0;
00685 virtual void bayesB ( const mat &Dt );
00688 virtual double logpred ( const vec &dt ) const{it_error ( "Not implemented" );return 0.0;}
00690 vec logpred_m ( const mat &dt ) const{vec tmp ( dt.cols() );for ( int i=0;i<dt.cols();i++ ) {tmp ( i ) =logpred ( dt.get_col ( i ) );}return tmp;}
00691
00693 virtual epdf* epredictor ( ) const {it_error ( "Not implemented" );return NULL;};
00695 virtual mpdf* predictor ( ) const {it_error ( "Not implemented" );return NULL;};
00697
00702
00704 RV rvc;
00706 const RV& _rvc() const {return rvc;}
00707
00709 virtual void condition ( const vec &val ) {it_error ( "Not implemented!" );};
00710
00712
00713
00716
00717 const RV& _drv() const {return drv;}
00718 void set_drv ( const RV &rv ) {drv=rv;}
00719 void set_rv ( const RV &rv ) {const_cast<epdf&> ( posterior() ).set_rv ( rv );}
00720 double _ll() const {return ll;}
00721 void set_evalll ( bool evl0 ) {evalll=evl0;}
00722 virtual const epdf& posterior() const =0;
00723 virtual const epdf* _e() const =0;
00725
00728
00730 virtual void set_options ( const string &opt ) {
00731 LFlags(0)=1;
00732 if ( opt.find ( "logbounds" ) !=string::npos ) {LFlags(1)=1; LFlags(2)=1;}
00733 if ( opt.find ( "logll" ) !=string::npos ) {LFlags(3)=1;}
00734 }
00736 ivec LIDs;
00737
00739 ivec LFlags;
00741 virtual void log_add ( logger &L, const string &name="" ) {
00742
00743 RV r;
00744 if ( posterior().isnamed() ) {r=posterior()._rv();}
00745 else{r=RV ( "est", posterior().dimension() );};
00746
00747
00748 if (LFlags(0)) LIDs ( 0 ) =L.add ( r,name+"mean_" );
00749 if (LFlags(1)) LIDs ( 1 ) =L.add ( r,name+"lb_" );
00750 if (LFlags(2)) LIDs ( 2 ) =L.add ( r,name+"ub_" );
00751 if (LFlags(3)) LIDs ( 3 ) =L.add ( RV("ll",1),name );
00752 }
00753 virtual void logit ( logger &L ) {
00754 L.logit ( LIDs ( 0 ), posterior().mean() );
00755 if ( LFlags(1) || LFlags(2)) {
00756 vec ub,lb;
00757 posterior().qbounds ( lb,ub );
00758 L.logit ( LIDs ( 1 ), lb );
00759 L.logit ( LIDs ( 2 ), ub );
00760 }
00761 if (LFlags(3)) L.logit ( LIDs ( 3 ), ll );
00762 }
00764 };
00765
00766
00767 };
00768 #endif // BM_H