1 | |
---|
2 | #include "bdmbase.h" |
---|
3 | |
---|
4 | //! Space of basic BDM structures |
---|
5 | namespace bdm { |
---|
6 | |
---|
7 | const int RV::BUFFER_STEP = 1; |
---|
8 | |
---|
9 | Array<string> RV::NAMES ( RV::BUFFER_STEP ); |
---|
10 | |
---|
11 | ivec RV::SIZES ( RV::BUFFER_STEP ); |
---|
12 | |
---|
13 | RV::str2int_map RV::MAP; |
---|
14 | |
---|
15 | void RV::clear_all() { |
---|
16 | MAP.clear(); |
---|
17 | SIZES.clear(); |
---|
18 | NAMES = Array<string> ( BUFFER_STEP ); |
---|
19 | } |
---|
20 | |
---|
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 | |
---|
29 | int RV::init ( const string &name, int size ) { |
---|
30 | //Refer |
---|
31 | int id; |
---|
32 | str2int_map::const_iterator iter = MAP.find ( name ); |
---|
33 | if ( iter == MAP.end() || name.length() == 0 ) { //add new RV |
---|
34 | id = MAP.size() + 1; |
---|
35 | //debug |
---|
36 | /* { |
---|
37 | cout << endl; |
---|
38 | str2int_map::const_iterator iter = MAP.begin(); |
---|
39 | for(str2int_map::const_iterator iter=MAP.begin(); iter!=MAP.end(); iter++){ |
---|
40 | cout << "key: " << iter->first << " val: " << iter->second <<endl; |
---|
41 | } |
---|
42 | }*/ |
---|
43 | |
---|
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 ); |
---|
48 | } |
---|
49 | NAMES ( id ) = name; |
---|
50 | SIZES ( id ) = size; |
---|
51 | bdm_assert ( size > 0, "RV " + name + " does not exists. Default size (-1) can not be assigned " ); |
---|
52 | } else { |
---|
53 | id = iter->second; |
---|
54 | if ( size > 0 && name.length() > 0 ) { |
---|
55 | bdm_assert ( SIZES ( id ) == size, "RV " + name + " of size " + num2str ( SIZES ( id ) ) + " exists, requested size " + num2str ( size ) + "can not be assigned" ); |
---|
56 | } |
---|
57 | } |
---|
58 | return id; |
---|
59 | }; |
---|
60 | |
---|
61 | int RV::countsize() const { |
---|
62 | int tmp = 0; |
---|
63 | for ( int i = 0; i < len; i++ ) { |
---|
64 | tmp += SIZES ( ids ( i ) ); |
---|
65 | } |
---|
66 | return tmp; |
---|
67 | } |
---|
68 | |
---|
69 | ivec RV::cumsizes() const { |
---|
70 | ivec szs ( len ); |
---|
71 | int tmp = 0; |
---|
72 | for ( int i = 0; i < len; i++ ) { |
---|
73 | tmp += SIZES ( ids ( i ) ); |
---|
74 | szs ( i ) = tmp; |
---|
75 | } |
---|
76 | return szs; |
---|
77 | } |
---|
78 | |
---|
79 | void RV::init ( const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times ) { |
---|
80 | len = in_names.length(); |
---|
81 | bdm_assert ( in_names.length() == in_times.length(), "check \"times\" " ); |
---|
82 | bdm_assert ( in_names.length() == in_sizes.length(), "check \"sizes\" " ); |
---|
83 | |
---|
84 | times.set_length ( len ); |
---|
85 | ids.set_length ( len ); |
---|
86 | int id; |
---|
87 | for ( int i = 0; i < len; i++ ) { |
---|
88 | id = init ( in_names ( i ), in_sizes ( i ) ); |
---|
89 | ids ( i ) = id; |
---|
90 | } |
---|
91 | times = in_times; |
---|
92 | dsize = countsize(); |
---|
93 | } |
---|
94 | |
---|
95 | RV::RV ( string name, int sz, int tm ) { |
---|
96 | Array<string> A ( 1 ); |
---|
97 | A ( 0 ) = name; |
---|
98 | init ( A, vec_1 ( sz ), vec_1 ( tm ) ); |
---|
99 | } |
---|
100 | |
---|
101 | bool RV::add ( const RV &rv2 ) { |
---|
102 | // TODO |
---|
103 | if ( rv2.len > 0 ) { //rv2 is nonempty |
---|
104 | ivec ind = rv2.findself ( *this ); //should be -1 all the time |
---|
105 | ivec index = itpp::find ( ind == -1 ); |
---|
106 | |
---|
107 | if ( index.length() < rv2.len ) { //conflict |
---|
108 | ids = concat ( ids, rv2.ids ( index ) ); |
---|
109 | times = concat ( times, rv2.times ( index ) ); |
---|
110 | } else { |
---|
111 | ids = concat ( ids, rv2.ids ); |
---|
112 | times = concat ( times, rv2.times ); |
---|
113 | } |
---|
114 | len = ids.length(); |
---|
115 | dsize = countsize(); |
---|
116 | return ( index.length() == rv2.len ); //conflict or not |
---|
117 | } else { //rv2 is empty |
---|
118 | return true; // no conflict |
---|
119 | } |
---|
120 | }; |
---|
121 | |
---|
122 | RV RV::subselect ( const ivec &ind ) const { |
---|
123 | RV ret; |
---|
124 | ret.ids = ids ( ind ); |
---|
125 | ret.times = times ( ind ); |
---|
126 | ret.len = ind.length(); |
---|
127 | ret.dsize = ret.countsize(); |
---|
128 | return ret; |
---|
129 | } |
---|
130 | |
---|
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 ) ); |
---|
138 | } |
---|
139 | |
---|
140 | void RV::t_plus ( int delta ) { |
---|
141 | times += delta; |
---|
142 | } |
---|
143 | |
---|
144 | bool RV::equal ( const RV &rv2 ) const { |
---|
145 | return ( ids == rv2.ids ) && ( times == rv2.times ); |
---|
146 | } |
---|
147 | |
---|
148 | shared_ptr<pdf> epdf::condition ( const RV &rv ) const { |
---|
149 | bdm_warning ( "Not implemented" ); |
---|
150 | return shared_ptr<pdf>(); |
---|
151 | } |
---|
152 | |
---|
153 | shared_ptr<epdf> epdf::marginal ( const RV &rv ) const { |
---|
154 | bdm_warning ( "Not implemented" ); |
---|
155 | return shared_ptr<epdf>(); |
---|
156 | } |
---|
157 | |
---|
158 | mat epdf::sample_mat ( int N ) const { |
---|
159 | mat X = zeros ( dim, N ); |
---|
160 | for ( int i = 0; i < N; i++ ) X.set_col ( i, this->sample() ); |
---|
161 | return X; |
---|
162 | } |
---|
163 | |
---|
164 | vec epdf::evallog_mat ( 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 ) ); |
---|
168 | } |
---|
169 | |
---|
170 | return x; |
---|
171 | } |
---|
172 | |
---|
173 | vec epdf::evallog_mat ( 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 ) ); |
---|
177 | } |
---|
178 | |
---|
179 | return x; |
---|
180 | } |
---|
181 | |
---|
182 | mat pdf::samplecond_mat ( const vec &cond, int N ) { |
---|
183 | mat M ( dimension(), N ); |
---|
184 | for ( int i = 0; i < N; i++ ) { |
---|
185 | M.set_col ( i, samplecond ( cond ) ); |
---|
186 | } |
---|
187 | |
---|
188 | return M; |
---|
189 | } |
---|
190 | |
---|
191 | void pdf::from_setting ( const Setting &set ) { |
---|
192 | root::from_setting( set ); |
---|
193 | shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional ); |
---|
194 | if ( r ) { |
---|
195 | set_rv ( *r ); |
---|
196 | } |
---|
197 | |
---|
198 | r = UI::build<RV> ( set, "rvc", UI::optional ); |
---|
199 | if ( r ) { |
---|
200 | set_rvc ( *r ); |
---|
201 | } |
---|
202 | } |
---|
203 | |
---|
204 | void pdf::to_setting ( Setting &set ) const { |
---|
205 | root::to_setting( set ); |
---|
206 | UI::save( &rv, set, "rv" ); |
---|
207 | UI::save( &rvc, set, "rvc" ); |
---|
208 | } |
---|
209 | |
---|
210 | void datalink::set_connection ( const RV &rv, const RV &rv_up ) { |
---|
211 | downsize = rv._dsize(); |
---|
212 | upsize = rv_up._dsize(); |
---|
213 | v2v_up = rv.dataind ( rv_up ); |
---|
214 | bdm_assert_debug ( v2v_up.length() == downsize, "rv is not fully in rv_up" ); |
---|
215 | } |
---|
216 | |
---|
217 | void datalink::set_connection ( int ds, int us, const ivec &upind ) { |
---|
218 | downsize = ds; |
---|
219 | upsize = us; |
---|
220 | v2v_up = upind; |
---|
221 | bdm_assert_debug ( v2v_up.length() == downsize, "rv is not fully in rv_up" ); |
---|
222 | } |
---|
223 | |
---|
224 | void datalink_part::set_connection ( const RV &rv, const RV &rv_up ) { |
---|
225 | rv.dataind ( rv_up, v2v_down, v2v_up ); |
---|
226 | downsize = v2v_down.length(); |
---|
227 | upsize = v2v_up.length(); |
---|
228 | } |
---|
229 | |
---|
230 | void datalink_m2e::set_connection ( const RV &rv, const RV &rvc, const RV &rv_up ) { |
---|
231 | datalink::set_connection ( rv, rv_up ); |
---|
232 | condsize = rvc._dsize(); |
---|
233 | //establish v2c connection |
---|
234 | rvc.dataind ( rv_up, v2c_lo, v2c_up ); |
---|
235 | } |
---|
236 | |
---|
237 | vec datalink_m2e::get_cond ( const vec &val_up ) { |
---|
238 | vec tmp ( condsize ); |
---|
239 | set_subvector ( tmp, v2c_lo, val_up ( v2c_up ) ); |
---|
240 | return tmp; |
---|
241 | } |
---|
242 | |
---|
243 | void datalink_m2e::pushup_cond ( vec &val_up, const vec &val, const vec &cond ) { |
---|
244 | bdm_assert_debug ( downsize == val.length(), "Wrong val" ); |
---|
245 | bdm_assert_debug ( upsize == val_up.length(), "Wrong val_up" ); |
---|
246 | set_subvector ( val_up, v2v_up, val ); |
---|
247 | set_subvector ( val_up, v2c_up, cond ); |
---|
248 | } |
---|
249 | |
---|
250 | std::ostream &operator<< ( std::ostream &os, const RV &rv ) { |
---|
251 | int id; |
---|
252 | for ( int i = 0; i < rv.len ; i++ ) { |
---|
253 | id = rv.ids ( i ); |
---|
254 | os << id << "(" << RV::SIZES ( id ) << ")" << // id(size)= |
---|
255 | "=" << RV::NAMES ( id ) << "_{" << rv.times ( i ) << "}; "; //name_{time} |
---|
256 | } |
---|
257 | return os; |
---|
258 | } |
---|
259 | |
---|
260 | RV RV::expand_delayes() const { |
---|
261 | RV rvt = this->remove_time(); //rv at t=0 |
---|
262 | RV tmp = rvt; |
---|
263 | int td = mint(); |
---|
264 | for ( int i = -1; i >= td; i-- ) { |
---|
265 | rvt.t_plus ( -1 ); |
---|
266 | tmp.add ( rvt ); //shift u1 |
---|
267 | } |
---|
268 | return tmp; |
---|
269 | } |
---|
270 | |
---|
271 | str RV::tostr() const { |
---|
272 | ivec idlist ( dsize ); |
---|
273 | ivec tmlist ( dsize ); |
---|
274 | int i; |
---|
275 | int pos = 0; |
---|
276 | for ( i = 0; i < len; i++ ) { |
---|
277 | idlist.set_subvector ( pos, pos + size ( i ) - 1, ids ( i ) ); |
---|
278 | tmlist.set_subvector ( pos, pos + size ( i ) - 1, times ( i ) ); |
---|
279 | pos += size ( i ); |
---|
280 | } |
---|
281 | return str ( idlist, tmlist ); |
---|
282 | } |
---|
283 | |
---|
284 | ivec RV::dataind ( const RV &rv2 ) const { |
---|
285 | ivec res ( 0 ); |
---|
286 | if ( rv2._dsize() > 0 ) { |
---|
287 | str str2 = rv2.tostr(); |
---|
288 | ivec part; |
---|
289 | int i; |
---|
290 | for ( i = 0; i < len; i++ ) { |
---|
291 | part = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) ); |
---|
292 | res = concat ( res, part ); |
---|
293 | } |
---|
294 | } |
---|
295 | |
---|
296 | bdm_assert_debug ( res.length() == dsize, "this rv is not fully present in crv!" ); |
---|
297 | return res; |
---|
298 | |
---|
299 | } |
---|
300 | |
---|
301 | void RV::dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const { |
---|
302 | //clean results |
---|
303 | selfi.set_size ( 0 ); |
---|
304 | rv2i.set_size ( 0 ); |
---|
305 | |
---|
306 | // just in case any rv is empty |
---|
307 | if ( ( len == 0 ) || ( rv2.length() == 0 ) ) { |
---|
308 | return; |
---|
309 | } |
---|
310 | |
---|
311 | //find comon rv |
---|
312 | ivec cids = itpp::find ( this->findself ( rv2 ) >= 0 ); |
---|
313 | |
---|
314 | // index of |
---|
315 | if ( cids.length() > 0 ) { |
---|
316 | str str1 = tostr(); |
---|
317 | str str2 = rv2.tostr(); |
---|
318 | |
---|
319 | ivec part1; |
---|
320 | ivec part2; |
---|
321 | int i, j; |
---|
322 | // find common rv in strs |
---|
323 | for ( j = 0; j < cids.length(); j++ ) { |
---|
324 | i = cids ( j ); |
---|
325 | part1 = itpp::find ( ( str1.ids == ids ( i ) ) & ( str1.times == times ( i ) ) ); |
---|
326 | part2 = itpp::find ( ( str2.ids == ids ( i ) ) & ( str2.times == times ( i ) ) ); |
---|
327 | selfi = concat ( selfi, part1 ); |
---|
328 | rv2i = concat ( rv2i, part2 ); |
---|
329 | } |
---|
330 | } |
---|
331 | bdm_assert_debug ( selfi.length() == rv2i.length(), "this should not happen!" ); |
---|
332 | } |
---|
333 | |
---|
334 | RV RV::subt ( const RV &rv2 ) const { |
---|
335 | ivec res = this->findself ( rv2 ); // nonzeros |
---|
336 | ivec valid; |
---|
337 | if ( dsize > 0 ) { |
---|
338 | valid = itpp::find ( res == -1 ); //-1 => value not found => it remains |
---|
339 | } |
---|
340 | return ( *this ) ( valid ); //keep those that were not found in rv2 |
---|
341 | } |
---|
342 | |
---|
343 | std::string RV::scalarname ( int scalat ) const { |
---|
344 | bdm_assert ( scalat < dsize, "Wrong input index" ); |
---|
345 | int id = 0; |
---|
346 | int scalid = 0; |
---|
347 | while ( scalid + SIZES ( ids ( id ) ) <= scalat ) { |
---|
348 | scalid += SIZES ( ids ( id ) ); |
---|
349 | id++; |
---|
350 | }; |
---|
351 | //now id is the id of variable of interest |
---|
352 | if ( size ( id ) == 1 ) |
---|
353 | return NAMES ( ids ( id ) ); |
---|
354 | else |
---|
355 | return NAMES ( ids ( id ) ) + "_" + num2str ( scalat - scalid ); |
---|
356 | |
---|
357 | } |
---|
358 | |
---|
359 | ivec RV::findself ( const RV &rv2 ) const { |
---|
360 | int i, j; |
---|
361 | ivec tmp = -ones_i ( len ); |
---|
362 | for ( i = 0; i < len; i++ ) { |
---|
363 | for ( j = 0; j < rv2.length(); j++ ) { |
---|
364 | if ( ( ids ( i ) == rv2.ids ( j ) ) & ( times ( i ) == rv2.times ( j ) ) ) { |
---|
365 | tmp ( i ) = j; |
---|
366 | break; |
---|
367 | } |
---|
368 | } |
---|
369 | } |
---|
370 | return tmp; |
---|
371 | } |
---|
372 | |
---|
373 | ivec RV::findself_ids ( const RV &rv2 ) const { |
---|
374 | int i, j; |
---|
375 | ivec tmp = -ones_i ( len ); |
---|
376 | for ( i = 0; i < len; i++ ) { |
---|
377 | for ( j = 0; j < rv2.length(); j++ ) { |
---|
378 | if ( ( ids ( i ) == rv2.ids ( j ) ) ) { |
---|
379 | tmp ( i ) = j; |
---|
380 | break; |
---|
381 | } |
---|
382 | } |
---|
383 | } |
---|
384 | return tmp; |
---|
385 | } |
---|
386 | |
---|
387 | void RV::from_setting ( const Setting &set ) { |
---|
388 | Array<string> A; |
---|
389 | UI::get ( A, set, "names" ); |
---|
390 | |
---|
391 | ivec szs; |
---|
392 | if ( !UI::get ( szs, set, "sizes" ) ) |
---|
393 | szs = ones_i ( A.length() ); |
---|
394 | |
---|
395 | ivec tms; |
---|
396 | if ( !UI::get ( tms, set, "times" ) ) |
---|
397 | tms = zeros_i ( A.length() ); |
---|
398 | |
---|
399 | // TODO tady se bude plnit primo do jeho promennych, a pak se zavola validacnni metoda, takze cele prepsat, ano? |
---|
400 | init ( A, szs, tms ); |
---|
401 | } |
---|
402 | |
---|
403 | void RV::to_setting ( Setting &set ) const { |
---|
404 | Array<string> names ( len ); |
---|
405 | ivec sizes ( len ); |
---|
406 | for ( int i = 0; i < len; i++ ) { |
---|
407 | names ( i ) = name ( i ); |
---|
408 | sizes ( i ) = size ( i ); |
---|
409 | } |
---|
410 | UI::save ( names, set, "names" ); |
---|
411 | UI::save ( sizes, set, "sizes" ); |
---|
412 | UI::save ( times, set, "times" ); |
---|
413 | } |
---|
414 | |
---|
415 | RV concat ( const RV &rv1, const RV &rv2 ) { |
---|
416 | RV pom = rv1; |
---|
417 | pom.add ( rv2 ); |
---|
418 | return pom; |
---|
419 | } |
---|
420 | |
---|
421 | RV get_composite_rv ( const Array<shared_ptr<pdf> > &pdfs, |
---|
422 | bool checkoverlap ) { |
---|
423 | RV rv; //empty rv |
---|
424 | bool rvaddok; |
---|
425 | for ( int i = 0; i < pdfs.length(); i++ ) { |
---|
426 | rvaddok = rv.add ( pdfs ( i )->_rv() ); //add rv to common rvs. |
---|
427 | // If rvaddok==false, pdfs overlap => assert error. |
---|
428 | bdm_assert_debug ( rvaddok || !checkoverlap, "mprod::mprod() input pdfs overlap in rv!" ); |
---|
429 | } |
---|
430 | |
---|
431 | return rv; |
---|
432 | } |
---|
433 | |
---|
434 | int logger::add_vector ( const RV &rv, string prefix ) { |
---|
435 | int id; |
---|
436 | if ( rv._dsize() > 0 ) { |
---|
437 | id = entries.length(); |
---|
438 | names = concat ( names, prefix ); // diff |
---|
439 | entries.set_length ( id + 1, true ); |
---|
440 | entries ( id ) = rv; |
---|
441 | } else { |
---|
442 | id = -1; |
---|
443 | } |
---|
444 | return id; // identifier of the last entry |
---|
445 | } |
---|
446 | |
---|
447 | int logger::add_setting ( const string &prefix ) { |
---|
448 | Setting &root = setting_conf.getRoot(); |
---|
449 | int id = root.getLength(); //root must be group!! |
---|
450 | if ( prefix.length() > 0 ) { |
---|
451 | settings.set_length ( id + 1, true ); |
---|
452 | settings ( id ) = &root.add ( prefix, Setting::TypeList ); |
---|
453 | } else { |
---|
454 | id = -1; |
---|
455 | } |
---|
456 | return id; |
---|
457 | } |
---|
458 | |
---|
459 | void epdf::log_register ( logger &L, const string &prefix ) { |
---|
460 | RV r; |
---|
461 | if ( isnamed() ) { |
---|
462 | r = _rv(); |
---|
463 | } else { |
---|
464 | r = RV ( "", dimension() ); |
---|
465 | }; |
---|
466 | root::log_register ( L, prefix ); |
---|
467 | |
---|
468 | // log full data |
---|
469 | if ( log_level == 10 ) { |
---|
470 | logrec->ids.set_size ( 1 ); |
---|
471 | logrec->ids ( 0 ) = logrec->L.add_setting ( prefix ); |
---|
472 | } else { |
---|
473 | // log only |
---|
474 | logrec->ids.set_size ( 3 ); |
---|
475 | if ( log_level > 0 ) { |
---|
476 | logrec->ids ( 0 ) = logrec->L.add_vector ( r, prefix + logrec->L.prefix_sep() + "mean" ); |
---|
477 | } |
---|
478 | if ( log_level > 1 ) { |
---|
479 | logrec->ids ( 1 ) = logrec->L.add_vector ( r, prefix + logrec->L.prefix_sep() + "lb" ); |
---|
480 | logrec->ids ( 2 ) = logrec->L.add_vector ( r, prefix + logrec->L.prefix_sep() + "ub" ); |
---|
481 | } |
---|
482 | } |
---|
483 | } |
---|
484 | |
---|
485 | void epdf::log_write() const { |
---|
486 | if ( log_level == 10 ) { |
---|
487 | to_setting ( logrec->L.log_to_setting ( logrec->ids ( 0 ) ) ); |
---|
488 | } else { |
---|
489 | if ( log_level > 0 ) { |
---|
490 | logrec->L.log_vector ( logrec->ids ( 0 ), mean() ); |
---|
491 | } |
---|
492 | if ( log_level > 1 ) { |
---|
493 | vec lb; |
---|
494 | vec ub; |
---|
495 | qbounds ( lb, ub ); |
---|
496 | logrec->L.log_vector ( logrec->ids ( 1 ), lb ); |
---|
497 | logrec->L.log_vector ( logrec->ids ( 2 ), ub ); |
---|
498 | } |
---|
499 | } |
---|
500 | } |
---|
501 | |
---|
502 | void datalink_buffered::set_connection ( const RV &rv, const RV &rv_up ) { |
---|
503 | // create link between up and down |
---|
504 | datalink_part::set_connection ( rv, rv_up); // only non-delayed version |
---|
505 | |
---|
506 | // create rvs of history |
---|
507 | // we can store only what we get in rv_up - everything else is removed |
---|
508 | ivec valid_ids = rv.findself_ids ( rv_up ); // return on which position each id is |
---|
509 | RV rv_hist = rv.subselect ( find ( valid_ids >= 0 ) ); // select only rvs that are in rv_up, ie ind>0 |
---|
510 | RV rv_hist0 = rv_hist.remove_time(); // these RVs will form history at time =0 |
---|
511 | // now we need to know what is needed from Up |
---|
512 | rv_hist = rv_hist.expand_delayes(); // full regressor - including time 0 |
---|
513 | Hrv = rv_hist.subt ( rv_hist0 ); // remove time 0 |
---|
514 | history = zeros ( Hrv._dsize() ); |
---|
515 | |
---|
516 | // decide if we need to copy val to history |
---|
517 | if ( Hrv._dsize() > 0 ) { |
---|
518 | v2h_up = rv_hist0.dataind ( rv_up ); // indeces of elements of rv_up to be copied |
---|
519 | } // else v2h_up is empty |
---|
520 | |
---|
521 | Hrv.dataind ( rv, h2v_hist, h2v_down ); |
---|
522 | |
---|
523 | downsize = v2v_down.length() + h2v_down.length(); |
---|
524 | upsize = v2v_up.length(); |
---|
525 | } |
---|
526 | |
---|
527 | void datalink_buffered::set_history ( const RV& rv1, const vec &hist0 ) { |
---|
528 | bdm_assert ( rv1._dsize() == hist0.length(), "hist is not compatible with given rv1" ); |
---|
529 | ivec ind_H; |
---|
530 | ivec ind_h0; |
---|
531 | Hrv.dataind ( rv1, ind_H, ind_h0 ); // find indeces of rv in |
---|
532 | set_subvector ( history, ind_H, hist0 ( ind_h0 ) ); // copy given hist to appropriate places |
---|
533 | } |
---|
534 | |
---|
535 | void DS::log_register ( logger &L, const string &prefix ) { |
---|
536 | bdm_assert ( ytsize == Yrv._dsize(), "invalid DS: ytsize (" + num2str ( ytsize ) + ") different from Drv " + num2str ( Yrv._dsize() ) ); |
---|
537 | bdm_assert ( utsize == Urv._dsize(), "invalid DS: utsize (" + num2str ( utsize ) + ") different from Urv " + num2str ( Urv._dsize() ) ); |
---|
538 | |
---|
539 | root::log_register ( L, prefix ); |
---|
540 | //we know that |
---|
541 | if ( log_level > 0 ) { |
---|
542 | logrec->ids.set_size ( 2 ); |
---|
543 | logrec->ids ( 0 ) = logrec->L.add_vector ( Yrv, prefix ); |
---|
544 | logrec->ids ( 1 ) = logrec->L.add_vector ( Urv, prefix ); |
---|
545 | } |
---|
546 | } |
---|
547 | |
---|
548 | void DS::log_write ( ) const { |
---|
549 | if ( log_level > 0 ) { |
---|
550 | vec tmp ( Yrv._dsize() + Urv._dsize() ); |
---|
551 | getdata ( tmp ); |
---|
552 | // d is first in getdata |
---|
553 | logrec->L.log_vector ( logrec->ids ( 0 ), tmp.left ( Yrv._dsize() ) ); |
---|
554 | // u follows after d in getdata |
---|
555 | logrec->L.log_vector ( logrec->ids ( 1 ), tmp.mid ( Yrv._dsize(), Urv._dsize() ) ); |
---|
556 | } |
---|
557 | } |
---|
558 | |
---|
559 | void BM::set_options ( const string &opt ) { |
---|
560 | if ( opt.find ( "logfull" ) != string::npos ) { |
---|
561 | const_cast<epdf&> ( posterior() ).set_log_level ( 10 ) ; |
---|
562 | } else { |
---|
563 | if ( opt.find ( "logbounds" ) != string::npos ) { |
---|
564 | const_cast<epdf&> ( posterior() ).set_log_level ( 2 ) ; |
---|
565 | } else { |
---|
566 | const_cast<epdf&> ( posterior() ).set_log_level ( 1 ) ; |
---|
567 | } |
---|
568 | if ( opt.find ( "logll" ) != string::npos ) { |
---|
569 | log_level = 1; |
---|
570 | } |
---|
571 | } |
---|
572 | } |
---|
573 | |
---|
574 | void BM::log_register ( logger &L, const string &prefix ) { |
---|
575 | root::log_register ( L, prefix ); |
---|
576 | |
---|
577 | const_cast<epdf&> ( posterior() ).log_register ( L, prefix + L.prefix_sep() + "apost" ); |
---|
578 | |
---|
579 | if ( ( log_level ) > 0 ) { |
---|
580 | logrec->ids.set_size ( 1 ); |
---|
581 | logrec->ids ( 0 ) = L.add_vector ( RV ( "", 1 ), prefix + L.prefix_sep() + "ll" ); |
---|
582 | } |
---|
583 | } |
---|
584 | |
---|
585 | void BM::log_write ( ) const { |
---|
586 | posterior().log_write(); |
---|
587 | if ( log_level > 0 ) { |
---|
588 | logrec->L.logit ( logrec->ids ( 0 ), ll ); |
---|
589 | } |
---|
590 | } |
---|
591 | |
---|
592 | void BM::bayes_batch ( const mat &Data, const vec &cond ) { |
---|
593 | for ( int t = 0; t < Data.cols(); t++ ) { |
---|
594 | bayes ( Data.get_col ( t ), cond ); |
---|
595 | } |
---|
596 | } |
---|
597 | |
---|
598 | void BM::bayes_batch ( const mat &Data, const mat &Cond ) { |
---|
599 | for ( int t = 0; t < Data.cols(); t++ ) { |
---|
600 | bayes ( Data.get_col ( t ), Cond.get_col ( t ) ); |
---|
601 | } |
---|
602 | } |
---|
603 | |
---|
604 | } |
---|