| 1 | |
|---|
| 2 | #include "bdmbase.h" |
|---|
| 3 | |
|---|
| 4 | //! Space of basic BDM structures |
|---|
| 5 | namespace bdm { |
|---|
| 6 | |
|---|
| 7 | const int RV_BUFFER_STEP = 1; |
|---|
| 8 | RVmap RV_MAP; |
|---|
| 9 | Array<string> RV_NAMES ( RV_BUFFER_STEP ); |
|---|
| 10 | ivec RV_SIZES ( RV_BUFFER_STEP ); |
|---|
| 11 | |
|---|
| 12 | RV RV0 = RV(); |
|---|
| 13 | |
|---|
| 14 | void RV::clear_all() { |
|---|
| 15 | RV_MAP.clear(); |
|---|
| 16 | RV_SIZES.clear(); |
|---|
| 17 | RV_NAMES = Array<string> ( RV_BUFFER_STEP ); |
|---|
| 18 | } |
|---|
| 19 | |
|---|
| 20 | int RV::init ( const string &name, int size ) { |
|---|
| 21 | //Refer |
|---|
| 22 | int id; |
|---|
| 23 | RVmap::const_iterator iter = RV_MAP.find ( name ); |
|---|
| 24 | if ( iter == RV_MAP.end() ) { |
|---|
| 25 | id = RV_MAP.size() + 1; |
|---|
| 26 | //debug |
|---|
| 27 | /* { |
|---|
| 28 | cout << endl; |
|---|
| 29 | RVmap::const_iterator iter = RV_MAP.begin(); |
|---|
| 30 | for(RVmap::const_iterator iter=RV_MAP.begin(); iter!=RV_MAP.end(); iter++){ |
|---|
| 31 | cout << "key: " << iter->first << " val: " << iter->second <<endl; |
|---|
| 32 | } |
|---|
| 33 | }*/ |
|---|
| 34 | |
|---|
| 35 | RV_MAP.insert ( make_pair ( name, id ) ); //add new rv |
|---|
| 36 | if ( id >= RV_NAMES.length() ) { |
|---|
| 37 | RV_NAMES.set_length ( id + RV_BUFFER_STEP, true ); |
|---|
| 38 | RV_SIZES.set_length ( id + RV_BUFFER_STEP, true ); |
|---|
| 39 | } |
|---|
| 40 | RV_NAMES ( id ) = name; |
|---|
| 41 | RV_SIZES ( id ) = size; |
|---|
| 42 | } else { |
|---|
| 43 | id = iter->second; |
|---|
| 44 | it_assert ( RV_SIZES ( id ) == size, "RV " + name + " of different size already exists" ); |
|---|
| 45 | } |
|---|
| 46 | return id; |
|---|
| 47 | }; |
|---|
| 48 | |
|---|
| 49 | int RV::countsize() const { |
|---|
| 50 | int tmp = 0; |
|---|
| 51 | for ( int i = 0; i < len; i++ ) { |
|---|
| 52 | tmp += RV_SIZES ( ids ( i ) ); |
|---|
| 53 | } |
|---|
| 54 | return tmp; |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | ivec RV::cumsizes() const { |
|---|
| 58 | ivec szs ( len ); |
|---|
| 59 | int tmp = 0; |
|---|
| 60 | for ( int i = 0; i < len; i++ ) { |
|---|
| 61 | tmp += RV_SIZES ( ids ( i ) ); |
|---|
| 62 | szs ( i ) = tmp; |
|---|
| 63 | } |
|---|
| 64 | return szs; |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void RV::init ( const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times ) { |
|---|
| 68 | len = in_names.length(); |
|---|
| 69 | it_assert_debug ( in_names.length() == in_times.length(), "check \"times\" " ); |
|---|
| 70 | it_assert_debug ( in_names.length() == in_sizes.length(), "check \"sizes\" " ); |
|---|
| 71 | |
|---|
| 72 | times.set_length ( len ); |
|---|
| 73 | ids.set_length ( len ); |
|---|
| 74 | int id; |
|---|
| 75 | for ( int i = 0; i < len; i++ ) { |
|---|
| 76 | id = init ( in_names ( i ), in_sizes ( i ) ); |
|---|
| 77 | ids ( i ) = id; |
|---|
| 78 | } |
|---|
| 79 | times = in_times; |
|---|
| 80 | dsize = countsize(); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | RV::RV ( string name, int sz, int tm ) { |
|---|
| 84 | Array<string> A ( 1 ); |
|---|
| 85 | A ( 0 ) = name; |
|---|
| 86 | init ( A, vec_1 ( sz ), vec_1 ( tm ) ); |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | bool RV::add ( const RV &rv2 ) { |
|---|
| 90 | // TODO |
|---|
| 91 | if ( rv2.len > 0 ) { //rv2 is nonempty |
|---|
| 92 | ivec ind = rv2.findself ( *this ); //should be -1 all the time |
|---|
| 93 | ivec index = itpp::find ( ind == -1 ); |
|---|
| 94 | |
|---|
| 95 | if ( index.length() < rv2.len ) { //conflict |
|---|
| 96 | ids = concat ( ids, rv2.ids ( index ) ); |
|---|
| 97 | times = concat ( times, rv2.times ( index ) ); |
|---|
| 98 | } else { |
|---|
| 99 | ids = concat ( ids, rv2.ids ); |
|---|
| 100 | times = concat ( times, rv2.times ); |
|---|
| 101 | } |
|---|
| 102 | len = ids.length(); |
|---|
| 103 | dsize = countsize(); |
|---|
| 104 | return ( index.length() == rv2.len ); //conflict or not |
|---|
| 105 | } else { //rv2 is empty |
|---|
| 106 | return true; // no conflict |
|---|
| 107 | } |
|---|
| 108 | }; |
|---|
| 109 | |
|---|
| 110 | RV RV::subselect ( const ivec &ind ) const { |
|---|
| 111 | RV ret; |
|---|
| 112 | ret.ids = ids ( ind ); |
|---|
| 113 | ret.times = times ( ind ); |
|---|
| 114 | ret.len = ind.length(); |
|---|
| 115 | ret.dsize = ret.countsize(); |
|---|
| 116 | return ret; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | RV RV::operator() ( int di1, int di2 ) const { |
|---|
| 120 | ivec sz = cumsizes(); |
|---|
| 121 | int i1 = 0; |
|---|
| 122 | while ( sz ( i1 ) < di1 ) i1++; |
|---|
| 123 | int i2 = i1; |
|---|
| 124 | while ( sz ( i2 ) < di2 ) i2++; |
|---|
| 125 | return subselect ( linspace ( i1, i2 ) ); |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | void RV::t ( int delta ) { |
|---|
| 129 | times += delta; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | bool RV::equal ( const RV &rv2 ) const { |
|---|
| 133 | return ( ids == rv2.ids ) && ( times == rv2.times ); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | shared_ptr<mpdf> epdf::condition (const RV &rv) const { |
|---|
| 137 | it_warning ("Not implemented"); |
|---|
| 138 | return shared_ptr<mpdf>(); |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | shared_ptr<epdf> epdf::marginal (const RV &rv) const { |
|---|
| 142 | it_warning ("Not implemented"); |
|---|
| 143 | return shared_ptr<epdf>(); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | mat epdf::sample_m ( int N ) const { |
|---|
| 147 | mat X = zeros ( dim, N ); |
|---|
| 148 | for ( int i = 0; i < N; i++ ) X.set_col ( i, this->sample() ); |
|---|
| 149 | return X; |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | vec epdf::evallog_m (const mat &Val) const { |
|---|
| 153 | vec x (Val.cols()); |
|---|
| 154 | for (int i = 0; i < Val.cols(); i++) { |
|---|
| 155 | x (i) = evallog ( Val.get_col (i) ); |
|---|
| 156 | } |
|---|
| 157 | |
|---|
| 158 | return x; |
|---|
| 159 | } |
|---|
| 160 | |
|---|
| 161 | vec epdf::evallog_m (const Array<vec> &Avec) const { |
|---|
| 162 | vec x (Avec.size()); |
|---|
| 163 | for (int i = 0; i < Avec.size(); i++) { |
|---|
| 164 | x (i) = evallog ( Avec (i) ); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | return x; |
|---|
| 168 | } |
|---|
| 169 | |
|---|
| 170 | mat mpdf::samplecond_m (const vec &cond, int N) { |
|---|
| 171 | mat M( dimension(), N ); |
|---|
| 172 | for ( int i = 0; i < N; i++ ) { |
|---|
| 173 | M.set_col ( i, samplecond( cond ) ); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | return M; |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | void mpdf::from_setting ( const Setting &set ) { |
|---|
| 180 | shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional ); |
|---|
| 181 | if ( r ) { |
|---|
| 182 | set_rv ( *r ); |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | r = UI::build<RV> ( set, "rvc", UI::optional ); |
|---|
| 186 | if ( r ) { |
|---|
| 187 | set_rvc ( *r ); |
|---|
| 188 | } |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | std::ostream &operator<< ( std::ostream &os, const RV &rv ) { |
|---|
| 192 | int id; |
|---|
| 193 | for ( int i = 0; i < rv.len ; i++ ) { |
|---|
| 194 | id = rv.ids ( i ); |
|---|
| 195 | os << id << "(" << RV_SIZES ( id ) << ")" << // id(size)= |
|---|
| 196 | "=" << RV_NAMES ( id ) << "_{" << rv.times ( i ) << "}; "; //name_{time} |
|---|
| 197 | } |
|---|
| 198 | return os; |
|---|
| 199 | } |
|---|
| 200 | |
|---|
| 201 | str RV::tostr() const { |
|---|
| 202 | ivec idlist ( dsize ); |
|---|
| 203 | ivec tmlist ( dsize ); |
|---|
| 204 | int i; |
|---|
| 205 | int pos = 0; |
|---|
| 206 | for ( i = 0; i < len; i++ ) { |
|---|
| 207 | idlist.set_subvector ( pos, pos + size ( i ) - 1, ids ( i ) ); |
|---|
| 208 | tmlist.set_subvector ( pos, pos + size ( i ) - 1, times ( i ) ); |
|---|
| 209 | pos += size ( i ); |
|---|
| 210 | } |
|---|
| 211 | return str ( idlist, tmlist ); |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | ivec RV::dataind ( const RV &rv2 ) const { |
|---|
| 215 | ivec res ( 0 ); |
|---|
| 216 | if ( rv2._dsize() > 0 ) { |
|---|
| 217 | str str2 = rv2.tostr(); |
|---|
| 218 | ivec part; |
|---|
| 219 | int i; |
|---|
| 220 | for ( i = 0; i < len; i++ ) { |
|---|
| 221 | part = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) ); |
|---|
| 222 | res = concat ( res, part ); |
|---|
| 223 | } |
|---|
| 224 | } |
|---|
| 225 | it_assert_debug ( res.length() == dsize, "this rv is not fully present in crv!" ); |
|---|
| 226 | return res; |
|---|
| 227 | |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | void RV::dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const { |
|---|
| 231 | //clean results |
|---|
| 232 | selfi.set_size ( 0 ); |
|---|
| 233 | rv2i.set_size ( 0 ); |
|---|
| 234 | |
|---|
| 235 | // just in case any rv is empty |
|---|
| 236 | if ( ( len == 0 ) || ( rv2.length() == 0 ) ) { |
|---|
| 237 | return; |
|---|
| 238 | } |
|---|
| 239 | |
|---|
| 240 | //find comon rv |
|---|
| 241 | ivec cids = itpp::find ( this->findself ( rv2 ) >= 0 ); |
|---|
| 242 | |
|---|
| 243 | // index of |
|---|
| 244 | if ( cids.length() > 0 ) { |
|---|
| 245 | str str1 = tostr(); |
|---|
| 246 | str str2 = rv2.tostr(); |
|---|
| 247 | |
|---|
| 248 | ivec part1; |
|---|
| 249 | ivec part2; |
|---|
| 250 | int i, j; |
|---|
| 251 | // find common rv in strs |
|---|
| 252 | for ( j = 0; j < cids.length(); j++ ) { |
|---|
| 253 | i = cids ( j ); |
|---|
| 254 | part1 = itpp::find ( ( str1.ids == ids ( i ) ) & ( str1.times == times ( i ) ) ); |
|---|
| 255 | part2 = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) ); |
|---|
| 256 | selfi = concat ( selfi, part1 ); |
|---|
| 257 | rv2i = concat ( rv2i, part2 ); |
|---|
| 258 | } |
|---|
| 259 | } |
|---|
| 260 | it_assert_debug ( selfi.length() == rv2i.length(), "this should not happen!" ); |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | RV RV::subt ( const RV &rv2 ) const { |
|---|
| 264 | ivec res = this->findself ( rv2 ); // nonzeros |
|---|
| 265 | ivec valid; |
|---|
| 266 | if ( dsize > 0 ) { |
|---|
| 267 | valid = itpp::find ( res == -1 ); //-1 => value not found => it remains |
|---|
| 268 | } |
|---|
| 269 | return ( *this ) ( valid ); //keep those that were not found in rv2 |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | ivec RV::findself ( const RV &rv2 ) const { |
|---|
| 273 | int i, j; |
|---|
| 274 | ivec tmp = -ones_i ( len ); |
|---|
| 275 | for ( i = 0; i < len; i++ ) { |
|---|
| 276 | for ( j = 0; j < rv2.length(); j++ ) { |
|---|
| 277 | if ( ( ids ( i ) == rv2.ids ( j ) ) & ( times ( i ) == rv2.times ( j ) ) ) { |
|---|
| 278 | tmp ( i ) = j; |
|---|
| 279 | break; |
|---|
| 280 | } |
|---|
| 281 | } |
|---|
| 282 | } |
|---|
| 283 | return tmp; |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | void RV::from_setting ( const Setting &set ) { |
|---|
| 287 | Array<string> A; |
|---|
| 288 | it_assert ( !A.length(), "default array not empty" ); |
|---|
| 289 | UI::get ( A, set, "names" ); |
|---|
| 290 | |
|---|
| 291 | ivec szs; |
|---|
| 292 | if ( !UI::get ( szs, set, "sizes" ) ) |
|---|
| 293 | szs = ones_i ( A.length() ); |
|---|
| 294 | |
|---|
| 295 | ivec tms; |
|---|
| 296 | if ( !UI::get ( tms, set, "times" ) ) |
|---|
| 297 | tms = zeros_i ( A.length() ); |
|---|
| 298 | |
|---|
| 299 | // TODO tady se bude plnit primo do jeho promennych, a pak se zavola validacnni metoda, takze cele prepsat, ano? |
|---|
| 300 | init ( A, szs, tms ); |
|---|
| 301 | } |
|---|
| 302 | |
|---|
| 303 | RV concat ( const RV &rv1, const RV &rv2 ) { |
|---|
| 304 | RV pom = rv1; |
|---|
| 305 | pom.add ( rv2 ); |
|---|
| 306 | return pom; |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | void mepdf::from_setting ( const Setting &set ) { |
|---|
| 310 | shared_ptr<epdf> e ( UI::build<epdf> ( set, "epdf", UI::compulsory ) ); |
|---|
| 311 | iepdf = e; |
|---|
| 312 | set_ep(iepdf.get()); |
|---|
| 313 | } |
|---|
| 314 | |
|---|
| 315 | RV get_composite_rv ( const Array<shared_ptr<mpdf> > &mpdfs, |
|---|
| 316 | bool checkoverlap ) { |
|---|
| 317 | RV rv; //empty rv |
|---|
| 318 | bool rvaddok; |
|---|
| 319 | for ( int i = 0; i < mpdfs.length(); i++ ) { |
|---|
| 320 | rvaddok = rv.add ( mpdfs ( i )->_rv() ); //add rv to common rvs. |
|---|
| 321 | // If rvaddok==false, mpdfs overlap => assert error. |
|---|
| 322 | it_assert_debug ( rvaddok || !checkoverlap, "mprod::mprod() input mpdfs overlap in rv!" ); |
|---|
| 323 | } |
|---|
| 324 | |
|---|
| 325 | return rv; |
|---|
| 326 | } |
|---|
| 327 | |
|---|
| 328 | void BM::bayesB ( const mat &Data ) { |
|---|
| 329 | for ( int t = 0; t < Data.cols(); t++ ) { |
|---|
| 330 | bayes ( Data.get_col ( t ) ); |
|---|
| 331 | } |
|---|
| 332 | } |
|---|
| 333 | } |
|---|