- 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                         void mean_mat ( mat &M, mat&R ) const;
    00205                         double evallog_nn ( const vec &val ) const;
    00206                         double lognc () const;
    00207                         void pow ( double p ) {V*=p;nu*=p;};
    00208
    00211
    00212                         ldmat& _V() {return V;}
    00213                         const ldmat& _V() const {return V;}
    00214                         double& _nu()  {return nu;}
    00215                         const double& _nu() const {return nu;}
    00217         };
    00218
    00227         class eDirich: public eEF
    00228         {
    00229                 protected:
    00231                         vec beta;
    00233                         double gamma;
    00234                 public:
    00237
    00238                         eDirich () : eEF ( ) {};
    00239                         eDirich ( const eDirich &D0 ) : eEF () {set_parameters ( D0.beta );};
    00240                         eDirich ( const vec &beta0 ) {set_parameters ( beta0 );};
    00241                         void set_parameters ( const vec &beta0 )
    00242                         {
    00243                                 beta= beta0;
    00244                                 dim = beta.length();
    00245                                 gamma = sum ( beta );
    00246                         }
    00248
    00249                         vec sample() const {it_error ( "Not implemented" );return vec_1 ( 0.0 );};
    00250                         vec mean() const {return beta/gamma;};
    00251                         vec variance() const {return elem_mult ( beta, ( beta+1 ) ) / ( gamma* ( gamma+1 ) );}
    00253                         double evallog_nn ( const vec &val ) const
    00254                         {
    00255                                 double tmp; tmp= ( beta-1 ) *log ( val );               it_assert_debug ( std::isfinite ( tmp ),"Infinite value" );
    00256                                 return tmp;
    00257                         };
    00258                         double lognc () const
    00259                         {
    00260                                 double tmp;
    00261                                 double gam=sum ( beta );
    00262                                 double lgb=0.0;
    00263                                 for ( int i=0;i<beta.length();i++ ) {lgb+=lgamma ( beta ( i ) );}
    00264                                 tmp= lgb-lgamma ( gam );
    00265                                 it_assert_debug ( std::isfinite ( tmp ),"Infinite value" );
    00266                                 return tmp;
    00267                         };
    00269                         vec& _beta()  {return beta;}
    00271         };
    00272
    00274         class multiBM : public BMEF
    00275         {
    00276                 protected:
    00278                         eDirich est;
    00280                         vec &beta;
    00281                 public:
    00283                         multiBM ( ) : BMEF ( ),est ( ),beta ( est._beta() )
    00284                         {
    00285                                 if ( beta.length() >0 ) {last_lognc=est.lognc();}
    00286                                 else{last_lognc=0.0;}
    00287                         }
    00289                         multiBM ( const multiBM &B ) : BMEF ( B ),est ( B.est ),beta ( est._beta() ) {}
    00291                         void set_statistics ( const BM* mB0 ) {const multiBM* mB=dynamic_cast<const multiBM*> ( mB0 ); beta=mB->beta;}
    00292                         void bayes ( const vec &dt )
    00293                         {
    00294                                 if ( frg<1.0 ) {beta*=frg;last_lognc=est.lognc();}
    00295                                 beta+=dt;
    00296                                 if ( evalll ) {ll=est.lognc()-last_lognc;}
    00297                         }
    00298                         double logpred ( const vec &dt ) const
    00299                         {
    00300                                 eDirich pred ( est );
    00301                                 vec &beta = pred._beta();
    00302
    00303                                 double lll;
    00304                                 if ( frg<1.0 )
    00305                                         {beta*=frg;lll=pred.lognc();}
    00306                                 else
    00307                                         if ( evalll ) {lll=last_lognc;}
    00308                                         else{lll=pred.lognc();}
    00309
    00310                                 beta+=dt;
    00311                                 return pred.lognc()-lll;
    00312                         }
    00313                         void flatten ( const BMEF* B )
    00314                         {
    00315                                 const multiBM* E=dynamic_cast<const multiBM*> ( B );
    00316                                 // sum(beta) should be equal to sum(B.beta)
    00317                                 const vec &Eb=E->beta;//const_cast<multiBM*> ( E )->_beta();
    00318                                 beta*= ( sum ( Eb ) /sum ( beta ) );
    00319                                 if ( evalll ) {last_lognc=est.lognc();}
    00320                         }
    00321                         const epdf& posterior() const {return est;};
    00322                         const eDirich* _e() const {return &est;};
    00323                         void set_parameters ( const vec &beta0 )
    00324                         {
    00325                                 est.set_parameters ( beta0 );
    00326                                 if ( evalll ) {last_lognc=est.lognc();}
    00327                         }
    00328         };
    00329
    00339         class egamma : public eEF
    00340         {
    00341                 protected:
    00343                         vec alpha;
    00345                         vec beta;
    00346                 public :
    00349                         egamma ( ) :eEF ( ), alpha ( 0 ), beta ( 0 ) {};
    00350                         egamma ( const vec &a, const vec &b ) {set_parameters ( a, b );};
    00351                         void set_parameters ( const vec &a, const vec &b ) {alpha=a,beta=b;dim = alpha.length();};
    00353
    00354                         vec sample() const;
    00356 //      mat sample ( int N ) const;
    00357                         double evallog ( const vec &val ) const;
    00358                         double lognc () const;
    00360                         vec& _alpha() {return alpha;}
    00361                         vec& _beta() {return beta;}
    00362                         vec mean() const {return elem_div ( alpha,beta );}
    00363                         vec variance() const {return elem_div ( alpha,elem_mult ( beta,beta ) ); }
    00364         };
    00365
    00382         class eigamma : public egamma
    00383         {
    00384                 protected:
    00385                 public :
    00390
    00391                         vec sample() const {return 1.0/egamma::sample();};
    00393                         vec mean() const {return elem_div ( beta,alpha-1 );}
    00394                         vec variance() const {vec mea=mean(); return elem_div ( elem_mult ( mea,mea ),alpha-2 );}
    00395         };
    00396         /*
    00398         class emix : public epdf {
    00399         protected:
    00400                 int n;
    00401                 vec &w;
    00402                 Array<epdf*> Coms;
    00403         public:
    00405                 emix ( const RV &rv, vec &w0): epdf(rv), n(w0.length()), w(w0), Coms(n) {};
    00406                 void set_parameters( int &i, double wi, epdf* ep){w(i)=wi;Coms(i)=ep;}
    00407                 vec mean(){vec pom; for(int i=0;i<n;i++){pom+=Coms(i)->mean()*w(i);} return pom;};
    00408                 vec sample() {it_error ( "Not implemented" );return 0;}
    00409         };
    00410         */
    00411
    00413
    00414         class euni: public epdf
    00415         {
    00416                 protected:
    00418                         vec low;
    00420                         vec high;
    00422                         vec distance;
    00424                         double nk;
    00426                         double lnk;
    00427                 public:
    00430                         euni ( ) :epdf ( ) {}
    00431                         euni ( const vec &low0, const vec &high0 ) {set_parameters ( low0,high0 );}
    00432                         void set_parameters ( const vec &low0, const vec &high0 )
    00433                         {
    00434                                 distance = high0-low0;
    00435                                 it_assert_debug ( min ( distance ) >0.0,"bad support" );
    00436                                 low = low0;
    00437                                 high = high0;
    00438                                 nk = prod ( 1.0/distance );
    00439                                 lnk = log ( nk );
    00440                                 dim = low.length();
    00441                         }
    00443
    00444                         double eval ( const vec &val ) const  {return nk;}
    00445                         double evallog ( const vec &val ) const  {return lnk;}
    00446                         vec sample() const
    00447                         {
    00448                                 vec smp ( dim );
    00449 #pragma omp critical
    00450                                 UniRNG.sample_vector ( dim ,smp );
    00451                                 return low+elem_mult ( distance,smp );
    00452                         }
    00454                         vec mean() const {return ( high-low ) /2.0;}
    00455                         vec variance() const {return ( pow ( high,2 ) +pow ( low,2 ) +elem_mult ( high,low ) ) /3.0;}
    00456         };
    00457
    00458
    00464         template<class sq_T>
    00465         class mlnorm : public mEF
    00466         {
    00467                 protected:
    00469                         enorm<sq_T> epdf;
    00470                         mat A;
    00471                         vec mu_const;
    00472                         vec& _mu; //cached epdf.mu;
    00473                 public:
    00476                         mlnorm ( ) :mEF (),epdf ( ),A ( ),_mu ( epdf._mu() ) {ep =&epdf; };
    00477                         mlnorm ( const  mat &A, const vec &mu0, const sq_T &R ) :epdf ( ),_mu ( epdf._mu() )
    00478                         {
    00479                                 ep =&epdf; set_parameters ( A,mu0,R );
    00480                         };
    00482                         void set_parameters ( const  mat &A, const vec &mu0, const sq_T &R );
    00485                         void condition ( const vec &cond );
    00486
    00488                         vec& _mu_const() {return mu_const;}
    00490                         mat& _A() {return A;}
    00492                         mat _R() {return epdf._R().to_mat();}
    00493
    00494                         template<class sq_M>
    00495                         friend std::ostream &operator<< ( std::ostream &os,  mlnorm<sq_M> &ml );
    00496         };
    00497
    00499         template<class sq_T>
    00500         class mgnorm : public mEF
    00501         {
    00502                 protected:
    00504                         enorm<sq_T> epdf;
    00505                         vec &mu;
    00506                         fnc* g;
    00507                 public:
    00509                         mgnorm() :mu ( epdf._mu() ) {ep=&epdf;}
    00511                         void set_parameters ( fnc* g0, const sq_T &R0 ) {g=g0; epdf.set_parameters ( zeros ( g->dimension() ), R0 );}
    00512                         void condition ( const vec &cond ) {mu=g->eval ( cond );};
    00513         };
    00514
    00522         class mlstudent : public mlnorm<ldmat>
    00523         {
    00524                 protected:
    00525                         ldmat Lambda;
    00526                         ldmat &_R;
    00527                         ldmat Re;
    00528                 public:
    00529                         mlstudent ( ) :mlnorm<ldmat> (),
    00530                                         Lambda (),      _R ( epdf._R() ) {}
    00531                         void set_parameters ( const mat &A0, const vec &mu0, const ldmat &R0, const ldmat& Lambda0 )
    00532                         {
    00533                                 it_assert_debug ( A0.rows() ==mu0.length(),"" );
    00534                                 it_assert_debug ( R0.rows() ==A0.rows(),"" );
    00535
    00536                                 epdf.set_parameters ( mu0,Lambda ); //
    00537                                 A = A0;
    00538                                 mu_const = mu0;
    00539                                 Re=R0;
    00540                                 Lambda = Lambda0;
    00541                         }
    00542                         void condition ( const vec &cond )
    00543                         {
    00544                                 _mu = A*cond + mu_const;
    00545                                 double zeta;
    00546                                 //ugly hack!
    00547                                 if ( ( cond.length() +1 ) ==Lambda.rows() )
    00548                                 {
    00549                                         zeta = Lambda.invqform ( concat ( cond, vec_1 ( 1.0 ) ) );
    00550                                 }
    00551                                 else
    00552                                 {
    00553                                         zeta = Lambda.invqform ( cond );
    00554                                 }
    00555                                 _R = Re;
    00556                                 _R*= ( 1+zeta );// / ( nu ); << nu is in Re!!!!!!
    00557                         };
    00558
    00559         };
    00569         class mgamma : public mEF
    00570         {
    00571                 protected:
    00573                         egamma epdf;
    00575                         double k;
    00577                         vec &_beta;
    00578
    00579                 public:
    00581                         mgamma ( ) : mEF ( ), epdf (), _beta ( epdf._beta() ) {ep=&epdf;};
    00583                         void set_parameters ( double k, const vec &beta0 );
    00584                         void condition ( const vec &val ) {_beta=k/val;};
    00585         };
    00586
    00596         class migamma : public mEF
    00597         {
    00598                 protected:
    00600                         eigamma epdf;
    00602                         double k;
    00604                         vec &_alpha;
    00606                         vec &_beta;
    00607
    00608                 public:
    00611                         migamma ( ) : mEF (), epdf ( ), _alpha ( epdf._alpha() ), _beta ( epdf._beta() ) {ep=&epdf;};
    00612                         migamma ( const migamma &m ) : mEF (), epdf ( m.epdf ), _alpha ( epdf._alpha() ), _beta ( epdf._beta() ) {ep=&epdf;};
    00614
    00616                         void set_parameters ( int len, double k0 )
    00617                         {
    00618                                 k=k0;
    00619                                 epdf.set_parameters ( ( 1.0/ ( k*k ) +2.0 ) *ones ( len ) /*alpha*/, ones ( len ) /*beta*/ );
    00620                                 dimc = dimension();
    00621                         };
    00622                         void condition ( const vec &val )
    00623                         {
    00624                                 _beta=elem_mult ( val, ( _alpha-1.0 ) );
    00625                         };
    00626         };
    00627
    00639         class mgamma_fix : public mgamma
    00640         {
    00641                 protected:
    00643                         double l;
    00645                         vec refl;
    00646                 public:
    00648                         mgamma_fix ( ) : mgamma ( ),refl () {};
    00650                         void set_parameters ( double k0 , vec ref0, double l0 )
    00651                         {
    00652                                 mgamma::set_parameters ( k0, ref0 );
    00653                                 refl=pow ( ref0,1.0-l0 );l=l0;
    00654                                 dimc=dimension();
    00655                         };
    00656
    00657                         void condition ( const vec &val ) {vec mean=elem_mult ( refl,pow ( val,l ) ); _beta=k/mean;};
    00658         };
    00659
    00660
    00673         class migamma_ref : public migamma
    00674         {
    00675                 protected:
    00677                         double l;
    00679                         vec refl;
    00680                 public:
    00682                         migamma_ref ( ) : migamma (),refl ( ) {};
    00684                         void set_parameters ( double k0 , vec ref0, double l0 )
    00685                         {
    00686                                 migamma::set_parameters ( ref0.length(), k0 );
    00687                                 refl=pow ( ref0,1.0-l0 );
    00688                                 l=l0;
    00689                                 dimc = dimension();
    00690                         };
    00691
    00692                         void condition ( const vec &val )
    00693                         {
    00694                                 vec mean=elem_mult ( refl,pow ( val,l ) );
    00695                                 migamma::condition ( mean );
    00696                         };
    00697         };
    00698
    00708         class elognorm: public enorm<ldmat>
    00709         {
    00710                 public:
    00711                         vec sample() const {return exp ( enorm<ldmat>::sample() );};
    00712                         vec mean() const {vec var=enorm<ldmat>::variance();return exp ( mu - 0.5*var );};
    00713
    00714         };
    00715
    00727         class mlognorm : public mpdf
    00728         {
    00729                 protected:
    00730                         elognorm eno;
    00732                         double sig2;
    00734                         vec &mu;
    00735                 public:
    00737                         mlognorm ( ) : eno (), mu ( eno._mu() ) {ep=&eno;};
    00739                         void set_parameters ( int size, double k )
    00740                         {
    00741                                 sig2 = 0.5*log ( k*k+1 );
    00742                                 eno.set_parameters ( zeros ( size ),2*sig2*eye ( size ) );
    00743
    00744                                 dimc = size;
    00745                         };
    00746
    00747                         void condition ( const vec &val )
    00748                         {
    00749                                 mu=log ( val )-sig2;//elem_mult ( refl,pow ( val,l ) );
    00750                         };
    00751         };
    00752
    00756         class eWishartCh : public epdf
    00757         {
    00758                 protected:
    00760                         chmat Y;
    00762                         int p;
    00764                         double delta;
    00765                 public:
    00766                         void set_parameters ( const mat &Y0, const double delta0 ) {Y=chmat ( Y0 );delta=delta0; p=Y.rows(); dim = p*p; }
    00767                         mat sample_mat() const
    00768                         {
    00769                                 mat X=zeros ( p,p );
    00770
    00771                                 //sample diagonal
    00772                                 for ( int i=0;i<p;i++ )
    00773                                 {
    00774                                         GamRNG.setup ( 0.5* ( delta-i ) , 0.5 ); // no +1 !! index if from 0
    00775 #pragma omp critical
    00776                                         X ( i,i ) =sqrt ( GamRNG() );
    00777                                 }
    00778                                 //do the rest
    00779                                 for ( int i=0;i<p;i++ )
    00780                                 {
    00781                                         for ( int j=i+1;j<p;j++ )
    00782                                         {
    00783 #pragma omp critical
    00784                                                 X ( i,j ) =NorRNG.sample();
    00785                                         }
    00786                                 }
    00787                                 return X*Y._Ch();// return upper triangular part of the decomposition
    00788                         }
    00789                         vec sample () const
    00790                         {
    00791                                 return vec ( sample_mat()._data(),p*p );
    00792                         }
    00794                         void setY ( const mat &Ch0 ) {copy_vector ( dim,Ch0._data(), Y._Ch()._data() );}
    00796                         void _setY ( const vec &ch0 ) {copy_vector ( dim, ch0._data(), Y._Ch()._data() ); }
    00798                         const chmat& getY()const {return Y;}
    00799         };
    00800
    00801         class eiWishartCh: public epdf
    00802         {
    00803                 protected:
    00804                         eWishartCh W;
    00805                         int p;
    00806                         double delta;
    00807                 public:
    00808                         void set_parameters ( const mat &Y0, const double delta0) {
    00809                                 delta = delta0;
    00810                                 W.set_parameters ( inv ( Y0 ),delta0 );
    00811                                 dim = W.dimension(); p=Y0.rows();
    00812                         }
    00813                         vec sample() const {mat iCh; iCh=inv ( W.sample_mat() ); return vec ( iCh._data(),dim );}
    00814                         void _setY ( const vec &y0 )
    00815                         {
    00816                                 mat Ch ( p,p );
    00817                                 mat iCh ( p,p );
    00818                                 copy_vector ( dim, y0._data(), Ch._data() );
    00819
    00820                                 iCh=inv ( Ch );
    00821                                 W.setY ( iCh );
    00822                         }
    00823                         virtual double evallog ( const vec &val ) const {
    00824                                 chmat X(p);
    00825                                 const chmat& Y=W.getY();
    00826
    00827                                 copy_vector(p*p,val._data(),X._Ch()._data());
    00828                                 chmat iX(p);X.inv(iX);
    00829                                 // compute
    00830 //                              \frac{ |\Psi|^{m/2}|X|^{-(m+p+1)/2}e^{-tr(\Psi X^{-1})/2} }{ 2^{mp/2}\Gamma_p(m/2)},
    00831                                 mat M=Y.to_mat()*iX.to_mat();
    00832
    00833                                 double log1 = 0.5*p*(2*Y.logdet())-0.5*(delta+p+1)*(2*X.logdet())-0.5*trace(M);
    00834                                 //Fixme! Multivariate gamma omitted!! it is ok for sampling, but not otherwise!!
    00835
    00836 /*                              if (0) {
    00837                                         mat XX=X.to_mat();
    00838                                         mat YY=Y.to_mat();
    00839
    00840                                         double log2 = 0.5*p*log(det(YY))-0.5*(delta+p+1)*log(det(XX))-0.5*trace(YY*inv(XX));
    00841                                         cout << log1 << "," << log2 << endl;
    00842                                 }*/
    00843                                 return log1;
    00844                         };
    00845
    00846         };
    00847
    00848         class rwiWishartCh : public mpdf
    00849         {
    00850                 protected:
    00851                         eiWishartCh eiW;
    00853                         double sqd;
    00854                         //reference point for diagonal
    00855                         vec refl;
    00856                         double l;
    00857                         int p;
    00858                 public:
    00859                         void set_parameters ( int p0, double k, vec ref0, double l0  )
    00860                         {
    00861                                 p=p0;
    00862                                 double delta = 2/(k*k)+p+3;
    00863                                 sqd=sqrt ( delta-p-1 );
    00864                                 l=l0;
    00865                                 refl=pow(ref0,1-l);
    00866
    00867                                 eiW.set_parameters ( eye ( p ),delta );
    00868                                 ep=&eiW;
    00869                                 dimc=eiW.dimension();
    00870                         }
    00871                         void condition ( const vec &c ) {
    00872                                 vec z=c;
    00873                                 int ri=0;
    00874                                 for(int i=0;i<p*p;i+=(p+1)){//trace diagonal element
    00875                                         z(i) = pow(z(i),l)*refl(ri);
    00876                                         ri++;
    00877                                 }
    00878
    00879                                 eiW._setY ( sqd*z );
    00880                         }
    00881         };
    00882
    00884         enum RESAMPLING_METHOD { MULTINOMIAL = 0, STRATIFIED = 1, SYSTEMATIC = 3 };
    00890         class eEmp: public epdf
    00891         {
    00892                 protected :
    00894                         int n;
    00896                         vec w;
    00898                         Array<vec> samples;
    00899                 public:
    00902                         eEmp ( ) :epdf ( ),w ( ),samples ( ) {};
    00903                         eEmp ( const eEmp &e ) : epdf ( e ), w ( e.w ), samples ( e.samples ) {};
    00905
    00907                         void set_statistics ( const vec &w0, const epdf* pdf0 );
    00909                         void set_statistics ( const epdf* pdf0 , int n ) {set_statistics ( ones ( n ) /n,pdf0 );};
    00911                         void set_samples ( const epdf* pdf0 );
    00913                         void set_parameters ( int n0, bool copy=true ) {n=n0; w.set_size ( n0,copy );samples.set_size ( n0,copy );};
    00915                         vec& _w()  {return w;};
    00917                         const vec& _w() const {return w;};
    00919                         Array<vec>& _samples() {return samples;};
    00921                         const Array<vec>& _samples() const {return samples;};
    00923                         ivec resample ( RESAMPLING_METHOD method=SYSTEMATIC );
    00925                         vec sample() const {it_error ( "Not implemented" );return 0;}
    00927                         double evallog ( const vec &val ) const {it_error ( "Not implemented" );return 0.0;}
    00928                         vec mean() const
    00929                         {
    00930                                 vec pom=zeros ( dim );
    00931                                 for ( int i=0;i<n;i++ ) {pom+=samples ( i ) *w ( i );}
    00932                                 return pom;
    00933                         }
    00934                         vec variance() const
    00935                         {
    00936                                 vec pom=zeros ( dim );
    00937                                 for ( int i=0;i<n;i++ ) {pom+=pow ( samples ( i ),2 ) *w ( i );}
    00938                                 return pom-pow ( mean(),2 );
    00939                         }
    00941                         void qbounds ( vec &lb, vec &ub, double perc=0.95 ) const
    00942                         {
    00943                                 // lb in inf so than it will be pushed below;
    00944                                 lb.set_size ( dim );
    00945                                 ub.set_size ( dim );
    00946                                 lb = std::numeric_limits<double>::infinity();
    00947                                 ub = -std::numeric_limits<double>::infinity();
    00948                                 int j;
    00949                                 for ( int i=0;i<n;i++ )
    00950                                 {
    00951                                         for ( j=0;j<dim; j++ )
    00952                                         {
    00953                                                 if ( samples ( i ) ( j ) <lb ( j ) ) {lb ( j ) =samples ( i ) ( j );}
    00954                                                 if ( samples ( i ) ( j ) >ub ( j ) ) {ub ( j ) =samples ( i ) ( j );}
    00955                                         }
    00956                                 }
    00957                         }
    00958         };
    00959
    00960
    00962
    00963         template<class sq_T>
    00964         void enorm<sq_T>::set_parameters ( const vec &mu0, const sq_T &R0 )
    00965         {
    00966 //Fixme test dimensions of mu0 and R0;
    00967                 mu = mu0;
    00968                 R = R0;
    00969                 dim = mu0.length();
    00970         };
    00971
    00972         template<class sq_T>
    00973         void enorm<sq_T>::dupdate ( mat &v, double nu )
    00974         {
    00975                 //
    00976         };
    00977
    00978 // template<class sq_T>
    00979 // void enorm<sq_T>::tupdate ( double phi, mat &vbar, double nubar ) {
    00980 //      //
    00981 // };
    00982
    00983         template<class sq_T>
    00984         vec enorm<sq_T>::sample() const
    00985         {
    00986                 vec x ( dim );
    00987 #pragma omp critical
    00988                 NorRNG.sample_vector ( dim,x );
    00989                 vec smp = R.sqrt_mult ( x );
    00990
    00991                 smp += mu;
    00992                 return smp;
    00993         };
    00994
    00995         template<class sq_T>
    00996         mat enorm<sq_T>::sample ( int N ) const
    00997         {
    00998                 mat X ( dim,N );
    00999                 vec x ( dim );
    01000                 vec pom;
    01001                 int i;
    01002
    01003                 for ( i=0;i<N;i++ )
    01004                 {
    01005 #pragma omp critical
    01006                         NorRNG.sample_vector ( dim,x );
    01007                         pom = R.sqrt_mult ( x );
    01008                         pom +=mu;
    01009                         X.set_col ( i, pom );
    01010                 }
    01011
    01012                 return X;
    01013         };
    01014
    01015 // template<class sq_T>
    01016 // double enorm<sq_T>::eval ( const vec &val ) const {
    01017 //      double pdfl,e;
    01018 //      pdfl = evallog ( val );
    01019 //      e = exp ( pdfl );
    01020 //      return e;
    01021 // };
    01022
    01023         template<class sq_T>
    01024         double enorm<sq_T>::evallog_nn ( const vec &val ) const
    01025         {
    01026                 // 1.83787706640935 = log(2pi)
    01027                 double tmp=-0.5* ( R.invqform ( mu-val ) );// - lognc();
    01028                 return  tmp;
    01029         };
    01030
    01031         template<class sq_T>
    01032         inline double enorm<sq_T>::lognc () const
    01033         {
    01034                 // 1.83787706640935 = log(2pi)
    01035                 double tmp=0.5* ( R.cols() * 1.83787706640935 +R.logdet() );
    01036                 return tmp;
    01037         };
    01038
    01039         template<class sq_T>
    01040         void mlnorm<sq_T>::set_parameters ( const mat &A0, const vec &mu0, const sq_T &R0 )
    01041         {
    01042                 it_assert_debug ( A0.rows() ==mu0.length(),"" );
    01043                 it_assert_debug ( A0.rows() ==R0.rows(),"" );
    01044
    01045                 epdf.set_parameters ( zeros ( A0.rows() ),R0 );
    01046                 A = A0;
    01047                 mu_const = mu0;
    01048                 dimc=A0.cols();
    01049         }
    01050
    01051 // template<class sq_T>
    01052 // vec mlnorm<sq_T>::samplecond (const  vec &cond, double &lik ) {
    01053 //      this->condition ( cond );
    01054 //      vec smp = epdf.sample();
    01055 //      lik = epdf.eval ( smp );
    01056 //      return smp;
    01057 // }
    01058
    01059 // template<class sq_T>
    01060 // mat mlnorm<sq_T>::samplecond (const vec &cond, vec &lik, int n ) {
    01061 //      int i;
    01062 //      int dim = rv.count();
    01063 //      mat Smp ( dim,n );
    01064 //      vec smp ( dim );
    01065 //      this->condition ( cond );
    01066 //
    01067 //      for ( i=0; i<n; i++ ) {
    01068 //              smp = epdf.sample();
    01069 //              lik ( i ) = epdf.eval ( smp );
    01070 //              Smp.set_col ( i ,smp );
    01071 //      }
    01072 //
    01073 //      return Smp;
    01074 // }
    01075
    01076         template<class sq_T>
    01077         void mlnorm<sq_T>::condition ( const vec &cond )
    01078         {
    01079                 _mu = A*cond + mu_const;
    01080 //R is already assigned;
    01081         }
    01082
    01083         template<class sq_T>
    01084         enorm<sq_T>* enorm<sq_T>::marginal ( const RV &rvn ) const
    01085         {
    01086                 it_assert_debug ( isnamed(), "rv description is not assigned" );
    01087                 ivec irvn = rvn.dataind ( rv );
    01088
    01089                 sq_T Rn ( R,irvn ); //select rows and columns of R
    01090
    01091                 enorm<sq_T>* tmp = new enorm<sq_T>;
    01092                 tmp->set_rv ( rvn );
    01093                 tmp->set_parameters ( mu ( irvn ), Rn );
    01094                 return tmp;
    01095         }
    01096
    01097         template<class sq_T>
    01098         mpdf* enorm<sq_T>::condition ( const RV &rvn ) const
    01099         {
    01100
    01101                 it_assert_debug ( isnamed(),"rvs are not assigned" );
    01102
    01103                 RV rvc = rv.subt ( rvn );
    01104                 it_assert_debug ( ( rvc._dsize() +rvn._dsize() ==rv._dsize() ),"wrong rvn" );
    01105                 //Permutation vector of the new R
    01106                 ivec irvn = rvn.dataind ( rv );
    01107                 ivec irvc = rvc.dataind ( rv );
    01108                 ivec perm=concat ( irvn , irvc );
    01109                 sq_T Rn ( R,perm );
    01110
    01111                 //fixme - could this be done in general for all sq_T?
    01112                 mat S=Rn.to_mat();
    01113                 //fixme
    01114                 int n=rvn._dsize()-1;
    01115                 int end=R.rows()-1;
    01116                 mat S11 = S.get ( 0,n, 0, n );
    01117                 mat S12 = S.get ( 0, n , rvn._dsize(), end );
    01118                 mat S22 = S.get ( rvn._dsize(), end, rvn._dsize(), end );
    01119
    01120                 vec mu1 = mu ( irvn );
    01121                 vec mu2 = mu ( irvc );
    01122                 mat A=S12*inv ( S22 );
    01123                 sq_T R_n ( S11 - A *S12.T() );
    01124
    01125                 mlnorm<sq_T>* tmp=new mlnorm<sq_T> ( );
    01126                 tmp->set_rv ( rvn ); tmp->set_rvc ( rvc );
    01127                 tmp->set_parameters ( A,mu1-A*mu2,R_n );
    01128                 return tmp;
    01129         }
    01130
    01132
    01133         template<class sq_T>
    01134         std::ostream &operator<< ( std::ostream &os,  mlnorm<sq_T> &ml )
    01135         {
    01136                 os << "A:"<< ml.A<<endl;
    01137                 os << "mu:"<< ml.mu_const<<endl;
    01138                 os << "R:" << ml.epdf._R().to_mat() <<endl;
    01139                 return os;
    01140         };
    01141
    01142 }
    01143 #endif //EF_H