- Main Page
- Related Pages
- Modules
- Classes
- Files

- File List
- File Members

# libEF.h

Go to the documentation of this file.

    00001
    00013 #ifndef EF_H
    00014 #define EF_H
    00015
    00016
    00017 #include "libBM.h"
    00018 #include "../math/chmat.h"
    00019 //#include <std>
    00020
    00021 namespace bdm
    00022 {
    00023
    00024
    00026         extern Uniform_RNG UniRNG;
    00028         extern Normal_RNG NorRNG;
    00030         extern Gamma_RNG GamRNG;
    00031
    00038         class eEF : public epdf
    00039         {
    00040                 public:
    00041 //      eEF() :epdf() {};
    00043                         eEF ( ) :epdf ( ) {};
    00045                         virtual double lognc() const =0;
    00047                         virtual void dupdate ( mat &v ) {it_error ( "Not implemented" );};
    00049                         virtual double evallog_nn ( const vec &val ) const{it_error ( "Not implemented" );return 0.0;};
    00051                         virtual double evallog ( const vec &val ) const {double tmp;tmp= evallog_nn ( val )-lognc();it_assert_debug ( std::isfinite ( tmp ),"Infinite value" ); return tmp;}
    00053                         virtual vec evallog ( const mat &Val ) const
    00054                         {
    00055                                 vec x ( Val.cols() );
    00056                                 for ( int i=0;i<Val.cols();i++ ) {x ( i ) =evallog_nn ( Val.get_col ( i ) ) ;}
    00057                                 return x-lognc();
    00058                         }
    00060                         virtual void pow ( double p ) {it_error ( "Not implemented" );};
    00061         };
    00062
    00069         class mEF : public mpdf
    00070         {
    00071
    00072                 public:
    00074                         mEF ( ) :mpdf ( ) {};
    00075         };
    00076
    00078         class BMEF : public BM
    00079         {
    00080                 protected:
    00082                         double frg;
    00084                         double last_lognc;
    00085                 public:
    00087                         BMEF ( double frg0=1.0 ) :BM ( ), frg ( frg0 ) {}
    00089                         BMEF ( const BMEF &B ) :BM ( B ), frg ( B.frg ), last_lognc ( B.last_lognc ) {}
    00091                         virtual void set_statistics ( const BMEF* BM0 ) {it_error ( "Not implemented" );};
    00093                         virtual void bayes ( const vec &data, const double w ) {};
    00094                         //original Bayes
    00095                         void bayes ( const vec &dt );
    00097                         virtual void flatten ( const BMEF * B ) {it_error ( "Not implemented" );}
    00099 //      virtual void flatten ( double nu0 ) {it_error ( "Not implemented" );}
    00100
    00101                         BMEF* _copy_ () const {it_error ( "function _copy_ not implemented for this BM" ); return NULL;};
    00102         };
    00103
    00104         template<class sq_T>
    00105         class mlnorm;
    00106
    00112         template<class sq_T>
    00113         class enorm : public eEF
    00114         {
    00115                 protected:
    00117                         vec mu;
    00119                         sq_T R;
    00120                 public:
    00123
    00124                         enorm ( ) :eEF ( ), mu ( ),R ( ) {};
    00125                         enorm ( const vec &mu,const sq_T &R ) {set_parameters ( mu,R );}
    00126                         void set_parameters ( const vec &mu,const sq_T &R );
    00128
    00131
    00133                         void dupdate ( mat &v,double nu=1.0 );
    00134
    00135                         vec sample() const;
    00136                         mat sample ( int N ) const;
    00137                         double evallog_nn ( const vec &val ) const;
    00138                         double lognc () const;
    00139                         vec mean() const {return mu;}
    00140                         vec variance() const {return diag ( R.to_mat() );}
    00141 //      mlnorm<sq_T>* condition ( const RV &rvn ) const ; <=========== fails to cmpile. Why?
    00142                         mpdf* condition ( const RV &rvn ) const ;
    00143         enorm<sq_T>* marginal ( const RV &rv ) const;
    00144 //                      epdf* marginal ( const RV &rv ) const;
    00146
    00149
    00150                         vec& _mu() {return mu;}
    00151                         void set_mu ( const vec mu0 ) { mu=mu0;}
    00152                         sq_T& _R() {return R;}
    00153                         const sq_T& _R() const {return R;}
    00155
    00156         };
    00157
    00164         class egiw : public eEF
    00165         {
    00166                 protected:
    00168                         ldmat V;
    00170                         double nu;
    00172                         int dimx;
    00174                         int nPsi;
    00175                 public:
    00178                         egiw() :eEF() {};
    00179                         egiw ( int dimx0, ldmat V0, double nu0=-1.0 ) :eEF() {set_parameters ( dimx0,V0, nu0 );};
    00180
    00181                         void set_parameters ( int dimx0, ldmat V0, double nu0=-1.0 )
    00182                         {
    00183                                 dimx=dimx0;
    00184                                 nPsi = V0.rows()-dimx;
    00185                                 dim = dimx* ( dimx+nPsi ); // size(R) + size(Theta)
    00186
    00187                                 V=V0;
    00188                                 if ( nu0<0 )
    00189                                 {
    00190                                         nu = 0.1 +nPsi +2*dimx +2; // +2 assures finite expected value of R
    00191                                         // terms before that are sufficient for finite normalization
    00192                                 }
    00193                                 else
    00194                                 {
    00195                                         nu=nu0;
    00196                                 }
    00197                         }
    00199
    00200                         vec sample() const;
    00201                         vec mean() const;
    00202                         vec variance() const;
    00203
    00205                         vec est_theta() const;
    00206
    00208                         ldmat est_theta_cov() const;
    00209
    00210                         void mean_mat ( mat &M, mat&R ) const;
    00212                         double evallog_nn ( const vec &val ) const;
    00213                         double lognc () const;
    00214                         void pow ( double p ) {V*=p;nu*=p;};
    00215
    00218
    00219                         ldmat& _V() {return V;}
    00220                         const ldmat& _V() const {return V;}
    00221                         double& _nu()  {return nu;}
    00222                         const double& _nu() const {return nu;}
    00224         };
    00225
    00234         class eDirich: public eEF
    00235         {
    00236                 protected:
    00238                         vec beta;
    00239                 public:
    00242
    00243                         eDirich () : eEF ( ) {};
    00244                         eDirich ( const eDirich &D0 ) : eEF () {set_parameters ( D0.beta );};
    00245                         eDirich ( const vec &beta0 ) {set_parameters ( beta0 );};
    00246                         void set_parameters ( const vec &beta0 )
    00247                         {
    00248                                 beta= beta0;
    00249                                 dim = beta.length();
    00250                         }
    00252
    00253                         vec sample() const {it_error ( "Not implemented" );return vec_1 ( 0.0 );};
    00254                         vec mean() const {return beta/sum(beta);};
    00255                         vec variance() const {double gamma =sum(beta); return elem_mult ( beta, ( beta+1 ) ) / ( gamma* ( gamma+1 ) );}
    00257                         double evallog_nn ( const vec &val ) const
    00258                         {
    00259                                 double tmp; tmp= ( beta-1 ) *log ( val );               it_assert_debug ( std::isfinite ( tmp ),"Infinite value" );
    00260                                 return tmp;
    00261                         };
    00262                         double lognc () const
    00263                         {
    00264                                 double tmp;
    00265                                 double gam=sum ( beta );
    00266                                 double lgb=0.0;
    00267                                 for ( int i=0;i<beta.length();i++ ) {lgb+=lgamma ( beta ( i ) );}
    00268                                 tmp= lgb-lgamma ( gam );
    00269                                 it_assert_debug ( std::isfinite ( tmp ),"Infinite value" );
    00270                                 return tmp;
    00271                         };
    00273                         vec& _beta()  {return beta;}
    00275         };
    00276
    00278         class multiBM : public BMEF
    00279         {
    00280                 protected:
    00282                         eDirich est;
    00284                         vec &beta;
    00285                 public:
    00287                         multiBM ( ) : BMEF ( ),est ( ),beta ( est._beta() )
    00288                         {
    00289                                 if ( beta.length() >0 ) {last_lognc=est.lognc();}
    00290                                 else{last_lognc=0.0;}
    00291                         }
    00293                         multiBM ( const multiBM &B ) : BMEF ( B ),est ( B.est ),beta ( est._beta() ) {}
    00295                         void set_statistics ( const BM* mB0 ) {const multiBM* mB=dynamic_cast<const multiBM*> ( mB0 ); beta=mB->beta;}
    00296                         void bayes ( const vec &dt )
    00297                         {
    00298                                 if ( frg<1.0 ) {beta*=frg;last_lognc=est.lognc();}
    00299                                 beta+=dt;
    00300                                 if ( evalll ) {ll=est.lognc()-last_lognc;}
    00301                         }
    00302                         double logpred ( const vec &dt ) const
    00303                         {
    00304                                 eDirich pred ( est );
    00305                                 vec &beta = pred._beta();
    00306
    00307                                 double lll;
    00308                                 if ( frg<1.0 )
    00309                                         {beta*=frg;lll=pred.lognc();}
    00310                                 else
    00311                                         if ( evalll ) {lll=last_lognc;}
    00312                                         else{lll=pred.lognc();}
    00313
    00314                                 beta+=dt;
    00315                                 return pred.lognc()-lll;
    00316                         }
    00317                         void flatten ( const BMEF* B )
    00318                         {
    00319                                 const multiBM* E=dynamic_cast<const multiBM*> ( B );
    00320                                 // sum(beta) should be equal to sum(B.beta)
    00321                                 const vec &Eb=E->beta;//const_cast<multiBM*> ( E )->_beta();
    00322                                 beta*= ( sum ( Eb ) /sum ( beta ) );
    00323                                 if ( evalll ) {last_lognc=est.lognc();}
    00324                         }
    00325                         const epdf& posterior() const {return est;};
    00326                         const eDirich* _e() const {return &est;};
    00327                         void set_parameters ( const vec &beta0 )
    00328                         {
    00329                                 est.set_parameters ( beta0 );
    00330                                 if ( evalll ) {last_lognc=est.lognc();}
    00331                         }
    00332         };
    00333
    00343         class egamma : public eEF
    00344         {
    00345                 protected:
    00347                         vec alpha;
    00349                         vec beta;
    00350                 public :
    00353                         egamma ( ) :eEF ( ), alpha ( 0 ), beta ( 0 ) {};
    00354                         egamma ( const vec &a, const vec &b ) {set_parameters ( a, b );};
    00355                         void set_parameters ( const vec &a, const vec &b ) {alpha=a,beta=b;dim = alpha.length();};
    00357
    00358                         vec sample() const;
    00360 //      mat sample ( int N ) const;
    00361                         double evallog ( const vec &val ) const;
    00362                         double lognc () const;
    00364                         vec& _alpha() {return alpha;}
    00365                         vec& _beta() {return beta;}
    00366                         vec mean() const {return elem_div ( alpha,beta );}
    00367                         vec variance() const {return elem_div ( alpha,elem_mult ( beta,beta ) ); }
    00368         };
    00369
    00386         class eigamma : public egamma
    00387         {
    00388                 protected:
    00389                 public :
    00394
    00395                         vec sample() const {return 1.0/egamma::sample();};
    00397                         vec mean() const {return elem_div ( beta,alpha-1 );}
    00398                         vec variance() const {vec mea=mean(); return elem_div ( elem_mult ( mea,mea ),alpha-2 );}
    00399         };
    00400         /*
    00402         class emix : public epdf {
    00403         protected:
    00404                 int n;
    00405                 vec &w;
    00406                 Array<epdf*> Coms;
    00407         public:
    00409                 emix ( const RV &rv, vec &w0): epdf(rv), n(w0.length()), w(w0), Coms(n) {};
    00410                 void set_parameters( int &i, double wi, epdf* ep){w(i)=wi;Coms(i)=ep;}
    00411                 vec mean(){vec pom; for(int i=0;i<n;i++){pom+=Coms(i)->mean()*w(i);} return pom;};
    00412                 vec sample() {it_error ( "Not implemented" );return 0;}
    00413         };
    00414         */
    00415
    00417
    00418         class euni: public epdf
    00419         {
    00420                 protected:
    00422                         vec low;
    00424                         vec high;
    00426                         vec distance;
    00428                         double nk;
    00430                         double lnk;
    00431                 public:
    00434                         euni ( ) :epdf ( ) {}
    00435                         euni ( const vec &low0, const vec &high0 ) {set_parameters ( low0,high0 );}
    00436                         void set_parameters ( const vec &low0, const vec &high0 )
    00437                         {
    00438                                 distance = high0-low0;
    00439                                 it_assert_debug ( min ( distance ) >0.0,"bad support" );
    00440                                 low = low0;
    00441                                 high = high0;
    00442                                 nk = prod ( 1.0/distance );
    00443                                 lnk = log ( nk );
    00444                                 dim = low.length();
    00445                         }
    00447
    00448                         double eval ( const vec &val ) const  {return nk;}
    00449                         double evallog ( const vec &val ) const  {return lnk;}
    00450                         vec sample() const
    00451                         {
    00452                                 vec smp ( dim );
    00453 #pragma omp critical
    00454                                 UniRNG.sample_vector ( dim ,smp );
    00455                                 return low+elem_mult ( distance,smp );
    00456                         }
    00458                         vec mean() const {return ( high-low ) /2.0;}
    00459                         vec variance() const {return ( pow ( high,2 ) +pow ( low,2 ) +elem_mult ( high,low ) ) /3.0;}
    00460         };
    00461
    00462
    00468         template<class sq_T>
    00469         class mlnorm : public mEF
    00470         {
    00471                 protected:
    00473                         enorm<sq_T> epdf;
    00474                         mat A;
    00475                         vec mu_const;
    00476                         vec& _mu; //cached epdf.mu;
    00477                 public:
    00480                         mlnorm ( ) :mEF (),epdf ( ),A ( ),_mu ( epdf._mu() ) {ep =&epdf; };
    00481                         mlnorm ( const  mat &A, const vec &mu0, const sq_T &R ) :epdf ( ),_mu ( epdf._mu() )
    00482                         {
    00483                                 ep =&epdf; set_parameters ( A,mu0,R );
    00484                         };
    00486                         void set_parameters ( const  mat &A, const vec &mu0, const sq_T &R );
    00489                         void condition ( const vec &cond );
    00490
    00492                         vec& _mu_const() {return mu_const;}
    00494                         mat& _A() {return A;}
    00496                         mat _R() {return epdf._R().to_mat();}
    00497
    00498                         template<class sq_M>
    00499                         friend std::ostream &operator<< ( std::ostream &os,  mlnorm<sq_M> &ml );
    00500         };
    00501
    00503         template<class sq_T>
    00504         class mgnorm : public mEF
    00505         {
    00506                 protected:
    00508                         enorm<sq_T> epdf;
    00509                         vec &mu;
    00510                         fnc* g;
    00511                 public:
    00513                         mgnorm() :mu ( epdf._mu() ) {ep=&epdf;}
    00515                         void set_parameters ( fnc* g0, const sq_T &R0 ) {g=g0; epdf.set_parameters ( zeros ( g->dimension() ), R0 );}
    00516                         void condition ( const vec &cond ) {mu=g->eval ( cond );};
    00517         };
    00518
    00526         class mlstudent : public mlnorm<ldmat>
    00527         {
    00528                 protected:
    00529                         ldmat Lambda;
    00530                         ldmat &_R;
    00531                         ldmat Re;
    00532                 public:
    00533                         mlstudent ( ) :mlnorm<ldmat> (),
    00534                                         Lambda (),      _R ( epdf._R() ) {}
    00535                         void set_parameters ( const mat &A0, const vec &mu0, const ldmat &R0, const ldmat& Lambda0 )
    00536                         {
    00537                                 it_assert_debug ( A0.rows() ==mu0.length(),"" );
    00538                                 it_assert_debug ( R0.rows() ==A0.rows(),"" );
    00539
    00540                                 epdf.set_parameters ( mu0,Lambda ); //
    00541                                 A = A0;
    00542                                 mu_const = mu0;
    00543                                 Re=R0;
    00544                                 Lambda = Lambda0;
    00545                         }
    00546                         void condition ( const vec &cond )
    00547                         {
    00548                                 _mu = A*cond + mu_const;
    00549                                 double zeta;
    00550                                 //ugly hack!
    00551                                 if ( ( cond.length() +1 ) ==Lambda.rows() )
    00552                                 {
    00553                                         zeta = Lambda.invqform ( concat ( cond, vec_1 ( 1.0 ) ) );
    00554                                 }
    00555                                 else
    00556                                 {
    00557                                         zeta = Lambda.invqform ( cond );
    00558                                 }
    00559                                 _R = Re;
    00560                                 _R*= ( 1+zeta );// / ( nu ); << nu is in Re!!!!!!
    00561                         };
    00562
    00563         };
    00573         class mgamma : public mEF
    00574         {
    00575                 protected:
    00577                         egamma epdf;
    00579                         double k;
    00581                         vec &_beta;
    00582
    00583                 public:
    00585                         mgamma ( ) : mEF ( ), epdf (), _beta ( epdf._beta() ) {ep=&epdf;};
    00587                         void set_parameters ( double k, const vec &beta0 );
    00588                         void condition ( const vec &val ) {_beta=k/val;};
    00589         };
    00590
    00600         class migamma : public mEF
    00601         {
    00602                 protected:
    00604                         eigamma epdf;
    00606                         double k;
    00608                         vec &_alpha;
    00610                         vec &_beta;
    00611
    00612                 public:
    00615                         migamma ( ) : mEF (), epdf ( ), _alpha ( epdf._alpha() ), _beta ( epdf._beta() ) {ep=&epdf;};
    00616                         migamma ( const migamma &m ) : mEF (), epdf ( m.epdf ), _alpha ( epdf._alpha() ), _beta ( epdf._beta() ) {ep=&epdf;};
    00618
    00620                         void set_parameters ( int len, double k0 )
    00621                         {
    00622                                 k=k0;
    00623                                 epdf.set_parameters ( ( 1.0/ ( k*k ) +2.0 ) *ones ( len ) /*alpha*/, ones ( len ) /*beta*/ );
    00624                                 dimc = dimension();
    00625                         };
    00626                         void condition ( const vec &val )
    00627                         {
    00628                                 _beta=elem_mult ( val, ( _alpha-1.0 ) );
    00629                         };
    00630         };
    00631
    00643         class mgamma_fix : public mgamma
    00644         {
    00645                 protected:
    00647                         double l;
    00649                         vec refl;
    00650                 public:
    00652                         mgamma_fix ( ) : mgamma ( ),refl () {};
    00654                         void set_parameters ( double k0 , vec ref0, double l0 )
    00655                         {
    00656                                 mgamma::set_parameters ( k0, ref0 );
    00657                                 refl=pow ( ref0,1.0-l0 );l=l0;
    00658                                 dimc=dimension();
    00659                         };
    00660
    00661                         void condition ( const vec &val ) {vec mean=elem_mult ( refl,pow ( val,l ) ); _beta=k/mean;};
    00662         };
    00663
    00664
    00677         class migamma_ref : public migamma
    00678         {
    00679                 protected:
    00681                         double l;
    00683                         vec refl;
    00684                 public:
    00686                         migamma_ref ( ) : migamma (),refl ( ) {};
    00688                         void set_parameters ( double k0 , vec ref0, double l0 )
    00689                         {
    00690                                 migamma::set_parameters ( ref0.length(), k0 );
    00691                                 refl=pow ( ref0,1.0-l0 );
    00692                                 l=l0;
    00693                                 dimc = dimension();
    00694                         };
    00695
    00696                         void condition ( const vec &val )
    00697                         {
    00698                                 vec mean=elem_mult ( refl,pow ( val,l ) );
    00699                                 migamma::condition ( mean );
    00700                         };
    00701         };
    00702
    00712         class elognorm: public enorm<ldmat>
    00713         {
    00714                 public:
    00715                         vec sample() const {return exp ( enorm<ldmat>::sample() );};
    00716                         vec mean() const {vec var=enorm<ldmat>::variance();return exp ( mu - 0.5*var );};
    00717
    00718         };
    00719
    00731         class mlognorm : public mpdf
    00732         {
    00733                 protected:
    00734                         elognorm eno;
    00736                         double sig2;
    00738                         vec &mu;
    00739                 public:
    00741                         mlognorm ( ) : eno (), mu ( eno._mu() ) {ep=&eno;};
    00743                         void set_parameters ( int size, double k )
    00744                         {
    00745                                 sig2 = 0.5*log ( k*k+1 );
    00746                                 eno.set_parameters ( zeros ( size ),2*sig2*eye ( size ) );
    00747
    00748                                 dimc = size;
    00749                         };
    00750
    00751                         void condition ( const vec &val )
    00752                         {
    00753                                 mu=log ( val )-sig2;//elem_mult ( refl,pow ( val,l ) );
    00754                         };
    00755         };
    00756
    00760         class eWishartCh : public epdf
    00761         {
    00762                 protected:
    00764                         chmat Y;
    00766                         int p;
    00768                         double delta;
    00769                 public:
    00770                         void set_parameters ( const mat &Y0, const double delta0 ) {Y=chmat ( Y0 );delta=delta0; p=Y.rows(); dim = p*p; }
    00771                         mat sample_mat() const
    00772                         {
    00773                                 mat X=zeros ( p,p );
    00774
    00775                                 //sample diagonal
    00776                                 for ( int i=0;i<p;i++ )
    00777                                 {
    00778                                         GamRNG.setup ( 0.5* ( delta-i ) , 0.5 ); // no +1 !! index if from 0
    00779 #pragma omp critical
    00780                                         X ( i,i ) =sqrt ( GamRNG() );
    00781                                 }
    00782                                 //do the rest
    00783                                 for ( int i=0;i<p;i++ )
    00784                                 {
    00785                                         for ( int j=i+1;j<p;j++ )
    00786                                         {
    00787 #pragma omp critical
    00788                                                 X ( i,j ) =NorRNG.sample();
    00789                                         }
    00790                                 }
    00791                                 return X*Y._Ch();// return upper triangular part of the decomposition
    00792                         }
    00793                         vec sample () const
    00794                         {
    00795                                 return vec ( sample_mat()._data(),p*p );
    00796                         }
    00798                         void setY ( const mat &Ch0 ) {copy_vector ( dim,Ch0._data(), Y._Ch()._data() );}
    00800                         void _setY ( const vec &ch0 ) {copy_vector ( dim, ch0._data(), Y._Ch()._data() ); }
    00802                         const chmat& getY()const {return Y;}
    00803         };
    00804
    00805         class eiWishartCh: public epdf
    00806         {
    00807                 protected:
    00808                         eWishartCh W;
    00809                         int p;
    00810                         double delta;
    00811                 public:
    00812                         void set_parameters ( const mat &Y0, const double delta0) {
    00813                                 delta = delta0;
    00814                                 W.set_parameters ( inv ( Y0 ),delta0 );
    00815                                 dim = W.dimension(); p=Y0.rows();
    00816                         }
    00817                         vec sample() const {mat iCh; iCh=inv ( W.sample_mat() ); return vec ( iCh._data(),dim );}
    00818                         void _setY ( const vec &y0 )
    00819                         {
    00820                                 mat Ch ( p,p );
    00821                                 mat iCh ( p,p );
    00822                                 copy_vector ( dim, y0._data(), Ch._data() );
    00823
    00824                                 iCh=inv ( Ch );
    00825                                 W.setY ( iCh );
    00826                         }
    00827                         virtual double evallog ( const vec &val ) const {
    00828                                 chmat X(p);
    00829                                 const chmat& Y=W.getY();
    00830
    00831                                 copy_vector(p*p,val._data(),X._Ch()._data());
    00832                                 chmat iX(p);X.inv(iX);
    00833                                 // compute
    00834 //                              \frac{ |\Psi|^{m/2}|X|^{-(m+p+1)/2}e^{-tr(\Psi X^{-1})/2} }{ 2^{mp/2}\Gamma_p(m/2)},
    00835                                 mat M=Y.to_mat()*iX.to_mat();
    00836
    00837                                 double log1 = 0.5*p*(2*Y.logdet())-0.5*(delta+p+1)*(2*X.logdet())-0.5*trace(M);
    00838                                 //Fixme! Multivariate gamma omitted!! it is ok for sampling, but not otherwise!!
    00839
    00840 /*                              if (0) {
    00841                                         mat XX=X.to_mat();
    00842                                         mat YY=Y.to_mat();
    00843
    00844                                         double log2 = 0.5*p*log(det(YY))-0.5*(delta+p+1)*log(det(XX))-0.5*trace(YY*inv(XX));
    00845                                         cout << log1 << "," << log2 << endl;
    00846                                 }*/
    00847                                 return log1;
    00848                         };
    00849
    00850         };
    00851
    00852         class rwiWishartCh : public mpdf
    00853         {
    00854                 protected:
    00855                         eiWishartCh eiW;
    00857                         double sqd;
    00858                         //reference point for diagonal
    00859                         vec refl;
    00860                         double l;
    00861                         int p;
    00862                 public:
    00863                         void set_parameters ( int p0, double k, vec ref0, double l0  )
    00864                         {
    00865                                 p=p0;
    00866                                 double delta = 2/(k*k)+p+3;
    00867                                 sqd=sqrt ( delta-p-1 );
    00868                                 l=l0;
    00869                                 refl=pow(ref0,1-l);
    00870
    00871                                 eiW.set_parameters ( eye ( p ),delta );
    00872                                 ep=&eiW;
    00873                                 dimc=eiW.dimension();
    00874                         }
    00875                         void condition ( const vec &c ) {
    00876                                 vec z=c;
    00877                                 int ri=0;
    00878                                 for(int i=0;i<p*p;i+=(p+1)){//trace diagonal element
    00879                                         z(i) = pow(z(i),l)*refl(ri);
    00880                                         ri++;
    00881                                 }
    00882
    00883                                 eiW._setY ( sqd*z );
    00884                         }
    00885         };
    00886
    00888         enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 };
    00894         class eEmp: public epdf
    00895         {
    00896                 protected :
    00898                         int n;
    00900                         vec w;
    00902                         Array<vec> samples;
    00903                 public:
    00906                         eEmp ( ) :epdf ( ),w ( ),samples ( ) {};
    00907                         eEmp ( const eEmp &e ) : epdf ( e ), w ( e.w ), samples ( e.samples ) {};
    00909
    00911                         void set_statistics ( const vec &w0, const epdf* pdf0 );
    00913                         void set_statistics ( const epdf* pdf0 , int n ) {set_statistics ( ones ( n ) /n,pdf0 );};
    00915                         void set_samples ( const epdf* pdf0 );
    00917                         void set_parameters ( int n0, bool copy=true ) {n=n0; w.set_size ( n0,copy );samples.set_size ( n0,copy );};
    00919                         vec& _w()  {return w;};
    00921                         const vec& _w() const {return w;};
    00923                         Array<vec>& _samples() {return samples;};
    00925                         const Array<vec>& _samples() const {return samples;};
    00927                         ivec resample ( RESAMPLING_METHOD method=SYSTEMATIC );
    00929                         vec sample() const {it_error ( "Not implemented" );return 0;}
    00931                         double evallog ( const vec &val ) const {it_error ( "Not implemented" );return 0.0;}
    00932                         vec mean() const
    00933                         {
    00934                                 vec pom=zeros ( dim );
    00935                                 for ( int i=0;i<n;i++ ) {pom+=samples ( i ) *w ( i );}
    00936                                 return pom;
    00937                         }
    00938                         vec variance() const
    00939                         {
    00940                                 vec pom=zeros ( dim );
    00941                                 for ( int i=0;i<n;i++ ) {pom+=pow ( samples ( i ),2 ) *w ( i );}
    00942                                 return pom-pow ( mean(),2 );
    00943                         }
    00945                         void qbounds ( vec &lb, vec &ub, double perc=0.95 ) const
    00946                         {
    00947                                 // lb in inf so than it will be pushed below;
    00948                                 lb.set_size ( dim );
    00949                                 ub.set_size ( dim );
    00950                                 lb = std::numeric_limits<double>::infinity();
    00951                                 ub = -std::numeric_limits<double>::infinity();
    00952                                 int j;
    00953                                 for ( int i=0;i<n;i++ )
    00954                                 {
    00955                                         for ( j=0;j<dim; j++ )
    00956                                         {
    00957                                                 if ( samples ( i ) ( j ) <lb ( j ) ) {lb ( j ) =samples ( i ) ( j );}
    00958                                                 if ( samples ( i ) ( j ) >ub ( j ) ) {ub ( j ) =samples ( i ) ( j );}
    00959                                         }
    00960                                 }
    00961                         }
    00962         };
    00963
    00964
    00966
    00967         template<class sq_T>
    00968         void enorm<sq_T>::set_parameters ( const vec &mu0, const sq_T &R0 )
    00969         {
    00970 //Fixme test dimensions of mu0 and R0;
    00971                 mu = mu0;
    00972                 R = R0;
    00973                 dim = mu0.length();
    00974         };
    00975
    00976         template<class sq_T>
    00977         void enorm<sq_T>::dupdate ( mat &v, double nu )
    00978         {
    00979                 //
    00980         };
    00981
    00982 // template<class sq_T>
    00983 // void enorm<sq_T>::tupdate ( double phi, mat &vbar, double nubar ) {
    00984 //      //
    00985 // };
    00986
    00987         template<class sq_T>
    00988         vec enorm<sq_T>::sample() const
    00989         {
    00990                 vec x ( dim );
    00991 #pragma omp critical
    00992                 NorRNG.sample_vector ( dim,x );
    00993                 vec smp = R.sqrt_mult ( x );
    00994
    00995                 smp += mu;
    00996                 return smp;
    00997         };
    00998
    00999         template<class sq_T>
    01000         mat enorm<sq_T>::sample ( int N ) const
    01001         {
    01002                 mat X ( dim,N );
    01003                 vec x ( dim );
    01004                 vec pom;
    01005                 int i;
    01006
    01007                 for ( i=0;i<N;i++ )
    01008                 {
    01009 #pragma omp critical
    01010                         NorRNG.sample_vector ( dim,x );
    01011                         pom = R.sqrt_mult ( x );
    01012                         pom +=mu;
    01013                         X.set_col ( i, pom );
    01014                 }
    01015
    01016                 return X;
    01017         };
    01018
    01019 // template<class sq_T>
    01020 // double enorm<sq_T>::eval ( const vec &val ) const {
    01021 //      double pdfl,e;
    01022 //      pdfl = evallog ( val );
    01023 //      e = exp ( pdfl );
    01024 //      return e;
    01025 // };
    01026
    01027         template<class sq_T>
    01028         double enorm<sq_T>::evallog_nn ( const vec &val ) const
    01029         {
    01030                 // 1.83787706640935 = log(2pi)
    01031                 double tmp=-0.5* ( R.invqform ( mu-val ) );// - lognc();
    01032                 return  tmp;
    01033         };
    01034
    01035         template<class sq_T>
    01036         inline double enorm<sq_T>::lognc () const
    01037         {
    01038                 // 1.83787706640935 = log(2pi)
    01039                 double tmp=0.5* ( R.cols() * 1.83787706640935 +R.logdet() );
    01040                 return tmp;
    01041         };
    01042
    01043         template<class sq_T>
    01044         void mlnorm<sq_T>::set_parameters ( const mat &A0, const vec &mu0, const sq_T &R0 )
    01045         {
    01046                 it_assert_debug ( A0.rows() ==mu0.length(),"" );
    01047                 it_assert_debug ( A0.rows() ==R0.rows(),"" );
    01048
    01049                 epdf.set_parameters ( zeros ( A0.rows() ),R0 );
    01050                 A = A0;
    01051                 mu_const = mu0;
    01052                 dimc=A0.cols();
    01053         }
    01054
    01055 // template<class sq_T>
    01056 // vec mlnorm<sq_T>::samplecond (const  vec &cond, double &lik ) {
    01057 //      this->condition ( cond );
    01058 //      vec smp = epdf.sample();
    01059 //      lik = epdf.eval ( smp );
    01060 //      return smp;
    01061 // }
    01062
    01063 // template<class sq_T>
    01064 // mat mlnorm<sq_T>::samplecond (const vec &cond, vec &lik, int n ) {
    01065 //      int i;
    01066 //      int dim = rv.count();
    01067 //      mat Smp ( dim,n );
    01068 //      vec smp ( dim );
    01069 //      this->condition ( cond );
    01070 //
    01071 //      for ( i=0; i<n; i++ ) {
    01072 //              smp = epdf.sample();
    01073 //              lik ( i ) = epdf.eval ( smp );
    01074 //              Smp.set_col ( i ,smp );
    01075 //      }
    01076 //
    01077 //      return Smp;
    01078 // }
    01079
    01080         template<class sq_T>
    01081         void mlnorm<sq_T>::condition ( const vec &cond )
    01082         {
    01083                 _mu = A*cond + mu_const;
    01084 //R is already assigned;
    01085         }
    01086
    01087         template<class sq_T>
    01088         enorm<sq_T>* enorm<sq_T>::marginal ( const RV &rvn ) const
    01089         {
    01090                 it_assert_debug ( isnamed(), "rv description is not assigned" );
    01091                 ivec irvn = rvn.dataind ( rv );
    01092
    01093                 sq_T Rn ( R,irvn ); //select rows and columns of R
    01094
    01095                 enorm<sq_T>* tmp = new enorm<sq_T>;
    01096                 tmp->set_rv ( rvn );
    01097                 tmp->set_parameters ( mu ( irvn ), Rn );
    01098                 return tmp;
    01099         }
    01100
    01101         template<class sq_T>
    01102         mpdf* enorm<sq_T>::condition ( const RV &rvn ) const
    01103         {
    01104
    01105                 it_assert_debug ( isnamed(),"rvs are not assigned" );
    01106
    01107                 RV rvc = rv.subt ( rvn );
    01108                 it_assert_debug ( ( rvc._dsize() +rvn._dsize() ==rv._dsize() ),"wrong rvn" );
    01109                 //Permutation vector of the new R
    01110                 ivec irvn = rvn.dataind ( rv );
    01111                 ivec irvc = rvc.dataind ( rv );
    01112                 ivec perm=concat ( irvn , irvc );
    01113                 sq_T Rn ( R,perm );
    01114
    01115                 //fixme - could this be done in general for all sq_T?
    01116                 mat S=Rn.to_mat();
    01117                 //fixme
    01118                 int n=rvn._dsize()-1;
    01119                 int end=R.rows()-1;
    01120                 mat S11 = S.get ( 0,n, 0, n );
    01121                 mat S12 = S.get ( 0, n , rvn._dsize(), end );
    01122                 mat S22 = S.get ( rvn._dsize(), end, rvn._dsize(), end );
    01123
    01124                 vec mu1 = mu ( irvn );
    01125                 vec mu2 = mu ( irvc );
    01126                 mat A=S12*inv ( S22 );
    01127                 sq_T R_n ( S11 - A *S12.T() );
    01128
    01129                 mlnorm<sq_T>* tmp=new mlnorm<sq_T> ( );
    01130                 tmp->set_rv ( rvn ); tmp->set_rvc ( rvc );
    01131                 tmp->set_parameters ( A,mu1-A*mu2,R_n );
    01132                 return tmp;
    01133         }
    01134
    01136
    01137         template<class sq_T>
    01138         std::ostream &operator<< ( std::ostream &os,  mlnorm<sq_T> &ml )
    01139         {
    01140                 os << "A:"<< ml.A<<endl;
    01141                 os << "mu:"<< ml.mu_const<<endl;
    01142                 os << "R:" << ml.epdf._R().to_mat() <<endl;
    01143                 return os;
    01144         };
    01145
    01146 }
    01147 #endif //EF_H