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:
00024 ivec ids;
00025 ivec times;
00026 str(ivec ids0, ivec times0):ids(ids0),times(times0){
00027 it_assert_debug(times0.length()==ids0.length(),"Incompatible input");
00028 };
00029 };
00030
00037 class RV {
00038 protected:
00040 int tsize;
00042 int len;
00044 ivec ids;
00046 ivec sizes;
00048 ivec times;
00050 Array<std::string> names;
00051
00052 private:
00054 void init (ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times );
00055 public:
00057 RV ( Array<std::string> in_names, ivec in_sizes, ivec in_times );
00059 RV ( Array<std::string> in_names, ivec in_sizes );
00061 RV ( Array<std::string> in_names );
00063 RV ();
00064
00066 friend std::ostream &operator<< ( std::ostream &os, const RV &rv );
00067
00069 int count() const {return tsize;} ;
00071 int length() const {return len;} ;
00072
00073
00074
00076 ivec findself (const RV &rv2 ) const;
00078 bool equal (const RV &rv2 ) const;
00080 bool add ( const RV &rv2 );
00082 RV subt ( const RV rv2 ) const;
00084 RV subselect ( ivec ind ) const;
00086 RV operator() ( ivec ind ) const;
00088 void t ( int delta );
00090 str tostr() const;
00092 ivec dataind(RV crv) const;
00093
00095 Array<std::string>& _names() {return names;};
00096
00098 int id ( int at ) {return ids ( at );};
00100 int size ( int at ) {return sizes ( at );};
00102 int time ( int at ) {return times ( at );};
00104 std::string name ( int at ) {return names ( at );};
00105 };
00106
00108 RV concat ( const RV &rv1, const RV &rv2 );
00109
00110
00112
00113 class fnc {
00114 protected:
00116 int dimy;
00117 public:
00119 fnc ( int dy ) :dimy ( dy ) {};
00121 virtual vec eval ( const vec &cond ) {
00122 return vec ( 0 );
00123 };
00124
00126 int _dimy() const{return dimy;}
00127
00129 virtual ~fnc() {};
00130 };
00131
00132
00134
00135 class epdf {
00136 protected:
00138 RV rv;
00139 public:
00141 epdf() :rv ( ) {};
00142
00144 epdf ( const RV &rv0 ) :rv ( rv0 ) {};
00145
00147
00149 virtual vec sample () const =0;
00151 virtual mat sampleN ( int N ) const;
00153 virtual double eval ( const vec &val ) const {return exp ( this->evalpdflog ( val ) );};
00154
00156 virtual double evalpdflog ( const vec &val ) const =0;
00157
00159 virtual vec mean() const =0;
00160
00162 virtual ~epdf() {};
00164 RV& _rv() {return rv;}
00165 };
00166
00167
00169
00170
00171 class mpdf {
00172 protected:
00174 RV rv;
00176 RV rvc;
00178 epdf* ep;
00179 public:
00180
00182
00184 virtual vec samplecond (const vec &cond, double &ll ) {this->condition ( cond );
00185 vec temp= ep->sample();
00186 ll=ep->evalpdflog ( temp );return temp;};
00188 virtual mat samplecond (const vec &cond, vec &ll, int N ) {
00189 this->condition ( cond );
00190 mat temp ( rv.count(),N ); vec smp ( rv.count() );
00191 for ( int i=0;i<N;i++ ) {smp=ep->sample() ;temp.set_col ( i, smp );ll ( i ) =ep->evalpdflog ( smp );}
00192 return temp;
00193 };
00195 virtual void condition ( const vec &cond ) {};
00196
00198 virtual double evalcond ( const vec &dt, const vec &cond ) {this->condition ( cond );return ep->eval ( dt );};
00199
00201 virtual ~mpdf() {};
00202
00204 mpdf ( const RV &rv0, const RV &rvc0 ) :rv ( rv0 ),rvc ( rvc0 ) {};
00206 RV _rvc() {return rvc;}
00208 RV _rv() {return rv;}
00210 epdf& _epdf() {return *ep;}
00211 };
00212
00217 class mepdf : public mpdf {
00218 public:
00220 mepdf (epdf &em ) :mpdf ( em._rv(),RV() ) {ep=&em;};
00221 };
00222
00230 class DS {
00231 protected:
00233 RV Drv;
00235 RV Urv;
00236 public:
00238 void getdata ( vec &dt );
00240 void getdata ( vec &dt, ivec &indeces );
00242 void write ( vec &ut );
00244 void write ( vec &ut, ivec &indeces );
00250 void linkrvs ( RV &drv, RV &urv );
00251
00253 void step();
00254
00255 };
00256
00261 class BM {
00262 protected:
00264 RV rv;
00266 double ll;
00268 bool evalll;
00269 public:
00270
00272 BM ( const RV &rv0 ) :rv ( rv0 ), ll ( 0 ),evalll ( true ) {
00273 };
00274
00278 virtual void bayes ( const vec &dt ) = 0;
00280 void bayes ( mat Dt );
00282 virtual epdf& _epdf() =0;
00283
00285 virtual ~BM() {};
00287 const RV& _rv() const {return rv;}
00289 double _ll() const {return ll;}
00290 };
00291
00301 class BMcond {
00302 protected:
00304 RV rvc;
00305 public:
00307 virtual void condition ( const vec &val ) =0;
00309 BMcond ( RV &rv0 ) :rvc ( rv0 ) {};
00311 virtual ~BMcond() {};
00313 const RV& _rvc() const {return rvc;}
00314 };
00315
00316 #endif // BM_H