Changeset 271 for bdm/stat/libBM.h

Show
Ignore:
Timestamp:
02/16/09 10:03:13 (15 years ago)
Author:
smidl
Message:

Next major revision

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/libBM.h

    r270 r271  
    1111*/ 
    1212 
    13 /*! 
    14 \defgroup core Core BDM classes 
    15 @{ 
    16 \defgroup const Constructors 
    17 \defgroup math Mathematical operations 
    18 */ 
    1913#ifndef BM_H 
    2014#define BM_H 
     
    106100        int init ( const  string &name, int size ); 
    107101public: 
    108         //! \name Constructors  
    109         //!@{ 
    110          
    111         //! Full constructor  
     102        //! \name Constructors 
     103        //!@{ 
     104 
     105        //! Full constructor 
    112106        RV ( Array<std::string> in_names, ivec in_sizes, ivec in_times ) {init ( in_names,in_sizes,in_times );}; 
    113         //! Constructor with times=0  
     107        //! Constructor with times=0 
    114108        RV ( Array<std::string> in_names, ivec in_sizes ) {init ( in_names,in_sizes,zeros_i ( in_names.length() ) );}; 
    115         //! Constructor with sizes=1, times=0  
     109        //! Constructor with sizes=1, times=0 
    116110        RV ( Array<std::string> in_names ) {init ( in_names,ones_i ( in_names.length() ),zeros_i ( in_names.length() ) );} 
    117         //! Constructor of empty RV  
     111        //! Constructor of empty RV 
    118112        RV () :dsize ( 0 ),len ( 0 ),ids ( 0 ),times ( 0 ) {}; 
    119113        //! Constructor of a single RV with given id 
    120114        RV ( string name, int sz, int tm=0 ); 
    121115        //!@} 
    122          
     116 
    123117        //! \name Access functions 
    124118        //!@{ 
    125          
     119 
    126120        //! Printing output e.g. for debugging. 
    127121        friend std::ostream &operator<< ( std::ostream &os, const RV &rv ); 
     
    129123        //! Recount size of the corresponding data vector 
    130124        int countsize() const; 
     125        ivec cumsizes() const; 
    131126        int length() const {return len;} ; 
    132127        int id ( int at ) const{return ids ( at );}; 
    133         int size ( int at ) const {return RV_SIZES ( at );}; 
     128        int size ( int at ) const {return RV_SIZES ( ids(at) );}; 
    134129        int time ( int at ) const{return times ( at );}; 
    135         std::string name ( int at ) const {return RV_NAMES ( at );}; 
     130        std::string name ( int at ) const {return RV_NAMES ( ids(at) );}; 
    136131        void set_time ( int at, int time0 ) {times ( at ) =time0;}; 
    137132        //!@} 
    138          
     133 
    139134        //TODO why not inline and later?? 
    140135 
    141136        //! \name Algebra on Random Variables 
    142137        //!@{ 
    143          
     138 
    144139        //! Find indices of self in another rv, \return ivec of the same size as self. 
    145140        ivec findself ( const RV &rv2 ) const; 
     
    154149        //! Select only variables at indeces ind 
    155150        RV operator() ( const ivec &ind ) const {return subselect ( ind );}; 
     151        //! Select from data vector starting at di1 to di2 
     152        RV operator() ( int di1, int di2 ) const { 
     153                ivec sz=cumsizes(); 
     154                int i1=0; 
     155                while ( sz ( i1 ) <di1 ) i1++; 
     156                int i2=i1; 
     157                while ( sz ( i2 ) <di2 ) i2++; 
     158                return subselect ( linspace ( i1,i2 ) ); 
     159        }; 
    156160        //! Shift \c time shifted by delta. 
    157161        void t ( int delta ); 
    158162        //!@} 
    159          
    160         //!\name Relation to vectors  
    161         //!@{ 
    162          
     163 
     164        //!\name Relation to vectors 
     165        //!@{ 
     166 
    163167        //! generate \c str from rv, by expanding sizes 
    164168        str tostr() const; 
     
    172176        int mint () const {return min ( times );}; 
    173177        //!@} 
    174          
     178 
    175179}; 
    176180 
     
    216220public: 
    217221        /*! \name Constructors 
    218          Construction of each epdf should support two types of constructors:  
    219         \li empty constructor,  
     222         Construction of each epdf should support two types of constructors: 
     223        \li empty constructor, 
    220224        \li copy constructor, 
    221          
     225 
    222226        The following constructors should be supported for convenience: 
    223         \li constructor followed by calling \c set_parameters()  
     227        \li constructor followed by calling \c set_parameters() 
    224228        \li constructor accepting random variables calling \c set_rv() 
    225          
     229 
    226230         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. 
    227231        @{*/ 
    228         epdf() :dim(0),rv ( ) {}; 
    229         epdf(const epdf &e) :dim(e.dim),rv (e.rv) {}; 
    230         epdf(const RV &rv0) {set_rv(rv0);}; 
    231         void set_parameters(int dim0){dim=dim0;} 
    232         //!@} 
    233          
     232        epdf() :dim ( 0 ),rv ( ) {}; 
     233        epdf ( const epdf &e ) :dim ( e.dim ),rv ( e.rv ) {}; 
     234        epdf ( const RV &rv0 ) {set_rv ( rv0 );}; 
     235        void set_parameters ( int dim0 ) {dim=dim0;} 
     236        //!@} 
     237 
    234238        //! \name Matematical Operations 
    235239        //!@{ 
    236          
     240 
    237241        //! Returns a sample, \f$ x \f$ from density \f$ f_x()\f$ 
    238         virtual vec sample () const {it_error("not implemneted");return vec(0);}; 
     242        virtual vec sample () const {it_error ( "not implemneted" );return vec ( 0 );}; 
    239243        //! Returns N samples, \f$ [x_1 , x_2 , \ldots \ \f$  from density \f$ f_x(rv)\f$ 
    240244        virtual mat sample_m ( int N ) const; 
    241245        //! Compute log-probability of argument \c val 
    242         virtual double evallog ( const vec &val ) const {it_error("not implemneted");return 0.0;}; 
     246        virtual double evallog ( const vec &val ) const {it_error ( "not implemneted" );return 0.0;}; 
    243247        //! Compute log-probability of multiple values argument \c val 
    244248        virtual vec evallog_m ( const mat &Val ) const { 
     
    252256        virtual epdf* marginal ( const RV &rv ) const {it_warning ( "Not implemented" ); return NULL;} 
    253257        //! return expected value 
    254         virtual vec mean() const {it_error("not implemneted");return vec(0);}; 
     258        virtual vec mean() const {it_error ( "not implemneted" );return vec ( 0 );}; 
    255259        //! return expected variance (not covariance!) 
    256         virtual vec variance() const {it_error("not implemneted");return vec(0);}; 
    257         //!@} 
    258          
     260        virtual vec variance() const {it_error ( "not implemneted" );return vec ( 0 );}; 
     261        //!@} 
     262 
    259263        //! \name Connection to other classes 
    260         //! Description of the random quantity via attribute \c rv is optional.  
    261         //! For operations such as sampling \c rv does not need to be set. However, for \c marginalization  
    262         //! and \c conditioning \c rv has to be set. NB:  
     264        //! Description of the random quantity via attribute \c rv is optional. 
     265        //! For operations such as sampling \c rv does not need to be set. However, for \c marginalization 
     266        //! and \c conditioning \c rv has to be set. NB: 
    263267        //! @{ 
    264          
     268 
    265269        //!Name its rv 
    266270        void set_rv ( const RV &rv0 ) {rv = rv0; }//it_assert_debug(isnamed(),""); }; 
    267         //! True if rv is assigned  
    268         bool isnamed() const {bool b=( dim==rv._dsize() );return b;} 
     271        //! True if rv is assigned 
     272        bool isnamed() const {bool b= ( dim==rv._dsize() );return b;} 
    269273        //! Return name (fails when isnamed is false) 
    270274        const RV& _rv() const {it_assert_debug ( isnamed(),"" ); return rv;} 
    271275        //!@} 
    272          
     276 
    273277        //! \name Access to attributes 
    274278        //! @{ 
    275          
     279 
    276280        //! Size of the random variable 
    277281        int dimension() const {return dim;} 
    278282        //!@} 
    279          
     283 
    280284}; 
    281285 
     
    295299        //! \name Constructors 
    296300        //! @{ 
    297          
    298         mpdf ( ) :dimc(0),rvc ( ) {}; 
     301 
     302        mpdf ( ) :dimc ( 0 ),rvc ( ) {}; 
    299303        //! copy constructor does not set pointer \c ep - has to be done in offsprings! 
    300         mpdf (const mpdf &m ) :dimc(m.dimc),rvc (m.rvc ) {}; 
     304        mpdf ( const mpdf &m ) :dimc ( m.dimc ),rvc ( m.rvc ) {}; 
    301305        //!@} 
    302306 
    303307        //! \name Matematical operations 
    304308        //!@{ 
    305          
     309 
    306310        //! 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 
    307311        virtual vec samplecond ( const vec &cond ) { 
     
    330334        //! \name Access to attributes 
    331335        //! @{ 
    332          
     336 
    333337        RV _rv() {return ep->_rv();} 
    334338        RV _rvc() {it_assert_debug ( isnamed(),"" ); return rvc;} 
     
    338342        epdf* _e() {return ep;} 
    339343        //!@} 
    340          
     344 
    341345        //! \name Connection to other objects 
    342346        //!@{ 
    343347        void set_rvc ( const RV &rvc0 ) {rvc=rvc0;} 
    344         void set_rv ( const RV &rv0 ) {ep->set_rv(rv0);} 
    345         bool isnamed() {return (ep->isnamed())&&(dimc==rvc._dsize());} 
     348        void set_rv ( const RV &rv0 ) {ep->set_rv ( rv0 );} 
     349        bool isnamed() {return ( ep->isnamed() ) && ( dimc==rvc._dsize() );} 
    346350        //!@} 
    347351}; 
     
    382386public: 
    383387        //! Constructor 
    384         datalink ( const RV &rv, const RV &rv_up ) : 
    385                         downsize ( rv._dsize() ), upsize ( rv_up._dsize() ), v2v_up ( rv.dataind ( rv_up ) )  { 
     388        datalink () {}; 
     389        datalink ( const RV &rv, const RV &rv_up ) {set_connection ( rv,rv_up );}; 
     390        //! set connection, rv must be fully present in rv_up 
     391        void set_connection ( const RV &rv, const RV &rv_up ) { 
     392                downsize = rv._dsize(); 
     393                upsize = rv_up._dsize(); 
     394                v2v_up= ( rv.dataind ( rv_up ) ); 
     395 
    386396                it_assert_debug ( v2v_up.length() ==downsize,"rv is not fully in rv_up" ); 
    387397        } 
     
    471481        logger ( ) : entries ( 0 ),names ( 0 ) {} 
    472482 
    473         //! returns an identifier which will be later needed for calling the log() function 
     483        //! returns an identifier which will be later needed for calling the \c logit() function 
     484        //! For empty RV it returns -1, this entry will be ignored by \c logit(). 
    474485        virtual int add ( const RV &rv, string name="" ) { 
    475                 int id=entries.length(); 
    476                 names=concat ( names, name ); // diff 
    477                 entries.set_length ( id+1,true ); 
    478                 entries ( id ) = rv; 
     486                int id; 
     487                if ( rv._dsize() >0 ) { 
     488                        id=entries.length(); 
     489                        names=concat ( names, name ); // diff 
     490                        entries.set_length ( id+1,true ); 
     491                        entries ( id ) = rv; 
     492                } 
     493                else { id =-1;} 
    479494                return id; // identifier of the last entry 
    480495        } 
     
    539554public: 
    540555        //! default constructors 
    541         DS() :Drv (  ),Urv ( ) {}; 
     556        DS() :Drv ( ),Urv ( ) {}; 
    542557        //! Returns full vector of observed data=[output, input] 
    543558        virtual void getdata ( vec &dt ) {it_error ( "abstract class" );}; 
     
    590605        //! \name Constructors 
    591606        //! @{ 
    592          
    593         BM () :ll (0),evalll ( false) {}; 
    594         BM ( const BM &B ) :  drv(B.drv), ll ( B.ll ), evalll ( B.evalll ) {} 
     607 
     608        BM () :ll ( 0 ),evalll ( false ) {}; 
     609        BM ( const BM &B ) :  drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {} 
    595610        //! Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! 
    596611        //! Prototype: \code BM* _copy_(){return new BM(*this);} \endcode 
     
    600615        //! \name Mathematical operations 
    601616        //!@{ 
    602          
     617 
    603618        /*! \brief Incremental Bayes rule 
    604619        @param dt vector of input data 
     
    614629 
    615630        //!Constructs a predictive density \f$ f(d_{t+1} |d_{t}, \ldots d_{0}) \f$ 
    616         virtual epdf* epredictor (  ) const {it_error ( "Not implemented" );return NULL;}; 
     631        virtual epdf* epredictor ( ) const {it_error ( "Not implemented" );return NULL;}; 
    617632        //!Constructs a conditional density 1-step ahead predictor \f$ f(d_{t+1} |d_{t+h-1}, \ldots d_{t}) 
    618633        virtual mpdf* predictor ( ) const {it_error ( "Not implemented" );return NULL;}; 
    619634        //!@} 
    620          
     635 
    621636        //! \name Access to attributes 
    622637        //!@{ 
    623          
     638 
    624639        const RV& _drv() const {return drv;} 
    625640        void set_drv ( const RV &rv ) {drv=rv;} 
     641        void set_rv ( const RV &rv ) {const_cast<epdf&> ( posterior() ).set_rv ( rv );} 
    626642        double _ll() const {return ll;} 
    627643        void set_evalll ( bool evl0 ) {evalll=evl0;} 
    628         virtual const epdf& _epdf() const =0; 
     644        virtual const epdf& posterior() const =0; 
    629645        virtual const epdf* _e() const =0; 
    630646        //!@} 
     
    657673 
    658674}; //namespace 
    659 /*! @} */ 
    660675#endif // BM_H