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 );};
00106 void newids();
00107 };
00108
00110 RV concat ( const RV &rv1, const RV &rv2 );
00111
00112
00114
00115 class fnc {
00116 protected:
00118 int dimy;
00119 public:
00121 fnc ( int dy ) :dimy ( dy ) {};
00123 virtual vec eval ( const vec &cond ) {
00124 return vec ( 0 );
00125 };
00126
00128 int _dimy() const{return dimy;}
00129
00131 virtual ~fnc() {};
00132 };
00133
00134
00136
00137 class epdf {
00138 protected:
00140 RV rv;
00141 public:
00143 epdf() :rv ( ) {};
00144
00146 epdf ( const RV &rv0 ) :rv ( rv0 ) {};
00147
00148
00149
00150
00152 virtual vec sample () const =0;
00154 virtual mat sampleN ( int N ) const;
00156 virtual double eval ( const vec &val ) const {return exp ( this->evalpdflog ( val ) );};
00157
00159 virtual double evalpdflog ( const vec &val ) const =0;
00160
00162 virtual vec evalpdflog ( const mat &Val ) const {
00163 vec x ( Val.cols() );
00164 for ( int i=0;i<Val.cols();i++ ) {x ( i ) =evalpdflog( Val.get_col(i) ) ;}
00165 return x;
00166 }
00167
00169 virtual vec mean() const =0;
00170
00172 virtual ~epdf() {};
00174 const RV& _rv() const {return rv;}
00176 void _renewrv(const RV &in_rv){rv=in_rv;}
00177 };
00178
00179
00181
00182
00183 class mpdf {
00184 protected:
00186 RV rv;
00188 RV rvc;
00190 epdf* ep;
00191 public:
00192
00194
00196 virtual vec samplecond (const vec &cond, double &ll ) {this->condition ( cond );
00197 vec temp= ep->sample();
00198 ll=ep->evalpdflog ( temp );return temp;};
00200 virtual mat samplecond (const vec &cond, vec &ll, int N ) {
00201 this->condition ( cond );
00202 mat temp ( rv.count(),N ); vec smp ( rv.count() );
00203 for ( int i=0;i<N;i++ ) {smp=ep->sample() ;temp.set_col ( i, smp );ll ( i ) =ep->evalpdflog ( smp );}
00204 return temp;
00205 };
00207 virtual void condition ( const vec &cond ) {};
00208
00210 virtual double evalcond ( const vec &dt, const vec &cond ) {this->condition ( cond );return ep->eval ( dt );};
00211
00213 virtual ~mpdf() {};
00214
00216 mpdf ( const RV &rv0, const RV &rvc0 ) :rv ( rv0 ),rvc ( rvc0 ) {};
00218 RV _rvc() {return rvc;}
00220 RV _rv() {return rv;}
00222 epdf& _epdf() {return *ep;}
00223 };
00224
00229 class mepdf : public mpdf {
00230 public:
00232 mepdf (epdf &em ) :mpdf ( em._rv(),RV() ) {ep=&em;};
00233 };
00234
00242 class DS {
00243 protected:
00245 RV Drv;
00247 RV Urv;
00248 public:
00250 void getdata ( vec &dt );
00252 void getdata ( vec &dt, ivec &indeces );
00254 void write ( vec &ut );
00256 void write ( vec &ut, ivec &indeces );
00262 void linkrvs ( RV &drv, RV &urv );
00263
00265 void step();
00266
00267 };
00268
00273 class BM {
00274 protected:
00276 RV rv;
00278 double ll;
00280 bool evalll;
00281 public:
00282
00284 BM ( const RV &rv0, double ll0=0,bool evalll0=true ) :rv ( rv0 ), ll ( ll0 ),evalll ( evalll0) {
00285 };
00287 BM (const BM &B) : rv(B.rv), ll(B.ll), evalll(B.evalll) {}
00288
00292 virtual void bayes ( const vec &dt ) = 0;
00294 virtual void bayesB (const mat &Dt );
00296 virtual const epdf& _epdf() const =0;
00297
00300 virtual double logpred(const vec &dt)const{it_error("Not implemented");return 0.0;}
00301
00303 virtual ~BM() {};
00305 const RV& _rv() const {return rv;}
00307 double _ll() const {return ll;}
00309 void set_evalll(bool evl0){evalll=evl0;}
00310
00313 virtual BM* _copy_(bool changerv=false){it_error("function _copy_ not implemented for this BM"); return NULL;};
00314 };
00315
00325 class BMcond {
00326 protected:
00328 RV rvc;
00329 public:
00331 virtual void condition ( const vec &val ) =0;
00333 BMcond ( RV &rv0 ) :rvc ( rv0 ) {};
00335 virtual ~BMcond() {};
00337 const RV& _rvc() const {return rvc;}
00338 };
00339
00340 #endif // BM_H