Changeset 198 for bdm/stat/libEF.h

Show
Ignore:
Timestamp:
11/04/08 14:54:34 (16 years ago)
Author:
smidl
Message:

opravy + zavedeni studenta + zakomentovani debug v mergeru

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • bdm/stat/libEF.h

    r197 r198  
    9494        //!Flatten the posterior as if to keep nu0 data 
    9595//      virtual void flatten ( double nu0 ) {it_error ( "Not implemented" );} 
    96          
     96 
    9797        BMEF* _copy_ ( bool changerv=false ) {it_error ( "function _copy_ not implemented for this BM" ); return NULL;}; 
    9898}; 
     
    192192        //! returns a pointer to the internal statistics. Use with Care! 
    193193        double& _nu() {return nu;} 
    194         void pow ( double p ){V*=p;nu*=p;}; 
     194        void pow ( double p ) {V*=p;nu*=p;}; 
    195195}; 
    196196 
     
    239239        //! Conjugate prior and posterior 
    240240        eDirich est; 
    241         //! Pointer inside est to sufficient statistics  
     241        //! Pointer inside est to sufficient statistics 
    242242        vec β 
    243243public: 
     
    271271                // sum(beta) should be equal to sum(B.beta) 
    272272                const vec &Eb=E->beta;//const_cast<multiBM*> ( E )->_beta(); 
    273                 beta*= ( sum ( Eb) /sum ( beta ) ); 
     273                beta*= ( sum ( Eb ) /sum ( beta ) ); 
    274274                if ( evalll ) {last_lognc=est.lognc();} 
    275275        } 
     
    372372template<class sq_T> 
    373373class mlnorm : public mEF { 
     374protected: 
    374375        //! Internal epdf that arise by conditioning on \c rvc 
    375376        enorm<sq_T> epdf; 
     
    379380public: 
    380381        //! Constructor 
    381         mlnorm (const RV &rv, const RV &rvc ); 
     382        mlnorm ( const RV &rv, const RV &rvc ); 
    382383        //! Set \c A and \c R 
    383384        void set_parameters ( const  mat &A, const vec &mu0, const sq_T &R ); 
     
    387388//      mat samplecond (const vec &cond, vec &lik, int n ); 
    388389        //! Set value of \c rvc . Result of this operation is stored in \c epdf use function \c _ep to access it. 
    389         void condition (const vec &cond ); 
    390          
     390        void condition ( const vec &cond ); 
     391 
     392        //!access function 
     393        vec& _mu_const() {return mu_const;} 
     394        //!access function 
     395        mat& _A() {return A;} 
     396        //!access function 
     397        mat _R() {return epdf._R().to_mat();} 
     398 
    391399        template<class sq_M> 
    392400        friend std::ostream &operator<< ( std::ostream &os,  mlnorm<sq_M> &ml ); 
    393401}; 
    394402 
     403/*! (Approximate) Student t density with linear function of mean value 
     404*/ 
     405class mlstudent : public mlnorm<ldmat> { 
     406protected: 
     407        ldmat Lambda; 
     408        ldmat &_R; 
     409        ldmat Re; 
     410public: 
     411        mlstudent ( const RV &rv0, const RV &rvc0 ) :mlnorm<ldmat> ( rv0,rvc0 ), 
     412                        Lambda ( rv0.count() ), 
     413                        _R ( epdf._R() ) {} 
     414        void set_parameters ( const mat &A0, const vec &mu0, const ldmat &R0, const ldmat& Lambda0) { 
     415                epdf.set_parameters ( zeros ( rv.count() ),Lambda ); 
     416                A = A0; 
     417                mu_const = mu0; 
     418                Re=R0; 
     419                Lambda = Lambda0; 
     420        } 
     421        void condition ( const vec &cond ) { 
     422                _mu = A*cond + mu_const; 
     423                double zeta; 
     424                //ugly hack! 
     425                if ((cond.length()+1)==Lambda.rows()){ 
     426                        zeta = Lambda.invqform ( concat(cond, vec_1(1.0)) ); 
     427                } else { 
     428                        zeta = Lambda.invqform ( cond ); 
     429                } 
     430                _R = Re; 
     431                _R*=( 1+zeta );// / ( nu ); << nu is in Re!!!!!! 
     432                 
     433                cout << _mu <<" +- " << sqrt(_R.to_mat()) << endl; 
     434        }; 
     435 
     436}; 
    395437/*! 
    396438\brief  Gamma random walk 
     
    548590double enorm<sq_T>::evalpdflog_nn ( const vec &val ) const { 
    549591        // 1.83787706640935 = log(2pi) 
    550         return  -0.5* ( R.invqform ( mu-val ) );// - lognc(); 
     592        double tmp=-0.5* ( R.invqform ( mu-val ) );// - lognc(); 
     593        return  tmp; 
    551594}; 
    552595 
     
    554597inline double enorm<sq_T>::lognc () const { 
    555598        // 1.83787706640935 = log(2pi) 
    556         return 0.5* ( R.cols() * 1.83787706640935 +R.logdet() ); 
     599        double tmp=0.5* ( R.cols() * 1.83787706640935 +R.logdet() ); 
     600        return tmp; 
    557601}; 
    558602 
     
    584628//      vec smp ( dim ); 
    585629//      this->condition ( cond ); 
    586 //  
     630// 
    587631//      for ( i=0; i<n; i++ ) { 
    588632//              smp = epdf.sample(); 
     
    590634//              Smp.set_col ( i ,smp ); 
    591635//      } 
    592 //  
     636// 
    593637//      return Smp; 
    594638// } 
    595639 
    596640template<class sq_T> 
    597 void mlnorm<sq_T>::condition (const vec &cond ) { 
     641void mlnorm<sq_T>::condition ( const vec &cond ) { 
    598642        _mu = A*cond + mu_const; 
    599643//R is already assigned; 
     
    605649 
    606650        sq_T Rn ( R,irvn ); 
    607         enorm<sq_T>* tmp = new enorm<sq_T>( rvn ); 
     651        enorm<sq_T>* tmp = new enorm<sq_T> ( rvn ); 
    608652        tmp->set_parameters ( mu ( irvn ), Rn ); 
    609653        return tmp; 
     
    627671        int end=R.rows()-1; 
    628672        mat S11 = S.get ( 0,n, 0, n ); 
    629         mat S12 = S.get (0, n , rvn.count(), end ); 
     673        mat S12 = S.get ( 0, n , rvn.count(), end ); 
    630674        mat S22 = S.get ( rvn.count(), end, rvn.count(), end ); 
    631675 
     
    644688 
    645689template<class sq_T> 
    646 std::ostream &operator<< ( std::ostream &os,  mlnorm<sq_T> &ml ){ 
     690std::ostream &operator<< ( std::ostream &os,  mlnorm<sq_T> &ml ) { 
    647691        os << "A:"<< ml.A<<endl; 
    648692        os << "mu:"<< ml.mu_const<<endl;