Legend:
- Unmodified
- Added
- Removed
-
bdm/stat/emix.cpp
r141 r145 18 18 19 19 int i=0; 20 while ( ( w( i ) <u0 ) && ( i< ( w.length()-1 ) ) ) {i++;}20 while ( ( cumDist ( i ) <u0 ) && ( i< ( w.length()-1 ) ) ) {i++;} 21 21 22 22 return Coms ( i )->sample(); -
bdm/stat/emix.h
r141 r145 30 30 31 31 */ 32 class emix : public epdf 33 { 32 class emix : public epdf { 34 33 protected: 35 34 //! weights of the components … … 39 38 public: 40 39 //!Default constructor 41 emix ( RV &rv ) : epdf ( rv) {};40 emix(RV &rv) : epdf(rv) {}; 42 41 //! Set weights \c w and components \c R 43 void set_parameters ( const vec &w, const Array<epdf*> &Coms);42 void set_parameters(const vec &w, const Array<epdf*> &Coms); 44 43 45 44 vec sample() const; 46 vec mean() const 47 { 48 int i; vec mu=zeros ( rv.count() ); 49 for ( i=0;i<w.length();i++ ) {mu+=w ( i ) *Coms ( i )->mean(); } 45 vec mean() const { 46 int i; vec mu = zeros(rv.count()); 47 for (i = 0;i < w.length();i++) {mu += w(i) * Coms(i)->mean(); } 50 48 return mu; 51 49 } 52 double evalpdflog ( const vec &val ) const 53 { 50 double evalpdflog(const vec &val) const { 54 51 int i; 55 double sum =0.0;56 for ( i=0;i<w.length();i++ ) {sum+=w ( i ) *Coms ( i )->evalpdflog ( val);}57 return log ( sum);52 double sum = 0.0; 53 for (i = 0;i < w.length();i++) {sum += w(i) * Coms(i)->evalpdflog(val);} 54 return log(sum); 58 55 }; 59 56 … … 65 62 /*! \brief Chain rule decomposition of epdf 66 63 64 Probability density in the form of Chain-rule decomposition: 65 \[ 66 f(x_1,x_2,x_3) = f(x_1|x_2,x_3)f(x_2,x_3)f(x_3) 67 \] 68 Note that 69 */ 70 class eprod: public epdf { 71 protected: 72 // 73 int n; 74 // pointers to epdfs 75 Array<epdf*> epdfs; 76 Array<mpdf*> mpdfs; 77 // 78 Array<ivec> rvinds; 79 Array<ivec> rvcinds; 80 //! Indicate independence of its factors 81 bool independent; 82 //! Indicate internal creation of mpdfs which must be destroyed 83 bool intermpdfs; 84 public: 85 //!Constructor from list of eFacs or list of mFacs 86 eprod(Array<mpdf*> mFacs): epdf(RV()), n(mFacs.length()), epdfs(n), mpdfs(mFacs), rvinds(n), rvcinds(n) { 87 int i; 88 intermpdfs = false; 89 for (i = 0;i < n;i++) { 90 rv.add(mpdfs(i)->_rv()); //add rv to common rvs. 91 epdfs(i) = &(mpdfs(i)->_epdf()); // add pointer to epdf 92 }; 93 independent = true; 94 //test rvc of mpdfs and fill rvinds 95 for (i = 0;i < n;i++) { 96 // find ith rv in common rv 97 rvinds(i) = mpdfs(i)->_rv().dataind(rv); 98 // find ith rvc in common rv 99 rvcinds(i) = mpdfs(i)->_rvc().dataind(rv); 100 if (rvcinds(i).length()>0) {independent = false;} 101 } 102 103 }; 104 eprod(Array<epdf*> eFacs): epdf(RV()), n(eFacs.length()), epdfs(eFacs), mpdfs(n), rvinds(n), rvcinds(n) { 105 int i; 106 intermpdfs = true; 107 for (i = 0;i < n;i++) { 108 if (rv.add(eFacs(i)->_rv())) {it_error("Incompatible eFacs.rv!");} //add rv to common rvs. 109 mpdfs(i) = new mepdf(*(epdfs(i))); 110 epdfs(i) = &(mpdfs(i)->_epdf()); // add pointer to epdf 111 }; 112 independent = true; 113 //test rvc of mpdfs and fill rvinds 114 for (i = 0;i < n;i++) { 115 // find ith rv in common rv 116 rvinds(i) = mpdfs(i)->_rv().dataind(rv); 117 } 118 }; 119 120 double evalpdflog(const vec &val) const { 121 int i; 122 double res = 0.0; 123 for (i = n - 1;i > 0;i++) { 124 if (rvcinds(i).length() > 0) 125 {mpdfs(i)->condition(val(rvcinds(i)));} 126 // add logarithms 127 res += epdfs(i)->evalpdflog(val(rvinds(i))); 128 } 129 } 130 vec sample () const{ 131 vec smp=zeros(rv.count()); 132 for (int i = (n - 1);i >= 0;i--) { 133 if (rvcinds(i).length() > 0) 134 {mpdfs(i)->condition(smp(rvcinds(i)));} 135 set_subvector(smp,rvinds(i), epdfs(i)->sample()); 136 } 137 return smp; 138 } 139 vec mean() const{ 140 vec tmp(rv.count()); 141 if (independent) { 142 for (int i=0;i<n;i++) { 143 vec pom = epdfs(i)->mean(); 144 set_subvector(tmp,rvinds(i), pom); 145 } 146 } 147 else { 148 int N=50*rv.count(); 149 it_warning("eprod.mean() computed by sampling"); 150 tmp = zeros(rv.count()); 151 for (int i=0;i<N;i++){ tmp += sample();} 152 tmp /=N; 153 } 154 return tmp; 155 } 156 ~eprod(){if (intermpdfs) {for (int i=0;i<n;i++){delete mpdfs(i);}}}; 157 }; 158 159 /*! \brief Mixture of mpdfs with constant weights, all mpdfs are of equal type 67 160 68 161 */ 69 class eprod: public epdf 70 { 71 protected: 72 Array<epdf*> epdfs; 73 Array<mpdf*> mpdfs; 74 public: 75 76 77 }; 78 79 /*! \brief Mixture of mpdfs with constant weights 80 81 */ 82 class mmix : public mpdf 83 { 162 class mmix : public mpdf { 84 163 protected: 85 164 //! Component (epdfs) … … 89 168 public: 90 169 //!Default constructor 91 mmix ( RV &rv, RV &rvc ) : mpdf ( rv, rvc ), Epdf ( rv ) {ep=&Epdf;};170 mmix(RV &rv, RV &rvc) : mpdf(rv, rvc), Epdf(rv) {ep = &Epdf;}; 92 171 //! Set weights \c w and components \c R 93 void set_parameters ( const vec &w, const Array<mpdf*> &Coms ) 94 { 95 Array<epdf*> Eps ( Coms.length() ); 172 void set_parameters(const vec &w, const Array<mpdf*> &Coms) { 173 Array<epdf*> Eps(Coms.length()); 96 174 97 for ( int i=0;i<Coms.length();i++ ) 98 { 99 Eps ( i ) =& ( Coms ( i )->_epdf() ); 175 for (int i = 0;i < Coms.length();i++) { 176 Eps(i) = & (Coms(i)->_epdf()); 100 177 } 101 Epdf.set_parameters ( w,Eps);178 Epdf.set_parameters(w, Eps); 102 179 }; 103 180 104 void condition ( const vec &cond ) 105 { 106 for ( int i=0;i<Coms.length();i++ ) {Coms ( i )->condition ( cond );} 181 void condition(const vec &cond) { 182 for (int i = 0;i < Coms.length();i++) {Coms(i)->condition(cond);} 107 183 }; 108 184 }; -
bdm/stat/libBM.cpp
r102 r145 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 voidRV::add ( const RV &rv2 ) {36 bool RV::add ( const RV &rv2 ) { 37 37 // TODO 38 tsize+=rv2.tsize; 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 ); 38 ivec ind = rv2.findself ( *this ); //should be -1 all the time 39 ivec index = itpp::find(ind==-1); 40 41 if ( index.length() < rv2.len ) { //conflict 42 ids = concat ( ids, rv2.ids(index) ); 43 sizes = concat ( sizes, rv2.sizes(index) ); 44 times = concat ( times, rv2.times(index) ); 45 names = concat ( names, rv2.names(to_Arr(index)) ); 46 } 47 else { 48 ids = concat ( ids, rv2.ids ); 49 sizes = concat ( sizes, rv2.sizes ); 50 times = concat ( times, rv2.times ); 51 names = concat ( names, rv2.names ); 52 } 53 tsize = sum(sizes); 54 len = ids.length(); 55 return (index.length()<rv2.len); 56 44 57 // return *this; 45 58 }; … … 58 71 } 59 72 60 RV RV::subselect ( ivec ind ) {73 RV RV::subselect ( ivec ind ) const { 61 74 return RV ( ids ( ind ), names ( to_Arr ( ind ) ), sizes ( ind ), times ( ind ) ); 62 75 } 63 76 64 void RV::t ( int delta ) { times += delta;}77 void RV::t ( int delta ) { times += delta;} 65 78 66 RV RV::operator() ( ivec ind ) {79 RV RV::operator() ( ivec ind ) const { 67 80 return RV ( ids ( ind ), names ( to_Arr ( ind ) ), sizes ( ind ), times ( ind ) ); 68 81 } 69 82 70 bool RV::equal ( RVrv2 ) const {71 return ( ids ==rv2.ids ) && ( times == rv2.times ) && ( sizes==rv2.sizes );83 bool RV::equal ( const RV &rv2 ) const { 84 return ( ids == rv2.ids ) && ( times == rv2.times ) && ( sizes == rv2.sizes ); 72 85 } 73 86 74 87 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() );88 mat X = zeros ( rv.count(), N ); 89 for ( int i = 0;i < N;i++ ) X.set_col ( i, this->sample() ); 77 90 return X; 78 91 }; … … 88 101 } 89 102 90 ivec RV::indexlist() { 91 ivec indlist ( tsize ); 103 str RV::tostr() const { 104 ivec idlist ( tsize ); 105 ivec tmlist ( tsize ); 92 106 int i; 93 107 int pos = 0; 94 for ( i=0;i<len;i++ ) { 95 indlist.set_subvector ( pos,pos+sizes ( i )-1, ids ( i ) ); 108 for ( i = 0;i < len;i++ ) { 109 idlist.set_subvector ( pos, pos + sizes ( i ) - 1, ids ( i ) ); 110 tmlist.set_subvector ( pos, pos + sizes ( i ) - 1, times ( i ) ); 111 pos += sizes ( i ); 96 112 } 97 return indlist; 113 return str ( idlist, tmlist ); 114 } 115 116 ivec RV::dataind ( RV rv2 ) const { 117 str str2 = rv2.tostr(); 118 ivec res ( 0 ); 119 ivec part; 120 int i; 121 for ( i = 0;i < len;i++ ) { 122 part = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) ); 123 res = concat ( res, part ); 124 } 125 return res; 126 } 127 128 RV RV::subt ( const RV rv2 ) const { 129 ivec res = this->findself ( rv2 ); // nonzeros 130 ivec valid = itpp::find ( res == -1 ); //-1 => value not found => it remains 131 return ( *this ) ( valid ); //keep those that were not found in rv2 132 } 133 134 ivec RV::findself ( const RV &rv2 ) const { 135 int i, j; 136 ivec tmp = -ones_i ( len ); 137 for ( i = 0;i < len;i++ ) { 138 for ( j = 0;j < rv2.length();j++ ) { 139 if ( ( ids ( i ) == rv2.ids ( j ) ) & ( times ( i ) == rv2.times ( j ) ) ) { 140 tmp ( i ) = j; 141 break; 142 } 143 } 144 } 145 return tmp; 98 146 } 99 147 -
bdm/stat/libBM.h
r118 r145 18 18 19 19 using namespace itpp; 20 21 //! Structure of RV (used internally) 22 class str{ 23 public: 24 ivec ids; 25 ivec times; 26 str(ivec ids0, ivec times0):ids(ids0),times(times0){ 27 it_assert_debug(times0.length()==ids0.length(),"Incompatible input"); 28 }; 29 }; 20 30 21 31 /*! … … 61 71 //TODO why not inline and later?? 62 72 63 //! Find indexes of another rv in self64 ivec find ( RV rv2 );73 //! Find indexes of self in another rv, \return ivec of the same size as self. 74 ivec findself (const RV &rv2 ) const; 65 75 //! Compare if \c rv2 is identical to this \c RV 66 bool equal ( RV rv2 ) const; 67 //! Add (concat) another variable to the current one 68 void add ( const RV &rv2 ); 69 //! Add (concat) another variable to the current one 70 friend RV concat ( const RV &rv1, const RV &rv2 ); 76 bool equal (const RV &rv2 ) const; 77 //! Add (concat) another variable to the current one, \return 0 if all rv2 were added, 1 if rv2 is in conflict 78 bool add ( const RV &rv2 ); 71 79 //! Subtract another variable from the current one 72 RV subt ( RV rv2 );80 RV subt ( const RV rv2 ) const; 73 81 //! Select only variables at indeces ind 74 RV subselect ( ivec ind ) ;82 RV subselect ( ivec ind ) const; 75 83 //! Select only variables at indeces ind 76 RV operator() ( ivec ind ) ;77 //! Generate new \c RV with\c time shifted by delta.84 RV operator() ( ivec ind ) const; 85 //! Shift \c time shifted by delta. 78 86 void t ( int delta ); 79 //! generate a list of indeces, i.e. which 80 ivec indexlist(); 87 //! generate \c str from rv, by expanding sizes 88 str tostr() const; 89 //! generate indeces into \param crv data vector that form data vector of self. 90 ivec dataind(RV crv) const; 81 91 82 92 //!access function … … 92 102 std::string name ( int at ) {return names ( at );}; 93 103 }; 104 105 //! Concat two random variables 106 RV concat ( const RV &rv1, const RV &rv2 ); 94 107 95 108 … … 146 159 //! Destructor for future use; 147 160 virtual ~epdf() {}; 148 //! access function 149 RV _rv() const{return rv;}161 //! access function, possibly dangerous! 162 RV& _rv() {return rv;} 150 163 }; 151 164 … … 170 183 vec temp= ep->sample(); 171 184 ll=ep->evalpdflog ( temp );return temp;}; 172 //! 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.185 //! Returns \param 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. 173 186 virtual mat samplecond ( vec &cond, vec &ll, int N ) { 174 187 this->condition ( cond ); … … 190 203 //! access function 191 204 RV _rvc() {return rvc;} 205 //! access function 206 RV _rv() {return rv;} 192 207 //!access function 193 208 epdf& _epdf() {return *ep;} … … 201 216 public: 202 217 //!Default constructor 203 mepdf ( const RV &rv, const RV &rvc, epdf* em ) :mpdf ( rv,rvc ) {ep=em;};218 mepdf (epdf &em ) :mpdf ( em._rv(),RV() ) {ep=&em;}; 204 219 }; 205 220 -
bdm/stat/loggers.h
r102 r145 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 //! Save values into an itfile named after \c fname. 86 87 void itsave(const char* fname); 87 88 };