00001
00013 #ifndef BM_H
00014 #define BM_H
00015
00016
00017 #include "../itpp_ext.h"
00018 #include <map>
00019
00020 namespace bdm {
00021 using namespace itpp;
00022 using namespace std;
00023
00025 class bdmroot {
00026 public:
00028 virtual ~bdmroot() {}
00029 };
00030
00031 typedef std::map<string, int> RVmap;
00032 extern ivec RV_SIZES;
00033 extern Array<string> RV_NAMES;
00034
00036 class str {
00037 public:
00039 ivec ids;
00041 ivec times;
00043 str ( ivec ids0, ivec times0 ) :ids ( ids0 ),times ( times0 ) {
00044 it_assert_debug ( times0.length() ==ids0.length(),"Incompatible input" );
00045 };
00046 };
00047
00086 class RV :public bdmroot {
00087 protected:
00089 int dsize;
00091 int len;
00093 ivec ids;
00095 ivec times;
00096
00097 private:
00099 void init ( Array<std::string> in_names, ivec in_sizes, ivec in_times );
00100 int init ( const string &name, int size );
00101 public:
00104
00106 RV ( Array<std::string> in_names, ivec in_sizes, ivec in_times ) {init ( in_names,in_sizes,in_times );};
00108 RV ( Array<std::string> in_names, ivec in_sizes ) {init ( in_names,in_sizes,zeros_i ( in_names.length() ) );};
00110 RV ( Array<std::string> in_names ) {init ( in_names,ones_i ( in_names.length() ),zeros_i ( in_names.length() ) );}
00112 RV () :dsize ( 0 ),len ( 0 ),ids ( 0 ),times ( 0 ) {};
00114 RV ( string name, int sz, int tm=0 );
00116
00119
00121 friend std::ostream &operator<< ( std::ostream &os, const RV &rv );
00122 int _dsize() const {return dsize;} ;
00124 int countsize() const;
00125 ivec cumsizes() const;
00126 int length() const {return len;} ;
00127 int id ( int at ) const{return ids ( at );};
00128 int size ( int at ) const {return RV_SIZES ( ids(at) );};
00129 int time ( int at ) const{return times ( at );};
00130 std::string name ( int at ) const {return RV_NAMES ( ids(at) );};
00131 void set_time ( int at, int time0 ) {times ( at ) =time0;};
00133
00134
00135
00138
00140 ivec findself ( const RV &rv2 ) const;
00142 bool equal ( const RV &rv2 ) const;
00144 bool add ( const RV &rv2 );
00146 RV subt ( const RV &rv2 ) const;
00148 RV subselect ( const ivec &ind ) const;
00150 RV operator() ( const ivec &ind ) const {return subselect ( ind );};
00152 RV operator() ( int di1, int di2 ) const {
00153 ivec sz=cumsizes();
00154 int i1=0;
00155 while ( sz ( i1 ) <di1 ) i1++;
00156 int i2=i1;
00157 while ( sz ( i2 ) <di2 ) i2++;
00158 return subselect ( linspace ( i1,i2 ) );
00159 };
00161 void t ( int delta );
00163
00166
00168 str tostr() const;
00171 ivec dataind ( const RV &crv ) const;
00174 void dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const;
00176 int mint () const {return min ( times );};
00178
00179 };
00180
00181
00183 RV concat ( const RV &rv1, const RV &rv2 );
00184
00186 extern RV RV0;
00187
00189
00190 class fnc :public bdmroot {
00191 protected:
00193 int dimy;
00194 public:
00196 fnc ( ) {};
00198 virtual vec eval ( const vec &cond ) {
00199 return vec ( 0 );
00200 };
00201
00203 virtual void condition ( const vec &val ) {};
00204
00206 int _dimy() const{return dimy;}
00207 };
00208
00209 class mpdf;
00210
00212
00213 class epdf :public bdmroot {
00214 protected:
00216 int dim;
00218 RV rv;
00219
00220 public:
00232 epdf() :dim ( 0 ),rv ( ) {};
00233 epdf ( const epdf &e ) :dim ( e.dim ),rv ( e.rv ) {};
00234 epdf ( const RV &rv0 ) {set_rv ( rv0 );};
00235 void set_parameters ( int dim0 ) {dim=dim0;}
00237
00240
00242 virtual vec sample () const {it_error ( "not implemneted" );return vec ( 0 );};
00244 virtual mat sample_m ( int N ) const;
00246 virtual double evallog ( const vec &val ) const {it_error ( "not implemneted" );return 0.0;};
00248 virtual vec evallog_m ( const mat &Val ) const {
00249 vec x ( Val.cols() );
00250 for ( int i=0;i<Val.cols();i++ ) {x ( i ) =evallog ( Val.get_col ( i ) ) ;}
00251 return x;
00252 }
00254 virtual mpdf* condition ( const RV &rv ) const {it_warning ( "Not implemented" ); return NULL;}
00256 virtual epdf* marginal ( const RV &rv ) const {it_warning ( "Not implemented" ); return NULL;}
00258 virtual vec mean() const {it_error ( "not implemneted" );return vec ( 0 );};
00260 virtual vec variance() const {it_error ( "not implemneted" );return vec ( 0 );};
00262
00268
00270 void set_rv ( const RV &rv0 ) {rv = rv0; }
00272 bool isnamed() const {bool b= ( dim==rv._dsize() );return b;}
00274 const RV& _rv() const {it_assert_debug ( isnamed(),"" ); return rv;}
00276
00279
00281 int dimension() const {return dim;}
00283
00284 };
00285
00286
00288
00289
00290 class mpdf : public bdmroot {
00291 protected:
00293 int dimc;
00295 RV rvc;
00297 epdf* ep;
00298 public:
00301
00302 mpdf ( ) :dimc ( 0 ),rvc ( ) {};
00304 mpdf ( const mpdf &m ) :dimc ( m.dimc ),rvc ( m.rvc ) {};
00306
00309
00311 virtual vec samplecond ( const vec &cond ) {
00312 this->condition ( cond );
00313 vec temp= ep->sample();
00314 return temp;
00315 };
00317 virtual mat samplecond_m ( const vec &cond, int N ) {
00318 this->condition ( cond );
00319 mat temp ( ep->dimension(),N ); vec smp ( ep->dimension() );
00320 for ( int i=0;i<N;i++ ) {smp=ep->sample() ;temp.set_col ( i, smp );}
00321 return temp;
00322 };
00324 virtual void condition ( const vec &cond ) {it_error ( "Not implemented" );};
00325
00327 virtual double evallogcond ( const vec &dt, const vec &cond ) {
00328 double tmp; this->condition ( cond );tmp = ep->evallog ( dt ); it_assert_debug ( std::isfinite ( tmp ),"Infinite value" ); return tmp;
00329 };
00330
00332 virtual vec evallogcond_m ( const mat &Dt, const vec &cond ) {this->condition ( cond );return ep->evallog_m ( Dt );};
00333
00336
00337 RV _rv() {return ep->_rv();}
00338 RV _rvc() {it_assert_debug ( isnamed(),"" ); return rvc;}
00339 int dimension() {return ep->dimension();}
00340 int dimensionc() {return dimc;}
00341 epdf& _epdf() {return *ep;}
00342 epdf* _e() {return ep;}
00344
00347 void set_rvc ( const RV &rvc0 ) {rvc=rvc0;}
00348 void set_rv ( const RV &rv0 ) {ep->set_rv ( rv0 );}
00349 bool isnamed() {return ( ep->isnamed() ) && ( dimc==rvc._dsize() );}
00351 };
00352
00378 class datalink {
00379 protected:
00381 int downsize;
00383 int upsize;
00385 ivec v2v_up;
00386 public:
00388 datalink () {};
00389 datalink ( const RV &rv, const RV &rv_up ) {set_connection ( rv,rv_up );};
00391 void set_connection ( const RV &rv, const RV &rv_up ) {
00392 downsize = rv._dsize();
00393 upsize = rv_up._dsize();
00394 v2v_up= ( rv.dataind ( rv_up ) );
00395
00396 it_assert_debug ( v2v_up.length() ==downsize,"rv is not fully in rv_up" );
00397 }
00399 vec pushdown ( const vec &val_up ) {
00400 it_assert_debug ( upsize==val_up.length(),"Wrong val_up" );
00401 return get_vec ( val_up,v2v_up );
00402 }
00404 void pushup ( vec &val_up, const vec &val ) {
00405 it_assert_debug ( downsize==val.length(),"Wrong val" );
00406 it_assert_debug ( upsize==val_up.length(),"Wrong val_up" );
00407 set_subvector ( val_up, v2v_up, val );
00408 }
00409 };
00410
00412 class datalink_m2e: public datalink {
00413 protected:
00415 int condsize;
00417 ivec v2c_up;
00419 ivec v2c_lo;
00420
00421 public:
00423 datalink_m2e ( const RV &rv, const RV &rvc, const RV &rv_up ) :
00424 datalink ( rv,rv_up ), condsize ( rvc._dsize() ) {
00425
00426 rvc.dataind ( rv_up, v2c_lo, v2c_up );
00427 }
00429 vec get_cond ( const vec &val_up ) {
00430 vec tmp ( condsize );
00431 set_subvector ( tmp,v2c_lo,val_up ( v2c_up ) );
00432 return tmp;
00433 }
00434 void pushup_cond ( vec &val_up, const vec &val, const vec &cond ) {
00435 it_assert_debug ( downsize==val.length(),"Wrong val" );
00436 it_assert_debug ( upsize==val_up.length(),"Wrong val_up" );
00437 set_subvector ( val_up, v2v_up, val );
00438 set_subvector ( val_up, v2c_up, cond );
00439 }
00440 };
00443 class datalink_m2m: public datalink_m2e {
00444 protected:
00446 ivec c2c_up;
00448 ivec c2c_lo;
00449 public:
00451 datalink_m2m ( const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up ) :
00452 datalink_m2e ( rv, rvc, rv_up ) {
00453
00454 rvc.dataind ( rvc_up, c2c_lo, c2c_up );
00455 it_assert_debug ( c2c_lo.length() +v2c_lo.length() ==condsize, "cond is not fully given" );
00456 }
00458 vec get_cond ( const vec &val_up, const vec &cond_up ) {
00459 vec tmp ( condsize );
00460 set_subvector ( tmp,v2c_lo,val_up ( v2c_up ) );
00461 set_subvector ( tmp,c2c_lo,cond_up ( c2c_up ) );
00462 return tmp;
00463 }
00465
00466 };
00467
00473 class logger : public bdmroot {
00474 protected:
00476 Array<RV> entries;
00478 Array<string> names;
00479 public:
00481 logger ( ) : entries ( 0 ),names ( 0 ) {}
00482
00485 virtual int add ( const RV &rv, string name="" ) {
00486 int id;
00487 if ( rv._dsize() >0 ) {
00488 id=entries.length();
00489 names=concat ( names, name );
00490 entries.set_length ( id+1,true );
00491 entries ( id ) = rv;
00492 }
00493 else { id =-1;}
00494 return id;
00495 }
00496
00498 virtual void logit ( int id, const vec &v ) =0;
00499
00501 virtual void step() =0;
00502
00504 virtual void finalize() {};
00505
00507 virtual void init() {};
00508
00509 };
00510
00514 class mepdf : public mpdf {
00515 public:
00517 mepdf ( const epdf* em ) :mpdf ( ) {ep=const_cast<epdf*> ( em );};
00518 void condition ( const vec &cond ) {}
00519 };
00520
00523 class compositepdf {
00524 protected:
00526 int n;
00528 Array<mpdf*> mpdfs;
00529 public:
00530 compositepdf ( Array<mpdf*> A0 ) : n ( A0.length() ), mpdfs ( A0 ) {};
00532 RV getrv ( bool checkoverlap=false );
00534 void setrvc ( const RV &rv, RV &rvc );
00535 };
00536
00544 class DS : public bdmroot {
00545 protected:
00546 int dtsize;
00547 int utsize;
00549 RV Drv;
00551 RV Urv;
00553 int L_dt, L_ut;
00554 public:
00556 DS() :Drv ( ),Urv ( ) {};
00558 virtual void getdata ( vec &dt ) {it_error ( "abstract class" );};
00560 virtual void getdata ( vec &dt, const ivec &indeces ) {it_error ( "abstract class" );};
00562 virtual void write ( vec &ut ) {it_error ( "abstract class" );};
00564 virtual void write ( vec &ut, const ivec &indeces ) {it_error ( "abstract class" );};
00565
00567 virtual void step() =0;
00568
00570 virtual void log_add ( logger &L ) {
00571 it_assert_debug ( dtsize==Drv._dsize(),"" );
00572 it_assert_debug ( utsize==Urv._dsize(),"" );
00573
00574 L_dt=L.add ( Drv,"" );
00575 L_ut=L.add ( Urv,"" );
00576 }
00578 virtual void logit ( logger &L ) {
00579 vec tmp ( Drv._dsize() +Urv._dsize() );
00580 getdata ( tmp );
00581
00582 L.logit ( L_dt,tmp.left ( Drv._dsize() ) );
00583
00584 L.logit ( L_ut,tmp.mid ( Drv._dsize(), Urv._dsize() ) );
00585 }
00587 virtual RV _drv() const {return concat ( Drv,Urv );}
00589 const RV& _urv() const {return Urv;}
00590 };
00591
00596 class BM :public bdmroot {
00597 protected:
00599 RV drv;
00601 double ll;
00603 bool evalll;
00604 public:
00607
00608 BM () :ll ( 0 ),evalll ( false ) {};
00609 BM ( const BM &B ) : drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {}
00612 virtual BM* _copy_ () {return NULL;};
00614
00617
00621 virtual void bayes ( const vec &dt ) = 0;
00623 virtual void bayesB ( const mat &Dt );
00626 virtual double logpred ( const vec &dt ) const{it_error ( "Not implemented" );return 0.0;}
00628 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;}
00629
00631 virtual epdf* epredictor ( ) const {it_error ( "Not implemented" );return NULL;};
00633 virtual mpdf* predictor ( ) const {it_error ( "Not implemented" );return NULL;};
00635
00638
00639 const RV& _drv() const {return drv;}
00640 void set_drv ( const RV &rv ) {drv=rv;}
00641 void set_rv ( const RV &rv ) {const_cast<epdf&> ( posterior() ).set_rv ( rv );}
00642 double _ll() const {return ll;}
00643 void set_evalll ( bool evl0 ) {evalll=evl0;}
00644 virtual const epdf& posterior() const =0;
00645 virtual const epdf* _e() const =0;
00647
00648 };
00649
00659 class BMcond :public bdmroot {
00660 protected:
00662 RV rvc;
00663 public:
00665 virtual void condition ( const vec &val ) =0;
00667 BMcond ( ) :rvc ( ) {};
00669 virtual ~BMcond() {};
00671 const RV& _rvc() const {return rvc;}
00672 };
00673
00674 };
00675 #endif // BM_H