- Main Page
- Related Pages
- Classes
- Files

# work/mixpp/bdm/stat/libEF.h

Go to the documentation of this file.

    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 //#include <std>
    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 //      eEF() :epdf() {};
    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 //      enorm() :eEF() {};
    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 //Access methods
    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         //Access
    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         mat sample ( int N ) const;
    00165         double evalpdflog ( const vec &val ) const;
    00166         double lognc () const;
    00168         void _param ( vec* &a, vec* &b ) {a=&alpha;b=&beta;};
    00169         vec mean() const {vec pom ( alpha ); pom/=beta; return pom;}
    00170 };
    00171 /*
    00173 class emix : public epdf {
    00174 protected:
    00175         int n;
    00176         vec &w;
    00177         Array<epdf*> Coms;
    00178 public:
    00180         emix ( const RV &rv, vec &w0): epdf(rv), n(w0.length()), w(w0), Coms(n) {};
    00181         void set_parameters( int &i, double wi, epdf* ep){w(i)=wi;Coms(i)=ep;}
    00182         vec mean(){vec pom; for(int i=0;i<n;i++){pom+=Coms(i)->mean()*w(i);} return pom;};
    00183         vec sample() {it_error ( "Not implemented" );return 0;}
    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() ); UniRNG.sample_vector ( rv.count(),smp );
    00208                 return low+distance*smp;
    00209         }
    00211         void set_parameters ( const vec &low0, const vec &high0 ) {
    00212                 distance = high0-low0;
    00213                 it_assert_debug ( min ( distance ) >0.0,"bad support" );
    00214                 low = low0;
    00215                 high = high0;
    00216                 nk = prod ( 1.0/distance );
    00217                 lnk = log ( nk );
    00218         }
    00219         vec mean() const {vec pom=high; pom-=low; pom/=2.0; return pom;}
    00220 };
    00221
    00222
    00228 template<class sq_T>
    00229 class mlnorm : public mEF {
    00231         enorm<sq_T> epdf;
    00232         mat A;
    00233         vec& _mu; //cached epdf.mu;
    00234 public:
    00236         mlnorm ( RV &rv,RV &rvc );
    00238         void set_parameters ( const  mat &A, const sq_T &R );
    00240         vec samplecond ( vec &cond, double &lik );
    00242         mat samplecond ( vec &cond, vec &lik, int n );
    00244         void condition ( vec &cond );
    00245 };
    00246
    00256 class mgamma : public mEF {
    00257 protected:
    00259         egamma epdf;
    00261         double k;
    00263         vec* _beta;
    00264
    00265 public:
    00267         mgamma ( const RV &rv,const RV &rvc );
    00269         void set_parameters ( double k );
    00271         vec samplecond ( vec &cond, double &lik );
    00273         mat samplecond ( vec &cond, vec &lik, int n );
    00274         void condition ( const vec &val ) {*_beta=k/val;};
    00275 };
    00276
    00288 class mgamma_fix : public mgamma {
    00289 protected:
    00291         double l;
    00293         vec refl;
    00294 public:
    00296         mgamma_fix ( const RV &rv,const RV &rvc ) : mgamma ( rv,rvc ),refl ( rv.count() ) {};
    00298         void set_parameters ( double k0 , vec ref0, double l0 ) {
    00299                 mgamma::set_parameters ( k0 );
    00300                 refl=pow ( ref0,1.0-l0 );l=l0;
    00301         };
    00302
    00303         void condition ( const vec &val ) {vec mean=elem_mult ( refl,pow ( val,l ) ); *_beta=k/mean;};
    00304 };
    00305
    00307 enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 };
    00313 class eEmp: public epdf {
    00314 protected :
    00316         int n;
    00318         vec w;
    00320         Array<vec> samples;
    00321 public:
    00323         eEmp ( const RV &rv0 ,int n0 ) :epdf ( rv0 ),n ( n0 ),w ( n ),samples ( n ) {};
    00325         void set_parameters ( const vec &w0, epdf* pdf0 );
    00327         vec& _w()  {return w;};
    00329         Array<vec>& _samples() {return samples;};
    00331         ivec resample ( RESAMPLING_METHOD method = SYSTEMATIC );
    00333         vec sample() const {it_error ( "Not implemented" );return 0;}
    00335         double evalpdflog ( const vec &val ) const {it_error ( "Not implemented" );return 0.0;}
    00336         vec mean() const {
    00337                 vec pom=zeros ( rv.count() );
    00338                 for ( int i=0;i<n;i++ ) {pom+=samples ( i ) *w ( i );}
    00339                 return pom;
    00340         }
    00341 };
    00342
    00343
    00345
    00346 template<class sq_T>
    00347 enorm<sq_T>::enorm ( RV &rv ) :eEF ( rv ), mu ( rv.count() ),R ( rv.count() ),dim ( rv.count() ) {};
    00348
    00349 template<class sq_T>
    00350 void enorm<sq_T>::set_parameters ( const vec &mu0, const sq_T &R0 ) {
    00351 //Fixme test dimensions of mu0 and R0;
    00352         mu = mu0;
    00353         R = R0;
    00354 };
    00355
    00356 template<class sq_T>
    00357 void enorm<sq_T>::dupdate ( mat &v, double nu ) {
    00358         //
    00359 };
    00360
    00361 template<class sq_T>
    00362 void enorm<sq_T>::tupdate ( double phi, mat &vbar, double nubar ) {
    00363         //
    00364 };
    00365
    00366 template<class sq_T>
    00367 vec enorm<sq_T>::sample() const {
    00368         vec x ( dim );
    00369         NorRNG.sample_vector ( dim,x );
    00370         vec smp = R.sqrt_mult ( x );
    00371
    00372         smp += mu;
    00373         return smp;
    00374 };
    00375
    00376 template<class sq_T>
    00377 mat enorm<sq_T>::sample ( int N ) const {
    00378         mat X ( dim,N );
    00379         vec x ( dim );
    00380         vec pom;
    00381         int i;
    00382
    00383         for ( i=0;i<N;i++ ) {
    00384                 NorRNG.sample_vector ( dim,x );
    00385                 pom = R.sqrt_mult ( x );
    00386                 pom +=mu;
    00387                 X.set_col ( i, pom );
    00388         }
    00389
    00390         return X;
    00391 };
    00392
    00393 template<class sq_T>
    00394 double enorm<sq_T>::eval ( const vec &val ) const {
    00395         double pdfl,e;
    00396         pdfl = evalpdflog ( val );
    00397         e = exp ( pdfl );
    00398         return e;
    00399 };
    00400
    00401 template<class sq_T>
    00402 double enorm<sq_T>::evalpdflog ( const vec &val ) const {
    00403         // 1.83787706640935 = log(2pi)
    00404         return  -0.5* (  +R.invqform ( mu-val ) ) - lognc();
    00405 };
    00406
    00407 template<class sq_T>
    00408 inline double enorm<sq_T>::lognc () const {
    00409         // 1.83787706640935 = log(2pi)
    00410         return -0.5* ( R.cols() * 1.83787706640935 +R.logdet());
    00411 };
    00412
    00413 template<class sq_T>
    00414 mlnorm<sq_T>::mlnorm ( RV &rv0,RV &rvc0 ) :mEF ( rv0,rvc0 ),epdf ( rv ),A ( rv0.count(),rv0.count() ),_mu(epdf._mu()) {
    00415 }
    00416
    00417 template<class sq_T>
    00418 void mlnorm<sq_T>::set_parameters ( const mat &A0, const sq_T &R0 ) {
    00419         epdf.set_parameters ( zeros ( rv.count() ),R0 );
    00420         A = A0;
    00421 }
    00422
    00423 template<class sq_T>
    00424 vec mlnorm<sq_T>::samplecond ( vec &cond, double &lik ) {
    00425         this->condition ( cond );
    00426         vec smp = epdf.sample();
    00427         lik = epdf.eval ( smp );
    00428         return smp;
    00429 }
    00430
    00431 template<class sq_T>
    00432 mat mlnorm<sq_T>::samplecond ( vec &cond, vec &lik, int n ) {
    00433         int i;
    00434         int dim = rv.count();
    00435         mat Smp ( dim,n );
    00436         vec smp ( dim );
    00437         this->condition ( cond );
    00438
    00439         for ( i=0; i<n; i++ ) {
    00440                 smp = epdf.sample();
    00441                 lik ( i ) = epdf.eval ( smp );
    00442                 Smp.set_col ( i ,smp );
    00443         }
    00444
    00445         return Smp;
    00446 }
    00447
    00448 template<class sq_T>
    00449 void mlnorm<sq_T>::condition ( vec &cond ) {
    00450         _mu = A*cond;
    00451 //R is already assigned;
    00452 }
    00453
    00455
    00456
    00457 #endif //EF_H