root/library/bdm/base/bdmbase.h @ 1020

Revision 1020, 46.1 kB (checked in by smidl, 14 years ago)

DEBUG macro for conditional output + new class

  • Property svn:eol-style set to native
Line 
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
23using namespace libconfig;
24using namespace itpp;
25using namespace std;
26
27namespace bdm {
28
29//! Structure of RV, i.e. RVs expanded into a flat list of IDs, used for debugging.
30class str {
31public:
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
45The 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
47The class is implemented using global variables to assure uniqueness of description:
48
49 In is a vector
50\dot
51digraph datalink {
52rankdir=LR;
53subgraph cluster0 {
54node [shape=record];
55label = "MAP \n std::map<string,int>";
56map [label="{{\"a\"| \"b\" | \"c\"} | {<3> 3 |<1> 1|<2> 2}}"];
57color = "white"
58}
59subgraph cluster1{
60node [shape=record];
61label = "NAMES";
62names [label="{<1> \"b\" | <2> \"c\" | <3>\"a\" }"];
63color = "white"
64}
65subgraph cluster2{
66node [shape=record];
67label = "SIZES";
68labelloc = b;
69sizes [label="{<1>1 |<2> 4 |<3> 1}"];
70color = "white"
71}
72map:1 -> names:1;
73map:1 -> sizes:1;
74map:3 -> names:3;
75map:3 -> sizes:3;
76}
77\enddot
78
79RV 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
82class RV : public root {
83private:
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
95public:
96
97protected:
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
107private:
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        }
121public:
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};
294UIREGISTER ( RV );
295SHAREDPTR ( RV );
296
297//! Concat two random variables
298RV 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.
308template<class T> class log_level_intermediate : public log_level_base {
309protected:
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        }
323public:
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
384This 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*/
386class logger : public root {
387protected:
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;
416public:
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
490template<class T> class log_level_template : public log_level_intermediate<T> {
491public:
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
586class fnc : public root {       
587protected:
588        //! Length of the output vector
589        int dimy;
590        //! Length of the input vector
591        int dimc;
592public:
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
614class 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.
617class pdf : public root {
618protected:
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
631public:
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};
725SHAREDPTR ( pdf );
726
727//! Probability density function with numerical statistics, e.g. posterior density.
728class 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
742public:
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};
841SHAREDPTR ( epdf );
842
843//! pdf with internal epdf that is modified by function \c condition
844template <class EPDF>
845class pdf_internal: public pdf {
846protected :
847        //! Internal epdf used for sampling
848        EPDF iepdf;
849public:
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
886Up can be longer than Down. Down must be fully present in Up (TODO optional)
887See chart:
888\dot
889digraph 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*/
909class datalink {
910protected:
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
920public:
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*/
966class datalink_part : public datalink {
967protected:
968        //! indices of values in vector downsize
969        ivec v2v_down;
970public:
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
980Up is current data, Down is their subset with possibly delayed values
981*/
982class datalink_buffered: public datalink_part {
983protected:
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;
994public:
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
1024class 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
1042class 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
1062class datalink_2to1_buffered {
1063protected:
1064        //! link 1st vector to down
1065        datalink_buffered dl1;
1066        //! link 2nd vector to down
1067        datalink_buffered dl2;
1068public:
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.
1090class datalink_m2e: public datalink {
1091protected:
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
1101public:
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
1117class datalink_m2m: public datalink_m2e {
1118protected:
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
1124public:
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.
1153RV get_composite_rv ( const Array<shared_ptr<pdf> > &pdfs, bool checkoverlap = false );
1154
1155/*! \brief Abstract class for discrete-time sources of data.
1156
1157The 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.
1161Moreover, 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
1163The DataSource has three main data interaction structures:
1164\li input, \f$ u_t \f$,
1165\li output \f$ d_t \f$,
1166In simulators, d_t may contain "hidden" variables as well.
1167*/
1168
1169class DS : public root {
1170        //! \var log_level_enums logdt
1171        //! log all outputs
1172
1173        //! \var log_level_enums logut
1174        //! log all inputs
1175        LOG_LEVEL(DS,logdt,logut);
1176
1177protected:
1178        //! size of data returned by \c getdata()
1179        int dtsize;
1180        //! size of data
1181        int utsize;
1182        //!Description of data returned by \c getdata().
1183        RV Drv;
1184        //!Description of data witten by by \c write().
1185        RV Urv; //
1186public:
1187        //! default constructors
1188        DS() : dtsize ( 0 ), utsize ( 0 ), Drv(), Urv(){
1189                log_level[logdt] = true;
1190                log_level[logut] = true;
1191        };
1192
1193        //! 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().
1194        virtual int max_length() {
1195                return std::numeric_limits< int >::max();
1196        }
1197        //! Returns full vector of observed data=[output, input]
1198        virtual void getdata ( vec &dt ) const = 0;
1199
1200        //! Returns data records at indices. Default is inefficent.
1201        virtual void getdata ( vec &dt, const ivec &indices ) {
1202                vec tmp(dtsize);
1203                getdata(tmp);
1204                dt = tmp(indices);
1205        };
1206
1207        //! Accepts action variable and schedule it for application.   
1208        virtual void write ( const vec &ut ) NOT_IMPLEMENTED_VOID;
1209
1210        //! Accepts action variables at specific indices
1211        virtual void write ( const vec &ut, const ivec &indices ) NOT_IMPLEMENTED_VOID;
1212
1213        //! Moves from \f$ t \f$ to \f$ t+1 \f$, i.e. perfroms the actions and reads response of the system.
1214        virtual void step() = 0;
1215
1216        //! Register DS for logging into logger L
1217        virtual void log_register ( logger &L,  const string &prefix );
1218        //! Register DS for logging into logger L
1219        virtual void log_write ( ) const;
1220        //!access function
1221        virtual const RV& _drv() const {
1222                return Drv;
1223        }
1224        //!access function
1225        const RV& _urv() const {
1226                return Urv;
1227        }
1228
1229        //! set random variables
1230        virtual void set_drv ( const  RV &drv, const RV &urv) {
1231                Drv = drv;
1232                Urv = urv;
1233        }
1234
1235        void from_setting ( const Setting &set );
1236
1237        void validate();
1238};
1239
1240/*! \brief Bayesian Model of a system, i.e. all uncertainty is modeled by probabilities.
1241
1242This object represents exact or approximate evaluation of the Bayes rule:
1243\f[
1244f(\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})}
1245\f]
1246where:
1247 * \f$ y_t \f$ is the variable
1248Access to the resulting posterior density is via function \c posterior().
1249
1250As a "side-effect" it also evaluates log-likelihood of the data, which can be accessed via function _ll().
1251It can also evaluate predictors of future values of \f$y_t\f$, see functions epredictor() and predictor().
1252
1253Alternatively, it can evaluate posterior density with rvc replaced by the given values, \f$ c_t \f$:
1254\f[
1255f(\theta_t | c_t, d_1,\ldots,d_t) \propto  f(y_t,\theta_t|c_t,\cdot, d_1,\ldots,d_{t-1})
1256\f]
1257
1258*/
1259
1260class BM : public root {
1261        //! \var log_level_enums logfull
1262        //! log full description of posterior in descriptive structure
1263
1264        //! \var log_level_enums logevidence
1265        //! log value of eveidence in each step
1266       
1267        //! \var log_level_enums logbounds
1268        //! log lower and upper bounds of estimates
1269        LOG_LEVEL(BM,logfull,logevidence,logbounds);
1270
1271protected:
1272        //! Random variable of the data (optional)
1273        RV yrv;
1274        //! size of the data record
1275        int dimy;
1276        //! Name of extension variable
1277        RV rvc;
1278        //! size of the conditioning vector
1279        int dimc;
1280
1281        //!Logarithm of marginalized data likelihood.
1282        double ll;
1283        //!  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.
1284        bool evalll;
1285
1286public:
1287        //! \name Constructors
1288        //! @{
1289
1290        BM() : yrv(), dimy ( 0 ), rvc(), dimc ( 0 ), ll ( 0 ), evalll ( true ) { };
1291        //      BM ( const BM &B ) :  yrv ( B.yrv ), dimy(B.dimy), rvc ( B.rvc ),dimc(B.dimc), ll ( B.ll ), evalll ( B.evalll ) {}
1292        //! \brief Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually!
1293        //! Prototype: \code BM* _copy() const {return new BM(*this);} \endcode
1294        virtual BM* _copy() const NOT_IMPLEMENTED(NULL);
1295        //!@}
1296
1297        //! \name Mathematical operations
1298        //!@{
1299
1300        /*! \brief Incremental Bayes rule
1301        @param dt vector of input data
1302        */
1303        virtual void bayes ( const vec &yt, const vec &cond = empty_vec ) =0;
1304        //! Batch Bayes rule (columns of Dt are observations)
1305        virtual double bayes_batch ( const mat &Dt, const vec &cond = empty_vec );
1306        //! Batch Bayes rule (columns of Dt are observations, columns of Cond are conditions)
1307        virtual double bayes_batch ( const mat &Dt, const mat &Cond );
1308        //! Evaluates predictive log-likelihood of the given data record
1309        //! I.e. marginal likelihood of the data with the posterior integrated out.
1310        //! This function evaluates only \f$ y_t \f$, condition is assumed to be the last used in bayes().
1311        //! See bdm::BM::predictor for conditional version.
1312        virtual double logpred ( const vec &yt, const vec &cond ) const NOT_IMPLEMENTED(0.0);
1313
1314        //! Matrix version of logpred
1315        vec logpred_mat ( const mat &Yt, const mat &Cond ) const {
1316                vec tmp ( Yt.cols() );
1317                for ( int i = 0; i < Yt.cols(); i++ ) {
1318                        tmp ( i ) = logpred ( Yt.get_col ( i ), Cond.get_col(i) );
1319                }
1320                return tmp;
1321        }
1322
1323        //!Constructs a predictive density \f$ f(d_{t+1} |d_{t}, \ldots d_{0}) \f$
1324        virtual epdf* epredictor(const vec &cond=vec()) const NOT_IMPLEMENTED(NULL);
1325
1326        //!Constructs conditional density of 1-step ahead predictor \f$ f(d_{t+1} |d_{t+h-1}, \ldots d_{t}) \f$
1327        virtual pdf* predictor() const NOT_IMPLEMENTED(NULL);
1328
1329        //!@}
1330
1331
1332        //! \name Access to attributes
1333        //!@{
1334        //! access function
1335        const RV& _rvc() const {
1336                return rvc;
1337        }
1338        //! access function
1339        int dimensionc() const {
1340                return dimc;
1341        }
1342        //! access function
1343        int dimensiony() const {
1344                return dimy;
1345        }
1346        //! access function
1347        int dimension() const {
1348                return posterior().dimension();
1349        }
1350        //! access function
1351        const RV& _rv() const {
1352                return posterior()._rv();
1353        }
1354        //! access function
1355        const RV& _yrv() const {
1356                return yrv;
1357        }
1358        //! access function
1359        void set_yrv ( const RV &rv ) {
1360                yrv = rv;
1361        }
1362        //! access function
1363        void set_rvc ( const RV &rv ) {
1364                rvc = rv;
1365        }
1366        //! access to rv of the posterior
1367        void set_rv ( const RV &rv ) {
1368                const_cast<epdf&> ( posterior() ).set_rv ( rv );
1369        }
1370        //! access function
1371        void set_dim ( int dim ) {
1372                const_cast<epdf&> ( posterior() ).set_dim ( dim );
1373        }
1374        //! return internal log-likelihood of the last data vector
1375        double _ll() const {
1376                return ll;
1377        }
1378        //! switch evaluation of log-likelihood on/off
1379        void set_evalll ( bool evl0 ) {
1380                evalll = evl0;
1381        }
1382        //! return posterior density
1383        virtual const epdf& posterior() const = 0;
1384       
1385        epdf& prior() {return const_cast<epdf&>(posterior());}
1386        //! set prior density -- same as posterior but writable
1387        virtual void set_prior(const epdf *pdf0) NOT_IMPLEMENTED_VOID;
1388       
1389        //!@}
1390
1391        //! \name Logging of results
1392        //!@{
1393
1394        //! Add all logged variables to a logger
1395        //! Log levels two digits: xy where
1396        //!  * y = 0/1 log-likelihood is to be logged
1397        //!  * x = level of the posterior (typically 0/1/2 for nothing/mean/bounds)
1398        virtual void log_register ( logger &L, const string &prefix = "" );
1399
1400        //! Save results to the given logger, details of what is stored is configured by \c LIDs and \c options
1401        virtual void log_write ( ) const;
1402
1403        //!@}
1404        //! \brief Read names of random variables from setting
1405        /*!
1406        reading structure:
1407        \TODO check if not remove... rv...
1408        \code
1409        yrv  = RV();               // names of modelled data
1410        rvc  = RV();               // names of data in condition
1411    rv   = RV();               // names of parameters
1412        log_level = "logmean";     // identifiers of levels of detail to store to loggers
1413        \endcode
1414        */
1415        void from_setting ( const Setting &set ) {
1416                UI::get(yrv, set, "yrv", UI::optional );
1417                UI::get(rvc, set, "rvc", UI::optional );
1418                RV r;
1419                UI::get(r, set, "rv", UI::optional );
1420                set_rv ( r );
1421       
1422                UI::get ( log_level, set, "log_level", UI::optional );
1423        }
1424
1425        void to_setting ( Setting &set ) const {
1426                root::to_setting( set );
1427                UI::save( &yrv, set, "yrv" );
1428                UI::save( &rvc, set, "rvc" );           
1429                UI::save( &posterior()._rv(), set, "rv" );
1430                UI::save( log_level, set );
1431        }
1432
1433        //! process
1434        void validate()
1435        {
1436                if ( log_level[logbounds] ) {
1437                        const_cast<epdf&> ( posterior() ).log_level[epdf::loglbound] = true;
1438                } else {
1439                        const_cast<epdf&> ( posterior() ).log_level[epdf::logmean] = true;;
1440                }
1441        }
1442};
1443
1444//! array of pointers to epdf
1445typedef Array<shared_ptr<epdf> > epdf_array;
1446//! array of pointers to pdf
1447typedef Array<shared_ptr<pdf> > pdf_array;
1448
1449template<class EPDF>
1450vec pdf_internal<EPDF>::samplecond ( const vec &cond ) {
1451        condition ( cond );
1452        vec temp = iepdf.sample();
1453        return temp;
1454}
1455
1456template<class EPDF>
1457mat pdf_internal<EPDF>::samplecond_mat ( const vec &cond, int N ) {
1458        condition ( cond );
1459        mat temp ( dimension(), N );
1460        vec smp ( dimension() );
1461        for ( int i = 0; i < N; i++ ) {
1462                smp = iepdf.sample();
1463                temp.set_col ( i, smp );
1464        }
1465
1466        return temp;
1467}
1468
1469template<class EPDF>
1470double pdf_internal<EPDF>::evallogcond ( const vec &yt, const vec &cond ) {
1471        double tmp;
1472        condition ( cond );
1473        tmp = iepdf.evallog ( yt );
1474        return tmp;
1475}
1476
1477template<class EPDF>
1478vec pdf_internal<EPDF>::evallogcond_mat ( const mat &Yt, const vec &cond ) {
1479        condition ( cond );
1480        return iepdf.evallog_mat ( Yt );
1481}
1482
1483template<class EPDF>
1484vec pdf_internal<EPDF>::evallogcond_mat ( const Array<vec> &Yt, const vec &cond ) {
1485        condition ( cond );
1486        return iepdf.evallog_mat ( Yt );
1487}
1488
1489}; //namespace
1490#endif // BDMBASE_H
Note: See TracBrowser for help on using the browser.