1 | /*! |
---|
2 | \file |
---|
3 | \brief Basic structures of probability calculus: random variables, probability densities, Bayes rule |
---|
4 | \author Vaclav Smidl. |
---|
5 | |
---|
6 | ----------------------------------- |
---|
7 | BDM++ - C++ library for Bayesian Decision Making under Uncertainty |
---|
8 | |
---|
9 | Using IT++ for numerical operations |
---|
10 | ----------------------------------- |
---|
11 | */ |
---|
12 | |
---|
13 | #ifndef BDMBASE_H |
---|
14 | #define BDMBASE_H |
---|
15 | |
---|
16 | #include <map> |
---|
17 | |
---|
18 | #include "../itpp_ext.h" |
---|
19 | #include "../bdmroot.h" |
---|
20 | #include "../shared_ptr.h" |
---|
21 | #include "user_info.h" |
---|
22 | |
---|
23 | using namespace libconfig; |
---|
24 | using namespace itpp; |
---|
25 | using namespace std; |
---|
26 | |
---|
27 | namespace bdm { |
---|
28 | |
---|
29 | //! Structure of RV, i.e. RVs expanded into a flat list of IDs, used for debugging. |
---|
30 | class str { |
---|
31 | public: |
---|
32 | //! vector id ids (non-unique!) |
---|
33 | ivec ids; |
---|
34 | //! vector of times |
---|
35 | ivec times; |
---|
36 | //!Default constructor |
---|
37 | str ( ivec ids0, ivec times0 ) : ids ( ids0 ), times ( times0 ) { |
---|
38 | bdm_assert ( times0.length() == ids0.length(), "Incompatible input" ); |
---|
39 | }; |
---|
40 | }; |
---|
41 | |
---|
42 | /*! |
---|
43 | * \brief Class representing variables, most often random variables |
---|
44 | |
---|
45 | The purpose of this class is to decribe a vector of data. Such description is used for connecting various vectors between each other, see class datalink. |
---|
46 | |
---|
47 | The class is implemented using global variables to assure uniqueness of description: |
---|
48 | |
---|
49 | In is a vector |
---|
50 | \dot |
---|
51 | digraph datalink { |
---|
52 | rankdir=LR; |
---|
53 | subgraph cluster0 { |
---|
54 | node [shape=record]; |
---|
55 | label = "MAP \n std::map<string,int>"; |
---|
56 | map [label="{{\"a\"| \"b\" | \"c\"} | {<3> 3 |<1> 1|<2> 2}}"]; |
---|
57 | color = "white" |
---|
58 | } |
---|
59 | subgraph cluster1{ |
---|
60 | node [shape=record]; |
---|
61 | label = "NAMES"; |
---|
62 | names [label="{<1> \"b\" | <2> \"c\" | <3>\"a\" }"]; |
---|
63 | color = "white" |
---|
64 | } |
---|
65 | subgraph cluster2{ |
---|
66 | node [shape=record]; |
---|
67 | label = "SIZES"; |
---|
68 | labelloc = b; |
---|
69 | sizes [label="{<1>1 |<2> 4 |<3> 1}"]; |
---|
70 | color = "white" |
---|
71 | } |
---|
72 | map:1 -> names:1; |
---|
73 | map:1 -> sizes:1; |
---|
74 | map:3 -> names:3; |
---|
75 | map:3 -> sizes:3; |
---|
76 | } |
---|
77 | \enddot |
---|
78 | |
---|
79 | RV name may be empty string! In that case, it is not a valid name but only a size holder. It will fail in comparison. |
---|
80 | */ |
---|
81 | |
---|
82 | class RV : public root { |
---|
83 | private: |
---|
84 | typedef std::map<string, int> str2int_map; |
---|
85 | |
---|
86 | //! Internal global variable storing sizes of RVs |
---|
87 | static ivec SIZES; |
---|
88 | //! Internal global variable storing names of RVs |
---|
89 | static Array<string> NAMES; |
---|
90 | //! TODO |
---|
91 | const static int BUFFER_STEP; |
---|
92 | //! TODO |
---|
93 | static str2int_map MAP; |
---|
94 | |
---|
95 | public: |
---|
96 | |
---|
97 | protected: |
---|
98 | //! size of the data vector |
---|
99 | int dsize; |
---|
100 | //! number of individual rvs |
---|
101 | int len; |
---|
102 | //! Vector of unique IDs |
---|
103 | ivec ids; |
---|
104 | //! Vector of shifts from current time |
---|
105 | ivec times; |
---|
106 | |
---|
107 | private: |
---|
108 | enum enum_dummy {dummy}; |
---|
109 | |
---|
110 | //! auxiliary function used in constructor |
---|
111 | void init( const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times ); |
---|
112 | |
---|
113 | //! auxiliary function assigning unique integer index related to the passed name and size of the random variable |
---|
114 | int assign_id( const string &name, int size ); |
---|
115 | |
---|
116 | //! Private constructor from IDs, potentially dangerous since all ids must be valid! |
---|
117 | //! dummy is there to prevent confusion with RV(" string"); |
---|
118 | explicit RV ( const ivec &ids0, enum_dummy dum ) : dsize ( 0 ), len ( ids0.length() ), ids ( ids0 ), times ( zeros_i ( ids0.length() ) ) { |
---|
119 | dsize = countsize(); |
---|
120 | } |
---|
121 | public: |
---|
122 | //! \name Constructors |
---|
123 | //!@{ |
---|
124 | |
---|
125 | //! Full constructor |
---|
126 | RV ( const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times ) { |
---|
127 | init ( in_names, in_sizes, in_times ); |
---|
128 | } |
---|
129 | |
---|
130 | //! Constructor with times=0 |
---|
131 | RV ( const Array<std::string> &in_names, const ivec &in_sizes ) { |
---|
132 | init ( in_names, in_sizes, zeros_i ( in_names.length() ) ); |
---|
133 | } |
---|
134 | |
---|
135 | //! Constructor with sizes=1, times=0 |
---|
136 | RV ( const Array<std::string> &in_names ) { |
---|
137 | init ( in_names, ones_i ( in_names.length() ), zeros_i ( in_names.length() ) ); |
---|
138 | } |
---|
139 | |
---|
140 | //! Constructor of empty RV |
---|
141 | RV() : dsize ( 0 ), len ( 0 ), ids ( 0 ), times ( 0 ) {} |
---|
142 | |
---|
143 | //! Constructor of a single RV |
---|
144 | RV ( string name, int sz, int tm = 0 ); |
---|
145 | |
---|
146 | //! Constructor of a single nameless RV |
---|
147 | RV ( int sz, int tm = 0 ); |
---|
148 | |
---|
149 | // compiler-generated copy constructor is used |
---|
150 | //!@} |
---|
151 | |
---|
152 | //! \name Access functions |
---|
153 | //!@{ |
---|
154 | |
---|
155 | //! State output, e.g. for debugging. |
---|
156 | friend std::ostream &operator<< ( std::ostream &os, const RV &rv ); |
---|
157 | |
---|
158 | string to_string() const { |
---|
159 | ostringstream o; |
---|
160 | o << *this; |
---|
161 | return o.str(); |
---|
162 | } |
---|
163 | |
---|
164 | //! total size of a random variable |
---|
165 | int _dsize() const { |
---|
166 | return dsize; |
---|
167 | } |
---|
168 | |
---|
169 | //! access function |
---|
170 | const ivec& _ids() const { |
---|
171 | return ids; |
---|
172 | } |
---|
173 | |
---|
174 | //! Recount size of the corresponding data vector |
---|
175 | int countsize() const; |
---|
176 | //! Vector of cumulative sizes of RV |
---|
177 | ivec cumsizes() const; |
---|
178 | //! Number of named parts |
---|
179 | int length() const { |
---|
180 | return len; |
---|
181 | } |
---|
182 | int id ( int at ) const { |
---|
183 | return ids ( at ); |
---|
184 | } |
---|
185 | int size ( int at ) const { |
---|
186 | return SIZES ( ids ( at ) ); |
---|
187 | } |
---|
188 | int time ( int at ) const { |
---|
189 | return times ( at ); |
---|
190 | } |
---|
191 | std::string name ( int at ) const { |
---|
192 | return NAMES ( ids ( at ) ); |
---|
193 | } |
---|
194 | //! returns name of a scalar at position scalat, i.e. it can be in the middle of vector name, in that case it adds "_%d" to it |
---|
195 | std::string scalarname ( int scalat ) const; |
---|
196 | |
---|
197 | void set_time ( int at, int time0 ) { |
---|
198 | times ( at ) = time0; |
---|
199 | } |
---|
200 | //!@} |
---|
201 | |
---|
202 | //! \name Algebra on Random Variables |
---|
203 | //!@{ |
---|
204 | |
---|
205 | //! Find indices of self in another rv, \return ivec of the same size as self. |
---|
206 | ivec findself ( const RV &rv2 ) const; |
---|
207 | //! Find indices of self in another rv, ignore time, \return ivec of the same size as self. |
---|
208 | ivec findself_ids ( const RV &rv2 ) const; |
---|
209 | //! Compare if \c rv2 is identical to this \c RV |
---|
210 | bool equal ( const RV &rv2 ) const; |
---|
211 | //! Add (concat) another variable to the current one, \return true if all rv2 were added, false if rv2 is in conflict |
---|
212 | bool add ( const RV &rv2 ); |
---|
213 | //! Subtract another variable from the current one |
---|
214 | RV subt ( const RV &rv2 ) const; |
---|
215 | //! Select only variables at indices ind |
---|
216 | RV subselect ( const ivec &ind ) const; |
---|
217 | |
---|
218 | //! Select only variables at indices ind |
---|
219 | RV operator() ( const ivec &ind ) const { |
---|
220 | return subselect ( ind ); |
---|
221 | } |
---|
222 | |
---|
223 | //! Select from data vector starting at di1 to di2 |
---|
224 | RV operator() ( int di1, int di2 ) const; |
---|
225 | |
---|
226 | //! Shift \c time by delta. |
---|
227 | void t_plus ( int delta ); |
---|
228 | //!@} |
---|
229 | |
---|
230 | //! @{ \name Time manipulation functions |
---|
231 | //! returns rvs with time set to 0 and removed duplicates |
---|
232 | RV remove_time() const { |
---|
233 | return RV ( unique ( ids ), dummy ); |
---|
234 | } |
---|
235 | //! create new RV from the current one with time shifted by given value |
---|
236 | RV copy_t ( int dt ) const { |
---|
237 | RV tmp = *this; |
---|
238 | tmp.t_plus ( dt ); |
---|
239 | return tmp; |
---|
240 | } |
---|
241 | //! return rvs with expanded delayes and sorted in the order of: \f$ [ rv_{0}, rv_{-1},\ldots rv_{max_delay}]\f$ |
---|
242 | RV expand_delayes() const; |
---|
243 | //!@} |
---|
244 | |
---|
245 | //!\name Relation to vectors |
---|
246 | //!@{ |
---|
247 | |
---|
248 | //! generate \c str from rv, by expanding sizes |
---|
249 | str tostr() const; |
---|
250 | //! when this rv is a part of bigger rv, this function returns indices of self in the data vector of the bigger crv. |
---|
251 | //! Then, data can be copied via: data_of_this = cdata(ind); |
---|
252 | ivec dataind ( const RV &crv ) const; |
---|
253 | //! same as dataind but this time crv should not be complete supperset of rv. |
---|
254 | ivec dataind_part ( const RV &crv ) const; |
---|
255 | //! generate mutual indices when copying data between self and crv. |
---|
256 | //! Data are copied via: data_of_this(selfi) = data_of_rv2(rv2i) |
---|
257 | void dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const; |
---|
258 | //! Minimum time-offset |
---|
259 | int mint() const { |
---|
260 | return times.length() > 0 ? min ( times ) : 0; |
---|
261 | } |
---|
262 | //! Minimum time-offset of ids of given RVs |
---|
263 | int mint ( const RV &rv ) const { |
---|
264 | bvec belong = zeros_b ( len ); |
---|
265 | for ( int r = 0; r < rv.length(); r++ ) { |
---|
266 | belong = belong | ( ids == rv.id ( r ) ); |
---|
267 | } |
---|
268 | return times.length() > 0 ? min ( get_from_bvec ( times, belong ) ) : 0; |
---|
269 | } |
---|
270 | //!@} |
---|
271 | |
---|
272 | /*! \brief UI for class RV (description of data vectors) |
---|
273 | |
---|
274 | \code |
---|
275 | class = 'RV'; |
---|
276 | names = {'a', 'b', 'c', ...}; // UNIQUE IDENTIFIER same names = same variable |
---|
277 | // names are also used when storing results |
---|
278 | --- optional --- |
---|
279 | sizes = [1, 2, 3, ...]; // size of each name. default = ones() |
---|
280 | // if size = -1, it is found out from previous instances of the same name |
---|
281 | times = [-1, -2, 0, ...]; // time shifts with respect to current time, default = zeros() |
---|
282 | \endcode |
---|
283 | */ |
---|
284 | void from_setting ( const Setting &set ); |
---|
285 | |
---|
286 | void to_setting ( Setting &set ) const; |
---|
287 | |
---|
288 | //! Invalidate all named RVs. Use before initializing any RV instances, with care... |
---|
289 | static void clear_all(); |
---|
290 | //! function for debugging RV related stuff |
---|
291 | string show_all(); |
---|
292 | |
---|
293 | }; |
---|
294 | UIREGISTER ( RV ); |
---|
295 | SHAREDPTR ( RV ); |
---|
296 | |
---|
297 | //! Concat two random variables |
---|
298 | RV concat ( const RV &rv1, const RV &rv2 ); |
---|
299 | |
---|
300 | |
---|
301 | |
---|
302 | //! This class stores a details that will be logged to a logger |
---|
303 | //! |
---|
304 | //! This is only the first part of the whole declaration, which has to be however separated into |
---|
305 | //! two different classes for allowing the compilation of source code. For more details |
---|
306 | //! see logger::add_setting(...) method and mainly log_level_template<T>::template<class U> void store( const enum T::log_level_enums log_level_enum, const U data ) const |
---|
307 | //! method. For the reason the second one is templated, it was necessary to declare this whole class. |
---|
308 | template<class T> class log_level_intermediate : public log_level_base { |
---|
309 | protected: |
---|
310 | //! boolean flags related indicating which details will be logged to a logger |
---|
311 | bitset<32> values; |
---|
312 | |
---|
313 | //! default constructor is protected to prevent creating instances elsewhere than in log_level_template descendant class |
---|
314 | log_level_intermediate( ) { |
---|
315 | int len = names().length(); |
---|
316 | ids.set_size( len ); |
---|
317 | for( int i = 0; i<len; i++ ) |
---|
318 | { |
---|
319 | ids(i).set_size ( 1 ); |
---|
320 | ids(i) = -1; |
---|
321 | } |
---|
322 | } |
---|
323 | public: |
---|
324 | //! a general utility transforming a comma-separated sequence of strings into an instance of Array<strings> |
---|
325 | static Array<string> string2Array( const string &input ) |
---|
326 | { |
---|
327 | string result = input; |
---|
328 | string::size_type loc; |
---|
329 | while( loc = result.find( ',' ), loc != string::npos ) |
---|
330 | result[loc] = ' '; |
---|
331 | return Array<string>("{ " + result + " }" ); |
---|
332 | } |
---|
333 | |
---|
334 | //! this method adds new id to its proper position and return the name of this position |
---|
335 | string store_id_and_give_name( enum T::log_level_enums const log_level_enum, int enum_subindex, int id ) { |
---|
336 | if( ids(log_level_enum).length() <= enum_subindex ) |
---|
337 | ids(log_level_enum).set_size( enum_subindex+1, true ); |
---|
338 | ids(log_level_enum)(enum_subindex) = id; |
---|
339 | |
---|
340 | // here we remove a "log" prefix from name, i.e., for instance it transforms "logevidence" to "evidence" |
---|
341 | ostringstream stream; |
---|
342 | string name_with_prefix = names()(log_level_enum); |
---|
343 | string possible_log_prefix = name_with_prefix.substr(0,3); |
---|
344 | if( possible_log_prefix == "log" ) |
---|
345 | stream << name_with_prefix.substr(3,name_with_prefix.length()-3); |
---|
346 | else |
---|
347 | stream << name_with_prefix; |
---|
348 | |
---|
349 | // add number to name only in the case there are more registered vectors with the same log_level_enum |
---|
350 | if( ids(log_level_enum).length() > 1 ) |
---|
351 | stream << "_" << enum_subindex; |
---|
352 | |
---|
353 | return stream.str(); |
---|
354 | } |
---|
355 | |
---|
356 | //! string equivalents of the used enumerations which are filled with a help of #LOG_LEVEL macro within class T |
---|
357 | const Array<string> &names() const |
---|
358 | { |
---|
359 | return T::log_level_names(); |
---|
360 | } |
---|
361 | |
---|
362 | //! read only operator for testing individual fields of log_level |
---|
363 | //! |
---|
364 | //! it is necessary to acces it with a proper enumeration type, thus this approach is type-safe |
---|
365 | bool operator [] (const enum T::log_level_enums &log_level_enum ) const |
---|
366 | { |
---|
367 | return values[log_level_enum]; |
---|
368 | } |
---|
369 | |
---|
370 | //! operator for setting an individual field of log_level |
---|
371 | //! |
---|
372 | //! it is necessary to acces it with a proper enumeration type, thus this approach is type-safe |
---|
373 | bitset<32>::reference operator [] (const enum T::log_level_enums &log_level_enum ) |
---|
374 | { |
---|
375 | return values[log_level_enum]; |
---|
376 | } |
---|
377 | }; |
---|
378 | //UIREGISTER IS FORBIDDEN FOR THIS CLASS, AS IT SHOULD BE LOADED ONLY THROUGH THE SPECIALIZED UI::GET(...) METHOD |
---|
379 | |
---|
380 | |
---|
381 | /*! |
---|
382 | @brief Class for storing results (and semi-results) of an experiment |
---|
383 | |
---|
384 | This class abstracts logging of results from implementation. This class replaces direct logging of results (e.g. to files or to global variables) by calling methods of a logger. Specializations of this abstract class for specific storage method are designed. |
---|
385 | */ |
---|
386 | class logger : public root { |
---|
387 | protected: |
---|
388 | //! RVs of all logged variables. |
---|
389 | Array<RV> entries; |
---|
390 | //! Names of logged quantities, e.g. names of algorithm variants |
---|
391 | Array<string> names; |
---|
392 | //! Root Setting for storing Settings |
---|
393 | Config setting_conf; |
---|
394 | //! list of Settings for specific ids |
---|
395 | Array<Setting*> settings; |
---|
396 | |
---|
397 | //! log this instance to Setting |
---|
398 | //! |
---|
399 | //! this method has to be called only through \c log_level class to assure the validity of the passed id |
---|
400 | template<class U> void log_setting ( int id, const U data ) { |
---|
401 | UI::save(data, *settings ( id ) ); |
---|
402 | } |
---|
403 | |
---|
404 | //! log this vector |
---|
405 | //! |
---|
406 | //! this method has to be called only through \c log_level class to assure the validity of the passed id |
---|
407 | virtual void log_vector ( int id, const vec &v ) NOT_IMPLEMENTED_VOID; |
---|
408 | |
---|
409 | //! log this double |
---|
410 | //! |
---|
411 | //! this method has to be called only through \c log_level class to assure the validity of the passed id |
---|
412 | virtual void log_double ( int id, const double &d ) NOT_IMPLEMENTED_VOID; |
---|
413 | |
---|
414 | //! it is necessary to allow log_levels to call log_setting, log_vector and log_double methods |
---|
415 | template<class T> friend class log_level_template; |
---|
416 | public: |
---|
417 | //!separator of prefixes of entries |
---|
418 | //! |
---|
419 | //! It is a constant string, thus it can be safely declared as public without creating any accessor method |
---|
420 | const string separator; |
---|
421 | |
---|
422 | //!Default constructor |
---|
423 | logger ( const string separator ) : entries ( 0 ), names ( 0 ), separator ( separator ) {} |
---|
424 | |
---|
425 | //!Destructor calls the finalize method |
---|
426 | ~logger() { |
---|
427 | finalize(); |
---|
428 | } |
---|
429 | |
---|
430 | //! sets up the ids identifier in the passed log_level instance to permit future calls of the log_level_template<T>::store(...) method |
---|
431 | //! |
---|
432 | //! It also sets a pointer to logger or justify it is correctly assigned from previous call to this procedure |
---|
433 | //! Entries with empty RV will be ignored |
---|
434 | //! |
---|
435 | //! passing the last parameter \c enum_subindex one can store multiple vectors in the position of one enum |
---|
436 | template<class T> void add_vector ( log_level_intermediate<T> &log_level, enum T::log_level_enums const log_level_enum, const RV &rv, const string &prefix, int enum_subindex = 0 ) |
---|
437 | { |
---|
438 | if( !log_level.registered_logger ) |
---|
439 | log_level.registered_logger = this; |
---|
440 | else |
---|
441 | bdm_assert_debug ( log_level.registered_logger == this, "This log_level is already registered to another logger!"); |
---|
442 | |
---|
443 | if ( rv._dsize() == 0 ) |
---|
444 | return; |
---|
445 | |
---|
446 | int id = entries.length(); |
---|
447 | string adjusted_name = log_level.store_id_and_give_name( log_level_enum, enum_subindex, id ); |
---|
448 | |
---|
449 | names = concat ( names, prefix + separator + adjusted_name ); // diff |
---|
450 | entries.set_length ( id + 1, true ); |
---|
451 | entries ( id ) = rv; |
---|
452 | } |
---|
453 | |
---|
454 | //! sets up the ids identifier in the passed log_level instance to permit future calls of the log_level_template<T>::store(...) method |
---|
455 | //! |
---|
456 | //! It also sets a pointer to logger or justify it is correctly assigned from previous call to this procedure |
---|
457 | //! |
---|
458 | //! To allow both arguments log_level and log_level_enum be templated, it was necessary to declare log_level_intermediate<T> class. |
---|
459 | //! This way we check compatibility of the passed log_level and log_level_enum, which would be impossible using just log_level_base class |
---|
460 | //! here. |
---|
461 | //! |
---|
462 | //! |
---|
463 | //! passing the last parameter \c enum_subindex one can store multiple settings in the position of one enum |
---|
464 | template<class T> void add_setting ( log_level_intermediate<T> &log_level, enum T::log_level_enums const log_level_enum, const string &prefix, int enum_subindex = 0 ) { |
---|
465 | if( !log_level.registered_logger ) |
---|
466 | log_level.registered_logger = this; |
---|
467 | else |
---|
468 | bdm_assert_debug ( log_level.registered_logger == this, "This log_level is already registered to another logger!"); |
---|
469 | |
---|
470 | Setting &root = setting_conf.getRoot(); |
---|
471 | int id = root.getLength(); //root must be group!! |
---|
472 | string adjusted_name = log_level.store_id_and_give_name( log_level_enum, enum_subindex, id ); |
---|
473 | settings.set_length ( id + 1, true ); |
---|
474 | settings ( id ) = &root.add ( prefix + separator + adjusted_name, Setting::TypeList ); |
---|
475 | } |
---|
476 | |
---|
477 | //! Shifts storage position for another time step. |
---|
478 | virtual void step() = 0; |
---|
479 | |
---|
480 | //! Finalize storing information |
---|
481 | //! |
---|
482 | //! This method is called either directly or via destructor ~logger(), therefore it has to permit repetitive calls for the case it is called twice |
---|
483 | virtual void finalize() {}; |
---|
484 | |
---|
485 | //! Initialize the storage |
---|
486 | virtual void init() {}; |
---|
487 | }; |
---|
488 | |
---|
489 | //! This class stores a details that will be logged to a logger |
---|
490 | template<class T> class log_level_template : public log_level_intermediate<T> { |
---|
491 | public: |
---|
492 | |
---|
493 | //! Set log_levels according to the Setting element |
---|
494 | void from_setting ( const Setting &element ) |
---|
495 | { |
---|
496 | string raw_log_level; |
---|
497 | UI::get( raw_log_level, element ); |
---|
498 | Array<string> loaded_log_level = this->string2Array( raw_log_level ); |
---|
499 | |
---|
500 | this->values.reset(); |
---|
501 | |
---|
502 | for( int i = 0; i < loaded_log_level.length(); i++ ) |
---|
503 | for( int j = 0; j < this->names().length(); j++ ){ |
---|
504 | if( loaded_log_level(i) == this->names()(j) ) |
---|
505 | { |
---|
506 | this->values[j] = true; |
---|
507 | break; |
---|
508 | } |
---|
509 | } |
---|
510 | } |
---|
511 | |
---|
512 | //! Store log_levels into the Setting element |
---|
513 | void to_setting ( Setting &element ) const |
---|
514 | { |
---|
515 | // HERE WE WANT NOT TO DELETE PREVIOUS DATA STORED BY OTHER LOG_LEVELS, SEE SPECIAL IMPLEMENTATION OF UI::GET(...) FOR THIS CLASS |
---|
516 | const char* ch_elem=( const char* ) element; |
---|
517 | string string_to_write; |
---|
518 | if (ch_elem) |
---|
519 | string_to_write=ch_elem; |
---|
520 | else |
---|
521 | string_to_write=""; |
---|
522 | |
---|
523 | for( unsigned int i = 0; i < this->values.size(); i++ ) |
---|
524 | if( this->values[i] ) |
---|
525 | { |
---|
526 | if( string_to_write.length() > 0 ) |
---|
527 | string_to_write = string_to_write + ','; |
---|
528 | string_to_write = string_to_write + this->names()(i); |
---|
529 | } |
---|
530 | |
---|
531 | element = string_to_write; |
---|
532 | } |
---|
533 | |
---|
534 | //! This method stores a vector to the proper place in registered logger |
---|
535 | //! |
---|
536 | //! parameter \c enum_subindex identifies the precise position of vector in the case there is more vectors registered to with this enum |
---|
537 | void store( const enum T::log_level_enums log_level_enum, const vec &vect, int enum_subindex = 0 ) const |
---|
538 | { |
---|
539 | bdm_assert_debug( this->registered_logger != NULL, "You have to register instance to a logger first! Use root::log_register(...) method."); |
---|
540 | bdm_assert_debug( ids( log_level_enum )( enum_subindex ) >= 0, "This particular vector was not added to logger! Use logger::add_vector(...) method."); |
---|
541 | this->registered_logger->log_vector( ids( log_level_enum )( enum_subindex ), vect ); |
---|
542 | } |
---|
543 | |
---|
544 | //! This method stores a double to the proper place in registered logger |
---|
545 | //! |
---|
546 | //! parameter \c enum_subindex identifies the precise position of double in the case there is more doubles registered to with this enum |
---|
547 | void store( const enum T::log_level_enums log_level_enum, const double &dbl, int enum_subindex = 0 ) const |
---|
548 | { |
---|
549 | bdm_assert_debug( this->registered_logger != NULL, "You have to register instance to a logger first! See root::log_register(...) method."); |
---|
550 | bdm_assert_debug( ids( log_level_enum )( enum_subindex ) >= 0, "This particular double was not added to logger! Use logger::add_vector(...) method."); |
---|
551 | this->registered_logger->log_double( ids( log_level_enum )( enum_subindex ), dbl ); |
---|
552 | } |
---|
553 | |
---|
554 | //! This method stores a Setting obtained by call of UI::save( data, .. ) to the proper place in registered logger |
---|
555 | //! |
---|
556 | //! parameter \c enum_subindex identifies the precise position of setting in the case there is more settings registered to with this enum |
---|
557 | template<class U> void store( const enum T::log_level_enums log_level_enum, const U data, int enum_subindex = 0 ) const |
---|
558 | { |
---|
559 | bdm_assert_debug( this->registered_logger != NULL, "You have to register instance to a logger first! See root::log_register(...) method."); |
---|
560 | bdm_assert_debug( ids( log_level_enum )(enum_subindex ) >= 0, "This particular vector was not added to logger! Use logger::add_setting(...) method."); |
---|
561 | this->registered_logger->log_setting( ids( log_level_enum )( enum_subindex ), data); |
---|
562 | } |
---|
563 | }; |
---|
564 | //UIREGISTER IS FORBIDDEN FOR THIS CLASS, AS IT SHOULD BE LOADED ONLY THROUGH THE SPECIALIZED UI::GET(...) METHOD |
---|
565 | |
---|
566 | |
---|
567 | /*! |
---|
568 | \def LOG_LEVEL(classname,...) |
---|
569 | \brief Macro for defining a log_level attribute with a specific set of enumerations related to a specific class |
---|
570 | |
---|
571 | This macro has to be called within a class declaration. Its argument \a classname has to correspond to that wrapping class. |
---|
572 | This macro defines a log_level instance which can be modified either directly or by the means of #UI class. |
---|
573 | |
---|
574 | One of the main purposes of this macro is to allow variability in using enumerations. By relating them to their names through |
---|
575 | an array of strings, we are no more dependant on their precise ordering. What is more, we can add or remove any without harming |
---|
576 | any applications which are using this library. |
---|
577 | |
---|
578 | \todo Write a more detailed explanation including also examples |
---|
579 | |
---|
580 | \ref ui |
---|
581 | */ |
---|
582 | #define LOG_LEVEL(classname,...) public: enum log_level_enums { __VA_ARGS__ }; log_level_template<classname> log_level; private: friend class log_level_intermediate<classname>; static const Array<string> &log_level_names() { static const Array<string> log_level_names = log_level_template<classname>::string2Array( #__VA_ARGS__ ); return log_level_names; } |
---|
583 | |
---|
584 | |
---|
585 | //! Class representing function \f$f(x)\f$ of variable \f$x\f$ represented by \c rv |
---|
586 | class fnc : public root { |
---|
587 | protected: |
---|
588 | //! Length of the output vector |
---|
589 | int dimy; |
---|
590 | //! Length of the input vector |
---|
591 | int dimc; |
---|
592 | public: |
---|
593 | //!default constructor |
---|
594 | fnc() {}; |
---|
595 | //! function evaluates numerical value of \f$f(x)\f$ at \f$x=\f$ \c cond |
---|
596 | virtual vec eval ( const vec &cond ) { |
---|
597 | return vec ( 0 ); |
---|
598 | }; |
---|
599 | |
---|
600 | //! access function |
---|
601 | int dimension() const { |
---|
602 | return dimy; |
---|
603 | } |
---|
604 | //! access function |
---|
605 | int dimensionc() const { |
---|
606 | return dimc; |
---|
607 | } |
---|
608 | void from_setting(const Setting &set){ |
---|
609 | UI::get(dimy, set, "dim", UI::optional); |
---|
610 | UI::get(dimc, set, "dimc", UI::optional); |
---|
611 | } |
---|
612 | }; |
---|
613 | |
---|
614 | class epdf; |
---|
615 | |
---|
616 | //! Conditional probability density, e.g. modeling \f$ f( x | y) \f$, where \f$ x \f$ is random variable, \c rv, and \f$ y \f$ is conditioning variable, \c rvc. |
---|
617 | class pdf : public root { |
---|
618 | protected: |
---|
619 | //!dimension of the condition |
---|
620 | int dimc; |
---|
621 | |
---|
622 | //! random variable in condition |
---|
623 | RV rvc; |
---|
624 | |
---|
625 | //! dimension of random variable |
---|
626 | int dim; |
---|
627 | |
---|
628 | //! random variable |
---|
629 | RV rv; |
---|
630 | |
---|
631 | public: |
---|
632 | //! \name Constructors |
---|
633 | //! @{ |
---|
634 | |
---|
635 | pdf() : dimc ( 0 ), rvc(), dim ( 0 ), rv() { } |
---|
636 | |
---|
637 | pdf ( const pdf &m ) : dimc ( m.dimc ), rvc ( m.rvc ), dim ( m.dim ), rv ( m.rv ) { } |
---|
638 | |
---|
639 | //!@} |
---|
640 | |
---|
641 | //! \name Matematical operations |
---|
642 | //!@{ |
---|
643 | |
---|
644 | //! 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 |
---|
645 | virtual vec samplecond ( const vec &cond ) = 0; |
---|
646 | |
---|
647 | //! 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 |
---|
648 | virtual mat samplecond_mat ( const vec &cond, int N ); |
---|
649 | |
---|
650 | //! Shortcut for conditioning and evaluation of the internal epdf. In some cases, this operation can be implemented efficiently. |
---|
651 | virtual double evallogcond ( const vec &yt, const vec &cond ) = 0; |
---|
652 | |
---|
653 | //! Matrix version of evallogcond |
---|
654 | virtual vec evallogcond_mat ( const mat &Yt, const vec &cond ) { |
---|
655 | vec v ( Yt.cols() ); |
---|
656 | for ( int i = 0; i < Yt.cols(); i++ ) { |
---|
657 | v ( i ) = evallogcond ( Yt.get_col ( i ), cond ); |
---|
658 | } |
---|
659 | return v; |
---|
660 | } |
---|
661 | |
---|
662 | //! Array<vec> version of evallogcond |
---|
663 | virtual vec evallogcond_mat ( const Array<vec> &Yt, const vec &cond ) { |
---|
664 | vec v ( Yt.length() ); |
---|
665 | for ( int i = 0; i < Yt.length(); i++ ) { |
---|
666 | v ( i ) = evallogcond ( Yt( i ), cond ); |
---|
667 | } |
---|
668 | return v; |
---|
669 | } |
---|
670 | |
---|
671 | //! \name Access to attributes |
---|
672 | //! @{ |
---|
673 | |
---|
674 | const RV& _rv() const { |
---|
675 | return rv; |
---|
676 | } |
---|
677 | const RV& _rvc() const { |
---|
678 | return rvc; |
---|
679 | } |
---|
680 | |
---|
681 | int dimension() const { |
---|
682 | return dim; |
---|
683 | } |
---|
684 | int dimensionc() { |
---|
685 | return dimc; |
---|
686 | } |
---|
687 | |
---|
688 | //! access function |
---|
689 | void set_dim ( int d ) { |
---|
690 | dim = d; |
---|
691 | } |
---|
692 | //! access function |
---|
693 | void set_dimc ( int d ) { |
---|
694 | dimc = d; |
---|
695 | } |
---|
696 | //! Load from structure with elements: |
---|
697 | //! \code |
---|
698 | //! { class = "pdf_offspring", |
---|
699 | //! rv = {class="RV", names=(...),}; // RV describing meaning of random variable |
---|
700 | //! rvc= {class="RV", names=(...),}; // RV describing meaning of random variable in condition |
---|
701 | //! // elements of offsprings |
---|
702 | //! } |
---|
703 | //! \endcode |
---|
704 | //!@} |
---|
705 | void from_setting ( const Setting &set ); |
---|
706 | |
---|
707 | void to_setting ( Setting &set ) const; |
---|
708 | //!@} |
---|
709 | |
---|
710 | //! \name Connection to other objects |
---|
711 | //!@{ |
---|
712 | void set_rvc ( const RV &rvc0 ) { |
---|
713 | rvc = rvc0; |
---|
714 | } |
---|
715 | void set_rv ( const RV &rv0 ) { |
---|
716 | rv = rv0; |
---|
717 | } |
---|
718 | |
---|
719 | //! Names of variables stored in RV are considered to be valid only if their size match size of the parameters (dim). |
---|
720 | bool isnamed() const { |
---|
721 | return ( dim == rv._dsize() ) && ( dimc == rvc._dsize() ); |
---|
722 | } |
---|
723 | //!@} |
---|
724 | }; |
---|
725 | SHAREDPTR ( pdf ); |
---|
726 | |
---|
727 | //! Probability density function with numerical statistics, e.g. posterior density. |
---|
728 | class epdf : public pdf { |
---|
729 | //! \var log_level_enums logmean |
---|
730 | //! log mean value of the density when requested |
---|
731 | |
---|
732 | //! \var log_level_enums loglbound |
---|
733 | //! log lower bound of the density (see function qbounds) |
---|
734 | |
---|
735 | //! \var log_level_enums logubound |
---|
736 | //! log upper bound of the density (see function qbounds) |
---|
737 | |
---|
738 | //! \var log_level_enums logfull |
---|
739 | //! log full record of the density in the form of setting |
---|
740 | LOG_LEVEL(epdf,logmean,loglbound,logubound); |
---|
741 | |
---|
742 | public: |
---|
743 | /*! \name Constructors |
---|
744 | Construction of each epdf should support two types of constructors: |
---|
745 | \li empty constructor, |
---|
746 | \li copy constructor, |
---|
747 | |
---|
748 | The following constructors should be supported for convenience: |
---|
749 | \li constructor followed by calling \c set_parameters() WHICH IS OBSOLETE (TODO) |
---|
750 | \li constructor accepting random variables calling \c set_rv() |
---|
751 | |
---|
752 | All internal data structures are constructed as empty. Their values (including sizes) will be |
---|
753 | set by method \c set_parameters() WHICH IS OBSOLETE (TODO). This way references can be initialized in constructors. |
---|
754 | @{*/ |
---|
755 | epdf() {}; |
---|
756 | epdf ( const epdf &e ) : pdf ( e ) {}; |
---|
757 | |
---|
758 | |
---|
759 | //!@} |
---|
760 | |
---|
761 | //! \name Matematical Operations |
---|
762 | //!@{ |
---|
763 | |
---|
764 | //! Returns a sample, \f$ x \f$ from density \f$ f_x()\f$ |
---|
765 | virtual vec sample() const = 0; |
---|
766 | |
---|
767 | //! Returns N samples, \f$ [x_1 , x_2 , \ldots \ \f$ from density \f$ f_x(rv)\f$ |
---|
768 | virtual mat sample_mat ( int N ) const; |
---|
769 | |
---|
770 | //! Compute log-probability of argument \c val |
---|
771 | //! In case the argument is out of suport return -Infinity |
---|
772 | virtual double evallog ( const vec &val ) const = 0; |
---|
773 | |
---|
774 | //! Compute log-probability of multiple values argument \c val |
---|
775 | virtual vec evallog_mat ( const mat &Val ) const; |
---|
776 | |
---|
777 | //! Compute log-probability of multiple values argument \c val |
---|
778 | virtual vec evallog_mat ( const Array<vec> &Avec ) const; |
---|
779 | |
---|
780 | //! Return conditional density on the given RV, the remaining rvs will be in conditioning |
---|
781 | virtual shared_ptr<pdf> condition ( const RV &rv ) const; |
---|
782 | |
---|
783 | //! Return marginal density on the given RV, the remainig rvs are intergrated out |
---|
784 | virtual shared_ptr<epdf> marginal ( const RV &rv ) const; |
---|
785 | |
---|
786 | virtual vec mean() const = 0; |
---|
787 | |
---|
788 | //! return expected variance (not covariance!) |
---|
789 | virtual vec variance() const = 0; |
---|
790 | |
---|
791 | //! return expected covariance -- default is diag(variance)!! |
---|
792 | virtual mat covariance() const {return diag(variance());}; |
---|
793 | |
---|
794 | //! Lower and upper bounds of \c percentage % quantile, returns mean-2*sigma as default |
---|
795 | virtual void qbounds ( vec &lb, vec &ub, double percentage = 0.95 ) const { |
---|
796 | vec mea = mean(); |
---|
797 | vec std = sqrt ( variance() ); |
---|
798 | lb = mea - 2 * std; |
---|
799 | ub = mea + 2 * std; |
---|
800 | }; |
---|
801 | //! Set statistics to match given input epdf. Typically it copies statistics from epdf of the same type and projects those form different types |
---|
802 | //! \param pdf0 epdf to match |
---|
803 | //! \param option placeholder for potential options |
---|
804 | void set_statistics(const epdf *pdf0) NOT_IMPLEMENTED_VOID; |
---|
805 | //!@} |
---|
806 | |
---|
807 | //! \name Connection to other classes |
---|
808 | //! Description of the random quantity via attribute \c rv is optional. |
---|
809 | //! For operations such as sampling \c rv does not need to be set. However, for \c marginalization |
---|
810 | //! and \c conditioning \c rv has to be set. NB: |
---|
811 | //! @{ |
---|
812 | |
---|
813 | //! store values of the epdf on the following levels: |
---|
814 | //! #1 mean |
---|
815 | //! #2 mean + lower & upper bound |
---|
816 | void log_register ( logger &L, const string &prefix ); |
---|
817 | |
---|
818 | void log_write() const; |
---|
819 | //!@} |
---|
820 | |
---|
821 | //! \name Access to attributes |
---|
822 | //! @{ |
---|
823 | |
---|
824 | //! Load from structure with elements: |
---|
825 | //! \code |
---|
826 | //! { rv = {class="RV", names=(...),}; // RV describing meaning of random variable |
---|
827 | //! // elements of offsprings |
---|
828 | //! } |
---|
829 | //! \endcode |
---|
830 | //!@} |
---|
831 | void from_setting ( const Setting &set ); |
---|
832 | void to_setting ( Setting &set ) const; |
---|
833 | |
---|
834 | vec samplecond ( const vec &cond ) { |
---|
835 | return sample(); |
---|
836 | } |
---|
837 | double evallogcond ( const vec &val, const vec &cond ) { |
---|
838 | return evallog ( val ); |
---|
839 | } |
---|
840 | }; |
---|
841 | SHAREDPTR ( epdf ); |
---|
842 | |
---|
843 | //! pdf with internal epdf that is modified by function \c condition |
---|
844 | template <class EPDF> |
---|
845 | class pdf_internal: public pdf { |
---|
846 | protected : |
---|
847 | //! Internal epdf used for sampling |
---|
848 | EPDF iepdf; |
---|
849 | public: |
---|
850 | //! constructor |
---|
851 | pdf_internal() : pdf(), iepdf() { |
---|
852 | } |
---|
853 | |
---|
854 | //! Update \c iepdf so that it represents this pdf conditioned on \c rvc = cond |
---|
855 | //! This function provides convenient reimplementation in offsprings |
---|
856 | virtual void condition ( const vec &cond ) = 0; |
---|
857 | |
---|
858 | //!access function to iepdf |
---|
859 | EPDF& e() { |
---|
860 | return iepdf; |
---|
861 | } |
---|
862 | |
---|
863 | //! Reimplements samplecond using \c condition() |
---|
864 | vec samplecond ( const vec &cond ); |
---|
865 | //! Reimplements evallogcond using \c condition() |
---|
866 | double evallogcond ( const vec &val, const vec &cond ); |
---|
867 | //! Efficient version of evallogcond for matrices |
---|
868 | virtual vec evallogcond_mat ( const mat &Dt, const vec &cond ); |
---|
869 | //! Efficient version of evallogcond for Array<vec> |
---|
870 | virtual vec evallogcond_mat ( const Array<vec> &Dt, const vec &cond ); |
---|
871 | //! Efficient version of samplecond |
---|
872 | virtual mat samplecond_mat ( const vec &cond, int N ); |
---|
873 | |
---|
874 | void validate() { |
---|
875 | pdf::validate(); |
---|
876 | iepdf.validate(); |
---|
877 | if ( rv._dsize() < iepdf._rv()._dsize() ) { |
---|
878 | rv = iepdf._rv(); |
---|
879 | }; |
---|
880 | dim = iepdf.dimension(); |
---|
881 | } |
---|
882 | }; |
---|
883 | |
---|
884 | /*! \brief DataLink is a connection between two data vectors Up and Down |
---|
885 | |
---|
886 | Up can be longer than Down. Down must be fully present in Up (TODO optional) |
---|
887 | See chart: |
---|
888 | \dot |
---|
889 | digraph datalink { |
---|
890 | node [shape=record]; |
---|
891 | subgraph cluster0 { |
---|
892 | label = "Up"; |
---|
893 | up [label="<1>|<2>|<3>|<4>|<5>"]; |
---|
894 | color = "white" |
---|
895 | } |
---|
896 | subgraph cluster1{ |
---|
897 | label = "Down"; |
---|
898 | labelloc = b; |
---|
899 | down [label="<1>|<2>|<3>"]; |
---|
900 | color = "white" |
---|
901 | } |
---|
902 | up:1 -> down:1; |
---|
903 | up:3 -> down:2; |
---|
904 | up:5 -> down:3; |
---|
905 | } |
---|
906 | \enddot |
---|
907 | |
---|
908 | */ |
---|
909 | class datalink { |
---|
910 | protected: |
---|
911 | //! Remember how long val should be |
---|
912 | int downsize; |
---|
913 | |
---|
914 | //! Remember how long val of "Up" should be |
---|
915 | int upsize; |
---|
916 | |
---|
917 | //! val-to-val link, indices of the upper val |
---|
918 | ivec v2v_up; |
---|
919 | |
---|
920 | public: |
---|
921 | //! Constructor |
---|
922 | datalink() : downsize ( 0 ), upsize ( 0 ) { } |
---|
923 | |
---|
924 | //! Convenience constructor |
---|
925 | datalink ( const RV &rv, const RV &rv_up ) { |
---|
926 | set_connection ( rv, rv_up ); |
---|
927 | } |
---|
928 | |
---|
929 | //! set connection, rv must be fully present in rv_up |
---|
930 | virtual void set_connection ( const RV &rv, const RV &rv_up ); |
---|
931 | |
---|
932 | //! set connection using indices |
---|
933 | virtual void set_connection ( int ds, int us, const ivec &upind ); |
---|
934 | |
---|
935 | //! Get val for myself from val of "Up" |
---|
936 | vec pushdown ( const vec &val_up ) { |
---|
937 | vec tmp ( downsize ); |
---|
938 | filldown ( val_up, tmp ); |
---|
939 | return tmp; |
---|
940 | } |
---|
941 | //! Get val for vector val_down from val of "Up" |
---|
942 | virtual void filldown ( const vec &val_up, vec &val_down ) { |
---|
943 | bdm_assert_debug ( upsize == val_up.length(), "Wrong val_up" ); |
---|
944 | val_down = val_up ( v2v_up ); |
---|
945 | } |
---|
946 | //! Fill val of "Up" by my pieces |
---|
947 | virtual void pushup ( vec &val_up, const vec &val ) { |
---|
948 | bdm_assert_debug ( downsize == val.length(), "Wrong val" ); |
---|
949 | bdm_assert_debug ( upsize == val_up.length(), "Wrong val_up" ); |
---|
950 | set_subvector ( val_up, v2v_up, val ); |
---|
951 | } |
---|
952 | //! access functions |
---|
953 | int _upsize() { |
---|
954 | return upsize; |
---|
955 | } |
---|
956 | //! access functions |
---|
957 | int _downsize() { |
---|
958 | return downsize; |
---|
959 | } |
---|
960 | //! for future use |
---|
961 | virtual ~datalink() {} |
---|
962 | }; |
---|
963 | |
---|
964 | /*! Extension of datalink to fill only part of Down |
---|
965 | */ |
---|
966 | class datalink_part : public datalink { |
---|
967 | protected: |
---|
968 | //! indices of values in vector downsize |
---|
969 | ivec v2v_down; |
---|
970 | public: |
---|
971 | void set_connection ( const RV &rv, const RV &rv_up ); |
---|
972 | //! Get val for vector val_down from val of "Up" |
---|
973 | void filldown ( const vec &val_up, vec &val_down ) { |
---|
974 | set_subvector ( val_down, v2v_down, val_up ( v2v_up ) ); |
---|
975 | } |
---|
976 | }; |
---|
977 | |
---|
978 | /*! \brief Datalink that buffers delayed values - do not forget to call step() |
---|
979 | |
---|
980 | Up is current data, Down is their subset with possibly delayed values |
---|
981 | */ |
---|
982 | class datalink_buffered: public datalink_part { |
---|
983 | protected: |
---|
984 | //! History, ordered as \f$[Up_{t-1},Up_{t-2}, \ldots]\f$ |
---|
985 | vec history; |
---|
986 | //! rv of the history |
---|
987 | RV Hrv; |
---|
988 | //! h2v : indices in down |
---|
989 | ivec h2v_down; |
---|
990 | //! h2v : indices in history |
---|
991 | ivec h2v_hist; |
---|
992 | //! v2h: indices of up too be pushed to h |
---|
993 | ivec v2h_up; |
---|
994 | public: |
---|
995 | |
---|
996 | datalink_buffered() : datalink_part(), history ( 0 ), h2v_down ( 0 ), h2v_hist ( 0 ) {}; |
---|
997 | //! push current data to history |
---|
998 | void store_data ( const vec &val_up ) { |
---|
999 | if ( v2h_up.length() > 0 ) { |
---|
1000 | history.shift_right ( 0, v2h_up.length() ); |
---|
1001 | history.set_subvector ( 0, val_up ( v2h_up ) ); |
---|
1002 | } |
---|
1003 | } |
---|
1004 | //! Get val for myself from val of "Up" |
---|
1005 | vec pushdown ( const vec &val_up ) { |
---|
1006 | vec tmp ( downsize ); |
---|
1007 | filldown ( val_up, tmp ); |
---|
1008 | return tmp; |
---|
1009 | } |
---|
1010 | |
---|
1011 | void filldown ( const vec &val_up, vec &val_down ) { |
---|
1012 | bdm_assert_debug ( val_down.length() >= downsize, "short val_down" ); |
---|
1013 | |
---|
1014 | set_subvector ( val_down, v2v_down, val_up ( v2v_up ) ); // copy direct values |
---|
1015 | set_subvector ( val_down, h2v_down, history ( h2v_hist ) ); // copy delayed values |
---|
1016 | } |
---|
1017 | |
---|
1018 | void set_connection ( const RV &rv, const RV &rv_up ); |
---|
1019 | |
---|
1020 | //! set history of variable given by \c rv1 to values of \c hist. |
---|
1021 | void set_history ( const RV& rv1, const vec &hist0 ); |
---|
1022 | }; |
---|
1023 | |
---|
1024 | class vec_from_vec: public vec { |
---|
1025 | protected: |
---|
1026 | datalink_part dl; |
---|
1027 | int vecfromlen; |
---|
1028 | public: |
---|
1029 | void update(const vec &v1){ |
---|
1030 | bdm_assert_debug(length()==dl._downsize(),"vec_from_vec incompatible"); |
---|
1031 | bdm_assert_debug(v1.length()==vecfromlen,"This vec was connected to vec of length "+num2str(vecfromlen)+ |
---|
1032 | ", but vec of length "+num2str(v1.length())+" was obtained" ); |
---|
1033 | dl.filldown(v1,*this); |
---|
1034 | }; |
---|
1035 | void connect(const RV &vecrv, const RV & vec1){ |
---|
1036 | bdm_assert(vecrv._dsize()==length(),"Description of this vector, "+ vecrv.to_string() +" does not math it length: " + num2str(length())); |
---|
1037 | dl.set_connection(vecrv,vec1); |
---|
1038 | bdm_assert(dl._downsize()==length(), "Rv of this vector, "+vecrv.to_string() +" was not found in given rv: "+vec1.to_string()); |
---|
1039 | }; |
---|
1040 | }; |
---|
1041 | |
---|
1042 | class vec_from_2vec: public vec { |
---|
1043 | protected: |
---|
1044 | datalink_part dl1; |
---|
1045 | datalink_part dl2; |
---|
1046 | public: |
---|
1047 | void update(const vec &v1, const vec &v2){ |
---|
1048 | bdm_assert_debug(length()==dl1._downsize()+dl2._downsize(),"vec_from_vec incompatible"); |
---|
1049 | bdm_assert_debug(v1.length()>=dl1._upsize(),"vec_from_vec incompatible"); |
---|
1050 | bdm_assert_debug(v2.length()>=dl2._upsize(),"vec_from_vec incompatible"); |
---|
1051 | dl1.filldown(v1,*this); |
---|
1052 | dl2.filldown(v2,*this); |
---|
1053 | }; |
---|
1054 | void connect(const RV &rv, const RV & rv1, const RV &rv2){ |
---|
1055 | dl1.set_connection(rv,rv1); |
---|
1056 | dl2.set_connection(rv,rv2); |
---|
1057 | set_length(rv._dsize()); |
---|
1058 | }; |
---|
1059 | }; |
---|
1060 | |
---|
1061 | //! buffered datalink from 2 vectors to 1 |
---|
1062 | class datalink_2to1_buffered { |
---|
1063 | protected: |
---|
1064 | //! link 1st vector to down |
---|
1065 | datalink_buffered dl1; |
---|
1066 | //! link 2nd vector to down |
---|
1067 | datalink_buffered dl2; |
---|
1068 | public: |
---|
1069 | //! set connection between RVs |
---|
1070 | void set_connection ( const RV &rv, const RV &rv_up1, const RV &rv_up2 ) { |
---|
1071 | dl1.set_connection ( rv, rv_up1 ); |
---|
1072 | dl2.set_connection ( rv, rv_up2 ); |
---|
1073 | } |
---|
1074 | //! fill values of down from the values of the two up vectors |
---|
1075 | void filldown ( const vec &val1, const vec &val2, vec &val_down ) { |
---|
1076 | bdm_assert_debug ( val_down.length() >= dl1._downsize() + dl2._downsize(), "short val_down" ); |
---|
1077 | dl1.filldown ( val1, val_down ); |
---|
1078 | dl2.filldown ( val2, val_down ); |
---|
1079 | } |
---|
1080 | //! update buffer |
---|
1081 | void step ( const vec &dt, const vec &ut ) { |
---|
1082 | dl1.store_data ( dt ); |
---|
1083 | dl2.store_data ( ut ); |
---|
1084 | } |
---|
1085 | }; |
---|
1086 | |
---|
1087 | |
---|
1088 | |
---|
1089 | //! Data link with a condition. |
---|
1090 | class datalink_m2e: public datalink { |
---|
1091 | protected: |
---|
1092 | //! Remember how long cond should be |
---|
1093 | int condsize; |
---|
1094 | |
---|
1095 | //!upper_val-to-local_cond link, indices of the upper val |
---|
1096 | ivec v2c_up; |
---|
1097 | |
---|
1098 | //!upper_val-to-local_cond link, indices of the local cond |
---|
1099 | ivec v2c_lo; |
---|
1100 | |
---|
1101 | public: |
---|
1102 | //! Constructor |
---|
1103 | datalink_m2e() : condsize ( 0 ) { } |
---|
1104 | |
---|
1105 | //! Set connection between vectors |
---|
1106 | void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up ); |
---|
1107 | |
---|
1108 | //!Construct condition |
---|
1109 | vec get_cond ( const vec &val_up ); |
---|
1110 | |
---|
1111 | //! Copy corresponding values to Up.condition |
---|
1112 | void pushup_cond ( vec &val_up, const vec &val, const vec &cond ); |
---|
1113 | }; |
---|
1114 | |
---|
1115 | //!DataLink is a connection between pdf and its superordinate (Up) |
---|
1116 | //! This class links |
---|
1117 | class datalink_m2m: public datalink_m2e { |
---|
1118 | protected: |
---|
1119 | //!cond-to-cond link, indices of the upper cond |
---|
1120 | ivec c2c_up; |
---|
1121 | //!cond-to-cond link, indices of the local cond |
---|
1122 | ivec c2c_lo; |
---|
1123 | |
---|
1124 | public: |
---|
1125 | //! Constructor |
---|
1126 | datalink_m2m() {}; |
---|
1127 | //! Set connection between the vectors |
---|
1128 | void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up ) { |
---|
1129 | datalink_m2e::set_connection ( rv, rvc, rv_up ); |
---|
1130 | //establish c2c connection |
---|
1131 | rvc.dataind ( rvc_up, c2c_lo, c2c_up ); |
---|
1132 | // bdm_assert_debug ( c2c_lo.length() + v2c_lo.length() == condsize, "cond is not fully given" ); |
---|
1133 | } |
---|
1134 | |
---|
1135 | //! Get cond for myself from val and cond of "Up" |
---|
1136 | vec get_cond ( const vec &val_up, const vec &cond_up ) { |
---|
1137 | vec tmp ( condsize ); |
---|
1138 | fill_cond ( val_up, cond_up, tmp ); |
---|
1139 | return tmp; |
---|
1140 | } |
---|
1141 | //! fill condition |
---|
1142 | void fill_cond ( const vec &val_up, const vec &cond_up, vec& cond_out ) { |
---|
1143 | bdm_assert_debug ( cond_out.length() >= condsize, "dl.fill_cond: cond_out is too small" ); |
---|
1144 | set_subvector ( cond_out, v2c_lo, val_up ( v2c_up ) ); |
---|
1145 | set_subvector ( cond_out, c2c_lo, cond_up ( c2c_up ) ); |
---|
1146 | } |
---|
1147 | //! Fill |
---|
1148 | |
---|
1149 | }; |
---|
1150 | |
---|
1151 | |
---|
1152 | //! \brief Combines RVs from a list of pdfs to a single one. |
---|
1153 | RV get_composite_rv ( const Array<shared_ptr<pdf> > &pdfs, bool checkoverlap = false ); |
---|
1154 | |
---|
1155 | /*! \brief Abstract class for discrete-time sources of data. |
---|
1156 | |
---|
1157 | The class abstracts operations of: |
---|
1158 | \li data aquisition, |
---|
1159 | \li data-preprocessing, such as scaling of data, |
---|
1160 | \li data resampling from the task of estimation and control. |
---|
1161 | Moreover, for controlled systems, it is able to receive the desired control action and perform it in the next step. (Or as soon as possible). |
---|
1162 | |
---|
1163 | The DataSource has three main data interaction structures: |
---|
1164 | \li input, \f$ u_t \f$, which are described via RVs in attribute \c urv |
---|
1165 | \li observations \f$ d_t \f$, which are described in \c drv |
---|
1166 | In simulators, dt may contain "hidden" variables as well. |
---|
1167 | |
---|
1168 | */ |
---|
1169 | |
---|
1170 | class DS : public root { |
---|
1171 | //! \var log_level_enums logdt |
---|
1172 | //! log all outputs |
---|
1173 | |
---|
1174 | //! \var log_level_enums logut |
---|
1175 | //! log all inputs |
---|
1176 | LOG_LEVEL(DS,logdt,logut); |
---|
1177 | |
---|
1178 | protected: |
---|
1179 | //! size of data returned by \c getdata() |
---|
1180 | int dtsize; |
---|
1181 | //! size of data |
---|
1182 | int utsize; |
---|
1183 | //!Description of data returned by \c getdata(). |
---|
1184 | RV Drv; |
---|
1185 | //!Description of data witten by by \c write(). |
---|
1186 | RV Urv; // |
---|
1187 | public: |
---|
1188 | //! publicly acessible vector of observations |
---|
1189 | vec dt; |
---|
1190 | //! publicly writeble vector of inputs |
---|
1191 | vec ut; |
---|
1192 | |
---|
1193 | //! default constructors |
---|
1194 | DS() : dtsize ( 0 ), utsize ( 0 ), Drv(), Urv(){ |
---|
1195 | log_level[logdt] = true; |
---|
1196 | log_level[logut] = true; |
---|
1197 | }; |
---|
1198 | |
---|
1199 | //! Returns maximum number of provided data, by default it is set to maximum allowed length, shorter DS should overload this method! See, MemDS.max_length(). |
---|
1200 | virtual int max_length() { |
---|
1201 | return std::numeric_limits< int >::max(); |
---|
1202 | } |
---|
1203 | //! Returns full vector of observed data=[output, input] |
---|
1204 | virtual void getdata ( vec &dt_out ) const { |
---|
1205 | bdm_assert_debug(dtsize==dt.length(),"DS::getdata: dt is not of declared size;"); |
---|
1206 | dt_out=dt; |
---|
1207 | }; |
---|
1208 | |
---|
1209 | //! Returns data records at indices. Default is inefficent. |
---|
1210 | virtual void getdata ( vec &dt_out, const ivec &indices ) { |
---|
1211 | bdm_assert_debug(max(indices)<dt.length(), "DS::getdata: indeces out of bounds"); |
---|
1212 | dt_out = dt(indices); |
---|
1213 | }; |
---|
1214 | |
---|
1215 | //! Accepts action variable and schedule it for application. |
---|
1216 | virtual void write ( const vec &ut ) NOT_IMPLEMENTED_VOID; |
---|
1217 | |
---|
1218 | //! Accepts action variables at specific indices |
---|
1219 | virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID; |
---|
1220 | |
---|
1221 | //! Moves from \f$ t \f$ to \f$ t+1 \f$, i.e. perfroms the actions and reads response of the system. |
---|
1222 | virtual void step() = 0; |
---|
1223 | |
---|
1224 | //! Register DS for logging into logger L |
---|
1225 | virtual void log_register ( logger &L, const string &prefix ); |
---|
1226 | //! Register DS for logging into logger L |
---|
1227 | virtual void log_write ( ) const; |
---|
1228 | //!access function |
---|
1229 | virtual const RV& _drv() const { |
---|
1230 | return Drv; |
---|
1231 | } |
---|
1232 | //!access function |
---|
1233 | const RV& _urv() const { |
---|
1234 | return Urv; |
---|
1235 | } |
---|
1236 | |
---|
1237 | /*! |
---|
1238 | \code |
---|
1239 | drv = bdm::RV::from_setting // decription of observed data |
---|
1240 | urv = bdm::RV::from_setting // decription of input data |
---|
1241 | log_level = "logdt,logut" // when set, both the simulated data and the inputs are stored to the logger |
---|
1242 | // By default both are on. It makes sense to switch them off for MemDS where the data are already stored. |
---|
1243 | \endcode |
---|
1244 | */ |
---|
1245 | void from_setting ( const Setting &set ); |
---|
1246 | |
---|
1247 | void validate(); |
---|
1248 | }; |
---|
1249 | |
---|
1250 | /*! \brief Bayesian Model of a system, i.e. all uncertainty is modeled by probabilities. |
---|
1251 | |
---|
1252 | This object represents exact or approximate evaluation of the Bayes rule: |
---|
1253 | \f[ |
---|
1254 | f(\theta_t | y_1,\ldots,y_t, u_1,\ldots,u_t) = \frac{f(y_t|\theta_t,\cdot) f(\theta_t|d_1,\ldots,d_{t-1})}{f(y_t|d_1,\ldots,d_{t-1})} |
---|
1255 | \f] |
---|
1256 | where: |
---|
1257 | * \f$ y_t \f$ is the variable |
---|
1258 | Access to the resulting posterior density is via function \c posterior(). |
---|
1259 | |
---|
1260 | As a "side-effect" it also evaluates log-likelihood of the data, which can be accessed via function _ll(). |
---|
1261 | It can also evaluate predictors of future values of \f$y_t\f$, see functions epredictor() and predictor(). |
---|
1262 | |
---|
1263 | Alternatively, it can evaluate posterior density with rvc replaced by the given values, \f$ c_t \f$: |
---|
1264 | \f[ |
---|
1265 | f(\theta_t | c_t, d_1,\ldots,d_t) \propto f(y_t,\theta_t|c_t,\cdot, d_1,\ldots,d_{t-1}) |
---|
1266 | \f] |
---|
1267 | |
---|
1268 | */ |
---|
1269 | |
---|
1270 | class BM : public root { |
---|
1271 | //! \var log_level_enums logfull |
---|
1272 | //! log full description of posterior in descriptive structure |
---|
1273 | |
---|
1274 | //! \var log_level_enums logevidence |
---|
1275 | //! log value of eveidence in each step |
---|
1276 | |
---|
1277 | //! \var log_level_enums logbounds |
---|
1278 | //! log lower and upper bounds of estimates |
---|
1279 | LOG_LEVEL(BM,logfull,logevidence,logbounds); |
---|
1280 | |
---|
1281 | protected: |
---|
1282 | //! Random variable of the data (optional) |
---|
1283 | RV yrv; |
---|
1284 | //! size of the data record |
---|
1285 | int dimy; |
---|
1286 | //! Name of extension variable |
---|
1287 | RV rvc; |
---|
1288 | //! size of the conditioning vector |
---|
1289 | int dimc; |
---|
1290 | |
---|
1291 | //!Logarithm of marginalized data likelihood. |
---|
1292 | double ll; |
---|
1293 | //! If true, the filter will compute likelihood of the data record and store it in \c ll . Set to false if you want to save computational time. |
---|
1294 | bool evalll; |
---|
1295 | |
---|
1296 | public: |
---|
1297 | //! \name Constructors |
---|
1298 | //! @{ |
---|
1299 | |
---|
1300 | BM() : yrv(), dimy ( 0 ), rvc(), dimc ( 0 ), ll ( 0 ), evalll ( true ) { }; |
---|
1301 | // BM ( const BM &B ) : yrv ( B.yrv ), dimy(B.dimy), rvc ( B.rvc ),dimc(B.dimc), ll ( B.ll ), evalll ( B.evalll ) {} |
---|
1302 | //! \brief Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! |
---|
1303 | //! Prototype: \code BM* _copy() const {return new BM(*this);} \endcode |
---|
1304 | virtual BM* _copy() const NOT_IMPLEMENTED(NULL); |
---|
1305 | //!@} |
---|
1306 | |
---|
1307 | //! \name Mathematical operations |
---|
1308 | //!@{ |
---|
1309 | |
---|
1310 | /*! \brief Incremental Bayes rule |
---|
1311 | @param dt vector of input data |
---|
1312 | */ |
---|
1313 | virtual void bayes ( const vec &yt, const vec &cond = empty_vec ) =0; |
---|
1314 | //! Batch Bayes rule (columns of Dt are observations) |
---|
1315 | virtual double bayes_batch ( const mat &Dt, const vec &cond = empty_vec ); |
---|
1316 | //! Batch Bayes rule (columns of Dt are observations, columns of Cond are conditions) |
---|
1317 | virtual double bayes_batch ( const mat &Dt, const mat &Cond ); |
---|
1318 | //! Evaluates predictive log-likelihood of the given data record |
---|
1319 | //! I.e. marginal likelihood of the data with the posterior integrated out. |
---|
1320 | //! This function evaluates only \f$ y_t \f$, condition is assumed to be the last used in bayes(). |
---|
1321 | //! See bdm::BM::predictor for conditional version. |
---|
1322 | virtual double logpred ( const vec &yt, const vec &cond ) const NOT_IMPLEMENTED(0.0); |
---|
1323 | |
---|
1324 | //! Matrix version of logpred |
---|
1325 | vec logpred_mat ( const mat &Yt, const mat &Cond ) const { |
---|
1326 | vec tmp ( Yt.cols() ); |
---|
1327 | for ( int i = 0; i < Yt.cols(); i++ ) { |
---|
1328 | tmp ( i ) = logpred ( Yt.get_col ( i ), Cond.get_col(i) ); |
---|
1329 | } |
---|
1330 | return tmp; |
---|
1331 | } |
---|
1332 | |
---|
1333 | //!Constructs a predictive density \f$ f(d_{t+1} |d_{t}, \ldots d_{0}) \f$ |
---|
1334 | virtual epdf* epredictor(const vec &cond=vec()) const NOT_IMPLEMENTED(NULL); |
---|
1335 | |
---|
1336 | //!Constructs conditional density of 1-step ahead predictor \f$ f(d_{t+1} |d_{t+h-1}, \ldots d_{t}) \f$ |
---|
1337 | virtual pdf* predictor() const NOT_IMPLEMENTED(NULL); |
---|
1338 | |
---|
1339 | //!@} |
---|
1340 | |
---|
1341 | |
---|
1342 | //! \name Access to attributes |
---|
1343 | //!@{ |
---|
1344 | //! access function |
---|
1345 | const RV& _rvc() const { |
---|
1346 | return rvc; |
---|
1347 | } |
---|
1348 | //! access function |
---|
1349 | int dimensionc() const { |
---|
1350 | return dimc; |
---|
1351 | } |
---|
1352 | //! access function |
---|
1353 | int dimensiony() const { |
---|
1354 | return dimy; |
---|
1355 | } |
---|
1356 | //! access function |
---|
1357 | int dimension() const { |
---|
1358 | return posterior().dimension(); |
---|
1359 | } |
---|
1360 | //! access function |
---|
1361 | const RV& _rv() const { |
---|
1362 | return posterior()._rv(); |
---|
1363 | } |
---|
1364 | //! access function |
---|
1365 | const RV& _yrv() const { |
---|
1366 | return yrv; |
---|
1367 | } |
---|
1368 | //! access function |
---|
1369 | void set_yrv ( const RV &rv ) { |
---|
1370 | yrv = rv; |
---|
1371 | } |
---|
1372 | //! access function |
---|
1373 | void set_rvc ( const RV &rv ) { |
---|
1374 | rvc = rv; |
---|
1375 | } |
---|
1376 | //! access to rv of the posterior |
---|
1377 | void set_rv ( const RV &rv ) { |
---|
1378 | const_cast<epdf&> ( posterior() ).set_rv ( rv ); |
---|
1379 | } |
---|
1380 | //! access function |
---|
1381 | void set_dim ( int dim ) { |
---|
1382 | const_cast<epdf&> ( posterior() ).set_dim ( dim ); |
---|
1383 | } |
---|
1384 | //! return internal log-likelihood of the last data vector |
---|
1385 | double _ll() const { |
---|
1386 | return ll; |
---|
1387 | } |
---|
1388 | //! switch evaluation of log-likelihood on/off |
---|
1389 | void set_evalll ( bool evl0 ) { |
---|
1390 | evalll = evl0; |
---|
1391 | } |
---|
1392 | //! return posterior density |
---|
1393 | virtual const epdf& posterior() const = 0; |
---|
1394 | |
---|
1395 | epdf& prior() {return const_cast<epdf&>(posterior());} |
---|
1396 | //! set prior density -- same as posterior but writable |
---|
1397 | virtual void set_prior(const epdf *pdf0) NOT_IMPLEMENTED_VOID; |
---|
1398 | |
---|
1399 | //!@} |
---|
1400 | |
---|
1401 | //! \name Logging of results |
---|
1402 | //!@{ |
---|
1403 | |
---|
1404 | //! Add all logged variables to a logger |
---|
1405 | //! Log levels two digits: xy where |
---|
1406 | //! * y = 0/1 log-likelihood is to be logged |
---|
1407 | //! * x = level of the posterior (typically 0/1/2 for nothing/mean/bounds) |
---|
1408 | virtual void log_register ( logger &L, const string &prefix = "" ); |
---|
1409 | |
---|
1410 | //! Save results to the given logger, details of what is stored is configured by \c LIDs and \c options |
---|
1411 | virtual void log_write ( ) const; |
---|
1412 | |
---|
1413 | //!@} |
---|
1414 | //! \brief Read names of random variables from setting |
---|
1415 | /*! |
---|
1416 | reading structure: |
---|
1417 | \TODO check if not remove... rv... |
---|
1418 | \code |
---|
1419 | yrv = RV(); // names of modelled data |
---|
1420 | rvc = RV(); // names of data in condition |
---|
1421 | rv = RV(); // names of parameters |
---|
1422 | log_level = "logmean"; // identifiers of levels of detail to store to loggers |
---|
1423 | \endcode |
---|
1424 | */ |
---|
1425 | void from_setting ( const Setting &set ) { |
---|
1426 | UI::get(yrv, set, "yrv", UI::optional ); |
---|
1427 | UI::get(rvc, set, "rvc", UI::optional ); |
---|
1428 | RV r; |
---|
1429 | UI::get(r, set, "rv", UI::optional ); |
---|
1430 | set_rv ( r ); |
---|
1431 | |
---|
1432 | UI::get ( log_level, set, "log_level", UI::optional ); |
---|
1433 | } |
---|
1434 | |
---|
1435 | void to_setting ( Setting &set ) const { |
---|
1436 | root::to_setting( set ); |
---|
1437 | UI::save( &yrv, set, "yrv" ); |
---|
1438 | UI::save( &rvc, set, "rvc" ); |
---|
1439 | UI::save( &posterior()._rv(), set, "rv" ); |
---|
1440 | UI::save( log_level, set ); |
---|
1441 | } |
---|
1442 | |
---|
1443 | //! process |
---|
1444 | void validate() |
---|
1445 | { |
---|
1446 | if ( log_level[logbounds] ) { |
---|
1447 | const_cast<epdf&> ( posterior() ).log_level[epdf::loglbound] = true; |
---|
1448 | } else { |
---|
1449 | const_cast<epdf&> ( posterior() ).log_level[epdf::logmean] = true;; |
---|
1450 | } |
---|
1451 | } |
---|
1452 | }; |
---|
1453 | |
---|
1454 | //! array of pointers to epdf |
---|
1455 | typedef Array<shared_ptr<epdf> > epdf_array; |
---|
1456 | //! array of pointers to pdf |
---|
1457 | typedef Array<shared_ptr<pdf> > pdf_array; |
---|
1458 | |
---|
1459 | template<class EPDF> |
---|
1460 | vec pdf_internal<EPDF>::samplecond ( const vec &cond ) { |
---|
1461 | condition ( cond ); |
---|
1462 | vec temp = iepdf.sample(); |
---|
1463 | return temp; |
---|
1464 | } |
---|
1465 | |
---|
1466 | template<class EPDF> |
---|
1467 | mat pdf_internal<EPDF>::samplecond_mat ( const vec &cond, int N ) { |
---|
1468 | condition ( cond ); |
---|
1469 | mat temp ( dimension(), N ); |
---|
1470 | vec smp ( dimension() ); |
---|
1471 | for ( int i = 0; i < N; i++ ) { |
---|
1472 | smp = iepdf.sample(); |
---|
1473 | temp.set_col ( i, smp ); |
---|
1474 | } |
---|
1475 | |
---|
1476 | return temp; |
---|
1477 | } |
---|
1478 | |
---|
1479 | template<class EPDF> |
---|
1480 | double pdf_internal<EPDF>::evallogcond ( const vec &yt, const vec &cond ) { |
---|
1481 | double tmp; |
---|
1482 | condition ( cond ); |
---|
1483 | tmp = iepdf.evallog ( yt ); |
---|
1484 | return tmp; |
---|
1485 | } |
---|
1486 | |
---|
1487 | template<class EPDF> |
---|
1488 | vec pdf_internal<EPDF>::evallogcond_mat ( const mat &Yt, const vec &cond ) { |
---|
1489 | condition ( cond ); |
---|
1490 | return iepdf.evallog_mat ( Yt ); |
---|
1491 | } |
---|
1492 | |
---|
1493 | template<class EPDF> |
---|
1494 | vec pdf_internal<EPDF>::evallogcond_mat ( const Array<vec> &Yt, const vec &cond ) { |
---|
1495 | condition ( cond ); |
---|
1496 | return iepdf.evallog_mat ( Yt ); |
---|
1497 | } |
---|
1498 | |
---|
1499 | }; //namespace |
---|
1500 | #endif // BDMBASE_H |
---|