00001
00013 #ifndef BM_H
00014 #define BM_H
00015
00016 #include <itpp/itbase.h>
00017
00018
00019 using namespace itpp;
00020
00022 class str{
00023 public:
00025 ivec ids;
00027 ivec times;
00029 str(ivec ids0, ivec times0):ids(ids0),times(times0){
00030 it_assert_debug(times0.length()==ids0.length(),"Incompatible input");
00031 };
00032 };
00033
00040 class RV {
00041 protected:
00043 int tsize;
00045 int len;
00047 ivec ids;
00049 ivec sizes;
00051 ivec times;
00053 Array<std::string> names;
00054
00055 private:
00057 void init (ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times );
00058 public:
00060 RV ( Array<std::string> in_names, ivec in_sizes, ivec in_times );
00062 RV ( Array<std::string> in_names, ivec in_sizes );
00064 RV ( Array<std::string> in_names );
00066 RV ();
00067
00069 friend std::ostream &operator<< ( std::ostream &os, const RV &rv );
00070
00072 int count() const {return tsize;} ;
00074 int length() const {return len;} ;
00075
00076
00077
00079 ivec findself (const RV &rv2 ) const;
00081 bool equal (const RV &rv2 ) const;
00083 bool add ( const RV &rv2 );
00085 RV subt ( const RV &rv2 ) const;
00087 RV subselect ( const ivec &ind ) const;
00089 RV operator() ( const ivec &ind ) const;
00091 void t ( int delta );
00093 str tostr() const;
00095 ivec dataind(const RV &crv) const;
00096
00098 Array<std::string>& _names() {return names;};
00099
00101 int id ( int at ) {return ids ( at );};
00103 int size ( int at ) {return sizes ( at );};
00105 int time ( int at ) {return times ( at );};
00107 std::string name ( int at ) {return names ( at );};
00108
00110 void set_id ( int at, int id0 ) {ids ( at )=id0;};
00112 void set_size ( int at, int size0 ) {sizes ( at )=size0; tsize=sum(sizes);};
00114 void set_time ( int at, int time0 ) {times ( at )=time0;};
00115
00117 void newids();
00118 };
00119
00121 RV concat ( const RV &rv1, const RV &rv2 );
00122
00123
00125
00126 class fnc {
00127 protected:
00129 int dimy;
00130 public:
00132 fnc ( int dy ) :dimy ( dy ) {};
00134 virtual vec eval ( const vec &cond ) {
00135 return vec ( 0 );
00136 };
00137
00139 int _dimy() const{return dimy;}
00140
00142 virtual ~fnc() {};
00143 };
00144
00145 class mpdf;
00146
00148
00149 class epdf {
00150 protected:
00152 RV rv;
00153 public:
00155 epdf() :rv ( ) {};
00156
00158 epdf ( const RV &rv0 ) :rv ( rv0 ) {};
00159
00160
00161
00162
00164 virtual vec sample () const =0;
00166 virtual mat sampleN ( int N ) const;
00168 virtual double eval ( const vec &val ) const {return exp ( this->evalpdflog ( val ) );};
00169
00171 virtual double evalpdflog ( const vec &val ) const =0;
00172
00174 virtual vec evalpdflog_m ( const mat &Val ) const {
00175 vec x ( Val.cols() );
00176 for ( int i=0;i<Val.cols();i++ ) {x ( i ) =evalpdflog( Val.get_col(i) ) ;}
00177 return x;
00178 }
00180 mpdf* condition(const RV &rv){it_warning("Not implemented"); return NULL;}
00182 epdf* marginal(const RV &rv){it_warning("Not implemented"); return NULL;}
00183
00185 virtual vec mean() const =0;
00186
00188 virtual ~epdf() {};
00190 const RV& _rv() const {return rv;}
00192 void _renewrv(const RV &in_rv){rv=in_rv;}
00194 };
00195
00196
00198
00199
00200 class mpdf {
00201 protected:
00203 RV rv;
00205 RV rvc;
00207 epdf* ep;
00208 public:
00209
00211
00213 virtual vec samplecond (const vec &cond, double &ll ) {this->condition ( cond );
00214 vec temp= ep->sample();
00215 ll=ep->evalpdflog ( temp );return temp;};
00217 virtual mat samplecond (const vec &cond, vec &ll, int N ) {
00218 this->condition ( cond );
00219 mat temp ( rv.count(),N ); vec smp ( rv.count() );
00220 for ( int i=0;i<N;i++ ) {smp=ep->sample() ;temp.set_col ( i, smp );ll ( i ) =ep->evalpdflog ( smp );}
00221 return temp;
00222 };
00224 virtual void condition ( const vec &cond ) {it_error("Not implemented");};
00225
00227 virtual double evalcond ( const vec &dt, const vec &cond ) {this->condition ( cond );return ep->eval ( dt );};
00228
00230 virtual ~mpdf() {};
00231
00233 mpdf ( const RV &rv0, const RV &rvc0 ) :rv ( rv0 ),rvc ( rvc0 ) {};
00235 RV _rvc() {return rvc;}
00237 RV _rv() {return rv;}
00239 epdf& _epdf() {return *ep;}
00240 };
00241
00246 class mepdf : public mpdf {
00247 public:
00249 mepdf (epdf &em ) :mpdf ( em._rv(),RV() ) {ep=&em;};
00250 };
00251
00253 class compositepdf{
00254 protected:
00256 int n;
00258 Array<mpdf*> mpdfs;
00260 Array<ivec> rvsinrv;
00262 Array<ivec> rvcsinrv;
00264 Array<ivec> rvinrvcs;
00265 public:
00266 compositepdf(Array<mpdf*> A0): n(A0.length()), mpdfs(A0), rvsinrv(n), rvcsinrv(n),rvinrvcs(n){};
00268 RV getrv(bool checkoverlap=false);
00270 void setrvc(const RV &rv, RV &rvc);
00272 void setindices(const RV &rv);
00274 void setrvcinrv(const RV &rvc, Array<ivec> &rvcind);
00275 };
00276
00284 class DS {
00285 protected:
00287 RV Drv;
00289 RV Urv;
00290 public:
00292 void getdata ( vec &dt );
00294 void getdata ( vec &dt, ivec &indeces );
00296 void write ( vec &ut );
00298 void write ( vec &ut, ivec &indeces );
00304 void linkrvs ( RV &drv, RV &urv );
00305
00307 void step();
00308
00309 };
00310
00315 class BM {
00316 protected:
00318 RV rv;
00320 double ll;
00322 bool evalll;
00323 public:
00324
00326 BM ( const RV &rv0, double ll0=0,bool evalll0=true ) :rv ( rv0 ), ll ( ll0 ),evalll ( evalll0) {
00327 };
00329 BM (const BM &B) : rv(B.rv), ll(B.ll), evalll(B.evalll) {}
00330
00334 virtual void bayes ( const vec &dt ) = 0;
00336 virtual void bayesB (const mat &Dt );
00338 virtual const epdf& _epdf() const =0;
00339
00342 virtual double logpred(const vec &dt)const{it_error("Not implemented");return 0.0;}
00344 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;}
00345
00347 virtual epdf* predictor(const RV &rv){it_error("Not implemented");return NULL;};
00348
00350 virtual ~BM() {};
00352 const RV& _rv() const {return rv;}
00354 double _ll() const {return ll;}
00356 void set_evalll(bool evl0){evalll=evl0;}
00357
00360 virtual BM* _copy_(bool changerv=false){it_error("function _copy_ not implemented for this BM"); return NULL;};
00361 };
00362
00372 class BMcond {
00373 protected:
00375 RV rvc;
00376 public:
00378 virtual void condition ( const vec &val ) =0;
00380 BMcond ( RV &rv0 ) :rvc ( rv0 ) {};
00382 virtual ~BMcond() {};
00384 const RV& _rvc() const {return rvc;}
00385 };
00386
00387 #endif // BM_H