Changeset 600 for library/bdm

Show
Ignore:
Timestamp:
09/03/09 22:12:12 (15 years ago)
Author:
mido
Message:

presun globalnich veci v RV dovnitr tridy kde byly zadefinovany jako staticke promenne
(plus drobne zacisteni dokumentace a adresarove struktury)

Location:
library/bdm/base
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/base/bdmbase.cpp

    r598 r600  
    55namespace bdm { 
    66 
    7 const int RV_BUFFER_STEP = 1; 
    8 RVmap RV_MAP; 
    9 Array<string> RV_NAMES ( RV_BUFFER_STEP ); 
    10 ivec RV_SIZES ( RV_BUFFER_STEP ); 
    11  
    12 RV RV0 = RV(); 
     7const int RV::BUFFER_STEP = 1; 
     8 
     9Array<string> RV::NAMES ( RV::BUFFER_STEP ); 
     10 
     11ivec RV::SIZES ( RV::BUFFER_STEP ); 
     12 
     13RV::str2int_map RV::MAP; 
    1314 
    1415void RV::clear_all() { 
    15         RV_MAP.clear(); 
    16         RV_SIZES.clear(); 
    17         RV_NAMES = Array<string> ( RV_BUFFER_STEP ); 
     16        MAP.clear(); 
     17        SIZES.clear(); 
     18        NAMES = Array<string> ( BUFFER_STEP ); 
    1819} 
    1920 
     
    2122        //Refer 
    2223        int id; 
    23         RVmap::const_iterator iter = RV_MAP.find ( name ); 
    24         if ( iter == RV_MAP.end() ) { 
    25                 id = RV_MAP.size() + 1; 
     24        str2int_map::const_iterator iter = MAP.find ( name ); 
     25        if ( iter == MAP.end() ) { 
     26                id = MAP.size() + 1; 
    2627                //debug 
    2728                /*              { 
    2829                                        cout << endl; 
    29                                         RVmap::const_iterator iter = RV_MAP.begin(); 
    30                                         for(RVmap::const_iterator iter=RV_MAP.begin(); iter!=RV_MAP.end(); iter++){ 
     30                                        str2int_map::const_iterator iter = MAP.begin(); 
     31                                        for(str2int_map::const_iterator iter=MAP.begin(); iter!=MAP.end(); iter++){ 
    3132                                                cout << "key: " << iter->first << " val: " << iter->second <<endl; 
    3233                                        } 
    3334                                }*/ 
    3435 
    35                 RV_MAP.insert ( make_pair ( name, id ) ); //add new rv 
    36                 if ( id >= RV_NAMES.length() ) { 
    37                         RV_NAMES.set_length ( id + RV_BUFFER_STEP, true ); 
    38                         RV_SIZES.set_length ( id + RV_BUFFER_STEP, true ); 
    39                 } 
    40                 RV_NAMES ( id ) = name; 
    41                 RV_SIZES ( id ) = size; 
     36                MAP.insert ( make_pair ( name, id ) ); //add new rv 
     37                if ( id >= NAMES.length() ) { 
     38                        NAMES.set_length ( id + BUFFER_STEP, true ); 
     39                        SIZES.set_length ( id + BUFFER_STEP, true ); 
     40                } 
     41                NAMES ( id ) = name; 
     42                SIZES ( id ) = size; 
    4243        } else { 
    4344                id = iter->second; 
    44                 bdm_assert_debug ( RV_SIZES ( id ) == size, "RV " + name + " of different size already exists" ); 
     45                bdm_assert_debug ( SIZES ( id ) == size, "RV " + name + " of different size already exists" ); 
    4546        } 
    4647        return id; 
     
    5051        int tmp = 0; 
    5152        for ( int i = 0; i < len; i++ ) { 
    52                 tmp += RV_SIZES ( ids ( i ) ); 
     53                tmp += SIZES ( ids ( i ) ); 
    5354        } 
    5455        return tmp; 
     
    5960        int tmp = 0; 
    6061        for ( int i = 0; i < len; i++ ) { 
    61                 tmp += RV_SIZES ( ids ( i ) ); 
     62                tmp += SIZES ( ids ( i ) ); 
    6263                szs ( i ) = tmp; 
    6364        } 
     
    134135} 
    135136 
    136 shared_ptr<mpdf> epdf::condition (const RV &rv) const { 
    137         bdm_warning ("Not implemented"); 
     137shared_ptr<mpdf> epdf::condition ( const RV &rv ) const { 
     138        bdm_warning ( "Not implemented" ); 
    138139        return shared_ptr<mpdf>(); 
    139140} 
    140141 
    141 shared_ptr<epdf> epdf::marginal (const RV &rv) const { 
    142         bdm_warning ("Not implemented"); 
     142shared_ptr<epdf> epdf::marginal ( const RV &rv ) const { 
     143        bdm_warning ( "Not implemented" ); 
    143144        return shared_ptr<epdf>(); 
    144145} 
     
    150151} 
    151152 
    152 vec epdf::evallog_m (const mat &Val) const { 
    153         vec x (Val.cols()); 
    154         for (int i = 0; i < Val.cols(); i++) { 
    155                 x (i) = evallog ( Val.get_col (i) ); 
     153vec epdf::evallog_m ( const mat &Val ) const { 
     154        vec x ( Val.cols() ); 
     155        for ( int i = 0; i < Val.cols(); i++ ) { 
     156                x ( i ) = evallog ( Val.get_col ( i ) ); 
    156157        } 
    157158 
     
    159160} 
    160161 
    161 vec epdf::evallog_m (const Array<vec> &Avec) const { 
    162         vec x (Avec.size()); 
    163         for (int i = 0; i < Avec.size(); i++) { 
    164                 x (i) = evallog ( Avec (i) ); 
     162vec epdf::evallog_m ( const Array<vec> &Avec ) const { 
     163        vec x ( Avec.size() ); 
     164        for ( int i = 0; i < Avec.size(); i++ ) { 
     165                x ( i ) = evallog ( Avec ( i ) ); 
    165166        } 
    166167 
     
    168169} 
    169170 
    170 mat mpdf::samplecond_m (const vec &cond, int N) { 
    171         mat M( dimension(), N ); 
     171mat mpdf::samplecond_m ( const vec &cond, int N ) { 
     172        mat M ( dimension(), N ); 
    172173        for ( int i = 0; i < N; i++ ) { 
    173                 M.set_col ( i, samplecond( cond ) ); 
     174                M.set_col ( i, samplecond ( cond ) ); 
    174175        } 
    175176 
     
    189190} 
    190191 
    191 void datalink::set_connection (const RV &rv, const RV &rv_up) { 
     192void datalink::set_connection ( const RV &rv, const RV &rv_up ) { 
    192193        downsize = rv._dsize(); 
    193           upsize = rv_up._dsize(); 
    194           v2v_up = rv.dataind (rv_up); 
    195           bdm_assert_debug (v2v_up.length() == downsize, "rv is not fully in rv_up"); 
    196 } 
    197  
    198 void datalink::set_connection (int ds, int us, const ivec &upind) { 
     194        upsize = rv_up._dsize(); 
     195        v2v_up = rv.dataind ( rv_up ); 
     196        bdm_assert_debug ( v2v_up.length() == downsize, "rv is not fully in rv_up" ); 
     197} 
     198 
     199void datalink::set_connection ( int ds, int us, const ivec &upind ) { 
    199200        downsize = ds; 
    200201        upsize = us; 
    201202        v2v_up = upind; 
    202     bdm_assert_debug (v2v_up.length() == downsize, "rv is not fully in rv_up"); 
    203 } 
    204  
    205 void datalink_part::set_connection (const RV &rv, const RV &rv_up) { 
    206          rv.dataind (rv_up, v2v_down, v2v_up); 
    207          downsize = v2v_down.length(); 
    208          upsize = v2v_up.length(); 
    209 } 
    210  
    211 void datalink_m2e::set_connection (const RV &rv, const RV &rvc, const RV &rv_up) { 
    212         datalink::set_connection (rv, rv_up); 
     203        bdm_assert_debug ( v2v_up.length() == downsize, "rv is not fully in rv_up" ); 
     204} 
     205 
     206void datalink_part::set_connection ( const RV &rv, const RV &rv_up ) { 
     207        rv.dataind ( rv_up, v2v_down, v2v_up ); 
     208        downsize = v2v_down.length(); 
     209        upsize = v2v_up.length(); 
     210} 
     211 
     212void datalink_m2e::set_connection ( const RV &rv, const RV &rvc, const RV &rv_up ) { 
     213        datalink::set_connection ( rv, rv_up ); 
    213214        condsize = rvc._dsize(); 
    214215        //establish v2c connection 
    215         rvc.dataind (rv_up, v2c_lo, v2c_up); 
    216 } 
    217  
    218 vec datalink_m2e::get_cond (const vec &val_up) { 
    219         vec tmp (condsize); 
    220         set_subvector (tmp, v2c_lo, val_up (v2c_up)); 
     216        rvc.dataind ( rv_up, v2c_lo, v2c_up ); 
     217} 
     218 
     219vec datalink_m2e::get_cond ( const vec &val_up ) { 
     220        vec tmp ( condsize ); 
     221        set_subvector ( tmp, v2c_lo, val_up ( v2c_up ) ); 
    221222        return tmp; 
    222223} 
    223224 
    224 void datalink_m2e::pushup_cond (vec &val_up, const vec &val, const vec &cond) { 
    225         bdm_assert_debug (downsize == val.length(), "Wrong val"); 
    226         bdm_assert_debug (upsize == val_up.length(), "Wrong val_up"); 
    227         set_subvector (val_up, v2v_up, val); 
    228         set_subvector (val_up, v2c_up, cond); 
     225void datalink_m2e::pushup_cond ( vec &val_up, const vec &val, const vec &cond ) { 
     226        bdm_assert_debug ( downsize == val.length(), "Wrong val" ); 
     227        bdm_assert_debug ( upsize == val_up.length(), "Wrong val_up" ); 
     228        set_subvector ( val_up, v2v_up, val ); 
     229        set_subvector ( val_up, v2c_up, cond ); 
    229230} 
    230231 
     
    233234        for ( int i = 0; i < rv.len ; i++ ) { 
    234235                id = rv.ids ( i ); 
    235                 os << id << "(" << RV_SIZES ( id ) << ")" <<  // id(size)= 
    236                 "=" << RV_NAMES ( id )  << "_{"  << rv.times ( i ) << "}; "; //name_{time} 
     236                os << id << "(" << RV::SIZES ( id ) << ")" <<  // id(size)= 
     237                "=" << RV::NAMES ( id )  << "_{"  << rv.times ( i ) << "}; "; //name_{time} 
    237238        } 
    238239        return os; 
     
    364365        shared_ptr<epdf> e ( UI::build<epdf> ( set, "epdf", UI::compulsory ) ); 
    365366        iepdf = e; 
    366         set_ep(iepdf.get()); 
     367        set_ep ( iepdf.get() ); 
    367368} 
    368369 
    369370RV get_composite_rv ( const Array<shared_ptr<mpdf> > &mpdfs, 
    370                       bool checkoverlap ) { 
     371                      bool checkoverlap ) { 
    371372        RV rv; //empty rv 
    372373        bool rvaddok; 
  • library/bdm/base/bdmbase.h

    r598 r600  
    2525using namespace std; 
    2626 
    27 namespace bdm 
    28 { 
    29  
    30 typedef std::map<string, int> RVmap; 
    31 //! Internal global variable storing sizes of RVs 
    32 extern ivec RV_SIZES; 
    33 //! Internal global variable storing names of RVs 
    34 extern Array<string> RV_NAMES; 
    35  
     27namespace bdm { 
    3628//! Structure of RV, i.e. RVs expanded into a flat list of IDs, used for debugging. 
    37 class str 
    38 { 
    39         public: 
    40                 //! vector id ids (non-unique!) 
    41                 ivec ids; 
    42                 //! vector of times 
    43                 ivec times; 
    44                 //!Default constructor 
    45                 str (ivec ids0, ivec times0) : ids (ids0), times (times0) { 
    46                         bdm_assert_debug (times0.length() == ids0.length(), "Incompatible input"); 
    47                 }; 
     29class str { 
     30public: 
     31        //! vector id ids (non-unique!) 
     32        ivec ids; 
     33        //! vector of times 
     34        ivec times; 
     35        //!Default constructor 
     36        str ( ivec ids0, ivec times0 ) : ids ( ids0 ), times ( times0 ) { 
     37                bdm_assert_debug ( times0.length() == ids0.length(), "Incompatible input" ); 
     38        }; 
    4839}; 
    4940 
     
    6152subgraph cluster0 { 
    6253node [shape=record]; 
    63 label = "RV_MAP \n std::map<string,int>"; 
     54label = "MAP \n std::map<string,int>"; 
    6455map [label="{{\"a\"| \"b\" | \"c\"} | {<3> 3 |<1> 1|<2> 2}}"]; 
    6556color = "white" 
     
    6758subgraph cluster1{ 
    6859node [shape=record]; 
    69 label = "RV_NAMES"; 
     60label = "NAMES"; 
    7061names [label="{<1> \"b\" | <2> \"c\" | <3>\"a\" }"]; 
    7162color = "white" 
     
    7364subgraph cluster2{ 
    7465node [shape=record]; 
    75 label = "RV_SIZES"; 
     66label = "SIZES"; 
    7667labelloc = b; 
    7768sizes [label="{<1>1 |<2> 4 |<3> 1}"]; 
     
    8677*/ 
    8778 
    88 class RV : public root 
    89 { 
    90         protected: 
    91                 //! size of the data vector 
    92                 int dsize; 
    93                 //! number of individual rvs 
    94                 int len; 
    95                 //! Vector of unique IDs 
    96                 ivec ids; 
    97                 //! Vector of shifts from current time 
    98                 ivec times; 
    99  
    100         private: 
    101           enum enum_dummy {dummy}; 
    102                 //! auxiliary function used in constructor 
    103                 void init (const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times); 
    104                 int init (const string &name, int size); 
    105                 //! Private constructor from IDs, potentially dangerous since all ids must be valid! 
    106                 //! dummy is there to prevent confusion with RV(" string"); 
    107                 explicit RV (const ivec &ids0, enum_dummy dum): dsize(0), len(ids0.length()), ids(ids0), times(zeros_i(ids0.length())) { 
    108                   dsize = countsize(); 
    109                 } 
    110         public: 
    111                 //! \name Constructors 
    112                 //!@{ 
    113  
    114                 //! Full constructor 
    115                 RV (const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times) { 
    116                         init (in_names, in_sizes, in_times); 
    117                 } 
    118  
    119                 //! Constructor with times=0 
    120                 RV (const Array<std::string> &in_names, const ivec &in_sizes) { 
    121                         init (in_names, in_sizes, zeros_i (in_names.length())); 
    122                 } 
    123  
    124                 //! Constructor with sizes=1, times=0 
    125                 RV (const Array<std::string> &in_names) { 
    126                         init (in_names, ones_i (in_names.length()), zeros_i (in_names.length())); 
    127                 } 
    128  
    129                 //! Constructor of empty RV 
    130                 RV() : dsize (0), len (0), ids (0), times (0) {} 
    131  
    132                 //! Constructor of a single RV with given id 
    133                 RV (string name, int sz, int tm = 0); 
    134  
    135                 // compiler-generated copy constructor is used 
    136                 //!@} 
    137  
    138                 //! \name Access functions 
    139                 //!@{ 
    140  
    141                 //! State output, e.g. for debugging. 
    142                 friend std::ostream &operator<< (std::ostream &os, const RV &rv); 
    143  
    144                 int _dsize() const { 
    145                         return dsize; 
    146                 } 
    147                  
    148                 //! access function 
    149                 const ivec& _ids() const { return ids;} 
    150  
    151                 //! Recount size of the corresponding data vector 
    152                 int countsize() const; 
    153                 ivec cumsizes() const; 
    154                 int length() const { 
    155                         return len; 
    156                 } 
    157                 int id (int at) const { 
    158                         return ids (at); 
    159                 } 
    160                 int size (int at) const { 
    161                         return RV_SIZES (ids (at)); 
    162                 } 
    163                 int time (int at) const { 
    164                         return times (at); 
    165                 } 
    166                 std::string name (int at) const { 
    167                         return RV_NAMES (ids (at)); 
    168                 } 
    169                 void set_time (int at, int time0) { 
    170                         times (at) = time0; 
    171                 } 
    172                 //!@} 
    173  
    174                 //! \name Algebra on Random Variables 
    175                 //!@{ 
    176  
    177                 //! Find indices of self in another rv, \return ivec of the same size as self. 
    178                 ivec findself (const RV &rv2) const; 
    179                 //! Find indices of self in another rv, ignore time, \return ivec of the same size as self. 
    180                 ivec findself_ids (const RV &rv2) const; 
    181                 //! Compare if \c rv2 is identical to this \c RV 
    182                 bool equal (const RV &rv2) const; 
    183                 //! Add (concat) another variable to the current one, \return true if all rv2 were added, false if rv2 is in conflict 
    184                 bool add (const RV &rv2); 
    185                 //! Subtract  another variable from the current one 
    186                 RV subt (const RV &rv2) const; 
    187                 //! Select only variables at indices ind 
    188                 RV subselect (const ivec &ind) const; 
    189  
    190                 //! Select only variables at indices ind 
    191                 RV operator() (const ivec &ind) const { 
    192                         return subselect (ind); 
    193                 } 
    194  
    195                 //! Select from data vector starting at di1 to di2 
    196                 RV operator() (int di1, int di2) const; 
    197  
    198                 //! Shift \c time by delta. 
    199                 void t (int delta); 
    200                 //!@} 
    201  
    202                 //! @{ \name Auxiliary functions 
    203                 //! returns rvs with time set to 0 and removed duplicates 
    204                 RV remove_time() const {  return RV(unique(ids), dummy);        } 
    205                 //! return rvs with expanded delayes and sorted in the order of: \f$ [ rv_{0}, rv_{-1}, rv_{ 
    206                 RV expand_delayes() const { 
    207                   RV rvt=this->remove_time(); //rv at t=0 
    208                   RV tmp = rvt; 
    209                   int td = mint(); 
    210                   for ( int i = -1; i >= td; i-- ) { 
     79class RV : public root { 
     80private: 
     81        typedef std::map<string, int> str2int_map; 
     82 
     83        //! Internal global variable storing sizes of RVs 
     84        static ivec SIZES; 
     85        //! Internal global variable storing names of RVs 
     86        static Array<string> NAMES; 
     87 
     88        //! TODO 
     89        const static int BUFFER_STEP; 
     90 
     91        //! TODO 
     92        static str2int_map MAP; 
     93 
     94public: 
     95 
     96protected: 
     97        //! size of the data vector 
     98        int dsize; 
     99        //! number of individual rvs 
     100        int len; 
     101        //! Vector of unique IDs 
     102        ivec ids; 
     103        //! Vector of shifts from current time 
     104        ivec times; 
     105 
     106private: 
     107        enum enum_dummy {dummy}; 
     108 
     109        //! auxiliary function used in constructor 
     110        void init ( const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times ); 
     111        int init ( const string &name, int size ); 
     112        //! Private constructor from IDs, potentially dangerous since all ids must be valid! 
     113        //! dummy is there to prevent confusion with RV(" string"); 
     114        explicit RV ( const ivec &ids0, enum_dummy dum ) : dsize ( 0 ), len ( ids0.length() ), ids ( ids0 ), times ( zeros_i ( ids0.length() ) ) { 
     115                dsize = countsize(); 
     116        } 
     117public: 
     118        //! \name Constructors 
     119        //!@{ 
     120 
     121        //! Full constructor 
     122        RV ( const Array<std::string> &in_names, const ivec &in_sizes, const ivec &in_times ) { 
     123                init ( in_names, in_sizes, in_times ); 
     124        } 
     125 
     126        //! Constructor with times=0 
     127        RV ( const Array<std::string> &in_names, const ivec &in_sizes ) { 
     128                init ( in_names, in_sizes, zeros_i ( in_names.length() ) ); 
     129        } 
     130 
     131        //! Constructor with sizes=1, times=0 
     132        RV ( const Array<std::string> &in_names ) { 
     133                init ( in_names, ones_i ( in_names.length() ), zeros_i ( in_names.length() ) ); 
     134        } 
     135 
     136        //! Constructor of empty RV 
     137        RV() : dsize ( 0 ), len ( 0 ), ids ( 0 ), times ( 0 ) {} 
     138 
     139        //! Constructor of a single RV with given id 
     140        RV ( string name, int sz, int tm = 0 ); 
     141 
     142        // compiler-generated copy constructor is used 
     143        //!@} 
     144 
     145        //! \name Access functions 
     146        //!@{ 
     147 
     148        //! State output, e.g. for debugging. 
     149        friend std::ostream &operator<< ( std::ostream &os, const RV &rv ); 
     150 
     151        int _dsize() const { 
     152                return dsize; 
     153        } 
     154 
     155        //! access function 
     156        const ivec& _ids() const { 
     157                return ids; 
     158        } 
     159 
     160        //! Recount size of the corresponding data vector 
     161        int countsize() const; 
     162        ivec cumsizes() const; 
     163        int length() const { 
     164                return len; 
     165        } 
     166        int id ( int at ) const { 
     167                return ids ( at ); 
     168        } 
     169        int size ( int at ) const { 
     170                return SIZES ( ids ( at ) ); 
     171        } 
     172        int time ( int at ) const { 
     173                return times ( at ); 
     174        } 
     175        std::string name ( int at ) const { 
     176                return NAMES ( ids ( at ) ); 
     177        } 
     178        void set_time ( int at, int time0 ) { 
     179                times ( at ) = time0; 
     180        } 
     181        //!@} 
     182 
     183        //! \name Algebra on Random Variables 
     184        //!@{ 
     185 
     186        //! Find indices of self in another rv, \return ivec of the same size as self. 
     187        ivec findself ( const RV &rv2 ) const; 
     188        //! Find indices of self in another rv, ignore time, \return ivec of the same size as self. 
     189        ivec findself_ids ( const RV &rv2 ) const; 
     190        //! Compare if \c rv2 is identical to this \c RV 
     191        bool equal ( const RV &rv2 ) const; 
     192        //! Add (concat) another variable to the current one, \return true if all rv2 were added, false if rv2 is in conflict 
     193        bool add ( const RV &rv2 ); 
     194        //! Subtract  another variable from the current one 
     195        RV subt ( const RV &rv2 ) const; 
     196        //! Select only variables at indices ind 
     197        RV subselect ( const ivec &ind ) const; 
     198 
     199        //! Select only variables at indices ind 
     200        RV operator() ( const ivec &ind ) const { 
     201                return subselect ( ind ); 
     202        } 
     203 
     204        //! Select from data vector starting at di1 to di2 
     205        RV operator() ( int di1, int di2 ) const; 
     206 
     207        //! Shift \c time by delta. 
     208        void t ( int delta ); 
     209        //!@} 
     210 
     211        //! @{ \name Auxiliary functions 
     212        //! returns rvs with time set to 0 and removed duplicates 
     213        RV remove_time() const { 
     214                return RV ( unique ( ids ), dummy ); 
     215        } 
     216        //! return rvs with expanded delayes and sorted in the order of: \f$ [ rv_{0}, rv_{-1}, rv_{ 
     217        RV expand_delayes() const { 
     218                RV rvt = this->remove_time(); //rv at t=0 
     219                RV tmp = rvt; 
     220                int td = mint(); 
     221                for ( int i = -1; i >= td; i-- ) { 
    211222                        rvt.t ( -1 ); 
    212223                        tmp.add ( rvt ); //shift u1 
    213                   } 
    214                   return tmp; 
    215224                } 
    216                 //!@} 
    217                  
    218                 //!\name Relation to vectors 
    219                 //!@{ 
    220  
    221                 //! generate \c str from rv, by expanding sizes  
    222                 str tostr() const; 
    223                 //! when this rv is a part of bigger rv, this function returns indices of self in the data vector of the bigger crv. 
    224                 //! Then, data can be copied via: data_of_this = cdata(ind); 
    225                 ivec dataind (const RV &crv) const; 
    226                 //! same as dataind but this time crv should not be complete supperset of rv. 
    227                 ivec dataind_part (const RV &crv) const; 
    228                 //! generate mutual indices when copying data between self and crv. 
    229                 //! Data are copied via: data_of_this(selfi) = data_of_rv2(rv2i) 
    230                 void dataind (const RV &rv2, ivec &selfi, ivec &rv2i) const; 
    231                 //! Minimum time-offset 
    232                 int mint() const { 
    233                         return min (times); 
     225                return tmp; 
     226        } 
     227        //!@} 
     228 
     229        //!\name Relation to vectors 
     230        //!@{ 
     231 
     232        //! generate \c str from rv, by expanding sizes 
     233        str tostr() const; 
     234        //! when this rv is a part of bigger rv, this function returns indices of self in the data vector of the bigger crv. 
     235        //! Then, data can be copied via: data_of_this = cdata(ind); 
     236        ivec dataind ( const RV &crv ) const; 
     237        //! same as dataind but this time crv should not be complete supperset of rv. 
     238        ivec dataind_part ( const RV &crv ) const; 
     239        //! generate mutual indices when copying data between self and crv. 
     240        //! Data are copied via: data_of_this(selfi) = data_of_rv2(rv2i) 
     241        void dataind ( const RV &rv2, ivec &selfi, ivec &rv2i ) const; 
     242        //! Minimum time-offset 
     243        int mint() const { 
     244                return min ( times ); 
     245        } 
     246        //!@} 
     247 
     248        /*! \brief UI for class RV (description of data vectors) 
     249 
     250        \code 
     251        rv = { 
     252            class = "RV"; // class name 
     253          // UNIQUE IDENTIFIER same names = same variable 
     254          names = ( "a", "b", "c", ...);   // which will be used e.g. in loggers 
     255 
     256          //optional arguments 
     257          sizes = [1, 2, 3, ...];         // (optional) default = ones() 
     258          times = [-1, -2, 0, ...];       // time shifts with respect to current time (optional) default = zeros() 
     259        } 
     260        \endcode 
     261        */ 
     262        void from_setting ( const Setting &set ); 
     263 
     264        // TODO dodelat void to_setting( Setting &set ) const; 
     265 
     266        //! Invalidate all named RVs. Use before initializing any RV instances, with care... 
     267        static void clear_all(); 
     268}; 
     269UIREGISTER ( RV ); 
     270SHAREDPTR ( RV ); 
     271 
     272//! Concat two random variables 
     273RV concat ( const RV &rv1, const RV &rv2 ); 
     274 
     275//! Class representing function \f$f(x)\f$ of variable \f$x\f$ represented by \c rv 
     276class fnc : public root { 
     277protected: 
     278        //! Length of the output vector 
     279        int dimy; 
     280public: 
     281        //!default constructor 
     282        fnc() {}; 
     283        //! function evaluates numerical value of \f$f(x)\f$ at \f$x=\f$ \c cond 
     284        virtual vec eval ( const vec &cond ) { 
     285                return vec ( 0 ); 
     286        }; 
     287 
     288        //! function substitutes given value into an appropriate position 
     289        virtual void condition ( const vec &val ) {}; 
     290 
     291        //! access function 
     292        int dimension() const { 
     293                return dimy; 
     294        } 
     295}; 
     296 
     297class mpdf; 
     298 
     299//! Probability density function with numerical statistics, e.g. posterior density. 
     300 
     301class epdf : public root { 
     302protected: 
     303        //! dimension of the random variable 
     304        int dim; 
     305        //! Description of the random variable 
     306        RV rv; 
     307 
     308public: 
     309        /*! \name Constructors 
     310         Construction of each epdf should support two types of constructors: 
     311        \li empty constructor, 
     312        \li copy constructor, 
     313 
     314        The following constructors should be supported for convenience: 
     315        \li constructor followed by calling \c set_parameters() 
     316        \li constructor accepting random variables calling \c set_rv() 
     317 
     318         All internal data structures are constructed as empty. Their values (including sizes) will be set by method \c set_parameters(). This way references can be initialized in constructors. 
     319        @{*/ 
     320        epdf() : dim ( 0 ), rv() {}; 
     321        epdf ( const epdf &e ) : dim ( e.dim ), rv ( e.rv ) {}; 
     322        epdf ( const RV &rv0 ) : dim ( rv0._dsize() ) { 
     323                set_rv ( rv0 ); 
     324        }; 
     325        void set_parameters ( int dim0 ) { 
     326                dim = dim0; 
     327        } 
     328        //!@} 
     329 
     330        //! \name Matematical Operations 
     331        //!@{ 
     332 
     333        //! Returns a sample, \f$ x \f$ from density \f$ f_x()\f$ 
     334        virtual vec sample() const { 
     335                bdm_error ( "not implemented" ); 
     336                return vec(); 
     337        } 
     338 
     339        //! Returns N samples, \f$ [x_1 , x_2 , \ldots \ \f$  from density \f$ f_x(rv)\f$ 
     340        virtual mat sample_m ( int N ) const; 
     341 
     342        //! Compute log-probability of argument \c val 
     343        //! In case the argument is out of suport return -Infinity 
     344        virtual double evallog ( const vec &val ) const { 
     345                bdm_error ( "not implemented" ); 
     346                return 0.0; 
     347        } 
     348 
     349        //! Compute log-probability of multiple values argument \c val 
     350        virtual vec evallog_m ( const mat &Val ) const; 
     351 
     352        //! Compute log-probability of multiple values argument \c val 
     353        virtual vec evallog_m ( const Array<vec> &Avec ) const; 
     354 
     355        //! Return conditional density on the given RV, the remaining rvs will be in conditioning 
     356        virtual shared_ptr<mpdf> condition ( const RV &rv ) const; 
     357 
     358        //! Return marginal density on the given RV, the remainig rvs are intergrated out 
     359        virtual shared_ptr<epdf> marginal ( const RV &rv ) const; 
     360 
     361        //! return expected value 
     362        virtual vec mean() const { 
     363                bdm_error ( "not implemneted" ); 
     364                return vec(); 
     365        } 
     366 
     367        //! return expected variance (not covariance!) 
     368        virtual vec variance() const { 
     369                bdm_error ( "not implemneted" ); 
     370                return vec(); 
     371        } 
     372 
     373        //! Lower and upper bounds of \c percentage % quantile, returns mean-2*sigma as default 
     374        virtual void qbounds ( vec &lb, vec &ub, double percentage = 0.95 ) const { 
     375                vec mea = mean(); 
     376                vec std = sqrt ( variance() ); 
     377                lb = mea - 2 * std; 
     378                ub = mea + 2 * std; 
     379        }; 
     380        //!@} 
     381 
     382        //! \name Connection to other classes 
     383        //! Description of the random quantity via attribute \c rv is optional. 
     384        //! For operations such as sampling \c rv does not need to be set. However, for \c marginalization 
     385        //! and \c conditioning \c rv has to be set. NB: 
     386        //! @{ 
     387 
     388        //!Name its rv 
     389        void set_rv ( const RV &rv0 ) { 
     390                rv = rv0; 
     391        } 
     392 
     393        //! True if rv is assigned 
     394        bool isnamed() const { 
     395                bool b = ( dim == rv._dsize() ); 
     396                return b; 
     397        } 
     398 
     399        //! Return name (fails when isnamed is false) 
     400        const RV& _rv() const { 
     401                bdm_assert_debug ( isnamed(), "" ); 
     402                return rv; 
     403        } 
     404        //!@} 
     405 
     406        //! \name Access to attributes 
     407        //! @{ 
     408 
     409        //! Size of the random variable 
     410        int dimension() const { 
     411                return dim; 
     412        } 
     413        //! Load from structure with elements: 
     414        //!  \code 
     415        //! { rv = {class="RV", names=(...),}; // RV describing meaning of random variable 
     416        //!   // elements of offsprings 
     417        //! } 
     418        //! \endcode 
     419        //!@} 
     420        void from_setting ( const Setting &set ) { 
     421                shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional ); 
     422                if ( r ) { 
     423                        set_rv ( *r ); 
    234424                } 
    235                 //!@} 
    236  
    237                 /*! \brief UI for class RV (description of data vectors) 
    238  
    239                 \code 
    240                 rv = { 
    241                   class = "RV"; // class name 
    242                   // UNIQUE IDENTIFIER same names = same variable 
    243                   names = ( "a", "b", "c", ...);   // which will be used e.g. in loggers 
    244  
    245                   //optional arguments 
    246                   sizes = [1, 2, 3, ...];         // (optional) default = ones() 
    247                   times = [-1, -2, 0, ...];       // time shifts with respect to current time (optional) default = zeros() 
     425        } 
     426 
     427}; 
     428SHAREDPTR ( epdf ); 
     429 
     430//! 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. 
     431class mpdf : public root { 
     432protected: 
     433        //!dimension of the condition 
     434        int dimc; 
     435        //! random variable in condition 
     436        RV rvc; 
     437private: 
     438        //! internal epdf, used only as cache to avoid virtual calls of \c _rv() and \c _dimension() 
     439        epdf* ep; 
     440 
     441protected: 
     442        //! set internal pointer \c ep to point to given \c iepdf 
     443        void set_ep ( epdf &iepdf ) { 
     444                ep = &iepdf; 
     445        } 
     446        //! set internal pointer \c ep to point to given \c iepdf 
     447        void set_ep ( epdf *iepdfp ) { 
     448                ep = iepdfp; 
     449        } 
     450 
     451public: 
     452        //! \name Constructors 
     453        //! @{ 
     454 
     455        mpdf() : dimc ( 0 ), rvc(), ep ( NULL ) { } 
     456 
     457        mpdf ( const mpdf &m ) : dimc ( m.dimc ), rvc ( m.rvc ), ep ( m.ep ) { } 
     458        //!@} 
     459 
     460        //! \name Matematical operations 
     461        //!@{ 
     462 
     463        //! 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 
     464        virtual vec samplecond ( const vec &cond ) { 
     465                bdm_error ( "Not implemented" ); 
     466                return vec(); 
     467        } 
     468 
     469        //! 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 
     470        virtual mat samplecond_m ( const vec &cond, int N ); 
     471 
     472        //! Shortcut for conditioning and evaluation of the internal epdf. In some cases,  this operation can be implemented efficiently. 
     473        virtual double evallogcond ( const vec &dt, const vec &cond ) { 
     474                bdm_error ( "Not implemented" ); 
     475                return 0.0; 
     476        } 
     477 
     478        //! Matrix version of evallogcond 
     479        virtual vec evallogcond_m ( const mat &Dt, const vec &cond ) { 
     480                vec v ( Dt.cols() ); 
     481                for ( int i = 0; i < Dt.cols(); i++ ) { 
     482                        v ( i ) = evallogcond ( Dt.get_col ( i ), cond ); 
    248483                } 
    249                 \endcode 
    250                 */ 
    251                 void from_setting (const Setting &set); 
    252  
    253                 // TODO dodelat void to_setting( Setting &set ) const; 
    254                 //! Invalidate all named RVs. Use before initializing any RV instances, with care... 
    255                 static void clear_all(); 
    256 }; 
    257 UIREGISTER (RV); 
    258 SHAREDPTR (RV); 
    259  
    260 //! Concat two random variables 
    261 RV concat (const RV &rv1, const RV &rv2); 
    262  
    263 //!Default empty RV that can be used as default argument 
    264 extern RV RV0; 
    265  
    266 //! Class representing function \f$f(x)\f$ of variable \f$x\f$ represented by \c rv 
    267  
    268 class fnc : public root 
    269 { 
    270         protected: 
    271                 //! Length of the output vector 
    272                 int dimy; 
    273         public: 
    274                 //!default constructor 
    275                 fnc() {}; 
    276                 //! function evaluates numerical value of \f$f(x)\f$ at \f$x=\f$ \c cond 
    277                 virtual vec eval (const vec &cond) { 
    278                         return vec (0); 
    279                 }; 
    280  
    281                 //! function substitutes given value into an appropriate position 
    282                 virtual void condition (const vec &val) {}; 
    283  
    284                 //! access function 
    285                 int dimension() const { 
    286                         return dimy; 
    287                 } 
    288 }; 
    289  
    290 class mpdf; 
    291  
    292 //! Probability density function with numerical statistics, e.g. posterior density. 
    293  
    294 class epdf : public root 
    295 { 
    296         protected: 
    297                 //! dimension of the random variable 
    298                 int dim; 
    299                 //! Description of the random variable 
    300                 RV rv; 
    301  
    302         public: 
    303                 /*! \name Constructors 
    304                  Construction of each epdf should support two types of constructors: 
    305                 \li empty constructor, 
    306                 \li copy constructor, 
    307  
    308                 The following constructors should be supported for convenience: 
    309                 \li constructor followed by calling \c set_parameters() 
    310                 \li constructor accepting random variables calling \c set_rv() 
    311  
    312                  All internal data structures are constructed as empty. Their values (including sizes) will be set by method \c set_parameters(). This way references can be initialized in constructors. 
    313                 @{*/ 
    314                 epdf() : dim (0), rv() {}; 
    315                 epdf (const epdf &e) : dim (e.dim), rv (e.rv) {}; 
    316                 epdf (const RV &rv0) : dim (rv0._dsize()) { 
    317                         set_rv (rv0); 
    318                 }; 
    319                 void set_parameters (int dim0) { 
    320                         dim = dim0; 
    321                 } 
    322                 //!@} 
    323  
    324                 //! \name Matematical Operations 
    325                 //!@{ 
    326  
    327                 //! Returns a sample, \f$ x \f$ from density \f$ f_x()\f$ 
    328                 virtual vec sample() const { 
    329                         bdm_error ("not implemented"); 
    330                         return vec(); 
    331                 } 
    332  
    333                 //! Returns N samples, \f$ [x_1 , x_2 , \ldots \ \f$  from density \f$ f_x(rv)\f$ 
    334                 virtual mat sample_m (int N) const; 
    335  
    336                 //! Compute log-probability of argument \c val 
    337                 //! In case the argument is out of suport return -Infinity 
    338                 virtual double evallog (const vec &val) const { 
    339                         bdm_error ("not implemented"); 
    340                         return 0.0; 
    341                 } 
    342  
    343                 //! Compute log-probability of multiple values argument \c val 
    344                 virtual vec evallog_m (const mat &Val) const; 
    345  
    346                 //! Compute log-probability of multiple values argument \c val 
    347                 virtual vec evallog_m (const Array<vec> &Avec) const; 
    348  
    349                 //! Return conditional density on the given RV, the remaining rvs will be in conditioning 
    350                 virtual shared_ptr<mpdf> condition (const RV &rv) const; 
    351  
    352                 //! Return marginal density on the given RV, the remainig rvs are intergrated out 
    353                 virtual shared_ptr<epdf> marginal (const RV &rv) const; 
    354  
    355                 //! return expected value 
    356                 virtual vec mean() const { 
    357                         bdm_error ("not implemneted"); 
    358                         return vec(); 
    359                 } 
    360  
    361                 //! return expected variance (not covariance!) 
    362                 virtual vec variance() const { 
    363                         bdm_error ("not implemneted"); 
    364                         return vec(); 
    365                 } 
    366  
    367                 //! Lower and upper bounds of \c percentage % quantile, returns mean-2*sigma as default 
    368                 virtual void qbounds (vec &lb, vec &ub, double percentage = 0.95) const { 
    369                         vec mea = mean(); 
    370                         vec std = sqrt (variance()); 
    371                         lb = mea - 2 * std; 
    372                         ub = mea + 2 * std; 
    373                 }; 
    374                 //!@} 
    375  
    376                 //! \name Connection to other classes 
    377                 //! Description of the random quantity via attribute \c rv is optional. 
    378                 //! For operations such as sampling \c rv does not need to be set. However, for \c marginalization 
    379                 //! and \c conditioning \c rv has to be set. NB: 
    380                 //! @{ 
    381  
    382                 //!Name its rv 
    383                 void set_rv (const RV &rv0) { 
    384                         rv = rv0; 
    385                 } 
    386  
    387                 //! True if rv is assigned 
    388                 bool isnamed() const { 
    389                         bool b = (dim == rv._dsize()); 
    390                         return b; 
    391                 } 
    392  
    393                 //! Return name (fails when isnamed is false) 
    394                 const RV& _rv() const { 
    395                         bdm_assert_debug (isnamed(), ""); 
    396                         return rv; 
    397                 } 
    398                 //!@} 
    399  
    400                 //! \name Access to attributes 
    401                 //! @{ 
    402  
    403                 //! Size of the random variable 
    404                 int dimension() const { 
    405                         return dim; 
    406                 } 
    407                 //! Load from structure with elements: 
    408                 //!  \code 
    409                 //! { rv = {class="RV", names=(...),}; // RV describing meaning of random variable 
    410                 //!   // elements of offsprings 
    411                 //! } 
    412                 //! \endcode 
    413                 //!@} 
    414                 void from_setting (const Setting &set) { 
    415                         shared_ptr<RV> r = UI::build<RV> ( set, "rv", UI::optional ); 
    416                         if (r) { 
    417                                 set_rv (*r); 
    418                         } 
    419                 } 
    420  
    421 }; 
    422 SHAREDPTR(epdf); 
    423  
    424 //! 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. 
    425 class mpdf : public root 
    426 { 
    427         protected: 
    428                 //!dimension of the condition 
    429                 int dimc; 
    430                 //! random variable in condition 
    431                 RV rvc; 
    432         private: 
    433                 //! internal epdf, used only as cache to avoid virtual calls of \c _rv() and \c _dimension() 
    434                 epdf* ep; 
    435  
    436         protected: 
    437                 //! set internal pointer \c ep to point to given \c iepdf 
    438                 void set_ep (epdf &iepdf) { 
    439                         ep = &iepdf; 
    440                 } 
    441                 //! set internal pointer \c ep to point to given \c iepdf 
    442                 void set_ep (epdf *iepdfp) { 
    443                         ep = iepdfp; 
    444                 } 
    445  
    446         public: 
    447                 //! \name Constructors 
    448                 //! @{ 
    449  
    450                 mpdf() : dimc (0), rvc(), ep (NULL) { } 
    451  
    452                 mpdf (const mpdf &m) : dimc (m.dimc), rvc (m.rvc), ep (m.ep) { } 
    453                 //!@} 
    454  
    455                 //! \name Matematical operations 
    456                 //!@{ 
    457  
    458                 //! 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 
    459                 virtual vec samplecond (const vec &cond) { 
    460                         bdm_error ("Not implemented"); 
    461                         return vec(); 
    462                 } 
    463  
    464                 //! 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 
    465                 virtual mat samplecond_m (const vec &cond, int N); 
    466  
    467                 //! Shortcut for conditioning and evaluation of the internal epdf. In some cases,  this operation can be implemented efficiently. 
    468                 virtual double evallogcond (const vec &dt, const vec &cond) { 
    469                         bdm_error ("Not implemented"); 
    470                         return 0.0; 
    471                 } 
    472  
    473                 //! Matrix version of evallogcond 
    474                 virtual vec evallogcond_m (const mat &Dt, const vec &cond) { 
    475                         vec v(Dt.cols()); 
    476                         for(int i=0;i<Dt.cols();i++){v(i)= evallogcond(Dt.get_col(i),cond);} 
    477                         return v; 
    478                 } 
    479  
    480                 //! Array<vec> version of evallogcond 
    481                 virtual vec evallogcond_m (const Array<vec> &Dt, const vec &cond) { 
    482                         bdm_error ("Not implemented"); 
    483                         return vec(); 
    484                 } 
    485  
    486                 //! \name Access to attributes 
    487                 //! @{ 
    488  
    489                 const RV& _rv() const { 
    490                         return ep->_rv(); 
    491                 } 
    492                 const RV& _rvc() { 
    493                         return rvc; 
    494                 } 
    495  
    496                 int dimension() const { 
    497                         return ep->dimension(); 
    498                 } 
    499                 int dimensionc() { 
    500                         return dimc; 
    501                 } 
    502  
    503                 //! Load from structure with elements: 
    504                 //!  \code 
    505                 //! { class = "mpdf_offspring", 
    506                 //!   rv = {class="RV", names=(...),}; // RV describing meaning of random variable 
    507                 //!   rvc= {class="RV", names=(...),}; // RV describing meaning of random variable in condition 
    508                 //!   // elements of offsprings 
    509                 //! } 
    510                 //! \endcode 
    511                 //!@} 
    512                 void from_setting (const Setting &set); 
    513                 //!@} 
    514  
    515                 //! \name Connection to other objects 
    516                 //!@{ 
    517                 void set_rvc (const RV &rvc0) { 
    518                         rvc = rvc0; 
    519                 } 
    520                 void set_rv (const RV &rv0) { 
    521                         ep->set_rv (rv0); 
    522                 } 
    523                 bool isnamed() { 
    524                         return (ep->isnamed()) && (dimc == rvc._dsize()); 
    525                 } 
    526                 //!@} 
    527 }; 
    528 SHAREDPTR(mpdf); 
     484                return v; 
     485        } 
     486 
     487        //! Array<vec> version of evallogcond 
     488        virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ) { 
     489                bdm_error ( "Not implemented" ); 
     490                return vec(); 
     491        } 
     492 
     493        //! \name Access to attributes 
     494        //! @{ 
     495 
     496        const RV& _rv() const { 
     497                return ep->_rv(); 
     498        } 
     499        const RV& _rvc() { 
     500                return rvc; 
     501        } 
     502 
     503        int dimension() const { 
     504                return ep->dimension(); 
     505        } 
     506        int dimensionc() { 
     507                return dimc; 
     508        } 
     509 
     510        //! Load from structure with elements: 
     511        //!  \code 
     512        //! { class = "mpdf_offspring", 
     513        //!   rv = {class="RV", names=(...),}; // RV describing meaning of random variable 
     514        //!   rvc= {class="RV", names=(...),}; // RV describing meaning of random variable in condition 
     515        //!   // elements of offsprings 
     516        //! } 
     517        //! \endcode 
     518        //!@} 
     519        void from_setting ( const Setting &set ); 
     520        //!@} 
     521 
     522        //! \name Connection to other objects 
     523        //!@{ 
     524        void set_rvc ( const RV &rvc0 ) { 
     525                rvc = rvc0; 
     526        } 
     527        void set_rv ( const RV &rv0 ) { 
     528                ep->set_rv ( rv0 ); 
     529        } 
     530        bool isnamed() { 
     531                return ( ep->isnamed() ) && ( dimc == rvc._dsize() ); 
     532        } 
     533        //!@} 
     534}; 
     535SHAREDPTR ( mpdf ); 
    529536 
    530537//! Mpdf with internal epdf that is modified by function \c condition 
    531538template <class EPDF> 
    532 class mpdf_internal: public mpdf 
    533 { 
    534         protected : 
    535                 //! Internal epdf used for sampling 
    536                 EPDF iepdf; 
    537         public: 
    538                 //! constructor 
    539                 mpdf_internal() : mpdf(), iepdf() {set_ep (iepdf);} 
    540  
    541                 //! Update \c iepdf so that it represents this mpdf conditioned on \c rvc = cond 
    542                 //! This function provides convenient reimplementation in offsprings 
    543                 virtual void condition (const vec &cond) { 
    544                         bdm_error ("Not implemented"); 
    545                 } 
    546  
    547                 //!access function to iepdf 
    548                 EPDF& e() {return iepdf;} 
    549  
    550                 //! Reimplements samplecond using \c condition() 
    551                 vec samplecond (const vec &cond); 
    552                 //! Reimplements evallogcond using \c condition() 
    553                 double evallogcond (const vec &val, const vec &cond); 
    554                 //! Efficient version of evallogcond for matrices 
    555                 virtual vec evallogcond_m (const mat &Dt, const vec &cond); 
    556                 //! Efficient version of evallogcond for Array<vec> 
    557                 virtual vec evallogcond_m (const Array<vec> &Dt, const vec &cond); 
    558                 //! Efficient version of samplecond 
    559                 virtual mat samplecond_m (const vec &cond, int N); 
     539class mpdf_internal: public mpdf { 
     540protected : 
     541        //! Internal epdf used for sampling 
     542        EPDF iepdf; 
     543public: 
     544        //! constructor 
     545        mpdf_internal() : mpdf(), iepdf() { 
     546                set_ep ( iepdf ); 
     547        } 
     548 
     549        //! Update \c iepdf so that it represents this mpdf conditioned on \c rvc = cond 
     550        //! This function provides convenient reimplementation in offsprings 
     551        virtual void condition ( const vec &cond ) { 
     552                bdm_error ( "Not implemented" ); 
     553        } 
     554 
     555        //!access function to iepdf 
     556        EPDF& e() { 
     557                return iepdf; 
     558        } 
     559 
     560        //! Reimplements samplecond using \c condition() 
     561        vec samplecond ( const vec &cond ); 
     562        //! Reimplements evallogcond using \c condition() 
     563        double evallogcond ( const vec &val, const vec &cond ); 
     564        //! Efficient version of evallogcond for matrices 
     565        virtual vec evallogcond_m ( const mat &Dt, const vec &cond ); 
     566        //! Efficient version of evallogcond for Array<vec> 
     567        virtual vec evallogcond_m ( const Array<vec> &Dt, const vec &cond ); 
     568        //! Efficient version of samplecond 
     569        virtual mat samplecond_m ( const vec &cond, int N ); 
    560570}; 
    561571 
     
    585595 
    586596*/ 
    587 class datalink 
    588 { 
    589         protected: 
    590                 //! Remember how long val should be 
    591                 int downsize; 
    592  
    593                 //! Remember how long val of "Up" should be 
    594                 int upsize; 
    595  
    596                 //! val-to-val link, indices of the upper val 
    597                 ivec v2v_up; 
    598                  
    599         public: 
    600                 //! Constructor 
    601                 datalink() : downsize (0), upsize (0) { } 
    602  
    603                 //! Convenience constructor 
    604                 datalink (const RV &rv, const RV &rv_up) { 
    605                         set_connection (rv, rv_up); 
    606                 } 
    607  
    608                 //! set connection, rv must be fully present in rv_up 
    609                 void set_connection (const RV &rv, const RV &rv_up); 
    610  
    611                 //! set connection using indices 
    612                 void set_connection (int ds, int us, const ivec &upind); 
    613  
    614                 //! Get val for myself from val of "Up" 
    615                 vec pushdown (const vec &val_up) { 
    616                   vec tmp(downsize); 
    617                   filldown(val_up, tmp); 
    618                         return tmp; 
    619                 } 
    620                 //! Get val for vector val_down from val of "Up" 
    621                 void filldown (const vec &val_up, vec &val_down) { 
    622                         bdm_assert_debug (upsize == val_up.length(), "Wrong val_up"); 
    623                         val_down=val_up(v2v_up); 
    624                 } 
    625  
    626                 //! Fill val of "Up" by my pieces 
    627                 void pushup (vec &val_up, const vec &val) { 
    628                         bdm_assert_debug (downsize == val.length(), "Wrong val"); 
    629                         bdm_assert_debug (upsize == val_up.length(), "Wrong val_up"); 
    630                         set_subvector (val_up, v2v_up, val); 
    631                 } 
    632                 //! access functions 
    633                 int _upsize(){return upsize;} 
    634                 //! access functions 
    635                 int _downsize(){return downsize;} 
     597class datalink { 
     598protected: 
     599        //! Remember how long val should be 
     600        int downsize; 
     601 
     602        //! Remember how long val of "Up" should be 
     603        int upsize; 
     604 
     605        //! val-to-val link, indices of the upper val 
     606        ivec v2v_up; 
     607 
     608public: 
     609        //! Constructor 
     610        datalink() : downsize ( 0 ), upsize ( 0 ) { } 
     611 
     612        //! Convenience constructor 
     613        datalink ( const RV &rv, const RV &rv_up ) { 
     614                set_connection ( rv, rv_up ); 
     615        } 
     616 
     617        //! set connection, rv must be fully present in rv_up 
     618        void set_connection ( const RV &rv, const RV &rv_up ); 
     619 
     620        //! set connection using indices 
     621        void set_connection ( int ds, int us, const ivec &upind ); 
     622 
     623        //! Get val for myself from val of "Up" 
     624        vec pushdown ( const vec &val_up ) { 
     625                vec tmp ( downsize ); 
     626                filldown ( val_up, tmp ); 
     627                return tmp; 
     628        } 
     629        //! Get val for vector val_down from val of "Up" 
     630        void filldown ( const vec &val_up, vec &val_down ) { 
     631                bdm_assert_debug ( upsize == val_up.length(), "Wrong val_up" ); 
     632                val_down = val_up ( v2v_up ); 
     633        } 
     634        //! Fill val of "Up" by my pieces 
     635        void pushup ( vec &val_up, const vec &val ) { 
     636                bdm_assert_debug ( downsize == val.length(), "Wrong val" ); 
     637                bdm_assert_debug ( upsize == val_up.length(), "Wrong val_up" ); 
     638                set_subvector ( val_up, v2v_up, val ); 
     639        } 
     640        //! access functions 
     641        int _upsize() { 
     642                return upsize; 
     643        } 
     644        //! access functions 
     645        int _downsize() { 
     646                return downsize; 
     647        } 
    636648}; 
    637649 
    638650/*! Extension of datalink to fill only part of Down 
    639651*/ 
    640 class datalink_part : public datalink{ 
    641   protected: 
     652class datalink_part : public datalink { 
     653protected: 
    642654        //! indeces of values in vector downsize 
    643655        ivec v2v_down; 
    644   public: 
    645         void set_connection(const RV &rv, const RV &rv_up); 
    646                 //! Get val for vector val_down from val of "Up" 
    647                 void filldown (const vec &val_up, vec &val_down) { 
    648                         set_subvector(val_down, v2v_down, val_up(v2v_up)); 
    649                 } 
     656public: 
     657        void set_connection ( const RV &rv, const RV &rv_up ); 
     658        //! Get val for vector val_down from val of "Up" 
     659        void filldown ( const vec &val_up, vec &val_down ) { 
     660                set_subvector ( val_down, v2v_down, val_up ( v2v_up ) ); 
     661        } 
    650662}; 
    651663 
     
    654666Up is current data, Down is their subset with possibly delayed values 
    655667*/ 
    656 class datalink_buffered: public datalink_part{ 
    657   protected: 
     668class datalink_buffered: public datalink_part { 
     669protected: 
    658670        //! History, ordered as \f$[Up_{t-1},Up_{t-2}, \ldots]\f$ 
    659671        vec history; 
    660672        //! rv of the history 
    661673        RV Hrv; 
    662         //! h2v : indeces in down  
     674        //! h2v : indeces in down 
    663675        ivec h2v_down; 
    664676        //! h2v : indeces in history 
    665677        ivec h2v_hist; 
    666   public: 
    667          
    668     datalink_buffered():datalink_part(),history(0), h2v_down(0), h2v_hist(0){}; 
     678public: 
     679 
     680        datalink_buffered() : datalink_part(), history ( 0 ), h2v_down ( 0 ), h2v_hist ( 0 ) {}; 
    669681        //! push current data to history 
    670         void step(const vec &val_up){ 
    671                 if (Hrv._dsize()>0){ 
    672                   history.shift_right ( 0, Hrv._dsize() ); 
    673                   history.set_subvector ( 0, val_up ); // write U after Drv 
     682        void step ( const vec &val_up ) { 
     683                if ( Hrv._dsize() > 0 ) { 
     684                        history.shift_right ( 0, Hrv._dsize() ); 
     685                        history.set_subvector ( 0, val_up ); // write U after Drv 
    674686                } 
    675687        } 
    676                 //! Get val for myself from val of "Up" 
    677                 vec pushdown (const vec &val_up) { 
    678                   vec tmp(downsize); 
    679                   filldown(val_up, tmp); 
    680                         return tmp; 
    681                 } 
    682          
    683         void filldown(const vec &val_up, vec &val_down){ 
    684           bdm_assert_debug(val_down.length()>=downsize, "short val_down"); 
    685          
    686           set_subvector(val_down, v2v_down, val_up(v2v_up)); // copy direct values 
    687           set_subvector(val_down, h2v_down, history(h2v_hist)); // copy delayed values 
    688         } 
    689          
    690         void set_connection(const RV &rv, const RV &rv_up){ 
    691           // create link between up and down 
    692           datalink_part::set_connection(rv, rv_up); 
    693            
    694           // create rvs of history  
    695           // we can store only what we get in rv_up - everything else is removed 
    696           ivec valid_ids=rv.findself_ids(rv_up); 
    697           RV rv_hist = rv.subselect( find(valid_ids>=0) ); // select only rvs that are in rv_up 
    698           rv_hist = rv_hist.expand_delayes(); // full regressor - including time 0 
    699           Hrv=rv_hist.subt(rv.remove_time());   // remove time 0 
    700           history = zeros(Hrv._dsize()); 
    701            
    702           Hrv.dataind(rv,h2v_hist,h2v_down); 
    703            
    704           downsize = v2v_down.length()+h2v_down.length(); 
    705           upsize = v2v_up.length(); 
     688        //! Get val for myself from val of "Up" 
     689        vec pushdown ( const vec &val_up ) { 
     690                vec tmp ( downsize ); 
     691                filldown ( val_up, tmp ); 
     692                return tmp; 
     693        } 
     694 
     695        void filldown ( const vec &val_up, vec &val_down ) { 
     696                bdm_assert_debug ( val_down.length() >= downsize, "short val_down" ); 
     697 
     698                set_subvector ( val_down, v2v_down, val_up ( v2v_up ) ); // copy direct values 
     699                set_subvector ( val_down, h2v_down, history ( h2v_hist ) ); // copy delayed values 
     700        } 
     701 
     702        void set_connection ( const RV &rv, const RV &rv_up ) { 
     703                // create link between up and down 
     704                datalink_part::set_connection ( rv, rv_up ); 
     705 
     706                // create rvs of history 
     707                // we can store only what we get in rv_up - everything else is removed 
     708                ivec valid_ids = rv.findself_ids ( rv_up ); 
     709                RV rv_hist = rv.subselect ( find ( valid_ids >= 0 ) ); // select only rvs that are in rv_up 
     710                rv_hist = rv_hist.expand_delayes(); // full regressor - including time 0 
     711                Hrv = rv_hist.subt ( rv.remove_time() );   // remove time 0 
     712                history = zeros ( Hrv._dsize() ); 
     713 
     714                Hrv.dataind ( rv, h2v_hist, h2v_down ); 
     715 
     716                downsize = v2v_down.length() + h2v_down.length(); 
     717                upsize = v2v_up.length(); 
    706718        } 
    707719}; 
    708720 
    709721//! buffered datalink from 2 vectors to 1 
    710 class datalink_2to1_buffered{ 
    711   protected: 
     722class datalink_2to1_buffered { 
     723protected: 
    712724        datalink_buffered dl1; 
    713725        datalink_buffered dl2; 
    714   public: 
     726public: 
    715727        //! set connection between RVs 
    716         void set_connection(const RV &rv, const RV &rv_up1, const RV &rv_up2){ 
    717           dl1.set_connection(rv,rv_up1); 
    718           dl2.set_connection(rv,rv_up2); 
    719         } 
    720         void filldown(const vec &val1, const vec &val2, vec &val_down){ 
    721           bdm_assert_debug(val_down.length()>=dl1._downsize()+dl2._downsize(), "short val_down"); 
    722           dl1.filldown(val1, val_down); 
    723           dl2.filldown(val2, val_down); 
    724         } 
    725         void step(const vec &dt, const vec &ut) {dl1.step(dt); dl2.step(ut);} 
    726 }; 
     728        void set_connection ( const RV &rv, const RV &rv_up1, const RV &rv_up2 ) { 
     729                dl1.set_connection ( rv, rv_up1 ); 
     730                dl2.set_connection ( rv, rv_up2 ); 
     731        } 
     732        void filldown ( const vec &val1, const vec &val2, vec &val_down ) { 
     733                bdm_assert_debug ( val_down.length() >= dl1._downsize() + dl2._downsize(), "short val_down" ); 
     734                dl1.filldown ( val1, val_down ); 
     735                dl2.filldown ( val2, val_down ); 
     736        } 
     737        void step ( const vec &dt, const vec &ut ) { 
     738                dl1.step ( dt ); 
     739                dl2.step ( ut ); 
     740        } 
     741}; 
     742 
     743 
    727744 
    728745//! Data link with a condition. 
    729 class datalink_m2e: public datalink 
    730 { 
    731         protected: 
    732                 //! Remember how long cond should be 
    733                 int condsize; 
    734  
    735                 //!upper_val-to-local_cond link, indices of the upper val 
    736                 ivec v2c_up; 
    737  
    738                 //!upper_val-to-local_cond link, indices of the local cond 
    739                 ivec v2c_lo; 
    740  
    741         public: 
    742                 //! Constructor 
    743                 datalink_m2e() : condsize (0) { } 
    744  
    745                 //! Set connection between vectors 
    746                 void set_connection (const RV &rv, const RV &rvc, const RV &rv_up); 
    747  
    748                 //!Construct condition 
    749                 vec get_cond (const vec &val_up); 
    750  
    751                 //! Copy corresponding values to Up.condition 
    752                 void pushup_cond (vec &val_up, const vec &val, const vec &cond); 
     746class datalink_m2e: public datalink { 
     747protected: 
     748        //! Remember how long cond should be 
     749        int condsize; 
     750 
     751        //!upper_val-to-local_cond link, indices of the upper val 
     752        ivec v2c_up; 
     753 
     754        //!upper_val-to-local_cond link, indices of the local cond 
     755        ivec v2c_lo; 
     756 
     757public: 
     758        //! Constructor 
     759        datalink_m2e() : condsize ( 0 ) { } 
     760 
     761        //! Set connection between vectors 
     762        void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up ); 
     763 
     764        //!Construct condition 
     765        vec get_cond ( const vec &val_up ); 
     766 
     767        //! Copy corresponding values to Up.condition 
     768        void pushup_cond ( vec &val_up, const vec &val, const vec &cond ); 
    753769}; 
    754770 
    755771//!DataLink is a connection between mpdf and its superordinate (Up) 
    756772//! This class links 
    757 class datalink_m2m: public datalink_m2e 
    758 { 
    759         protected: 
    760                 //!cond-to-cond link, indices of the upper cond 
    761                 ivec c2c_up; 
    762                 //!cond-to-cond link, indices of the local cond 
    763                 ivec c2c_lo; 
    764  
    765         public: 
    766                 //! Constructor 
    767                 datalink_m2m() {}; 
    768                 //! Set connection between the vectors 
    769                 void set_connection (const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up) { 
    770                         datalink_m2e::set_connection (rv, rvc, rv_up); 
    771                         //establish c2c connection 
    772                         rvc.dataind (rvc_up, c2c_lo, c2c_up); 
    773                         bdm_assert_debug (c2c_lo.length() + v2c_lo.length() == condsize, "cond is not fully given"); 
    774                 } 
    775  
    776                 //! Get cond for myself from val and cond of "Up" 
    777                 vec get_cond (const vec &val_up, const vec &cond_up) { 
    778                         vec tmp (condsize); 
    779                         set_subvector (tmp, v2c_lo, val_up (v2c_up)); 
    780                         set_subvector (tmp, c2c_lo, cond_up (c2c_up)); 
    781                         return tmp; 
    782                 } 
    783                 //! Fill 
     773class datalink_m2m: public datalink_m2e { 
     774protected: 
     775        //!cond-to-cond link, indices of the upper cond 
     776        ivec c2c_up; 
     777        //!cond-to-cond link, indices of the local cond 
     778        ivec c2c_lo; 
     779 
     780public: 
     781        //! Constructor 
     782        datalink_m2m() {}; 
     783        //! Set connection between the vectors 
     784        void set_connection ( const RV &rv, const RV &rvc, const RV &rv_up, const RV &rvc_up ) { 
     785                datalink_m2e::set_connection ( rv, rvc, rv_up ); 
     786                //establish c2c connection 
     787                rvc.dataind ( rvc_up, c2c_lo, c2c_up ); 
     788                bdm_assert_debug ( c2c_lo.length() + v2c_lo.length() == condsize, "cond is not fully given" ); 
     789        } 
     790 
     791        //! Get cond for myself from val and cond of "Up" 
     792        vec get_cond ( const vec &val_up, const vec &cond_up ) { 
     793                vec tmp ( condsize ); 
     794                set_subvector ( tmp, v2c_lo, val_up ( v2c_up ) ); 
     795                set_subvector ( tmp, c2c_lo, cond_up ( c2c_up ) ); 
     796                return tmp; 
     797        } 
     798        //! Fill 
    784799 
    785800}; 
     
    790805This 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. 
    791806 */ 
    792 class logger : public root 
    793 { 
    794         protected: 
    795                 //! RVs of all logged variables. 
    796                 Array<RV> entries; 
    797                 //! Names of logged quantities, e.g. names of algorithm variants 
    798                 Array<string> names; 
    799         public: 
    800                 //!Default constructor 
    801                 logger() : entries (0), names (0) {} 
    802  
    803                 //! returns an identifier which will be later needed for calling the \c logit() function 
    804                 //! For empty RV it returns -1, this entry will be ignored by \c logit(). 
    805                 virtual int add (const RV &rv, string prefix = "") { 
    806                         int id; 
    807                         if (rv._dsize() > 0) { 
    808                                 id = entries.length(); 
    809                                 names = concat (names, prefix);   // diff 
    810                                 entries.set_length (id + 1, true); 
    811                                 entries (id) = rv; 
    812                         } else { 
    813                                 id = -1; 
    814                         } 
    815                         return id; // identifier of the last entry 
     807class logger : public root { 
     808protected: 
     809        //! RVs of all logged variables. 
     810        Array<RV> entries; 
     811        //! Names of logged quantities, e.g. names of algorithm variants 
     812        Array<string> names; 
     813public: 
     814        //!Default constructor 
     815        logger() : entries ( 0 ), names ( 0 ) {} 
     816 
     817        //! returns an identifier which will be later needed for calling the \c logit() function 
     818        //! For empty RV it returns -1, this entry will be ignored by \c logit(). 
     819        virtual int add ( const RV &rv, string prefix = "" ) { 
     820                int id; 
     821                if ( rv._dsize() > 0 ) { 
     822                        id = entries.length(); 
     823                        names = concat ( names, prefix ); // diff 
     824                        entries.set_length ( id + 1, true ); 
     825                        entries ( id ) = rv; 
     826                } else { 
     827                        id = -1; 
    816828                } 
    817  
    818                 //! log this vector 
    819                 virtual void logit (int id, const vec &v) {bdm_error("Not implemented");}; 
    820                 //! log this double 
    821                 virtual void logit (int id, const double &d) {bdm_error("Not implemented");}; 
    822  
    823                 //! Shifts storage position for another time step. 
    824                 virtual void step() {bdm_error("Not implemneted");}; 
    825  
    826                 //! Finalize storing information 
    827                 virtual void finalize() {}; 
    828  
    829                 //! Initialize the storage 
    830                 virtual void init() {}; 
     829                return id; // identifier of the last entry 
     830        } 
     831 
     832        //! log this vector 
     833        virtual void logit ( int id, const vec &v ) { 
     834                bdm_error ( "Not implemented" ); 
     835        }; 
     836        //! log this double 
     837        virtual void logit ( int id, const double &d ) { 
     838                bdm_error ( "Not implemented" ); 
     839        }; 
     840 
     841        //! Shifts storage position for another time step. 
     842        virtual void step() { 
     843                bdm_error ( "Not implemneted" ); 
     844        }; 
     845 
     846        //! Finalize storing information 
     847        virtual void finalize() {}; 
     848 
     849        //! Initialize the storage 
     850        virtual void init() {}; 
    831851 
    832852}; 
     
    835855 
    836856*/ 
    837 class mepdf : public mpdf 
    838 { 
    839                 //! Internal shared pointer to epdf 
    840                 shared_ptr<epdf> iepdf; 
    841         public: 
    842                 //!Default constructor 
    843                 mepdf() { } 
    844                 //! Set internal shared pointer 
    845                 mepdf (shared_ptr<epdf> em) { 
    846                         iepdf = em; 
    847                         set_ep (*iepdf.get()); 
    848                         dimc = 0; 
    849                 } 
    850  
    851                 //! empty 
    852                 vec samplecond(const vec &cond){return iepdf->sample();} 
    853                 double evallogcond(const vec &val, const vec &cond){return iepdf->evallog(val);} 
    854  
    855                 //! Load from structure with elements: 
    856                 //!  \code 
    857                 //! { class = "mepdf", 
    858                 //!   epdf = {class="epdf_offspring",...} 
    859                 //! } 
    860                 //! \endcode 
    861                 //!@} 
    862                 void from_setting (const Setting &set); 
    863 }; 
    864 UIREGISTER (mepdf); 
    865 SHAREDPTR (mepdf); 
     857class mepdf : public mpdf { 
     858        //! Internal shared pointer to epdf 
     859        shared_ptr<epdf> iepdf; 
     860public: 
     861        //!Default constructor 
     862        mepdf() { } 
     863        //! Set internal shared pointer 
     864        mepdf ( shared_ptr<epdf> em ) { 
     865                iepdf = em; 
     866                set_ep ( *iepdf.get() ); 
     867                dimc = 0; 
     868        } 
     869 
     870        //! empty 
     871        vec samplecond ( const vec &cond ) { 
     872                return iepdf->sample(); 
     873        } 
     874        double evallogcond ( const vec &val, const vec &cond ) { 
     875                return iepdf->evallog ( val ); 
     876        } 
     877 
     878        //! Load from structure with elements: 
     879        //!  \code 
     880        //! { class = "mepdf", 
     881        //!   epdf = {class="epdf_offspring",...} 
     882        //! } 
     883        //! \endcode 
     884        //!@} 
     885        void from_setting ( const Setting &set ); 
     886}; 
     887UIREGISTER ( mepdf ); 
     888SHAREDPTR ( mepdf ); 
    866889 
    867890//! \brief Combines RVs from a list of mpdfs to a single one. 
     
    875898*/ 
    876899 
    877 class DS : public root 
    878 { 
    879         protected: 
    880                 int dtsize; 
    881                 int utsize; 
    882                 //!Description of data returned by \c getdata(). 
    883                 RV Drv; 
    884                 //!Description of data witten by by \c write(). 
    885                 RV Urv; // 
    886                 //! Remember its own index in Logger L 
    887                 int L_dt, L_ut; 
    888         public: 
    889                 //! default constructors 
    890                 DS() : Drv(), Urv() {}; 
    891  
    892                 //! Returns full vector of observed data=[output, input] 
    893                 virtual void getdata (vec &dt) { 
    894                         bdm_error ("abstract class"); 
    895                 } 
    896  
    897                 //! Returns data records at indeces. 
    898                 virtual void getdata (vec &dt, const ivec &indeces) { 
    899                         bdm_error ("abstract class"); 
    900                 } 
    901  
    902                 //! Accepts action variable and schedule it for application. 
    903                 virtual void write (vec &ut) { 
    904                         bdm_error ("abstract class"); 
    905                 } 
    906  
    907                 //! Accepts action variables at specific indeces 
    908                 virtual void write (vec &ut, const ivec &indeces) { 
    909                         bdm_error ("abstract class"); 
    910                 } 
    911  
    912                 //! Moves from \f$ t \f$ to \f$ t+1 \f$, i.e. perfroms the actions and reads response of the system. 
    913                 virtual void step() = 0; 
    914  
    915                 //! Register DS for logging into logger L 
    916                 virtual void log_add (logger &L) { 
    917                         bdm_assert_debug (dtsize == Drv._dsize(), "invalid DS: dtsize ("+ num2str(dtsize)+ ") different from Drv "+ num2str(Drv._dsize())); 
    918                         bdm_assert_debug (utsize == Urv._dsize(), "invalid DS: utsize ("+ num2str(utsize)+ ") different from Urv "+ num2str(Urv._dsize())); 
    919  
    920                         L_dt = L.add (Drv, ""); 
    921                         L_ut = L.add (Urv, ""); 
    922                 } 
    923                 //! Register DS for logging into logger L 
    924                 virtual void logit (logger &L) { 
    925                         vec tmp (Drv._dsize() + Urv._dsize()); 
    926                         getdata (tmp); 
    927                         // d is first in getdata 
    928                         L.logit (L_dt, tmp.left (Drv._dsize())); 
    929                         // u follows after d in getdata 
    930                         L.logit (L_ut, tmp.mid (Drv._dsize(), Urv._dsize())); 
    931                 } 
    932                 //!access function 
    933                 virtual const RV& _drv() const { 
    934         //              return concat (Drv, Urv);// why!!! 
    935                         return Drv;// why!!! 
    936                 } 
    937                 //!access function 
    938                 const RV& _urv() const { 
    939                         return Urv; 
    940                 } 
    941                 //! set random variables 
    942                 virtual void set_drv (const  RV &drv, const RV &urv) { 
    943                         Drv = drv; 
    944                         Urv = urv; 
    945                 } 
     900class DS : public root { 
     901protected: 
     902        int dtsize; 
     903        int utsize; 
     904        //!Description of data returned by \c getdata(). 
     905        RV Drv; 
     906        //!Description of data witten by by \c write(). 
     907        RV Urv; // 
     908        //! Remember its own index in Logger L 
     909        int L_dt, L_ut; 
     910public: 
     911        //! default constructors 
     912        DS() : Drv(), Urv() {}; 
     913 
     914        //! Returns full vector of observed data=[output, input] 
     915        virtual void getdata ( vec &dt ) { 
     916                bdm_error ( "abstract class" ); 
     917        } 
     918 
     919        //! Returns data records at indeces. 
     920        virtual void getdata ( vec &dt, const ivec &indeces ) { 
     921                bdm_error ( "abstract class" ); 
     922        } 
     923 
     924        //! Accepts action variable and schedule it for application. 
     925        virtual void write ( vec &ut ) { 
     926                bdm_error ( "abstract class" ); 
     927        } 
     928 
     929        //! Accepts action variables at specific indeces 
     930        virtual void write ( vec &ut, const ivec &indeces ) { 
     931                bdm_error ( "abstract class" ); 
     932        } 
     933 
     934        //! Moves from \f$ t \f$ to \f$ t+1 \f$, i.e. perfroms the actions and reads response of the system. 
     935        virtual void step() = 0; 
     936 
     937        //! Register DS for logging into logger L 
     938        virtual void log_add ( logger &L ) { 
     939                bdm_assert_debug ( dtsize == Drv._dsize(), "invalid DS: dtsize (" + num2str ( dtsize ) + ") different from Drv " + num2str ( Drv._dsize() ) ); 
     940                bdm_assert_debug ( utsize == Urv._dsize(), "invalid DS: utsize (" + num2str ( utsize ) + ") different from Urv " + num2str ( Urv._dsize() ) ); 
     941 
     942                L_dt = L.add ( Drv, "" ); 
     943                L_ut = L.add ( Urv, "" ); 
     944        } 
     945        //! Register DS for logging into logger L 
     946        virtual void logit ( logger &L ) { 
     947                vec tmp ( Drv._dsize() + Urv._dsize() ); 
     948                getdata ( tmp ); 
     949                // d is first in getdata 
     950                L.logit ( L_dt, tmp.left ( Drv._dsize() ) ); 
     951                // u follows after d in getdata 
     952                L.logit ( L_ut, tmp.mid ( Drv._dsize(), Urv._dsize() ) ); 
     953        } 
     954        //!access function 
     955        virtual const RV& _drv() const { 
     956                //              return concat (Drv, Urv);// why!!! 
     957                return Drv;// why!!! 
     958        } 
     959        //!access function 
     960        const RV& _urv() const { 
     961                return Urv; 
     962        } 
     963        //! set random variables 
     964        virtual void set_drv ( const  RV &drv, const RV &urv ) { 
     965                Drv = drv; 
     966                Urv = urv; 
     967        } 
    946968}; 
    947969 
     
    967989*/ 
    968990 
    969 class BM : public root 
    970 { 
    971         protected: 
    972                 //! Random variable of the data (optional) 
    973                 RV drv; 
    974                 //!Logarithm of marginalized data likelihood. 
    975                 double ll; 
    976                 //!  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. 
    977                 bool evalll; 
    978         public: 
    979                 //! \name Constructors 
    980                 //! @{ 
    981  
    982                 BM() : ll (0), evalll (true), LIDs (4), LFlags (4) { 
    983                         LIDs = -1;/*empty IDs*/ 
    984                         LFlags = 0; 
    985                         LFlags (0) = 1;  /*log only mean*/ 
     991class BM : public root { 
     992protected: 
     993        //! Random variable of the data (optional) 
     994        RV drv; 
     995        //!Logarithm of marginalized data likelihood. 
     996        double ll; 
     997        //!  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. 
     998        bool evalll; 
     999public: 
     1000        //! \name Constructors 
     1001        //! @{ 
     1002 
     1003        BM() : ll ( 0 ), evalll ( true ), LIDs ( 4 ), LFlags ( 4 ) { 
     1004                LIDs = -1;/*empty IDs*/ 
     1005                LFlags = 0; 
     1006                LFlags ( 0 ) = 1;  /*log only mean*/ 
     1007        }; 
     1008        BM ( const BM &B ) :  drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {} 
     1009        //! Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! 
     1010        //! Prototype: \code BM* _copy_() const {return new BM(*this);} \endcode 
     1011        virtual BM* _copy_() const { 
     1012                return NULL; 
     1013        }; 
     1014        //!@} 
     1015 
     1016        //! \name Mathematical operations 
     1017        //!@{ 
     1018 
     1019        /*! \brief Incremental Bayes rule 
     1020        @param dt vector of input data 
     1021        */ 
     1022        virtual void bayes ( const vec &dt ) = 0; 
     1023        //! Batch Bayes rule (columns of Dt are observations) 
     1024        virtual void bayesB ( const mat &Dt ); 
     1025        //! Evaluates predictive log-likelihood of the given data record 
     1026        //! I.e. marginal likelihood of the data with the posterior integrated out. 
     1027        virtual double logpred ( const vec &dt ) const { 
     1028                bdm_error ( "Not implemented" ); 
     1029                return 0.0; 
     1030        } 
     1031 
     1032        //! Matrix version of logpred 
     1033        vec logpred_m ( const mat &dt ) const { 
     1034                vec tmp ( dt.cols() ); 
     1035                for ( int i = 0; i < dt.cols(); i++ ) { 
     1036                        tmp ( i ) = logpred ( dt.get_col ( i ) ); 
     1037                } 
     1038                return tmp; 
     1039        } 
     1040 
     1041        //!Constructs a predictive density \f$ f(d_{t+1} |d_{t}, \ldots d_{0}) \f$ 
     1042        virtual epdf* epredictor() const { 
     1043                bdm_error ( "Not implemented" ); 
     1044                return NULL; 
     1045        }; 
     1046        //!Constructs conditional density of 1-step ahead predictor \f$ f(d_{t+1} |d_{t+h-1}, \ldots d_{t}) \f$ 
     1047        virtual mpdf* predictor() const { 
     1048                bdm_error ( "Not implemented" ); 
     1049                return NULL; 
     1050        }; 
     1051        //!@} 
     1052 
     1053        //! \name Extension to conditional BM 
     1054        //! This extension is useful e.g. in Marginalized Particle Filter (\ref bdm::MPF). 
     1055        //! Alternatively, it can be used for automated connection to DS when the condition is observed 
     1056        //!@{ 
     1057 
     1058        //! Name of extension variable 
     1059        RV rvc; 
     1060        //! access function 
     1061        const RV& _rvc() const { 
     1062                return rvc; 
     1063        } 
     1064 
     1065        //! Substitute \c val for \c rvc. 
     1066        virtual void condition ( const vec &val ) { 
     1067                bdm_error ( "Not implemented!" ); 
     1068        } 
     1069 
     1070        //!@} 
     1071 
     1072 
     1073        //! \name Access to attributes 
     1074        //!@{ 
     1075 
     1076        const RV& _drv() const { 
     1077                return drv; 
     1078        } 
     1079        void set_drv ( const RV &rv ) { 
     1080                drv = rv; 
     1081        } 
     1082        void set_rv ( const RV &rv ) { 
     1083                const_cast<epdf&> ( posterior() ).set_rv ( rv ); 
     1084        } 
     1085        double _ll() const { 
     1086                return ll; 
     1087        } 
     1088        void set_evalll ( bool evl0 ) { 
     1089                evalll = evl0; 
     1090        } 
     1091        virtual const epdf& posterior() const = 0; 
     1092        //!@} 
     1093 
     1094        //! \name Logging of results 
     1095        //!@{ 
     1096 
     1097        //! Set boolean options from a string, recognized are: "logbounds,logll" 
     1098        virtual void set_options ( const string &opt ) { 
     1099                LFlags ( 0 ) = 1; 
     1100                if ( opt.find ( "logbounds" ) != string::npos ) { 
     1101                        LFlags ( 1 ) = 1; 
     1102                        LFlags ( 2 ) = 1; 
     1103                } 
     1104                if ( opt.find ( "logll" ) != string::npos ) { 
     1105                        LFlags ( 3 ) = 1; 
     1106                } 
     1107        } 
     1108        //! IDs of storages in loggers 4:[1=mean,2=lb,3=ub,4=ll] 
     1109        ivec LIDs; 
     1110 
     1111        //! Flags for logging - same size as LIDs, each entry correspond to the same in LIDs 
     1112        ivec LFlags; 
     1113        //! Add all logged variables to a logger 
     1114        virtual void log_add ( logger &L, const string &name = "" ) { 
     1115                // internal 
     1116                RV r; 
     1117                if ( posterior().isnamed() ) { 
     1118                        r = posterior()._rv(); 
     1119                } else { 
     1120                        r = RV ( "est", posterior().dimension() ); 
    9861121                }; 
    987                 BM (const BM &B) :  drv (B.drv), ll (B.ll), evalll (B.evalll) {} 
    988                 //! Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! 
    989                 //! Prototype: \code BM* _copy_() const {return new BM(*this);} \endcode 
    990                 virtual BM* _copy_() const { 
    991                         return NULL; 
    992                 }; 
    993                 //!@} 
    994  
    995                 //! \name Mathematical operations 
    996                 //!@{ 
    997  
    998                 /*! \brief Incremental Bayes rule 
    999                 @param dt vector of input data 
    1000                 */ 
    1001                 virtual void bayes (const vec &dt) = 0; 
    1002                 //! Batch Bayes rule (columns of Dt are observations) 
    1003                 virtual void bayesB (const mat &Dt); 
    1004                 //! Evaluates predictive log-likelihood of the given data record 
    1005                 //! I.e. marginal likelihood of the data with the posterior integrated out. 
    1006                 virtual double logpred (const vec &dt) const { 
    1007                         bdm_error ("Not implemented"); 
    1008                         return 0.0; 
     1122 
     1123                // Add mean value 
     1124                if ( LFlags ( 0 ) ) LIDs ( 0 ) = L.add ( r, name + "mean_" ); 
     1125                if ( LFlags ( 1 ) ) LIDs ( 1 ) = L.add ( r, name + "lb_" ); 
     1126                if ( LFlags ( 2 ) ) LIDs ( 2 ) = L.add ( r, name + "ub_" ); 
     1127                if ( LFlags ( 3 ) ) LIDs ( 3 ) = L.add ( RV ( "ll", 1 ), name );    //TODO: "local" RV 
     1128        } 
     1129        virtual void logit ( logger &L ) { 
     1130                L.logit ( LIDs ( 0 ), posterior().mean() ); 
     1131                if ( LFlags ( 1 ) || LFlags ( 2 ) ) {  //if one of them is off, its LID==-1 and will not be stored 
     1132                        vec ub, lb; 
     1133                        posterior().qbounds ( lb, ub ); 
     1134                        L.logit ( LIDs ( 1 ), lb ); 
     1135                        L.logit ( LIDs ( 2 ), ub ); 
    10091136                } 
    1010  
    1011                 //! Matrix version of logpred 
    1012                 vec logpred_m (const mat &dt) const { 
    1013                         vec tmp (dt.cols()); 
    1014                         for (int i = 0; i < dt.cols(); i++) { 
    1015                                 tmp (i) = logpred (dt.get_col (i)); 
    1016                         } 
    1017                         return tmp; 
     1137                if ( LFlags ( 3 ) ) L.logit ( LIDs ( 3 ), ll ); 
     1138        } 
     1139        //!@} 
     1140        void from_setting ( const Setting &set ) { 
     1141                shared_ptr<RV> r = UI::build<RV> ( set, "drv", UI::optional ); 
     1142                if ( r ) { 
     1143                        set_drv ( *r ); 
    10181144                } 
    1019  
    1020                 //!Constructs a predictive density \f$ f(d_{t+1} |d_{t}, \ldots d_{0}) \f$ 
    1021                 virtual epdf* epredictor() const { 
    1022                         bdm_error ("Not implemented"); 
    1023                         return NULL; 
    1024                 }; 
    1025                 //!Constructs conditional density of 1-step ahead predictor \f$ f(d_{t+1} |d_{t+h-1}, \ldots d_{t}) \f$ 
    1026                 virtual mpdf* predictor() const { 
    1027                         bdm_error ("Not implemented"); 
    1028                         return NULL; 
    1029                 }; 
    1030                 //!@} 
    1031  
    1032                 //! \name Extension to conditional BM 
    1033                 //! This extension is useful e.g. in Marginalized Particle Filter (\ref bdm::MPF). 
    1034                 //! Alternatively, it can be used for automated connection to DS when the condition is observed 
    1035                 //!@{ 
    1036  
    1037                 //! Name of extension variable 
    1038                 RV rvc; 
    1039                 //! access function 
    1040                 const RV& _rvc() const { 
    1041                         return rvc; 
     1145                string opt; 
     1146                if ( !UI::get ( opt, set, "options", UI::optional ) ) { 
     1147                        set_options ( opt ); 
    10421148                } 
    1043  
    1044                 //! Substitute \c val for \c rvc. 
    1045                 virtual void condition (const vec &val) { 
    1046                         bdm_error ("Not implemented!"); 
    1047                 } 
    1048  
    1049                 //!@} 
    1050  
    1051  
    1052                 //! \name Access to attributes 
    1053                 //!@{ 
    1054  
    1055                 const RV& _drv() const { 
    1056                         return drv; 
    1057                 } 
    1058                 void set_drv (const RV &rv) { 
    1059                         drv = rv; 
    1060                 } 
    1061                 void set_rv (const RV &rv) { 
    1062                         const_cast<epdf&> (posterior()).set_rv (rv); 
    1063                 } 
    1064                 double _ll() const { 
    1065                         return ll; 
    1066                 } 
    1067                 void set_evalll (bool evl0) { 
    1068                         evalll = evl0; 
    1069                 } 
    1070                 virtual const epdf& posterior() const = 0; 
    1071                 //!@} 
    1072  
    1073                 //! \name Logging of results 
    1074                 //!@{ 
    1075  
    1076                 //! Set boolean options from a string, recognized are: "logbounds,logll" 
    1077                 virtual void set_options (const string &opt) { 
    1078                         LFlags (0) = 1; 
    1079                         if (opt.find ("logbounds") != string::npos) { 
    1080                                 LFlags (1) = 1; 
    1081                                 LFlags (2) = 1; 
    1082                         } 
    1083                         if (opt.find ("logll") != string::npos) { 
    1084                                 LFlags (3) = 1; 
    1085                         } 
    1086                 } 
    1087                 //! IDs of storages in loggers 4:[1=mean,2=lb,3=ub,4=ll] 
    1088                 ivec LIDs; 
    1089  
    1090                 //! Flags for logging - same size as LIDs, each entry correspond to the same in LIDs 
    1091                 ivec LFlags; 
    1092                 //! Add all logged variables to a logger 
    1093                 virtual void log_add (logger &L, const string &name = "") { 
    1094                         // internal 
    1095                         RV r; 
    1096                         if (posterior().isnamed()) { 
    1097                                 r = posterior()._rv(); 
    1098                         } else { 
    1099                                 r = RV ("est", posterior().dimension()); 
    1100                         }; 
    1101  
    1102                         // Add mean value 
    1103                         if (LFlags (0)) LIDs (0) = L.add (r, name + "mean_"); 
    1104                         if (LFlags (1)) LIDs (1) = L.add (r, name + "lb_"); 
    1105                         if (LFlags (2)) LIDs (2) = L.add (r, name + "ub_"); 
    1106                         if (LFlags (3)) LIDs (3) = L.add (RV ("ll", 1), name);              //TODO: "local" RV 
    1107                 } 
    1108                 virtual void logit (logger &L) { 
    1109                         L.logit (LIDs (0), posterior().mean()); 
    1110                         if (LFlags (1) || LFlags (2)) {        //if one of them is off, its LID==-1 and will not be stored 
    1111                                 vec ub, lb; 
    1112                                 posterior().qbounds (lb, ub); 
    1113                                 L.logit (LIDs (1), lb); 
    1114                                 L.logit (LIDs (2), ub); 
    1115                         } 
    1116                         if (LFlags (3)) L.logit (LIDs (3), ll); 
    1117                 } 
    1118                 //!@} 
    1119                 void from_setting (const Setting &set){ 
    1120                         shared_ptr<RV> r = UI::build<RV> ( set, "drv", UI::optional ); 
    1121                         if (r) { 
    1122                                 set_drv (*r); 
    1123                         } 
    1124                         string opt; 
    1125                         if(!UI::get(opt, set, "options", UI::optional)) { 
    1126                                 set_options(opt); 
    1127                         } 
    1128                 } 
     1149        } 
    11291150 
    11301151}; 
     
    11351156 
    11361157template<class EPDF> 
    1137 vec mpdf_internal<EPDF>::samplecond (const vec &cond) 
    1138 { 
    1139         condition (cond); 
     1158vec mpdf_internal<EPDF>::samplecond ( const vec &cond ) { 
     1159        condition ( cond ); 
    11401160        vec temp = iepdf.sample(); 
    11411161        return temp; 
     
    11431163 
    11441164template<class EPDF> 
    1145 mat mpdf_internal<EPDF>::samplecond_m (const vec &cond, int N) 
    1146 { 
    1147         condition (cond); 
    1148         mat temp (dimension(), N); 
    1149         vec smp (dimension()); 
    1150         for (int i = 0; i < N; i++) { 
     1165mat mpdf_internal<EPDF>::samplecond_m ( const vec &cond, int N ) { 
     1166        condition ( cond ); 
     1167        mat temp ( dimension(), N ); 
     1168        vec smp ( dimension() ); 
     1169        for ( int i = 0; i < N; i++ ) { 
    11511170                smp = iepdf.sample(); 
    1152                 temp.set_col (i, smp); 
     1171                temp.set_col ( i, smp ); 
    11531172        } 
    11541173 
     
    11571176 
    11581177template<class EPDF> 
    1159 double mpdf_internal<EPDF>::evallogcond (const vec &dt, const vec &cond) 
    1160 { 
     1178double mpdf_internal<EPDF>::evallogcond ( const vec &dt, const vec &cond ) { 
    11611179        double tmp; 
    1162         condition (cond); 
    1163         tmp = iepdf.evallog (dt); 
     1180        condition ( cond ); 
     1181        tmp = iepdf.evallog ( dt ); 
    11641182        return tmp; 
    11651183} 
    11661184 
    11671185template<class EPDF> 
    1168 vec mpdf_internal<EPDF>::evallogcond_m (const mat &Dt, const vec &cond) 
    1169 { 
    1170         condition (cond); 
    1171         return iepdf.evallog_m (Dt); 
     1186vec mpdf_internal<EPDF>::evallogcond_m ( const mat &Dt, const vec &cond ) { 
     1187        condition ( cond ); 
     1188        return iepdf.evallog_m ( Dt ); 
    11721189} 
    11731190 
    11741191template<class EPDF> 
    1175 vec mpdf_internal<EPDF>::evallogcond_m (const Array<vec> &Dt, const vec &cond) 
    1176 { 
    1177         condition (cond); 
    1178         return iepdf.evallog_m (Dt); 
     1192vec mpdf_internal<EPDF>::evallogcond_m ( const Array<vec> &Dt, const vec &cond ) { 
     1193        condition ( cond ); 
     1194        return iepdf.evallog_m ( Dt ); 
    11791195} 
    11801196 
  • library/bdm/base/user_info.h

    r565 r600  
    234234user-infos. 
    235235 
    236 See static methods 'build', 'get' and 'save'. Writing user-infos with these methods is rather  simple. The 
     236See static methods 'build', 'get' and 'save'. Writing user-infos with these methods is rather simple. The 
    237237rest of this class is intended for internal purposes only. Its meaning is to allow pointers to its templated 
    238238descendant ParticularUI<T>.