Legend:
- Unmodified
- Added
- Removed
-
bdm/stat/libBM.h
r27 r28 84 84 //! access function 85 85 int _dimy()const{return dimy;} 86 87 //! Destructor for future use; 88 virtual ~fnc(){}; 86 89 }; 87 90 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 constructor95 BM(){ll=0;};96 97 /*! \brief Incremental Bayes rule98 @param dt vector of input data99 @param evall If true, the filter will compute likelihood of the data record and store it in \c ll100 */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 };105 91 106 92 //! Probability density function with numerical statistics, e.g. posterior density. … … 108 94 RV rv; 109 95 public: 96 //!default constructor 97 epdf():rv(ivec(0)){}; 110 98 //! Returns the required moment of the epdf 111 99 // virtual vec moment ( const int order = 1 ); … … 113 101 virtual vec sample ()=0; 114 102 //! 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(){}; 119 109 }; 120 110 … … 130 120 // virtual fnc moment ( const int order = 1 ); 131 121 //! 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);}; 133 123 virtual void condition (vec &cond){}; 124 125 //! Destructor for future use; 126 virtual ~mpdf(){}; 134 127 }; 135 128 … … 164 157 //! Moves from $t$ to $t+1$, i.e. perfroms the actions and reads response of the system. 165 158 void step(); 159 166 160 }; 167 161 162 /*! \brief Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities. 163 164 */ 165 class BM { 166 public: 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 }; 168 187 169 188 #endif // BM_H -
bdm/stat/libEF.h
r27 r28 26 26 * More?... 27 27 */ 28 28 29 class eEF : public epdf { 29 30 30 31 public: 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 ) {}; 33 38 }; 34 39 … … 45 50 */ 46 51 template<class sq_T> 52 47 53 class enorm : public eEF { 54 protected: 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 ) 48 66 int dim; 49 vec mu; 50 sq_T R;51 public: 52 enorm ( RV &rv, vec &mu, sq_T &R);53 enorm();67 public: 68 enorm() :eEF() {}; 69 70 enorm ( RV &rv ); 71 void set_parameters ( const vec &mu,const sq_T &R ); 54 72 //! 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 62 76 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 μ} 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;} 66 93 }; 67 94 … … 70 97 */ 71 98 template<class sq_T> 99 72 100 class mlnorm : public mEF { 73 101 enorm<sq_T> epdf; … … 75 103 public: 76 104 //! Constructor 77 mlnorm ( RV &rv,RV &rvc, mat &A, sq_T &R );105 mlnorm ( RV &rv,RV &rvc, mat &A, sq_T &R ); 78 106 //!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 ); 82 110 }; 83 111 … … 85 113 86 114 template<class sq_T> 87 enorm<sq_T>::enorm( RV &rv, vec &mu0, sq_T &R0 ) { 88 dim = rv.count(); 115 enorm<sq_T>::enorm ( RV &rv ) :eEF(), mu ( rv.count() ),R(rv.count()),_iR(rv.count()),cached ( false ),dim ( rv.count() ) {}; 116 117 template<class sq_T> 118 void enorm<sq_T>::set_parameters ( const vec &mu0, const sq_T &R0 ) { 119 //Fixme test dimensions of mu0 and R0; 89 120 mu = mu0; 90 121 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 127 template<class sq_T> 128 void enorm<sq_T>::dupdate ( mat &v, double nu ) { 95 129 // 96 130 }; 97 131 98 132 template<class sq_T> 99 void enorm<sq_T>::tupdate ( double phi, mat &vbar, double nubar ) {133 void enorm<sq_T>::tupdate ( double phi, mat &vbar, double nubar ) { 100 134 // 101 135 }; … … 103 137 template<class sq_T> 104 138 vec 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 ); 108 142 109 143 smp += mu; … … 112 146 113 147 template<class sq_T> 114 mat enorm<sq_T>::sample ( int N ) {115 mat X ( dim,N );116 vec x ( dim );148 mat enorm<sq_T>::sample ( int N ) { 149 mat X ( dim,N ); 150 vec x ( dim ); 117 151 vec pom; 118 152 int i; 153 119 154 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 ); 122 157 pom +=mu; 123 X.set_col ( i, pom);158 X.set_col ( i, pom ); 124 159 } 160 125 161 return X; 126 162 }; 127 163 128 164 template<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 ) { 165 double enorm<sq_T>::eval ( const vec &val ) { 166 return exp ( evalpdflog ( val ) ); 167 }; 168 169 template<class sq_T> 170 double 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 177 template<class sq_T> 178 mlnorm<sq_T>::mlnorm ( RV &rv,RV &rvc, mat &A, sq_T &R ) { 139 179 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 185 template<class sq_T> 186 vec mlnorm<sq_T>::samplecond ( vec &cond, double &lik ) { 187 this->condition ( cond ); 148 188 vec smp = epdf.sample(); 149 lik = epdf.eval ( smp );189 lik = epdf.eval ( smp ); 150 190 return smp; 151 191 } 152 192 153 193 template<class sq_T> 154 mat mlnorm<sq_T>::samplecond ( vec &cond, vec &lik, int n ) {194 mat mlnorm<sq_T>::samplecond ( vec &cond, vec &lik, int n ) { 155 195 int i; 156 196 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 160 201 for ( i=0; i<dim; i++ ) { 161 202 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 ); 164 205 } 206 165 207 return Smp; 166 208 } 167 209 168 210 template<class sq_T> 169 void mlnorm<sq_T>::condition ( vec &cond ) {211 void mlnorm<sq_T>::condition ( vec &cond ) { 170 212 epdf.mu = A*cond; 171 213 //R is already assigned; 172 214 } 173 215 216 /////////// 217 218 //#ifdef HAVE_EXTERN_TEMPLATE 219 220 // extern template class enorm<fsqmat>; 221 // extern template class enorm<ldmat>; 222 //#endif 223 174 224 #endif //EF_H