Changeset 28 for bdm/stat

Show
Ignore:
Timestamp:
02/22/08 16:40:12 (16 years ago)
Author:
smidl
Message:

prelozitelna verze

Location:
bdm/stat
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/libBM.h

    r27 r28  
    8484        //! access function 
    8585        int _dimy()const{return dimy;} 
     86 
     87                //! Destructor for future use; 
     88                virtual ~fnc(){}; 
    8689}; 
    8790 
    88 //! Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities. 
    89 class BM { 
    90 public: 
    91         //!Logarithm of marginalized data likelihood. 
    92         double ll; 
    93  
    94         //!Default constructor 
    95         BM(){ll=0;}; 
    96          
    97         /*! \brief Incremental Bayes rule 
    98         @param dt vector of input data 
    99         @param evall If true, the filter will compute likelihood of the data record and store it in \c ll 
    100         */ 
    101         virtual void bayes ( const vec &dt, bool evall=true ) = 0; 
    102         //! Batch Bayes rule (columns of Dt are observations) 
    103         void bayes ( mat Dt ); 
    104 }; 
    10591 
    10692//! Probability density function with numerical statistics, e.g. posterior density. 
     
    10894        RV rv; 
    10995public: 
     96        //!default constructor 
     97        epdf():rv(ivec(0)){}; 
    11098        //! Returns the required moment of the epdf 
    11199//      virtual vec moment ( const int order = 1 ); 
     
    113101        virtual vec sample ()=0; 
    114102        //! Compute probability of argument \c val 
    115         virtual double eval(const vec &val) 
    116         { 
    117                 return 0.0; 
    118         }; 
     103        virtual double eval(const vec &val){return 0.0;}; 
     104        //! Compute log-probability of argument \c val 
     105        virtual double evalpdflog(const vec &val){return 0.0;}; 
     106 
     107                //! Destructor for future use; 
     108                virtual ~epdf(){}; 
    119109}; 
    120110 
     
    130120//      virtual fnc moment ( const int order = 1 ); 
    131121        //! Returns a sample from the density conditioned on \c cond, $x \sim epdf(rv|cond)$ 
    132         virtual vec samplecond (vec &cond, double lik){}; 
     122        virtual vec samplecond (vec &cond, double lik){return vec(0);}; 
    133123        virtual void condition (vec &cond){}; 
     124 
     125                //! Destructor for future use; 
     126                virtual ~mpdf(){}; 
    134127}; 
    135128 
     
    164157        //! Moves from $t$ to $t+1$, i.e. perfroms the actions and reads response of the system.  
    165158        void step(); 
     159                 
    166160}; 
    167161 
     162/*! \brief Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities. 
     163         
     164*/ 
     165class BM { 
     166public: 
     167        //!Logarithm of marginalized data likelihood. 
     168        double ll; 
     169        //!  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 time. 
     170        bool evalll; 
     171 
     172        //!Default constructor 
     173        BM():ll(0),evalll(true){}; 
     174         
     175        /*! \brief Incremental Bayes rule 
     176        @param dt vector of input data 
     177        */ 
     178        virtual void bayes ( const vec &dt) = 0; 
     179        //! Batch Bayes rule (columns of Dt are observations) 
     180        void bayes ( mat Dt ); 
     181        //! Returns a pointer to the epdf representing posterior density on parameters. Use with care! 
     182        virtual epdf* _epdf()=0; 
     183 
     184                //! Destructor for future use; 
     185                virtual ~BM(){}; 
     186}; 
    168187 
    169188#endif // BM_H 
  • bdm/stat/libEF.h

    r27 r28  
    2626* More?... 
    2727*/ 
     28 
    2829class eEF : public epdf { 
    2930 
    3031public: 
    31         virtual void tupdate( double phi, mat &vbar, double nubar ) {}; 
    32         virtual void dupdate( mat &v,double nu=1.0 ) {}; 
     32        //! default constructor 
     33        eEF() :epdf() {}; 
     34 
     35        virtual void tupdate ( double phi, mat &vbar, double nubar ) {}; 
     36 
     37        virtual void dupdate ( mat &v,double nu=1.0 ) {}; 
    3338}; 
    3439 
     
    4550*/ 
    4651template<class sq_T> 
     52 
    4753class enorm : public eEF { 
     54protected: 
     55        //! mean value 
     56        vec mu; 
     57        //! Covariance matrix in decomposed form 
     58        sq_T R; 
     59        //! Cache:  _iR = inv(R); 
     60        sq_T _iR; 
     61        //! indicator if \c _iR is chached 
     62        bool cached; 
     63        //! Random number generator for sampling; Fixme: what about *static* correct ? 
     64        Normal_RNG RNG; 
     65        //! dimension (redundant from rv.count() for easier coding ) 
    4866        int dim; 
    49         vec mu; 
    50         sq_T R; 
    51 public: 
    52         enorm( RV &rv, vec &mu, sq_T &R ); 
    53         enorm(); 
     67public: 
     68        enorm() :eEF() {}; 
     69 
     70        enorm ( RV &rv ); 
     71        void set_parameters ( const vec &mu,const sq_T &R ); 
    5472        //! tupdate in exponential form (not really handy) 
    55         void tupdate( double phi, mat &vbar, double nubar ); 
    56         void dupdate( mat &v,double nu=1.0 ); 
    57         //!tupdate used in KF 
    58         void tupdate(); 
    59         //!dupdate used in KF 
    60         double dupdate(); 
    61           
     73        void tupdate ( double phi, mat &vbar, double nubar ); 
     74        void dupdate ( mat &v,double nu=1.0 ); 
     75 
    6276        vec sample(); 
    63         mat sample(int N); 
    64         double eval( const vec &val ); 
    65         Normal_RNG RNG; 
     77        mat sample ( int N ); 
     78        double eval ( const vec &val ); 
     79        double evalpdflog ( const vec &val ); 
     80 
     81//Access methods 
     82        //! returns a pointer to the internal mean value. Use with Care! 
     83        vec* _mu() {return &mu;} 
     84 
     85        //! returns pointers to the internal variance and its inverse. Use with Care! 
     86        void _R ( sq_T* &pR, sq_T* &piR ) { 
     87                pR=&R; 
     88                piR=&_iR; 
     89        } 
     90 
     91        //! set cache as inconsistent 
     92        void _cached ( bool what ) {cached=what;} 
    6693}; 
    6794 
     
    7097*/ 
    7198template<class sq_T> 
     99 
    72100class mlnorm : public mEF { 
    73101        enorm<sq_T> epdf; 
     
    75103public: 
    76104        //! Constructor 
    77         mlnorm( RV &rv,RV &rvc, mat &A, sq_T &R ); 
     105        mlnorm ( RV &rv,RV &rvc, mat &A, sq_T &R ); 
    78106        //!Generate one sample of the posterior 
    79         vec samplecond( vec &cond, double &lik ); 
    80         mat samplecond( vec &cond, vec &lik, int n ); 
    81         void condition( vec &cond ); 
     107        vec samplecond ( vec &cond, double &lik ); 
     108        mat samplecond ( vec &cond, vec &lik, int n ); 
     109        void condition ( vec &cond ); 
    82110}; 
    83111 
     
    85113 
    86114template<class sq_T> 
    87 enorm<sq_T>::enorm( RV &rv, vec &mu0, sq_T &R0 ) { 
    88         dim = rv.count(); 
     115enorm<sq_T>::enorm ( RV &rv ) :eEF(), mu ( rv.count() ),R(rv.count()),_iR(rv.count()),cached ( false ),dim ( rv.count() ) {}; 
     116 
     117template<class sq_T> 
     118void enorm<sq_T>::set_parameters ( const vec &mu0, const sq_T &R0 ) { 
     119//Fixme test dimensions of mu0 and R0; 
    89120        mu = mu0; 
    90121        R = R0; 
    91 }; 
    92  
    93 template<class sq_T> 
    94 void enorm<sq_T>::dupdate( mat &v, double nu ) { 
     122        if(_iR.rows()!=R.rows()) _iR=R; // memory allocation! 
     123        R.inv ( _iR ); //update cache 
     124        cached=true; 
     125}; 
     126 
     127template<class sq_T> 
     128void enorm<sq_T>::dupdate ( mat &v, double nu ) { 
    95129        // 
    96130}; 
    97131 
    98132template<class sq_T> 
    99 void enorm<sq_T>::tupdate( double phi, mat &vbar, double nubar ) { 
     133void enorm<sq_T>::tupdate ( double phi, mat &vbar, double nubar ) { 
    100134        // 
    101135}; 
     
    103137template<class sq_T> 
    104138vec enorm<sq_T>::sample() { 
    105         vec x( dim ); 
    106         RNG.sample_vector( dim,x ); 
    107         vec smp = R.sqrt_mult( x ); 
     139        vec x ( dim ); 
     140        RNG.sample_vector ( dim,x ); 
     141        vec smp = R.sqrt_mult ( x ); 
    108142 
    109143        smp += mu; 
     
    112146 
    113147template<class sq_T> 
    114 mat enorm<sq_T>::sample( int N ) { 
    115         mat X( dim,N ); 
    116         vec x( dim ); 
     148mat enorm<sq_T>::sample ( int N ) { 
     149        mat X ( dim,N ); 
     150        vec x ( dim ); 
    117151        vec pom; 
    118152        int i; 
     153 
    119154        for ( i=0;i<N;i++ ) { 
    120                 RNG.sample_vector( dim,x ); 
    121                 pom = R.sqrt_mult( x ); 
     155                RNG.sample_vector ( dim,x ); 
     156                pom = R.sqrt_mult ( x ); 
    122157                pom +=mu; 
    123                 X.set_col( i, pom); 
     158                X.set_col ( i, pom ); 
    124159        } 
     160 
    125161        return X; 
    126162}; 
    127163 
    128164template<class sq_T> 
    129 double enorm<sq_T>::eval( const vec &val ) { 
    130         return 0.0;      
    131 }; 
    132  
    133  
    134 template<class sq_T> 
    135 enorm<sq_T>::enorm() {}; 
    136  
    137 template<class sq_T> 
    138 mlnorm<sq_T>::mlnorm( RV &rv,RV &rvc, mat &A, sq_T &R ) { 
     165double enorm<sq_T>::eval ( const vec &val ) { 
     166        return exp ( evalpdflog ( val ) ); 
     167}; 
     168 
     169template<class sq_T> 
     170double enorm<sq_T>::evalpdflog ( const vec &val ) { 
     171        if ( !cached ) {R.inv ( _iR );cached=true;} 
     172 
     173        return -0.5* ( R.cols() *0.79817986835811504957 +R.logdet() +_iR.qform ( mu-val ) ); 
     174}; 
     175 
     176 
     177template<class sq_T> 
     178mlnorm<sq_T>::mlnorm ( RV &rv,RV &rvc, mat &A, sq_T &R ) { 
    139179        int dim = rv.count(); 
    140         vec mu( dim ); 
    141  
    142         epdf = enorm<sq_T>( rv,mu,R ); 
    143 } 
    144  
    145 template<class sq_T> 
    146 vec mlnorm<sq_T>::samplecond( vec &cond, double &lik ) { 
    147         this->condition( cond ); 
     180        vec mu ( dim ); 
     181 
     182        epdf = enorm<sq_T> ( rv,mu,R ); 
     183} 
     184 
     185template<class sq_T> 
     186vec mlnorm<sq_T>::samplecond ( vec &cond, double &lik ) { 
     187        this->condition ( cond ); 
    148188        vec smp = epdf.sample(); 
    149         lik = epdf.eval( smp ); 
     189        lik = epdf.eval ( smp ); 
    150190        return smp; 
    151191} 
    152192 
    153193template<class sq_T> 
    154 mat mlnorm<sq_T>::samplecond( vec &cond, vec &lik, int n ) { 
     194mat mlnorm<sq_T>::samplecond ( vec &cond, vec &lik, int n ) { 
    155195        int i; 
    156196        int dim = rv.count(); 
    157         mat Smp( dim,n ); 
    158         vec smp( dim ); 
    159         this->condition( cond ); 
     197        mat Smp ( dim,n ); 
     198        vec smp ( dim ); 
     199        this->condition ( cond ); 
     200 
    160201        for ( i=0; i<dim; i++ ) { 
    161202                smp = epdf.sample(); 
    162                 lik( i ) = epdf.eval( smp ); 
    163                 Smp.set_col( i ,smp ); 
     203                lik ( i ) = epdf.eval ( smp ); 
     204                Smp.set_col ( i ,smp ); 
    164205        } 
     206 
    165207        return Smp; 
    166208} 
    167209 
    168210template<class sq_T> 
    169 void mlnorm<sq_T>::condition( vec &cond ) { 
     211void mlnorm<sq_T>::condition ( vec &cond ) { 
    170212        epdf.mu = A*cond; 
    171213//R is already assigned; 
    172214} 
    173215 
     216/////////// 
     217 
     218//#ifdef HAVE_EXTERN_TEMPLATE 
     219 
     220//  extern template class enorm<fsqmat>; 
     221//  extern template class enorm<ldmat>; 
     222//#endif 
     223 
    174224#endif //EF_H