Legend:
- Unmodified
- Added
- Removed
-
bdm/stat/libBM.cpp
r27 r32 7 7 using std::cout; 8 8 9 void RV::init( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times , ivec in_obs) {9 void RV::init( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times ) { 10 10 // 11 11 int i; … … 16 16 ( len != in_names.length() ) || \ 17 17 ( len != in_sizes.length() ) || \ 18 ( len != in_times.length() ) || \ 19 ( len != in_obs.length() ) ) { 18 ( len != in_times.length() )) { 20 19 it_error( "RV::RV inconsistent length of input vectors." ); 21 20 } … … 25 24 sizes = in_sizes; 26 25 times = in_times; 27 obs = in_obs;28 26 size = 0; 29 27 for(i=0;i<len;i++){size+=sizes(i);} 30 28 }; 31 29 32 RV::RV ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times , ivec in_obs) {33 init ( in_ids, in_names, in_sizes, in_times , in_obs);30 RV::RV ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times ) { 31 init ( in_ids, in_names, in_sizes, in_times ); 34 32 } 35 33 36 RV::RV () {}; 34 RV::RV () : size(0),len(0){}; 35 36 void RV::add (const RV &rv2) { 37 // TODO 38 size+=rv2.size; 39 len +=rv2.len; 40 ids=concat(ids,rv2.ids); 41 sizes=concat(sizes,rv2.sizes); 42 times=concat(times,rv2.times); 43 names=concat(names,rv2.names); 44 // return *this; 45 }; 37 46 38 47 RV::RV ( ivec in_ids ) { … … 46 55 } 47 56 48 init ( in_ids, A, ones_i( len ), zeros_i( len ) , zeros_i( len ));57 init ( in_ids, A, ones_i( len ), zeros_i( len ) ); 49 58 } 50 59 51 60 RV RV::subselect(ivec ind){ 52 return RV(ids(ind), names(to_Arr(ind)), sizes(ind), times(ind) , obs(ind));61 return RV(ids(ind), names(to_Arr(ind)), sizes(ind), times(ind)); 53 62 } 54 63 … … 56 65 57 66 RV RV::operator()(ivec ind){ 58 return RV(ids(ind), names(to_Arr(ind)), sizes(ind), times(ind) , obs(ind));67 return RV(ids(ind), names(to_Arr(ind)), sizes(ind), times(ind)); 59 68 } 60 69 … … 77 86 return indlist; 78 87 } 88 89 RV concat(const RV &rv1, const RV &rv2 ){ 90 RV pom = rv1; 91 pom.add(rv2); 92 return pom; 93 } -
bdm/stat/libBM.h
r28 r32 24 24 * More?... 25 25 */ 26 26 27 class RV { 28 protected: 27 29 //! size = sum of sizes 28 30 int size; … … 36 38 37 39 private: 38 void init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times , ivec in_obs);40 void init ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times); 39 41 public: 40 42 //! Full constructor which is called by the others 41 RV ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times , ivec in_obs);43 RV ( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times); 42 44 //! default constructor 43 45 RV ( ivec ids ); 44 46 //! Empty constructor will be set later 45 47 RV (); 46 48 47 49 //! Printing output e.g. for debugging. 48 50 friend std::ostream &operator<< ( std::ostream &os, const RV &rv ); … … 50 52 //! Return length (number of scalars) of the RV. 51 53 int count() const {return size;} ; 54 52 55 //TODO why not inline and later?? 53 56 54 57 //! Find indexes of another rv in self 55 ivec find (RV rv2);58 ivec find ( RV rv2 ); 56 59 //! Add (concat) another variable to the current one 57 RV add(RV rv2); 60 void add (const RV &rv2 ); 61 //! Add (concat) another variable to the current one 62 friend RV concat (const RV &rv1, const RV &rv2 ); 58 63 //! Subtract another variable from the current one 59 RV subt (RV rv2);64 RV subt ( RV rv2 ); 60 65 //! Select only variables at indeces ind 61 RV subselect (ivec ind);66 RV subselect ( ivec ind ); 62 67 //! Select only variables at indeces ind 63 RV operator() (ivec ind);68 RV operator() ( ivec ind ); 64 69 //! Generate new \c RV with \c time shifted by delta. 65 void t (int delta);66 //! generate a list of indeces, i.e. which 70 void t ( int delta ); 71 //! generate a list of indeces, i.e. which 67 72 ivec indexlist(); 68 73 }; 69 74 70 75 71 72 73 76 //! Class representing function $f(x)$ of variable $x$ represented by \c rv 77 74 78 class fnc { 75 79 protected: 76 80 int dimy; 77 public: 81 public: 78 82 //! function evaluates numerical value of $f(x)$ at $x=cond$ 79 virtual vec eval(const vec &cond) 80 { 81 return vec(0); 83 virtual vec eval ( const vec &cond ) { 84 return vec ( 0 ); 82 85 }; //Fixme: virtual? 83 86 84 87 //! access function 85 int _dimy() const{return dimy;}86 87 88 virtual ~fnc(){};88 int _dimy() const{return dimy;} 89 90 //! Destructor for future use; 91 virtual ~fnc() {}; 89 92 }; 90 93 91 94 92 95 //! Probability density function with numerical statistics, e.g. posterior density. 96 93 97 class epdf { 98 protected: 94 99 RV rv; 95 100 public: 96 101 //!default constructor 97 epdf():rv(ivec(0)){}; 102 epdf() :rv ( ivec ( 0 ) ) {}; 103 104 //!default constructor 105 epdf ( const RV &rv0 ) :rv ( rv0 ) {}; 106 98 107 //! Returns the required moment of the epdf 99 108 // virtual vec moment ( const int order = 1 ); 100 109 //! Returns a sample from the density, \f$x \sim epdf(rv)\f$ 101 virtual vec sample () =0;110 virtual vec sample () const =0; 102 111 //! Compute probability of argument \c val 103 virtual double eval(const vec &val){return 0.0;}; 112 virtual double eval ( const vec &val ) const {return exp(this->evalpdflog(val));}; 113 104 114 //! 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(){}; 109 }; 115 virtual double evalpdflog ( const vec &val ) const =0; 116 117 //! return expected value 118 virtual vec mean() const =0; 119 120 //! Destructor for future use; 121 virtual ~epdf() {}; 122 //! access function 123 RV _rv() const {return rv;} 124 }; 125 110 126 111 127 //! Conditional probability density, e.g. modeling some dependencies. 128 //TODO Samplecond can be generalized 129 112 130 class mpdf { 131 protected: 113 132 //! modeled random variable 114 133 RV rv; 115 134 //! random variable in condition 116 135 RV rvc; 136 //! pointer to internal epdf 137 epdf* ep; 117 138 public: 118 139 119 140 //! Returns the required moment of the epdf 120 141 // virtual fnc moment ( const int order = 1 ); 121 //! Returns a sample from the density conditioned on \c cond, $x \sim epdf(rv|cond)$ 122 virtual vec samplecond (vec &cond, double lik){return vec(0);}; 123 virtual void condition (vec &cond){}; 124 125 //! Destructor for future use; 126 virtual ~mpdf(){}; 142 //! Returns a sample from the density conditioned on \c cond, $x \sim epdf(rv|cond)$. \param ll is a return value of log-likelihood of the sample. 143 virtual vec samplecond ( vec &cond, double &ll ) {this->condition(cond);vec temp= ep->sample();ll=ep->evalpdflog(temp);return temp;}; 144 145 virtual void condition ( const vec &cond ) {}; 146 147 virtual double evalcond (const vec &dt, const vec &cond ) {this->condition(cond);return ep->eval(dt);}; 148 149 //! Destructor for future use; 150 virtual ~mpdf() {}; 151 152 //! Default constructor 153 mpdf ( const RV &rv0, const RV &rvc0 ) :rv ( rv0 ),rvc ( rvc0 ) {}; 154 //! access function 155 RV _rvc(){return rvc;} 156 //!access function 157 epdf& _epdf(){return *ep;} 127 158 }; 128 159 … … 133 164 134 165 */ 166 135 167 class DS { 136 168 protected: 137 169 //!Observed variables, returned by \c getdata(). 138 RV Drv; 170 RV Drv; 139 171 //!Action variables, accepted by \c write(). 140 172 RV Urv; // 141 173 public: 142 //! Returns full vector of observed data 143 void getdata (vec &dt);174 //! Returns full vector of observed data 175 void getdata ( vec &dt ); 144 176 //! Returns data records at indeces. 145 void getdata (vec &dt, ivec &indeces);177 void getdata ( vec &dt, ivec &indeces ); 146 178 //! Accepts action variable and schedule it for application. 147 void write (vec &ut);148 //! Accepts action variables at specific indeces 149 void write (vec &ut, ivec &indeces);150 /*! \brief Method that assigns random variables to the datasource. 179 void write ( vec &ut ); 180 //! Accepts action variables at specific indeces 181 void write ( vec &ut, ivec &indeces ); 182 /*! \brief Method that assigns random variables to the datasource. 151 183 Typically, the datasource will be constructed without knowledge of random variables. This method will associate existing variables with RVs. 152 184 153 185 (Inherited from m3k, may be deprecated soon). 154 186 */ 155 void linkrvs (RV &drv, RV &urv);156 157 //! Moves from $t$ to $t+1$, i.e. perfroms the actions and reads response of the system. 187 void linkrvs ( RV &drv, RV &urv ); 188 189 //! Moves from $t$ to $t+1$, i.e. perfroms the actions and reads response of the system. 158 190 void step(); 159 191 160 192 }; 161 193 162 194 /*! \brief Bayesian Model of the world, i.e. all uncertainty is modeled by probabilities. 163 164 */ 195 196 */ 197 165 198 class BM { 166 public: 199 protected: 200 //!Random variable of the posterior 201 RV rv; 167 202 //!Logarithm of marginalized data likelihood. 168 203 double ll; 169 204 //! 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 205 bool evalll; 206 public: 171 207 172 208 //!Default constructor 173 BM():ll(0),evalll(true){}; 174 209 BM(const RV &rv0) :rv(rv0), ll ( 0 ),evalll ( true ) {//Fixme: test rv 210 }; 211 175 212 /*! \brief Incremental Bayes rule 176 213 @param dt vector of input data 177 214 */ 178 virtual void bayes ( const vec &dt ) = 0;215 virtual void bayes ( const vec &dt ) = 0; 179 216 //! Batch Bayes rule (columns of Dt are observations) 180 217 void bayes ( mat Dt ); 181 218 //! 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(){}; 219 virtual epdf& _epdf()=0; 220 221 //! Destructor for future use; 222 virtual ~BM() {}; 223 //!access function 224 const RV& _rv() const {return rv;} 225 //!access function 226 double _ll() const {return ll;} 227 }; 228 229 /*! 230 \brief Conditional Bayesian Filter 231 232 Evaluates conditional filtering density $f(rv|rvc,data)$ for a given \c rvc which is specified in each step by calling function \c condition. 233 234 This is an interface class used to assure that certain BM has operation \c condition . 235 236 */ 237 238 class BMcond { 239 protected: 240 RV rvc; 241 public: 242 //! Substitute \c val for \c rvc. 243 virtual void condition ( const vec &val ) =0; 244 BMcond(RV &rv0):rvc(rv0){}; 245 virtual ~BMcond(){}; 246 const RV& _rvc() const {return rvc;} 186 247 }; 187 248 -
bdm/stat/libEF.cpp
r14 r32 1 1 #include <itpp/itbase.h> 2 #include <itpp/base/bessel.h> 2 3 #include "libEF.h" 4 #include <math.h> 3 5 4 6 using namespace itpp; 5 7 8 Uniform_RNG UniRNG; 9 Normal_RNG NorRNG; 10 Gamma_RNG GamRNG; 11 6 12 using std::cout; 7 13 14 vec egamma::sample() const { 15 vec smp ( rv.count() ); 16 int i; 17 18 for ( i=0; i<rv.count(); i++ ) { 19 GamRNG.setup ( alpha ( i ),beta ( i ) ); 20 smp ( i ) = GamRNG(); 21 } 22 23 return smp; 24 } 25 26 mat egamma::sample ( int N ) const { 27 mat Smp ( rv.count(),N ); 28 int i,j; 29 30 for ( i=0; i<rv.count(); i++ ) { 31 GamRNG.setup ( alpha ( i ),beta ( i ) ); 32 33 for ( j=0; j<N; j++ ) { 34 Smp ( i,j ) = GamRNG(); 35 } 36 } 37 38 return Smp; 39 } 40 41 double egamma::evalpdflog ( const vec &val ) const { 42 double res = 0.0; //will be added 43 int i; 44 45 for ( i=0; i<rv.count(); i++ ) { 46 res += ( alpha ( i ) - 1 ) *std::log ( val ( i ) ) + alpha ( i ) *std::log ( beta ( i ) ) - beta ( i ) *val ( i ) - lgamma ( alpha ( i ) ); 47 } 48 49 return res; 50 } 51 52 //MGamma 53 mgamma::mgamma ( const RV &rv,const RV &rvc ) : mEF ( rv,rvc ), epdf ( rv ) {vec* tmp; epdf._param ( tmp,_beta );}; 54 55 void mgamma::set_parameters ( double k0 ) { 56 k=k0; 57 ep = &epdf; 58 epdf.set_parameters ( k*ones ( rv.count() ),*_beta ); 59 }; 60 61 vec mgamma::samplecond ( vec &cond, double &ll ) { 62 *_beta=k / cond ; 63 vec smp = epdf.sample(); 64 ll = epdf.evalpdflog ( smp ); 65 return smp; 66 }; 67 68 //Fixme repetition of mlnorm.samplecond. 69 mat mgamma::samplecond ( vec &cond, vec &lik, int n ) { 70 int i; 71 int dim = rv.count(); 72 mat Smp ( dim,n ); 73 vec smp ( dim ); 74 this->condition ( cond ); 75 76 for ( i=0; i<n; i++ ) { 77 smp = epdf.sample(); 78 lik ( i ) = epdf.eval ( smp ); 79 Smp.set_col ( i ,smp ); 80 } 81 82 return Smp; 83 }; 84 85 ivec eEmp::resample ( RESAMPLING_METHOD method ) { 86 ivec ind=zeros_i ( n ); 87 ivec N_babies = zeros_i ( n ); 88 vec cumDist = cumsum ( w ); 89 vec u ( n ); 90 int i,j,parent; 91 double u0; 92 93 switch ( method ) { 94 case MULTINOMIAL: 95 u ( n - 1 ) = pow ( UniRNG.sample(), 1.0 / n ); 96 97 for ( i = n - 2;i >= 0;i-- ) { 98 u ( i ) = u ( i + 1 ) * pow ( UniRNG.sample(), 1.0 / ( i + 1 ) ); 99 } 100 101 break; 102 103 case STRATIFIED: 104 105 for ( i = 0;i < n;i++ ) { 106 u ( i ) = ( i + UniRNG.sample() ) / n; 107 } 108 109 break; 110 111 case SYSTEMATIC: 112 u0 = UniRNG.sample(); 113 114 for ( i = 0;i < n;i++ ) { 115 u ( i ) = ( i + u0 ) / n; 116 } 117 118 break; 119 120 default: 121 it_error ( "PF::resample(): Unknown resampling method" ); 122 } 123 124 // U is now full 125 j = 0; 126 127 for ( i = 0;i < n;i++ ) { 128 while ( u ( i ) > cumDist ( j ) ) j++; 129 130 N_babies ( j ) ++; 131 } 132 133 // We have assigned new babies for each Particle 134 // Now, we fill the resulting index such that: 135 // * particles with at least one baby should not move * 136 // This assures that reassignment can be done inplace; 137 138 // find the first parent; 139 parent=0; while ( N_babies ( parent ) ==0 ) parent++; 140 141 // Build index 142 for ( i = 0;i < n;i++ ) { 143 if ( N_babies ( i ) > 0 ) { 144 ind ( i ) = i; 145 N_babies ( i ) --; //this index was now replicated; 146 } else { 147 // test if the parent has been fully replicated 148 // if yes, find the next one 149 while ( ( N_babies ( parent ) ==0 ) || ( N_babies ( parent ) ==1 && parent>i ) ) parent++; 150 151 // Replicate parent 152 ind ( i ) = parent; 153 154 N_babies ( parent ) --; //this index was now replicated; 155 } 156 157 } 158 159 // copy the internals according to ind 160 for ( i=0;i<n;i++ ) { 161 if ( ind ( i ) !=i ) { 162 samples ( i ) =samples ( ind ( i ) ); 163 } 164 w ( i ) = 1.0/n; 165 } 166 167 return ind; 168 } 169 170 void eEmp::set_parameters ( const vec &w0, epdf* epdf0 ) { 171 //it_assert_debug(rv==epdf0->rv(),"Wrong epdf0"); 172 w=w0; 173 w/=sum ( w0 );//renormalize 174 n=w.length(); 175 samples.set_size ( n ); 176 177 for ( int i=0;i<n;i++ ) {samples ( i ) =epdf0->sample();} 178 } -
bdm/stat/libEF.h
r28 r32 17 17 #include "../math/libDC.h" 18 18 #include "libBM.h" 19 #include "../itpp_ext.h" 19 20 //#include <std> 20 21 21 22 using namespace itpp; 22 23 24 25 //! Global Uniform_RNG 26 extern Uniform_RNG UniRNG; 27 extern Normal_RNG NorRNG; 28 extern Gamma_RNG GamRNG; 29 30 23 31 /*! 24 32 * \brief General conjugate exponential family posterior density. … … 30 38 31 39 public: 40 // eEF() :epdf() {}; 32 41 //! default constructor 33 eEF () :epdf() {};42 eEF ( const RV &rv ) :epdf ( rv ) {}; 34 43 35 44 virtual void tupdate ( double phi, mat &vbar, double nubar ) {}; … … 41 50 42 51 public: 43 52 mEF ( const RV &rv0, const RV &rvc0 ) :mpdf ( rv0,rvc0 ) {}; 44 53 }; 45 54 … … 61 70 //! indicator if \c _iR is chached 62 71 bool cached; 63 //! Random number generator for sampling; Fixme: what about *static* correct ?64 Normal_RNG RNG;65 72 //! dimension (redundant from rv.count() for easier coding ) 66 73 int dim; 67 74 public: 68 enorm() :eEF() {};75 // enorm() :eEF() {}; 69 76 70 77 enorm ( RV &rv ); … … 74 81 void dupdate ( mat &v,double nu=1.0 ); 75 82 76 vec sample(); 77 mat sample ( int N ); 78 double eval ( const vec &val ); 79 double evalpdflog ( const vec &val ); 83 vec sample() const; 84 mat sample ( int N ) const; 85 double eval ( const vec &val ) const ; 86 double evalpdflog ( const vec &val ) const; 87 vec mean()const {return mu;} 80 88 81 89 //Access methods … … 94 102 95 103 /*! 96 \brief 97 */ 98 template<class sq_T> 99 104 \brief Gamma posterior density 105 106 Multvariate Gamma density as product of independent univariate densities. 107 \f[ 108 f(x|a,b) = \prod f(x_i|a_i,b_i) 109 \f] 110 */ 111 112 class egamma : public eEF { 113 protected: 114 vec alpha; 115 vec beta; 116 public : 117 //! Default constructor 118 egamma ( const RV &rv ) :eEF ( rv ) {}; 119 //! Sets parameters 120 void set_parameters ( const vec &a, const vec &b ) {alpha=a,beta=b;}; 121 vec sample() const; 122 mat sample ( int N ) const; 123 double evalpdflog ( const vec &val ) const; 124 //! Returns poiter to alpha and beta. Potentially dengerous: use with care! 125 void _param ( vec* &a, vec* &b ) {a=αb=β}; 126 vec mean()const {vec pom(alpha); pom/=beta; return pom;} 127 }; 128 129 //! Weighted mixture of epdfs with external owned components. 130 class emix : public epdf { 131 protected: 132 int n; 133 vec &w; 134 Array<epdf*> Coms; 135 public: 136 //! Default constructor 137 emix ( const RV &rv, vec &w0): epdf(rv), n(w0.length()), w(w0), Coms(n) {}; 138 void set_parameters( int &i, double wi, epdf* ep){w(i)=wi;Coms(i)=ep;} 139 vec mean(){vec pom; for(int i=0;i<n;i++){pom+=Coms(i)->mean()*w(i);} return pom;}; 140 vec sample() {it_error ( "Not implemented" );return 0;} 141 }; 142 143 //! Uniform distributed density on a rectangular support 144 145 class euni: public epdf { 146 protected: 147 //! lower bound on support 148 vec low; 149 //! upper bound on support 150 vec high; 151 //! internal 152 vec distance; 153 //! normalizing coefficients 154 double nk,lnk; 155 public: 156 euni ( const RV rv ) :epdf ( rv ) {} 157 double eval ( const vec &val ) const {return nk;} 158 double evalpdflog ( const vec &val ) const {return lnk;} 159 vec sample() const { 160 vec smp ( rv.count() ); UniRNG.sample_vector ( rv.count(),smp ); 161 return low+distance*smp; 162 } 163 void set_parameters ( const vec &low0, const vec &high0 ) { 164 distance = high0-low0; 165 it_assert_debug ( min ( distance ) >0.0,"bad support" ); 166 low = low0; 167 high = high0; 168 nk = prod ( 1.0/distance ); 169 lnk = log ( nk ); 170 } 171 vec mean() const {vec pom=high; pom-=low; pom/=2.0; return pom;} 172 }; 173 174 175 /*! 176 \brief Normal distributed linear function with linear function of mean value; 177 178 Mean value $mu=A*rvc$. 179 */ 180 template<class sq_T> 100 181 class mlnorm : public mEF { 101 182 enorm<sq_T> epdf; 183 vec* _mu; //cached epdf.mu; 102 184 mat A; 103 185 public: 104 186 //! Constructor 105 mlnorm ( RV &rv,RV &rvc, mat &A, sq_T &R ); 187 mlnorm ( RV &rv,RV &rvc ); 188 void set_parameters ( const mat &A, const sq_T &R ); 106 189 //!Generate one sample of the posterior 107 190 vec samplecond ( vec &cond, double &lik ); 191 //!Generate matrix of samples of the posterior 108 192 mat samplecond ( vec &cond, vec &lik, int n ); 109 193 void condition ( vec &cond ); 110 194 }; 111 195 196 /*! 197 \brief Gamma random walk 198 199 Mean value, $\mu$, of this density is given by \c rvc . 200 Standard deviation of the random walk is proportional to one $k$-th the mean. 201 This is achieved by setting $\alpha=k$ and $\beta=k/\mu$. 202 203 The standard deviation of the walk is then: $\mu/\sqrt(k)$. 204 */ 205 class mgamma : public mEF { 206 egamma epdf; 207 double k; 208 vec* _beta; 209 210 public: 211 //! Constructor 212 mgamma ( const RV &rv,const RV &rvc ); 213 void set_parameters ( double k ); 214 //!Generate one sample of the posterior 215 vec samplecond ( vec &cond, double &lik ); 216 //!Generate matrix of samples of the posterior 217 mat samplecond ( vec &cond, vec &lik, int n ); 218 void condition ( const vec &val ) {*_beta=k/val;}; 219 }; 220 221 //! Switch between various resampling methods. 222 enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 }; 223 /*! 224 \brief Weighted empirical density 225 226 Used e.g. in particle filters. 227 */ 228 class eEmp: public epdf { 229 protected : 230 //! Number of particles 231 int n; 232 vec w; 233 Array<vec> samples; 234 public: 235 eEmp ( const RV &rv0 ,int n0) :epdf ( rv0 ),n(n0),w(n),samples(n) {}; 236 void set_parameters ( const vec &w0, epdf* pdf0 ); 237 //! Potentially dangerous, use with care. 238 vec& _w() {return w;}; 239 Array<vec>& _samples() {return samples;}; 240 //! Function performs resampling, i.e. removal of low-weight samples and duplication of high-weight samples such that the new samples represent the same density. 241 ivec resample ( RESAMPLING_METHOD method = SYSTEMATIC ); 242 vec sample() const {it_error ( "Not implemented" );return 0;} 243 double evalpdflog(const vec &val) const {it_error ( "Not implemented" );return 0.0;} 244 vec mean()const {vec pom=zeros(rv.count()); 245 for (int i=0;i<n;i++){pom+=samples(i)*w(i);} 246 return pom; 247 } 248 }; 249 250 112 251 //////////////////////// 113 252 114 253 template<class sq_T> 115 enorm<sq_T>::enorm ( RV &rv ) :eEF( ), mu ( rv.count() ),R(rv.count()),_iR(rv.count()),cached ( false ),dim ( rv.count() ) {};254 enorm<sq_T>::enorm ( RV &rv ) :eEF(rv), mu ( rv.count() ),R ( rv.count() ),_iR ( rv.count() ),cached ( false ),dim ( rv.count() ) {}; 116 255 117 256 template<class sq_T> … … 120 259 mu = mu0; 121 260 R = R0; 122 if (_iR.rows()!=R.rows()) _iR=R; // memory allocation!261 if ( _iR.rows() !=R.rows() ) _iR=R; // memory allocation! 123 262 R.inv ( _iR ); //update cache 124 263 cached=true; … … 136 275 137 276 template<class sq_T> 138 vec enorm<sq_T>::sample() {277 vec enorm<sq_T>::sample() const { 139 278 vec x ( dim ); 140 RNG.sample_vector ( dim,x );279 NorRNG.sample_vector ( dim,x ); 141 280 vec smp = R.sqrt_mult ( x ); 142 281 … … 146 285 147 286 template<class sq_T> 148 mat enorm<sq_T>::sample ( int N ) {287 mat enorm<sq_T>::sample ( int N )const { 149 288 mat X ( dim,N ); 150 289 vec x ( dim ); … … 153 292 154 293 for ( i=0;i<N;i++ ) { 155 RNG.sample_vector ( dim,x );294 NorRNG.sample_vector ( dim,x ); 156 295 pom = R.sqrt_mult ( x ); 157 296 pom +=mu; … … 163 302 164 303 template<class sq_T> 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 ) { 179 int dim = rv.count(); 180 vec mu ( dim ); 181 182 epdf = enorm<sq_T> ( rv,mu,R ); 304 double enorm<sq_T>::eval ( const vec &val ) const { 305 double pdfl,e; 306 pdfl = evalpdflog ( val ); 307 e = exp ( pdfl ); 308 return e; 309 }; 310 311 template<class sq_T> 312 double enorm<sq_T>::evalpdflog ( const vec &val ) const { 313 if ( !cached ) {it_error("this should not happen, see cached");} 314 315 // 1.83787706640935 = log(2pi) 316 return -0.5* ( R.cols() * 1.83787706640935 +R.logdet() +_iR.qform ( mu-val ) ); 317 }; 318 319 320 template<class sq_T> 321 mlnorm<sq_T>::mlnorm ( RV &rv0,RV &rvc0 ) :mEF ( rv0,rvc0 ),epdf ( rv ),A ( rv0.count(),rv0.count() ) { 322 _mu = epdf._mu(); 323 } 324 325 template<class sq_T> 326 void mlnorm<sq_T>::set_parameters ( const mat &A0, const sq_T &R0 ) { 327 epdf.set_parameters ( zeros ( rv.count() ),R0 ); 328 A = A0; 183 329 } 184 330 … … 199 345 this->condition ( cond ); 200 346 201 for ( i=0; i< dim; i++ ) {347 for ( i=0; i<n; i++ ) { 202 348 smp = epdf.sample(); 203 349 lik ( i ) = epdf.eval ( smp ); … … 210 356 template<class sq_T> 211 357 void mlnorm<sq_T>::condition ( vec &cond ) { 212 epdf.mu = A*cond;358 *_mu = A*cond; 213 359 //R is already assigned; 214 360 } … … 216 362 /////////// 217 363 218 //#ifdef HAVE_EXTERN_TEMPLATE219 220 // extern template class enorm<fsqmat>;221 // extern template class enorm<ldmat>;222 //#endif223 364 224 365 #endif //EF_H