- Main Page
- Related Pages
- Classes
- Files

# work/git/mixpp/bdm/stat/libBM.h

Go to the documentation of this file.

    00001
    00013 #ifndef BM_H
    00014 #define BM_H
    00015
    00016 #include <itpp/itbase.h>
    00017 //#include <std>
    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         //TODO why not inline and later??
    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 ( const ivec &ind ) const;
    00086         RV operator() ( const ivec &ind ) const;
    00088         void t ( int delta );
    00090         str tostr() const;
    00092         ivec dataind(const 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
    00107         void set_id ( int at, int id0 ) {ids ( at )=id0;};
    00109         void set_size ( int at, int size0 ) {sizes ( at )=size0; tsize=sum(sizes);};
    00111         void set_time ( int at, int time0 ) {times ( at )=time0;};
    00112
    00114         void newids();
    00115 };
    00116
    00118 RV concat ( const RV &rv1, const RV &rv2 );
    00119
    00120
    00122
    00123 class fnc {
    00124 protected:
    00126         int dimy;
    00127 public:
    00129         fnc ( int dy ) :dimy ( dy ) {};
    00131         virtual vec eval ( const vec &cond ) {
    00132                 return vec ( 0 );
    00133         };
    00134
    00136         int _dimy() const{return dimy;}
    00137
    00139         virtual ~fnc() {};
    00140 };
    00141
    00142
    00144
    00145 class epdf {
    00146 protected:
    00148         RV rv;
    00149 public:
    00151         epdf() :rv ( ) {};
    00152
    00154         epdf ( const RV &rv0 ) :rv ( rv0 ) {};
    00155
    00156 //      //! Returns the required moment of the epdf
    00157 //      virtual vec moment ( const int order = 1 );
    00158
    00160         virtual vec sample () const =0;
    00162         virtual mat sampleN ( int N ) const;
    00164         virtual double eval ( const vec &val ) const {return exp ( this->evalpdflog ( val ) );};
    00165
    00167         virtual double evalpdflog ( const vec &val ) const =0;
    00168
    00170         virtual vec evalpdflog_m ( const mat &Val ) const {
    00171                 vec x ( Val.cols() );
    00172                 for ( int i=0;i<Val.cols();i++ ) {x ( i ) =evalpdflog( Val.get_col(i) ) ;}
    00173                 return x;
    00174         }
    00175
    00177         virtual vec mean() const =0;
    00178
    00180         virtual ~epdf() {};
    00182         const RV& _rv() const {return rv;}
    00184         void _renewrv(const RV &in_rv){rv=in_rv;}
    00185 };
    00186
    00187
    00189 //TODO Samplecond can be generalized
    00190
    00191 class mpdf {
    00192 protected:
    00194         RV rv;
    00196         RV rvc;
    00198         epdf* ep;
    00199 public:
    00200
    00202 //      virtual fnc moment ( const int order = 1 );
    00204         virtual vec samplecond (const vec &cond, double &ll ) {this->condition ( cond );
    00205         vec temp= ep->sample();
    00206         ll=ep->evalpdflog ( temp );return temp;};
    00208         virtual mat samplecond (const vec &cond, vec &ll, int N ) {
    00209                 this->condition ( cond );
    00210                 mat temp ( rv.count(),N ); vec smp ( rv.count() );
    00211                 for ( int i=0;i<N;i++ ) {smp=ep->sample() ;temp.set_col ( i, smp );ll ( i ) =ep->evalpdflog ( smp );}
    00212                 return temp;
    00213         };
    00215         virtual void condition ( const vec &cond ) {};
    00216
    00218         virtual double evalcond ( const vec &dt, const vec &cond ) {this->condition ( cond );return ep->eval ( dt );};
    00219
    00221         virtual ~mpdf() {};
    00222
    00224         mpdf ( const RV &rv0, const RV &rvc0 ) :rv ( rv0 ),rvc ( rvc0 ) {};
    00226         RV _rvc() {return rvc;}
    00228         RV _rv() {return rv;}
    00230         epdf& _epdf() {return *ep;}
    00231 };
    00232
    00237 class mepdf : public mpdf {
    00238 public:
    00240         mepdf (epdf &em ) :mpdf ( em._rv(),RV() ) {ep=&em;};
    00241 };
    00242
    00244 class compositepdf{
    00245         protected:
    00247                 int n;
    00249                 Array<mpdf*> mpdfs;
    00251                 Array<ivec> rvsinrv;
    00253                 Array<ivec> rvcsinrv;
    00254         public:
    00255                 compositepdf(Array<mpdf*> A0): n(A0.length()), mpdfs(A0), rvsinrv(n), rvcsinrv(n){};
    00256                 RV getrv(bool checkoverlap=false);
    00257                 void setrvc(const RV &rv, RV &rvc);
    00258                 void setindices(const RV &rv);
    00259                 void setrvcinrv(const RV &rvc, Array<ivec> &rvcind);
    00260 };
    00261
    00269 class DS {
    00270 protected:
    00272         RV Drv;
    00274         RV Urv; //
    00275 public:
    00277         void getdata ( vec &dt );
    00279         void getdata ( vec &dt, ivec &indeces );
    00281         void write ( vec &ut );
    00283         void write ( vec &ut, ivec &indeces );
    00289         void linkrvs ( RV &drv, RV &urv );
    00290
    00292         void step();
    00293
    00294 };
    00295
    00300 class BM {
    00301 protected:
    00303         RV rv;
    00305         double ll;
    00307         bool evalll;
    00308 public:
    00309
    00311         BM ( const RV &rv0, double ll0=0,bool evalll0=true ) :rv ( rv0 ), ll ( ll0 ),evalll ( evalll0) {//Fixme: test rv
    00312         };
    00314         BM (const BM &B) : rv(B.rv), ll(B.ll), evalll(B.evalll) {}
    00315
    00319         virtual void bayes ( const vec &dt ) = 0;
    00321         virtual void bayesB (const mat &Dt );
    00323         virtual const epdf& _epdf() const =0;
    00324
    00327         virtual double logpred(const vec &dt)const{it_error("Not implemented");return 0.0;}
    00329         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;}
    00330
    00332         virtual epdf* predictor(const RV &rv){it_error("Not implemented");return NULL;};
    00333
    00335         virtual ~BM() {};
    00337         const RV& _rv() const {return rv;}
    00339         double _ll() const {return ll;}
    00341         void set_evalll(bool evl0){evalll=evl0;}
    00342
    00345         virtual BM* _copy_(bool changerv=false){it_error("function _copy_ not implemented for this BM"); return NULL;};
    00346 };
    00347
    00357 class BMcond {
    00358 protected:
    00360         RV rvc;
    00361 public:
    00363         virtual void condition ( const vec &val ) =0;
    00365         BMcond ( RV &rv0 ) :rvc ( rv0 ) {};
    00367         virtual ~BMcond() {};
    00369         const RV& _rvc() const {return rvc;}
    00370 };
    00371
    00372 #endif // BM_H