00001
00017 #ifndef BM_H
00018 #define BM_H
00019
00020 #include <itpp/itbase.h>
00021 #include "../itpp_ext.h"
00022
00023
00024 namespace bdm{
00025 using namespace itpp;
00026
00028 class base{};
00029
00031 class str {
00032 public:
00034 ivec ids;
00036 ivec times;
00038 str ( ivec ids0, ivec times0 ) :ids ( ids0 ),times ( times0 ) {
00039 it_assert_debug ( times0.length() ==ids0.length(),"Incompatible input" );
00040 };
00041 };
00042
00049 class RV :base{
00050 protected:
00052 int tsize;
00054 int len;
00056 ivec ids;
00058 ivec sizes;
00060 ivec times;
00062 Array<std::string> names;
00063
00064 private:
00066 void init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times );
00067 public:
00069 RV ( Array<std::string> in_names, ivec in_sizes, ivec in_times );
00071 RV ( Array<std::string> in_names, ivec in_sizes );
00073 RV ( Array<std::string> in_names );
00075 RV ();
00076
00078 friend std::ostream &operator<< ( std::ostream &os, const RV &rv );
00079
00081 int count() const {return tsize;} ;
00083 int length() const {return len;} ;
00084
00085
00086
00088 ivec findself ( const RV &rv2 ) const;
00090 bool equal ( const RV &rv2 ) const;
00092 bool add ( const RV &rv2 );
00094 RV subt ( const RV &rv2 ) const;
00096 RV subselect ( const ivec &ind ) const;
00098 RV operator() ( const ivec &ind ) const;
00100 void t ( int delta );
00102 str tostr() const;
00105 ivec dataind ( const RV &crv ) const;
00108 void dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const;
00109
00111 Array<std::string>& _names() {return names;};
00112
00114 int id ( int at ) {return ids ( at );};
00116 int size ( int at ) {return sizes ( at );};
00118 int time ( int at ) {return times ( at );};
00120 std::string name ( int at ) {return names ( at );};
00121
00123 void set_id ( int at, int id0 ) {ids ( at ) =id0;};
00125 void set_size ( int at, int size0 ) {sizes ( at ) =size0; tsize=sum ( sizes );};
00127 void set_time ( int at, int time0 ) {times ( at ) =time0;};
00128
00130 void newids();
00131 };
00132
00134 RV concat ( const RV &rv1, const RV &rv2 );
00135
00137 extern RV RV0;
00138
00140
00141 class fnc :base{
00142 protected:
00144 int dimy;
00145 public:
00147 fnc ( int dy ) :dimy ( dy ) {};
00149 virtual vec eval ( const vec &cond ) {
00150 return vec ( 0 );
00151 };
00152
00154 virtual void condition(const vec &val){};
00155
00157 int _dimy() const{return dimy;}
00158
00160 virtual ~fnc() {};
00161 };
00162
00163 class mpdf;
00164
00166
00167 class epdf :base {
00168 protected:
00170 RV rv;
00171 public:
00173 epdf() :rv ( ) {};
00174
00176 epdf ( const RV &rv0 ) :rv ( rv0 ) {};
00177
00178
00179
00180
00182 virtual vec sample () const =0;
00184 virtual mat sample_m ( int N ) const;
00185
00187 virtual double evallog ( const vec &val ) const =0;
00188
00190 virtual vec evallog_m ( const mat &Val ) const {
00191 vec x ( Val.cols() );
00192 for ( int i=0;i<Val.cols();i++ ) {x ( i ) =evallog ( Val.get_col ( i ) ) ;}
00193 return x;
00194 }
00196 virtual mpdf* condition ( const RV &rv ) const {it_warning ( "Not implemented" ); return NULL;}
00198 virtual epdf* marginal ( const RV &rv ) const {it_warning ( "Not implemented" ); return NULL;}
00199
00201 virtual vec mean() const =0;
00202
00204 virtual vec variance() const = 0;
00205
00207 virtual ~epdf() {};
00209 const RV& _rv() const {return rv;}
00211 void _renewrv ( const RV &in_rv ) {rv=in_rv;}
00213 };
00214
00215
00217
00218
00219 class mpdf {
00220 protected:
00222 RV rv;
00224 RV rvc;
00226 epdf* ep;
00227 public:
00228
00230 virtual vec samplecond ( const vec &cond, double &ll ) {
00231 this->condition ( cond );
00232 vec temp= ep->sample();
00233 ll=ep->evallog ( temp );return temp;
00234 };
00236 virtual mat samplecond_m ( const vec &cond, vec &ll, int N ) {
00237 this->condition ( cond );
00238 mat temp ( rv.count(),N ); vec smp ( rv.count() );
00239 for ( int i=0;i<N;i++ ) {smp=ep->sample() ;temp.set_col ( i, smp );ll ( i ) =ep->evallog ( smp );}
00240 return temp;
00241 };
00243 virtual void condition ( const vec &cond ) {it_error ( "Not implemented" );};
00244
00246 virtual double evallogcond ( const vec &dt, const vec &cond ) {double tmp; this->condition ( cond );tmp = ep->evallog ( dt ); it_assert_debug(std::isfinite(tmp),"Infinite value"); return tmp;
00247 };
00248
00250 virtual vec evallogcond_m ( const mat &Dt, const vec &cond ) {this->condition ( cond );return ep->evallog_m ( Dt );};
00251
00253 virtual ~mpdf() {};
00254
00256 mpdf ( const RV &rv0, const RV &rvc0 ) :rv ( rv0 ),rvc ( rvc0 ) {};
00258 RV _rvc() const {return rvc;}
00260 RV _rv() const {return rv;}
00262 epdf& _epdf() {return *ep;}
00264 epdf* _e() {return ep;}
00265 };
00266
00269 class datalink_e2e {
00270 protected:
00272 int valsize;
00274 int valupsize;
00276 ivec v2v_up;
00277 public:
00279 datalink_e2e ( const RV &rv, const RV &rv_up ) :
00280 valsize ( rv.count() ), valupsize ( rv_up.count() ), v2v_up ( rv.dataind ( rv_up ) ) {
00281 it_assert_debug ( v2v_up.length() ==valsize,"rv is not fully in rv_up" );
00282 }
00284 vec get_val ( const vec &val_up ) {it_assert_debug ( valupsize==val_up.length(),"Wrong val_up" ); return get_vec ( val_up,v2v_up );}
00286 void fill_val ( vec &val_up, const vec &val ) {
00287 it_assert_debug ( valsize==val.length(),"Wrong val" );
00288 it_assert_debug ( valupsize==val_up.length(),"Wrong val_up" );
00289 set_subvector ( val_up, v2v_up, val );
00290 }
00291 };
00292
00294 class datalink_m2e: public datalink_e2e {
00295 protected:
00297 int condsize;
00299 ivec v2c_up;
00301 ivec v2c_lo;
00302
00303 public:
00305 datalink_m2e ( const RV &rv, const RV &rvc, const RV &rv_up ) :
00306 datalink_e2e ( rv,rv_up ), condsize ( rvc.count() ) {
00307
00308 rvc.dataind ( rv_up, v2c_lo, v2c_up );
00309 }
00311 vec get_cond ( const vec &val_up ) {
00312 vec tmp ( condsize );
00313 set_subvector ( tmp,v2c_lo,val_up ( v2c_up ) );
00314 return tmp;
00315 }
00316 void fill_val_cond ( vec &val_up, const vec &val, const vec &cond ) {
00317 it_assert_debug ( valsize==val.length(),"Wrong val" );
00318 it_assert_debug ( valupsize==val_up.length(),"Wrong val_up" );
00319 set_subvector ( val_up, v2v_up, val );
00320 set_subvector ( val_up, v2c_up, cond );
00321 }
00322 };
00325 class datalink_m2m: public datalink_m2e {
00326 protected:
00328 ivec c2c_up;
00330 ivec c2c_lo;
00331 public:
00333 datalink_m2m ( const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up ) :
00334 datalink_m2e ( rv, rvc, rv_up) {
00335
00336 rvc.dataind ( rvc_up, c2c_lo, c2c_up );
00337 it_assert_debug(c2c_lo.length()+v2c_lo.length()==condsize, "cond is not fully given");
00338 }
00340 vec get_cond ( const vec &val_up, const vec &cond_up ) {
00341 vec tmp ( condsize );
00342 set_subvector ( tmp,v2c_lo,val_up ( v2c_up ) );
00343 set_subvector ( tmp,c2c_lo,cond_up ( c2c_up ) );
00344 return tmp;
00345 }
00347
00348 };
00349
00353 class mepdf : public mpdf {
00354 public:
00356 mepdf (const epdf* em ) :mpdf ( em->_rv(),RV() ) {ep=const_cast<epdf*>(em);};
00357 void condition ( const vec &cond ) {}
00358 };
00359
00362 class compositepdf {
00363 protected:
00365 int n;
00367 Array<mpdf*> mpdfs;
00368 public:
00369 compositepdf ( Array<mpdf*> A0 ) : n ( A0.length() ), mpdfs ( A0 ) {};
00371 RV getrv ( bool checkoverlap=false );
00373 void setrvc ( const RV &rv, RV &rvc );
00374 };
00375
00383 class DS {
00384 protected:
00386 RV Drv;
00388 RV Urv;
00389 public:
00390 DS():Drv(RV0),Urv(RV0) {};
00392 void getdata ( vec &dt );
00394 void getdata ( vec &dt, ivec &indeces );
00396 void write ( vec &ut );
00398 void write ( vec &ut, ivec &indeces );
00404 void linkrvs ( RV &drv, RV &urv );
00405
00407 void step();
00408
00409 };
00410
00415 class BM {
00416 protected:
00418 RV rv;
00420 double ll;
00422 bool evalll;
00423 public:
00424
00426 BM ( const RV &rv0, double ll0=0,bool evalll0=true ) :rv ( rv0 ), ll ( ll0 ),evalll ( evalll0 ) {
00427 };
00429 BM ( const BM &B ) : rv ( B.rv ), ll ( B.ll ), evalll ( B.evalll ) {}
00430
00434 virtual void bayes ( const vec &dt ) = 0;
00436 virtual void bayesB ( const mat &Dt );
00438 virtual const epdf& _epdf() const =0;
00439
00441 virtual const epdf* _e() const =0;
00442
00445 virtual double logpred ( const vec &dt ) const{it_error ( "Not implemented" );return 0.0;}
00447 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;}
00448
00450 virtual epdf* predictor ( const RV &rv ) const {it_error ( "Not implemented" );return NULL;};
00451
00453 virtual ~BM() {};
00455 const RV& _rv() const {return rv;}
00457 double _ll() const {return ll;}
00459 void set_evalll ( bool evl0 ) {evalll=evl0;}
00460
00463 virtual BM* _copy_ ( bool changerv=false ) {it_error ( "function _copy_ not implemented for this BM" ); return NULL;};
00464 };
00465
00475 class BMcond :base{
00476 protected:
00478 RV rvc;
00479 public:
00481 virtual void condition ( const vec &val ) =0;
00483 BMcond ( RV &rv0 ) :rvc ( rv0 ) {};
00485 virtual ~BMcond() {};
00487 const RV& _rvc() const {return rvc;}
00488 };
00489
00490 };
00492 #endif // BM_H