00001
00013 #ifndef EF_H
00014 #define EF_H
00015
00016 #include <itpp/itbase.h>
00017 #include "../math/libDC.h"
00018 #include "libBM.h"
00019 #include "../itpp_ext.h"
00020
00021
00022 using namespace itpp;
00023
00024
00026 extern Uniform_RNG UniRNG;
00028 extern Normal_RNG NorRNG;
00030 extern Gamma_RNG GamRNG;
00031
00038 class eEF : public epdf {
00039 public:
00040
00042 eEF ( const RV &rv ) :epdf ( rv ) {};
00044 virtual double lognc()const =0;
00046 virtual void tupdate ( double phi, mat &vbar, double nubar ) {};
00048 virtual void dupdate ( mat &v,double nu=1.0 ) {};
00049 };
00050
00057 class mEF : public mpdf {
00058
00059 public:
00061 mEF ( const RV &rv0, const RV &rvc0 ) :mpdf ( rv0,rvc0 ) {};
00062 };
00063
00069 template<class sq_T>
00070
00071 class enorm : public eEF {
00072 protected:
00074 vec mu;
00076 sq_T R;
00078 int dim;
00079 public:
00080
00082 enorm ( RV &rv );
00084 void set_parameters ( const vec &mu,const sq_T &R );
00086 void tupdate ( double phi, mat &vbar, double nubar );
00088 void dupdate ( mat &v,double nu=1.0 );
00089
00090 vec sample() const;
00092 mat sample ( int N ) const;
00093 double eval ( const vec &val ) const ;
00094 double evalpdflog ( const vec &val ) const;
00095 double lognc () const;
00096 vec mean() const {return mu;}
00097
00098
00100 vec& _mu() {return mu;}
00101
00103 void set_mu(const vec mu0) { mu=mu0;}
00104
00106 sq_T& _R() {return R;}
00107
00109 mat getR () {return R.to_mat();}
00110 };
00111
00117 class egiw : public eEF {
00118 protected:
00120 ldmat V;
00122 double nu;
00123 public:
00125 egiw(RV rv, mat V0, double nu0): eEF(rv), V(V0), nu(nu0) {
00126 it_assert_debug(rv.count()==V.rows(),"Incompatible V0.");
00127 }
00128
00129 vec sample() const;
00130 vec mean() const;
00131 double evalpdflog ( const vec &val ) const;
00132 double lognc () const;
00133
00134
00136 ldmat& _V() {return V;}
00138 double& _nu() {return nu;}
00139
00140 };
00141
00151 class egamma : public eEF {
00152 protected:
00154 vec alpha;
00156 vec beta;
00157 public :
00159 egamma ( const RV &rv ) :eEF ( rv ) {};
00161 void set_parameters ( const vec &a, const vec &b ) {alpha=a,beta=b;};
00162 vec sample() const;
00164
00165 double evalpdflog ( const vec &val ) const;
00166 double lognc () const;
00168 void _param ( vec* &a, vec* &b ) {a=αb=β};
00169 vec mean() const {vec pom ( alpha ); pom/=beta; return pom;}
00170 };
00171
00173
00174
00175
00176
00177
00178
00180
00181
00182
00183
00184
00185
00186
00188
00189 class euni: public epdf {
00190 protected:
00192 vec low;
00194 vec high;
00196 vec distance;
00198 double nk;
00200 double lnk;
00201 public:
00203 euni ( const RV rv ) :epdf ( rv ) {}
00204 double eval ( const vec &val ) const {return nk;}
00205 double evalpdflog ( const vec &val ) const {return lnk;}
00206 vec sample() const {
00207 vec smp ( rv.count() );
00208 #pragma omp critical
00209 UniRNG.sample_vector ( rv.count(),smp );
00210 return low+elem_mult(distance,smp);
00211 }
00213 void set_parameters ( const vec &low0, const vec &high0 ) {
00214 distance = high0-low0;
00215 it_assert_debug ( min ( distance ) >0.0,"bad support" );
00216 low = low0;
00217 high = high0;
00218 nk = prod ( 1.0/distance );
00219 lnk = log ( nk );
00220 }
00221 vec mean() const {vec pom=high; pom-=low; pom/=2.0; return pom;}
00222 };
00223
00224
00230 template<class sq_T>
00231 class mlnorm : public mEF {
00233 enorm<sq_T> epdf;
00234 mat A;
00235 vec& _mu;
00236 public:
00238 mlnorm ( RV &rv,RV &rvc );
00240 void set_parameters ( const mat &A, const sq_T &R );
00242 vec samplecond ( vec &cond, double &lik );
00244 mat samplecond ( vec &cond, vec &lik, int n );
00246 void condition ( vec &cond );
00247 };
00248
00258 class mgamma : public mEF {
00259 protected:
00261 egamma epdf;
00263 double k;
00265 vec* _beta;
00266
00267 public:
00269 mgamma ( const RV &rv,const RV &rvc );
00271 void set_parameters ( double k );
00272 void condition ( const vec &val ) {*_beta=k/val;};
00273 };
00274
00286 class mgamma_fix : public mgamma {
00287 protected:
00289 double l;
00291 vec refl;
00292 public:
00294 mgamma_fix ( const RV &rv,const RV &rvc ) : mgamma ( rv,rvc ),refl ( rv.count() ) {};
00296 void set_parameters ( double k0 , vec ref0, double l0 ) {
00297 mgamma::set_parameters ( k0 );
00298 refl=pow ( ref0,1.0-l0 );l=l0;
00299 };
00300
00301 void condition ( const vec &val ) {vec mean=elem_mult ( refl,pow ( val,l ) ); *_beta=k/mean;};
00302 };
00303
00305 enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 };
00311 class eEmp: public epdf {
00312 protected :
00314 int n;
00316 vec w;
00318 Array<vec> samples;
00319 public:
00321 eEmp ( const RV &rv0 ,int n0 ) :epdf ( rv0 ),n ( n0 ),w ( n ),samples ( n ) {};
00323 void set_parameters ( const vec &w0, epdf* pdf0 );
00325 vec& _w() {return w;};
00327 Array<vec>& _samples() {return samples;};
00329 ivec resample ( RESAMPLING_METHOD method = SYSTEMATIC );
00331 vec sample() const {it_error ( "Not implemented" );return 0;}
00333 double evalpdflog ( const vec &val ) const {it_error ( "Not implemented" );return 0.0;}
00334 vec mean() const {
00335 vec pom=zeros ( rv.count() );
00336 for ( int i=0;i<n;i++ ) {pom+=samples ( i ) *w ( i );}
00337 return pom;
00338 }
00339 };
00340
00341
00343
00344 template<class sq_T>
00345 enorm<sq_T>::enorm ( RV &rv ) :eEF ( rv ), mu ( rv.count() ),R ( rv.count() ),dim ( rv.count() ) {};
00346
00347 template<class sq_T>
00348 void enorm<sq_T>::set_parameters ( const vec &mu0, const sq_T &R0 ) {
00349
00350 mu = mu0;
00351 R = R0;
00352 };
00353
00354 template<class sq_T>
00355 void enorm<sq_T>::dupdate ( mat &v, double nu ) {
00356
00357 };
00358
00359 template<class sq_T>
00360 void enorm<sq_T>::tupdate ( double phi, mat &vbar, double nubar ) {
00361
00362 };
00363
00364 template<class sq_T>
00365 vec enorm<sq_T>::sample() const {
00366 vec x ( dim );
00367 NorRNG.sample_vector ( dim,x );
00368 vec smp = R.sqrt_mult ( x );
00369
00370 smp += mu;
00371 return smp;
00372 };
00373
00374 template<class sq_T>
00375 mat enorm<sq_T>::sample ( int N ) const {
00376 mat X ( dim,N );
00377 vec x ( dim );
00378 vec pom;
00379 int i;
00380
00381 for ( i=0;i<N;i++ ) {
00382 NorRNG.sample_vector ( dim,x );
00383 pom = R.sqrt_mult ( x );
00384 pom +=mu;
00385 X.set_col ( i, pom );
00386 }
00387
00388 return X;
00389 };
00390
00391 template<class sq_T>
00392 double enorm<sq_T>::eval ( const vec &val ) const {
00393 double pdfl,e;
00394 pdfl = evalpdflog ( val );
00395 e = exp ( pdfl );
00396 return e;
00397 };
00398
00399 template<class sq_T>
00400 double enorm<sq_T>::evalpdflog ( const vec &val ) const {
00401
00402 return -0.5* ( +R.invqform ( mu-val ) ) - lognc();
00403 };
00404
00405 template<class sq_T>
00406 inline double enorm<sq_T>::lognc () const {
00407
00408 return -0.5* ( R.cols() * 1.83787706640935 +R.logdet());
00409 };
00410
00411 template<class sq_T>
00412 mlnorm<sq_T>::mlnorm ( RV &rv0,RV &rvc0 ) :mEF ( rv0,rvc0 ),epdf ( rv0 ),A ( rv0.count(),rv0.count() ),_mu(epdf._mu()) { ep =&epdf;
00413 }
00414
00415 template<class sq_T>
00416 void mlnorm<sq_T>::set_parameters ( const mat &A0, const sq_T &R0 ) {
00417 epdf.set_parameters ( zeros ( rv.count() ),R0 );
00418 A = A0;
00419 }
00420
00421 template<class sq_T>
00422 vec mlnorm<sq_T>::samplecond ( vec &cond, double &lik ) {
00423 this->condition ( cond );
00424 vec smp = epdf.sample();
00425 lik = epdf.eval ( smp );
00426 return smp;
00427 }
00428
00429 template<class sq_T>
00430 mat mlnorm<sq_T>::samplecond ( vec &cond, vec &lik, int n ) {
00431 int i;
00432 int dim = rv.count();
00433 mat Smp ( dim,n );
00434 vec smp ( dim );
00435 this->condition ( cond );
00436
00437 for ( i=0; i<n; i++ ) {
00438 smp = epdf.sample();
00439 lik ( i ) = epdf.eval ( smp );
00440 Smp.set_col ( i ,smp );
00441 }
00442
00443 return Smp;
00444 }
00445
00446 template<class sq_T>
00447 void mlnorm<sq_T>::condition ( vec &cond ) {
00448 _mu = A*cond;
00449
00450 }
00451
00453
00454
00455 #endif //EF_H