00001
00013 #ifndef BM_H
00014 #define BM_H
00015
00016 #include <itpp/itbase.h>
00017
00018
00019 using namespace itpp;
00020
00027 class RV {
00028 protected:
00030 int tsize;
00032 int len;
00034 ivec ids;
00036 ivec sizes;
00038 ivec times;
00040 Array<std::string> names;
00041
00042 private:
00044 void init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times );
00045 public:
00047 RV ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times );
00049 RV ( ivec ids );
00051 RV ();
00052
00054 friend std::ostream &operator<< ( std::ostream &os, const RV &rv );
00055
00057 int count() const {return tsize;} ;
00059 int length() const {return len;} ;
00060
00061
00062
00064 ivec find ( RV rv2 );
00066 bool equal ( RV rv2 ) const;
00068 void add ( const RV &rv2 );
00070 friend RV concat ( const RV &rv1, const RV &rv2 );
00072 RV subt ( RV rv2 );
00074 RV subselect ( ivec ind );
00076 RV operator() ( ivec ind );
00078 void t ( int delta );
00080 ivec indexlist();
00081
00083 Array<std::string>& _names() {return names;};
00084
00086 int id ( int at ) {return ids ( at );};
00088 int size ( int at ) {return sizes ( at );};
00090 int time ( int at ) {return times ( at );};
00092 std::string name ( int at ) {return names ( at );};
00093 };
00094
00095
00097
00098 class fnc {
00099 protected:
00101 int dimy;
00102 public:
00104 fnc ( int dy ) :dimy ( dy ) {};
00106 virtual vec eval ( const vec &cond ) {
00107 return vec ( 0 );
00108 };
00109
00111 int _dimy() const{return dimy;}
00112
00114 virtual ~fnc() {};
00115 };
00116
00117
00119
00120 class epdf {
00121 protected:
00123 RV rv;
00124 public:
00126 epdf() :rv ( ivec ( 0 ) ) {};
00127
00129 epdf ( const RV &rv0 ) :rv ( rv0 ) {};
00130
00132
00134 virtual vec sample () const =0;
00136 virtual mat sampleN ( int N ) const;
00138 virtual double eval ( const vec &val ) const {return exp ( this->evalpdflog ( val ) );};
00139
00141 virtual double evalpdflog ( const vec &val ) const =0;
00142
00144 virtual vec mean() const =0;
00145
00147 virtual ~epdf() {};
00149 RV _rv() const {return rv;}
00150 };
00151
00152
00154
00155
00156 class mpdf {
00157 protected:
00159 RV rv;
00161 RV rvc;
00163 epdf* ep;
00164 public:
00165
00167
00169 virtual vec samplecond ( vec &cond, double &ll ) {this->condition ( cond );
00170 vec temp= ep->sample();
00171 ll=ep->evalpdflog ( temp );return temp;};
00173 virtual mat samplecond ( vec &cond, vec &ll, int N ) {
00174 this->condition ( cond );
00175 mat temp ( rv.count(),N ); vec smp ( rv.count() );
00176 for ( int i=0;i<N;i++ ) {smp=ep->sample() ;temp.set_col ( i, smp );ll ( i ) =ep->evalpdflog ( smp );}
00177 return temp;
00178 };
00180 virtual void condition ( const vec &cond ) {};
00181
00183 virtual double evalcond ( const vec &dt, const vec &cond ) {this->condition ( cond );return ep->eval ( dt );};
00184
00186 virtual ~mpdf() {};
00187
00189 mpdf ( const RV &rv0, const RV &rvc0 ) :rv ( rv0 ),rvc ( rvc0 ) {};
00191 RV _rvc() {return rvc;}
00193 epdf& _epdf() {return *ep;}
00194 };
00195
00200 class mepdf : public mpdf {
00201 public:
00203 mepdf ( const RV &rv, const RV &rvc, epdf* em ) :mpdf ( rv,rvc ) {ep=em;};
00204 };
00205
00213 class DS {
00214 protected:
00216 RV Drv;
00218 RV Urv;
00219 public:
00221 void getdata ( vec &dt );
00223 void getdata ( vec &dt, ivec &indeces );
00225 void write ( vec &ut );
00227 void write ( vec &ut, ivec &indeces );
00233 void linkrvs ( RV &drv, RV &urv );
00234
00236 void step();
00237
00238 };
00239
00244 class BM {
00245 protected:
00247 RV rv;
00249 double ll;
00251 bool evalll;
00252 public:
00253
00255 BM ( const RV &rv0 ) :rv ( rv0 ), ll ( 0 ),evalll ( true ) {
00256 };
00257
00261 virtual void bayes ( const vec &dt ) = 0;
00263 void bayes ( mat Dt );
00265 virtual epdf& _epdf() =0;
00266
00268 virtual ~BM() {};
00270 const RV& _rv() const {return rv;}
00272 double _ll() const {return ll;}
00273 };
00274
00284 class BMcond {
00285 protected:
00287 RV rvc;
00288 public:
00290 virtual void condition ( const vec &val ) =0;
00292 BMcond ( RV &rv0 ) :rvc ( rv0 ) {};
00294 virtual ~BMcond() {};
00296 const RV& _rvc() const {return rvc;}
00297 };
00298
00299 #endif // BM_H