Changeset 102
Legend:
- Unmodified
- Added
- Removed
-
bdm/stat/libBM.cpp
r85 r102 7 7 using std::cout; 8 8 9 void RV::init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times ) {9 void RV::init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times ) { 10 10 // 11 11 int i; … … 13 13 //PRUDENT_MODE 14 14 // All vetors should be of same length 15 if ( 15 if ( ( len != in_ids.length() ) || \ 16 16 ( len != in_names.length() ) || \ 17 17 ( len != in_sizes.length() ) || \ 18 ( len != in_times.length() ) ) {19 it_error ( "RV::RV inconsistent length of input vectors." );18 ( len != in_times.length() ) ) { 19 it_error ( "RV::RV inconsistent length of input vectors." ); 20 20 } 21 21 … … 25 25 times = in_times; 26 26 tsize = 0; 27 for (i=0;i<len;i++){tsize+=sizes(i);}27 for ( i=0;i<len;i++ ) {tsize+=sizes ( i );} 28 28 }; 29 29 … … 32 32 } 33 33 34 RV::RV () : tsize (0),len(0){};34 RV::RV () : tsize ( 0 ),len ( 0 ) {}; 35 35 36 void RV::add ( const RV &rv2) {36 void RV::add ( const RV &rv2 ) { 37 37 // TODO 38 38 tsize+=rv2.tsize; 39 39 len +=rv2.len; 40 ids=concat (ids,rv2.ids);41 sizes=concat (sizes,rv2.sizes);42 times=concat (times,rv2.times);43 names=concat (names,rv2.names);40 ids=concat ( ids,rv2.ids ); 41 sizes=concat ( sizes,rv2.sizes ); 42 times=concat ( times,rv2.times ); 43 names=concat ( names,rv2.names ); 44 44 // return *this; 45 45 }; 46 46 47 47 RV::RV ( ivec in_ids ) { 48 48 49 49 len = in_ids.length(); 50 Array<std::string> A ( len );50 Array<std::string> A ( len ); 51 51 std::string rvstr = "rv"; 52 52 53 53 for ( int i = 0; i < len;i++ ) { 54 A ( i ) = rvstr + to_str(i);54 A ( i ) = rvstr + to_str ( i ); 55 55 } 56 56 57 init ( in_ids, A, ones_i ( len ), zeros_i( len ) );57 init ( in_ids, A, ones_i ( len ), zeros_i ( len ) ); 58 58 } 59 59 60 RV RV::subselect (ivec ind){61 return RV (ids(ind), names(to_Arr(ind)), sizes(ind), times(ind));60 RV RV::subselect ( ivec ind ) { 61 return RV ( ids ( ind ), names ( to_Arr ( ind ) ), sizes ( ind ), times ( ind ) ); 62 62 } 63 63 64 void RV::t (int delta){ times +=delta;}64 void RV::t ( int delta ) { times +=delta;} 65 65 66 RV RV::operator() (ivec ind){67 return RV (ids(ind), names(to_Arr(ind)), sizes(ind), times(ind));66 RV RV::operator() ( ivec ind ) { 67 return RV ( ids ( ind ), names ( to_Arr ( ind ) ), sizes ( ind ), times ( ind ) ); 68 68 } 69 69 70 std::ostream &operator<<( std::ostream &os, const RV &rv ) { 70 bool RV::equal ( RV rv2 ) const { 71 return ( ids==rv2.ids ) && ( times == rv2.times ) && ( sizes==rv2.sizes ); 72 } 73 74 mat epdf::sampleN ( int N ) const { 75 mat X =zeros ( rv.count(),N ); 76 for ( int i=0;i<N;i++ ) X.set_col ( i,this->sample() ); 77 return X; 78 }; 79 80 81 std::ostream &operator<< ( std::ostream &os, const RV &rv ) { 71 82 72 83 for ( int i = 0; i < rv.len ;i++ ) { 73 os << rv.ids ( i ) << "(" << rv.sizes( i ) << ")" << // id(size)=74 "=" << rv.names ( i ) << "_{" << rv.times( i ) << "}; "; //name_{time}84 os << rv.ids ( i ) << "(" << rv.sizes ( i ) << ")" << // id(size)= 85 "=" << rv.names ( i ) << "_{" << rv.times ( i ) << "}; "; //name_{time} 75 86 } 76 87 return os; 77 88 } 78 89 79 ivec RV::indexlist() {80 ivec indlist (tsize);90 ivec RV::indexlist() { 91 ivec indlist ( tsize ); 81 92 int i; 82 93 int pos = 0; 83 for (i=0;i<len;i++){84 indlist.set_subvector (pos,pos+sizes(i)-1, ids(i));94 for ( i=0;i<len;i++ ) { 95 indlist.set_subvector ( pos,pos+sizes ( i )-1, ids ( i ) ); 85 96 } 86 97 return indlist; 87 98 } 88 99 89 RV concat (const RV &rv1, const RV &rv2 ){100 RV concat ( const RV &rv1, const RV &rv2 ) { 90 101 RV pom = rv1; 91 pom.add (rv2);102 pom.add ( rv2 ); 92 103 return pom; 93 104 } -
bdm/stat/libBM.h
r96 r102 31 31 //! len = number of individual rvs 32 32 int len; 33 //! Vector of unique IDs 33 //! Vector of unique IDs 34 34 ivec ids; 35 35 //! Vector of sizes … … 42 42 private: 43 43 //! auxiliary function used in constructor 44 void init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times );44 void init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times ); 45 45 public: 46 46 //! Full constructor which is called by the others 47 RV ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times );47 RV ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times ); 48 48 //! default constructor 49 49 RV ( ivec ids ); … … 63 63 //! Find indexes of another rv in self 64 64 ivec find ( RV rv2 ); 65 //! Compare if \c rv2 is identical to this \c RV 66 bool equal ( RV rv2 ) const; 65 67 //! Add (concat) another variable to the current one 66 void add (const RV &rv2 );68 void add ( const RV &rv2 ); 67 69 //! Add (concat) another variable to the current one 68 friend RV concat ( const RV &rv1, const RV &rv2 );70 friend RV concat ( const RV &rv1, const RV &rv2 ); 69 71 //! Subtract another variable from the current one 70 72 RV subt ( RV rv2 ); … … 79 81 80 82 //!access function 81 Array<std::string>& _names() {return names;};82 83 //!access function 84 int id (int at){return ids(at);};85 //!access function 86 int size (int at){return sizes(at);};87 //!access function 88 int time (int at){return times(at);};89 //!access function 90 std::string name (int at){return names(at);};83 Array<std::string>& _names() {return names;}; 84 85 //!access function 86 int id ( int at ) {return ids ( at );}; 87 //!access function 88 int size ( int at ) {return sizes ( at );}; 89 //!access function 90 int time ( int at ) {return times ( at );}; 91 //!access function 92 std::string name ( int at ) {return names ( at );}; 91 93 }; 92 94 … … 100 102 public: 101 103 //!default constructor 102 fnc (int dy):dimy(dy){};104 fnc ( int dy ) :dimy ( dy ) {}; 103 105 //! function evaluates numerical value of \f$f(x)\f$ at \f$x=\f$ \c cond 104 106 virtual vec eval ( const vec &cond ) { 105 107 return vec ( 0 ); 106 }; 108 }; 107 109 108 110 //! access function … … 131 133 //! Returns a sample, \f$x\f$ from density \f$epdf(rv)\f$ 132 134 virtual vec sample () const =0; 135 //! Returns N samples from density \f$epdf(rv)\f$ 136 virtual mat sampleN ( int N ) const; 133 137 //! Compute probability of argument \c val 134 virtual double eval ( const vec &val ) const {return exp (this->evalpdflog(val));};138 virtual double eval ( const vec &val ) const {return exp ( this->evalpdflog ( val ) );}; 135 139 136 140 //! Compute log-probability of argument \c val 137 141 virtual double evalpdflog ( const vec &val ) const =0; 138 142 139 //! return expected value 143 //! return expected value 140 144 virtual vec mean() const =0; 141 145 142 146 //! Destructor for future use; 143 147 virtual ~epdf() {}; … … 163 167 // virtual fnc moment ( const int order = 1 ); 164 168 //! Returns a sample from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv \param ll is a return value of log-likelihood of the sample. 165 virtual vec samplecond ( vec &cond, double &ll ) {this->condition(cond);vec temp= ep->sample();ll=ep->evalpdflog(temp);return temp;}; 169 virtual vec samplecond ( vec &cond, double &ll ) {this->condition ( cond );vec temp= ep->sample();ll=ep->evalpdflog ( temp );return temp;}; 170 //! Returns N samples from the density conditioned on \c cond, \f$x \sim epdf(rv|cond)\f$. \param cond is numeric value of \c rv \param ll is a return value of log-likelihood of the sample. 171 virtual mat samplecond ( vec &cond, vec &ll, int N ) { 172 this->condition ( cond ); 173 mat temp ( rv.count(),N ); vec smp ( rv.count() ); for ( int i=0;i<N;i++ ) {smp=ep->sample() ;temp.set_col ( i, smp );ll ( i ) =ep->evalpdflog ( smp );} 174 return temp; 175 }; 166 176 //! Update \c ep so that it represents this mpdf conditioned on \c rvc = cond 167 177 virtual void condition ( const vec &cond ) {}; 168 178 169 179 //! Shortcut for conditioning and evaluation of the internal epdf. In some cases, this operation can be implemented efficiently. 170 virtual double evalcond ( const vec &dt, const vec &cond ) {this->condition(cond);return ep->eval(dt);};180 virtual double evalcond ( const vec &dt, const vec &cond ) {this->condition ( cond );return ep->eval ( dt );}; 171 181 172 182 //! Destructor for future use; … … 176 186 mpdf ( const RV &rv0, const RV &rvc0 ) :rv ( rv0 ),rvc ( rvc0 ) {}; 177 187 //! access function 178 RV _rvc() {return rvc;}179 //!access function 180 epdf& _epdf() {return *ep;}188 RV _rvc() {return rvc;} 189 //!access function 190 epdf& _epdf() {return *ep;} 181 191 }; 182 192 … … 230 240 231 241 //!Default constructor 232 BM (const RV &rv0) :rv(rv0), ll ( 0 ),evalll ( true ) {//Fixme: test rv242 BM ( const RV &rv0 ) :rv ( rv0 ), ll ( 0 ),evalll ( true ) {//Fixme: test rv 233 243 }; 234 244 … … 240 250 void bayes ( mat Dt ); 241 251 //! Returns a pointer to the epdf representing posterior density on parameters. Use with care! 242 virtual epdf& _epdf() =0;252 virtual epdf& _epdf() =0; 243 253 244 254 //! Destructor for future use; … … 267 277 virtual void condition ( const vec &val ) =0; 268 278 //! Default constructor 269 BMcond (RV &rv0):rvc(rv0){};279 BMcond ( RV &rv0 ) :rvc ( rv0 ) {}; 270 280 //! Destructor for future use 271 virtual ~BMcond() {};281 virtual ~BMcond() {}; 272 282 //! access function 273 283 const RV& _rvc() const {return rvc;} -
bdm/stat/libEF.cpp
r96 r102 65 65 } 66 66 67 mat egamma::sample ( int N ) const {68 mat Smp ( rv.count(),N );69 int i,j;70 71 for ( i=0; i<rv.count(); i++ ) {72 GamRNG.setup ( alpha ( i ),beta ( i ) );73 74 for ( j=0; j<N; j++ ) {75 Smp ( i,j ) = GamRNG();76 }77 }78 79 return Smp;80 }67 // mat egamma::sample ( int N ) const { 68 // mat Smp ( rv.count(),N ); 69 // int i,j; 70 // 71 // for ( i=0; i<rv.count(); i++ ) { 72 // GamRNG.setup ( alpha ( i ),beta ( i ) ); 73 // 74 // for ( j=0; j<N; j++ ) { 75 // Smp ( i,j ) = GamRNG(); 76 // } 77 // } 78 // 79 // return Smp; 80 // } 81 81 82 82 double egamma::evalpdflog ( const vec &val ) const { -
bdm/stat/libEF.h
r96 r102 162 162 vec sample() const; 163 163 //! TODO: is it used anywhere? 164 mat sample ( int N ) const;164 // mat sample ( int N ) const; 165 165 double evalpdflog ( const vec &val ) const; 166 166 double lognc () const; … … 206 206 vec sample() const { 207 207 vec smp ( rv.count() ); UniRNG.sample_vector ( rv.count(),smp ); 208 return low+ distance*smp;208 return low+elem_mult(distance,smp); 209 209 } 210 210 //! set values of \c low and \c high -
bdm/stat/loggers.cpp
r95 r102 7 7 #include <io.h> 8 8 #endif 9 10 void memlog::itsave(const char* fname){ 11 it_file itf(fname); 12 int i; 13 for (i=0; i<entries.length();i++){ 14 itf << Name(names(i)) << vectors(i); 15 } 16 } 9 17 10 18 void dirfilelog::init() { -
bdm/stat/loggers.h
r92 r102 84 84 void step(bool final=false) {if ( ind<maxlen ) ind++; else it_error ( "memlog::ind is too high;" );} 85 85 void logit ( int id, vec v ) {vectors ( id ).set_row ( ind,v );} 86 void itsave(const char* fname); 86 87 }; 87 88