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