Changeset 283 for bdm/stat/libBM.h

Show
Ignore:
Timestamp:
02/24/09 14:14:01 (15 years ago)
Author:
smidl
Message:

get rid of BMcond + adaptation in doprava/

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/libBM.h

    r281 r283  
    126126        int length() const {return len;} ; 
    127127        int id ( int at ) const{return ids ( at );}; 
    128         int size ( int at ) const {return RV_SIZES ( ids(at) );}; 
     128        int size ( int at ) const {return RV_SIZES ( ids ( at ) );}; 
    129129        int time ( int at ) const{return times ( at );}; 
    130         std::string name ( int at ) const {return RV_NAMES ( ids(at) );}; 
     130        std::string name ( int at ) const {return RV_NAMES ( ids ( at ) );}; 
    131131        void set_time ( int at, int time0 ) {times ( at ) =time0;}; 
    132132        //!@} 
     
    204204 
    205205        //! access function 
    206         int _dimy() const{return dimy;} 
     206        int dimension() const{return dimy;} 
    207207}; 
    208208 
     
    259259        //! return expected variance (not covariance!) 
    260260        virtual vec variance() const {it_error ( "not implemneted" );return vec ( 0 );}; 
     261        //! Lower and upper bounds of \c percentage % quantile, returns mean-2*sigma as default 
     262        virtual void qbounds ( vec &lb, vec &ub, double percentage=0.95 ) const { 
     263                vec mea=mean(); vec std=sqrt(variance());  
     264                lb = mea-2*std; ub=mea+2*std; 
     265        }; 
    261266        //!@} 
    262267 
     
    592597/*! \brief Bayesian Model of a system, i.e. all uncertainty is modeled by probabilities. 
    593598 
     599This object represents exact or approximate evaluation of the Bayes rule: 
     600\f[ 
     601f(\theta_t | d_1,\ldots,d_t) = \frac{f(y_t|\theta_t,\cdot) f(\theta_t|d_1,\ldots,d_{t-1})}{f(y_t|d_1,\ldots,d_{t-1})} 
     602\f] 
     603 
     604Access to the resulting posterior density is via function \c posterior(). 
     605 
     606As a "side-effect" it also evaluates log-likelihood of the data, which can be accessed via function _ll(). 
     607It can also evaluate predictors of future values of \f$y_t\f$, see functions epredictor() and predictor(). 
     608 
     609Alternatively, it can evaluate posterior density conditioned by a known constant, \f$ c_t \f$: 
     610\f[ 
     611f(\theta_t | c_t, d_1,\ldots,d_t) \propto  f(y_t,\theta_t|c_t,\cdot, d_1,\ldots,d_{t-1}) 
     612\f] 
     613 
     614The value of \f$ c_t \f$ is set by function condition(). 
     615 
    594616*/ 
    595617 
     
    606628        //! @{ 
    607629 
    608         BM () :ll ( 0 ),evalll ( true) {}; 
     630        BM () :ll ( 0 ),evalll ( true ), LIDs ( 3 ), opt_L_bounds ( false ) {}; 
    609631        BM ( const BM &B ) :  drv ( B.drv ), ll ( B.ll ), evalll ( B.evalll ) {} 
    610632        //! Copy function required in vectors, Arrays of BM etc. Have to be DELETED manually! 
    611         //! Prototype: \code BM* _copy_(){return new BM(*this);} \endcode 
    612         virtual BM* _copy_ () {return NULL;}; 
     633        //! Prototype: \code BM* _copy_() const {return new BM(*this);} \endcode 
     634        virtual BM* _copy_ () const {return NULL;}; 
    613635        //!@} 
    614636 
     
    634656        //!@} 
    635657 
     658        //! \name Extension to conditional BM 
     659        //! This extension is useful e.g. in Marginalized Particle Filter (\ref bdm::MPF). 
     660        //! Alternatively, it can be used for automated connection to DS when the condition is observed 
     661        //!@{ 
     662 
     663        //! Name of extension variable 
     664        RV rvc; 
     665        //! access function 
     666        const RV& _rvc() const {return rvc;} 
     667 
     668        //! Substitute \c val for \c rvc. 
     669        virtual void condition ( const vec &val ) {it_error ( "Not implemented!" );}; 
     670 
     671        //!@} 
     672 
     673 
    636674        //! \name Access to attributes 
    637675        //!@{ 
     
    646684        //!@} 
    647685 
    648 }; 
    649  
    650 /*! 
    651 \brief Conditional Bayesian Filter 
    652  
    653 Evaluates conditional filtering density \f$f(rv|rvc,data)\f$ for a given \c rvc which is specified in each step by calling function \c condition. 
    654  
    655 This is an interface class used to assure that certain BM has operation \c condition . 
    656  
    657 */ 
    658  
    659 class BMcond :public bdmroot { 
    660 protected: 
    661         //!dimension of the conditioning variable 
    662         int dimc; 
    663         //! Identificator of the conditioning variable 
    664         RV rvc; 
    665 public: 
    666         //! Substitute \c val for \c rvc. 
    667         virtual void condition ( const vec &val ) =0; 
    668         //! Default constructor 
    669         BMcond ( ) :rvc ( ) {}; 
    670         //! Destructor for future use 
    671         virtual ~BMcond() {}; 
    672         //! access function 
    673         const RV& _rvc() const {return rvc;} 
     686        //! \name Logging of results 
     687        //!@{ 
     688 
     689        //! Set boolean options from a string 
     690        void set_options ( const string &opt ) { 
     691                opt_L_bounds= ( opt.find ( "logbounds" ) !=string::npos ); 
     692        } 
     693        //! IDs of storages in loggers 
     694        ivec LIDs; 
     695 
     696        //! Option for logging bounds 
     697        bool opt_L_bounds; 
     698        //! Add all logged variables to a logger 
     699        void log_add ( logger *L, const string &name="" ) { 
     700                // internal 
     701                RV r; 
     702                if ( posterior().isnamed() ) {r=posterior()._rv();} 
     703                else{r=RV ( "est", posterior().dimension() );}; 
     704 
     705                // Add mean value 
     706                LIDs ( 0 ) =L->add ( r,name ); 
     707                if ( opt_L_bounds ) { 
     708                        LIDs ( 1 ) =L->add ( r,name+"_lb" ); 
     709                        LIDs ( 2 ) =L->add ( r,name+"_ub" ); 
     710                } 
     711        } 
     712        void logit ( logger *L ) { 
     713                L->logit ( LIDs ( 0 ), posterior().mean() ); 
     714                if ( opt_L_bounds ) { 
     715                        vec ub,lb; 
     716                        posterior().qbounds(lb,ub); 
     717                        L->logit ( LIDs ( 1 ), lb ); 
     718                        L->logit ( LIDs ( 2 ), ub ); 
     719                } 
     720        } 
     721        //!@} 
    674722}; 
    675723