Changeset 168

Show
Ignore:
Timestamp:
09/18/08 19:54:09 (16 years ago)
Author:
smidl
Message:

Work on mixtures of EF, small changes

Files:
15 modified

Legend:

Unmodified
Added
Removed
  • CMakeLists.txt

    r161 r168  
     1# Compulsory line 
     2cmake_minimum_required(VERSION 2.6) 
     3 
    14# The name of our project is "BDM".  CMakeLists files in this project can 
    25# refer to the root source directory of the project as ${BDM_SOURCE_DIR} and 
  • bdm/CMakeLists.txt

    r162 r168  
    33SET(BdmMath math/libDC.cpp math/libDC.h math/chmat.cpp math/chmat.h) 
    44SET(BdmStat stat/libDS.cpp stat/libDS.h stat/libFN.cpp stat/libFN.h stat/libBM.cpp stat/libBM.h stat/libEF.cpp stat/libEF.h stat/loggers.cpp stat/loggers.h stat/emix.cpp stat/emix.h stat/merger.cpp) 
    5 SET(BdmEstim estim/libKF.cpp estim/libKF.h estim/libPF.cpp estim/libPF.h estim/arx.cpp estim/arx.h) 
     5SET(BdmEstim estim/libKF.cpp estim/libKF.h estim/libPF.cpp estim/libPF.h estim/arx.cpp estim/arx.h estim/mixef.cpp estim/mixef.h) 
    66SET(BdmUI userinfo.cpp userinfo.h) 
    77 
  • bdm/estim/arx.h

    r145 r168  
    5050public: 
    5151        //! Full constructor 
    52         ARX (RV &rv, mat &V0, double &nu0, double frg0=1.0) : BM(rv),est(rv,V0,nu0), V(est._V()), nu(est._nu()), frg(frg0){last_lognc=est.lognc();tll=0.0;}; 
     52        ARX (const RV &rv, const mat &V0, const double &nu0, const double frg0=1.0) : BM(rv),est(rv,V0,nu0), V(est._V()), nu(est._nu()), frg(frg0){last_lognc=est.lognc();tll=0.0;}; 
    5353        //! Set sufficient statistics 
    54         void set_parameters(mat &V0, double &nu0){est._V()=V0;est._nu()=nu0;last_lognc=est.lognc();tll=last_lognc;} 
     54        void set_parameters(const mat &V0, const double &nu0){est._V()=V0;est._nu()=nu0;last_lognc=est.lognc();tll=last_lognc;} 
    5555        //! Returns sufficient statistics 
    5656        void get_parameters(mat &V0, double &nu0){V0=est._V().to_mat(); nu0=est._nu();} 
  • bdm/math/chmat.cpp

    r108 r168  
    1717        Ch = R ( 0, Ch.rows()-1, 0, Ch.cols()-1 ); 
    1818}; 
    19 mat chmat::to_mat() {mat F=Ch.T() *Ch;return F;}; 
     19mat chmat::to_mat() const {mat F=Ch.T() *Ch;return F;}; 
    2020void chmat::mult_sym ( const mat &C ) { 
    2121        it_error ( "not implemented" ); 
  • bdm/math/chmat.h

    r108 r168  
    3232 
    3333        void opupdt ( const vec &v, double w ); 
    34         mat to_mat(); 
     34        mat to_mat() const; 
    3535        void mult_sym ( const mat &C ); 
    3636        void mult_sym ( const mat &C , chmat &U ) const; 
  • bdm/math/libDC.cpp

    r162 r168  
    77 
    88void fsqmat::opupdt ( const vec &v, double w ) {M+=outer_product ( v,v*w );}; 
    9 mat fsqmat::to_mat() {return M;}; 
     9mat fsqmat::to_mat() const {return M;}; 
    1010void fsqmat::mult_sym ( const mat &C) {M=C *M*C.T();}; 
    1111void fsqmat::mult_sym_t ( const mat &C) {M=C.T() *M*C;}; 
     
    7878} 
    7979 
    80 mat ldmat::to_mat() { 
     80mat ldmat::to_mat() const { 
    8181        int dim = D.length(); 
    8282        mat V( dim, dim ); 
  • bdm/math/libDC.h

    r98 r168  
    4444                */ 
    4545 
    46                 virtual mat to_mat() =0; 
     46                virtual mat to_mat() const =0; 
    4747 
    4848                /*! \brief Inplace symmetric multiplication by a SQUARE matrix \f$C\f$, i.e. \f$V = C*V*C'\f$ 
     
    115115        public: 
    116116                void opupdt ( const vec &v, double w ); 
    117                 mat to_mat() ; 
     117                mat to_mat() const; 
    118118                void mult_sym ( const mat &C); 
    119119                void mult_sym_t ( const mat &C); 
     
    198198 
    199199                void opupdt ( const vec &v, double w ); 
    200                 mat to_mat(); 
     200                mat to_mat() const; 
    201201                void mult_sym ( const mat &C); 
    202202                void mult_sym_t ( const mat &C); 
  • bdm/stat/emix.cpp

    r165 r168  
    44 
    55void emix::set_parameters ( const vec &w0, const Array<epdf*> &Coms0 ) { 
    6         w = w0; 
     6        w = w0/sum(w0); 
    77        int i; 
    88        for ( i=0;i<w.length();i++ ) { 
     
    3838                }; 
    3939                 
    40                 independent = true; 
     40//              independent = true; 
    4141                //test rvc of mpdfs and fill rvinds 
    4242                for ( i = 0;i < n;i++ ) { 
     
    4848                        rvcinds ( i ) = mpdfs ( i )->_rvc().dataind ( rvc ); 
    4949                        // 
    50                         if ( rvcinds ( i ).length() >0 ) {independent = false;} 
    51                         if ( rvcinds ( i ).length() >0 ) {independent = false;} 
     50/*                      if ( rvcinrv ( i ).length() >0 ) {independent = false;} 
     51                        if ( rvcinds ( i ).length() >0 ) {independent = false;}*/ 
    5252                } 
    5353        }; 
  • bdm/stat/emix.h

    r165 r168  
    8282        //! Indeces of rvc in common rvc 
    8383        Array<ivec> rvcinds; 
    84         //! Indicate independence of its factors 
    85         bool independent; 
     84//      //! Indicate independence of its factors 
     85//      bool independent; 
    8686//      //! Indicate internal creation of mpdfs which must be destroyed 
    8787//      bool intermpdfs; 
    8888public: 
    89         /*!\brief Constructor from list of mFacs,  
     89        /*!\brief Constructor from list of mFacs, 
    9090        Additional parameter overlap is left for future use. Do not set to true for mprod. 
    9191        */ 
    92         mprod ( Array<mpdf*> mFacs, bool overlap=false );  
     92        mprod ( Array<mpdf*> mFacs, bool overlap=false ); 
    9393 
    9494        double evalpdflog ( const vec &val ) const { 
     
    122122                        set_subvector ( smp,rvinds ( i ), smpi ); 
    123123                        // add ith likelihood contribution 
    124                         ll+=epdfs(i)->evalpdflog(smpi); 
     124                        ll+=epdfs ( i )->evalpdflog ( smpi ); 
    125125                } 
    126126                return smp; 
    127127        } 
    128128        mat samplecond ( const vec &cond, vec &ll, int N ) { 
    129                 mat Smp(rv.count(),N); 
    130                 for(int i=0;i<N;i++){Smp.set_col(i,samplecond(cond,ll(i)));} 
     129                mat Smp ( rv.count(),N ); 
     130                for ( int i=0;i<N;i++ ) {Smp.set_col ( i,samplecond ( cond,ll ( i ) ) );} 
    131131                return Smp; 
    132132        } 
    133 //      vec mean() const { 
    134 //              vec tmp ( rv.count() ); 
    135 //              if ( independent ) { 
    136 //                      for ( int i=0;i<n;i++ ) { 
    137 //                              vec pom = epdfs ( i )->mean(); 
    138 //                              set_subvector ( tmp,rvinds ( i ), pom ); 
    139 //                      } 
    140 //              } 
    141 //              else { 
    142 //                      int N=50*rv.count(); 
    143 //                      it_warning ( "eprod.mean() computed by sampling" ); 
    144 //                      tmp = zeros ( rv.count() ); 
    145 //                      for ( int i=0;i<N;i++ ) { tmp += sample();} 
    146 //                      tmp /=N; 
    147 //              } 
    148 //              return tmp; 
    149 //      } 
    150133 
    151134        ~mprod() {}; 
    152135}; 
     136 
     137//! Product of independent epdfs. For dependent pdfs, use mprod. 
     138class eprod: public epdf { 
     139protected: 
     140        //! Components (epdfs) 
     141        Array<epdf*> epdfs; 
     142        //! Array of indeces 
     143        Array<ivec> rvinds; 
     144public: 
     145        eprod ( const Array<epdf*> epdfs0 ) : epdf ( RV() ),epdfs ( epdfs0 ),rvinds ( epdfs.length() ) { 
     146                bool independent=true; 
     147                for ( int i=0;i<epdfs.length();i++ ) { 
     148                        independent=rv.add ( epdfs ( i )->_rv() ); 
     149                        it_assert_debug ( independent==true, "eprod:: given components are not independent ." ); 
     150                        rvinds ( i ) = ( epdfs ( i )->_rv() ).dataind ( rv ); 
     151                } 
     152        } 
     153 
     154        vec mean() const { 
     155                vec tmp ( rv.count() ); 
     156                for ( int i=0;i<epdfs.length();i++ ) { 
     157                        vec pom = epdfs ( i )->mean(); 
     158                        set_subvector ( tmp,rvinds ( i ), pom ); 
     159                } 
     160                return tmp; 
     161        } 
     162        vec sample() const { 
     163                vec tmp ( rv.count() ); 
     164                for ( int i=0;i<epdfs.length();i++ ) { 
     165                        vec pom = epdfs ( i )->sample(); 
     166                        set_subvector ( tmp,rvinds ( i ), pom ); 
     167                } 
     168                return tmp; 
     169        } 
     170        double evalpdflog ( const vec &val ) const { 
     171                double tmp=0; 
     172                for ( int i=0;i<epdfs.length();i++ ) { 
     173                        tmp+=epdfs(i)->evalpdflog(val(rvinds(i))); 
     174                } 
     175                return tmp; 
     176        } 
     177}; 
     178 
    153179 
    154180/*! \brief Mixture of mpdfs with constant weights, all mpdfs are of equal type 
  • bdm/stat/libBM.cpp

    r165 r168  
    4747} 
    4848 
    49 RV::RV() : tsize ( 0 ), len ( 0 ), ids ( 0 ), names ( 0 ), sizes ( 0 ), times ( 0 ) {}; 
     49RV::RV() : tsize ( 0 ), len ( 0 ), ids ( 0 ),  sizes ( 0 ), times ( 0 ),names ( 0 ) {}; 
    5050 
    5151bool RV::add ( const RV &rv2 ) { 
  • bdm/stat/libBM.h

    r165 r168  
    265265        //!Logarithm of marginalized data likelihood. 
    266266        double ll; 
    267         //!  If true, the filter will compute likelihood of the data record and store it in \c ll . Set to false if you want to save time. 
     267        //!  If true, the filter will compute likelihood of the data record and store it in \c ll . Set to false if you want to save computational time. 
    268268        bool evalll; 
    269269public: 
     
    288288        //!access function 
    289289        double _ll() const {return ll;} 
     290        //!access function  
     291        void set_evalll(bool evl0){evalll=evl0;} 
    290292}; 
    291293 
  • bdm/stat/libEF.cpp

    r162 r168  
    1313 
    1414vec egiw::sample() const { 
    15         it_warning("Function not implemented"); 
    16         return vec_1(0.0); 
    17 } 
    18  
    19 double egiw::evalpdflog( const vec &val ) const { 
    20         int nPsi = rv.count()-1; // assuming 1dim y 
    21         double k = nu + nPsi + 2; 
    22          
    23         double r = val(nPsi); //last entry! 
    24         vec Psi(nPsi+1); 
    25         Psi(0) = -1.0; 
    26         Psi.set_subvector(1,val); // fill the rest 
    27          
    28         return -0.5*( k*log(r) + V.qform(Psi)) - lognc(); 
    29 } 
    30  
    31 double egiw::lognc() const{ 
     15        it_warning ( "Function not implemented" ); 
     16        return vec_1 ( 0.0 ); 
     17} 
     18 
     19double egiw::evalpdflog ( const vec &val ) const { 
     20 
     21        if ( xdim==1 ) { //same as the following, just quicker. 
     22                double r = val ( val.length()-1 ); //last entry! 
     23                vec Psi ( nPsi+xdim ); 
     24                Psi ( 0 ) = -1.0; 
     25                Psi.set_subvector ( 1,val ); // fill the rest 
     26 
     27                return -0.5* ( nu*log ( r ) + V.qform ( Psi ) /r ) + lognc(); 
     28        } 
     29        else { 
     30                int vend = val.length()-1; 
     31                mat Th= reshape ( val ( 0,nPsi*xdim-1 ),nPsi,xdim ); 
     32                fsqmat R ( reshape ( val ( nPsi*xdim,vend ),xdim,xdim ) ); 
     33                mat Tmp=concat_vertical ( -eye ( xdim ),Th ); 
     34                fsqmat iR ( xdim ); 
     35                R.inv ( iR ); 
     36                 
     37                return -0.5* ( nu*R.logdet() + trace ( iR.to_mat() *Tmp.T()*V.to_mat() *Tmp ) ) + lognc(); 
     38        } 
     39} 
     40 
     41double egiw::lognc() const { 
    3242        const vec& D = V._D(); 
    33         int nPsi = D.length()-1; // assuming 1dim y 
    34  
    35 // log(2) = 0.693147180559945286226763983 
    36 // log(pi) = 1.144729885849400163877476189 
    37 return lgamma(0.5*nu) + 0.5*((1.0-nu)*log(D(0)) - V.logdet() + (nu+nPsi)*0.693147180559945286226763983 + nPsi*1.144729885849400163877476189); 
     43 
     44        double m = nu - nPsi -xdim-1; 
     45#define log2  0.693147180559945286226763983 
     46#define logpi 1.144729885849400163877476189 
     47#define log2pi 1.83787706640935 
     48 
     49        double nkG = 0.5* xdim* ( -nPsi *log2pi + sum ( log ( D ( xdim,D.length()-1 ) ) ) ); 
     50        // temporary for lgamma in Wishart 
     51        double lg=0; 
     52        for ( int i =0; i<xdim;i++ ) {lg+=lgamma ( 0.5* ( m-i ) );} 
     53 
     54        double nkW = 0.5* ( m*sum ( log ( D ( 0,xdim-1 ) ) ) ) \ 
     55                     - 0.5*xdim* ( m*log2 + 0.5* ( xdim-1 ) *log2pi )  - lg; 
     56 
     57        return nkG+nkW; 
    3858} 
    3959 
     
    4161        const mat &L= V._L(); 
    4262        const vec &D= V._D(); 
     63        int end = L.rows()-1; 
     64 
     65        vec m(rv.count()); 
     66        mat iLsub = ltuinv ( L ( xdim,end,xdim,end ) ); 
    4367         
    44         int end = L.rows()-1; 
    45         vec L0 = L.get_col(0); 
    46          
    47         vec m(D.length()); 
    48         mat iLsub = ltuinv(L(1,end,1,end)); 
    49         m.set_subvector(0,iLsub*L0(1,end)); 
    50         m(end)= D(0)/(nu-2.0); 
    51          
     68        if ( xdim==1 ) { 
     69                vec L0 = L.get_col ( 0 ); 
     70 
     71                m.set_subvector ( 0,iLsub*L0 ( 1,end ) ); 
     72                m ( end ) = D ( 0 ) / ( nu -nPsi -2*xdim -2); 
     73        } 
     74        else { 
     75                ldmat ldR(L(0,xdim,0,xdim), D(0,xdim)/( nu -nPsi -2*xdim -2)); //exp val of R 
     76                mat iLsub=ltuinv ( L ( xdim,end,xdim,end ) ); 
     77                mat M = iLsub*L(xdim,end,0,xdim); 
     78                 
     79                m = cvectorize(concat_vertical(M,ldR.to_mat())); 
     80        } 
    5281        return m; 
    5382} 
     
    5988        for ( i=0; i<rv.count(); i++ ) { 
    6089                GamRNG.setup ( alpha ( i ),beta ( i ) ); 
    61                 #pragma omp critical 
     90#pragma omp critical 
    6291                smp ( i ) = GamRNG(); 
    6392        } 
     
    6998//      mat Smp ( rv.count(),N ); 
    7099//      int i,j; 
    71 //  
     100// 
    72101//      for ( i=0; i<rv.count(); i++ ) { 
    73102//              GamRNG.setup ( alpha ( i ),beta ( i ) ); 
    74 //  
     103// 
    75104//              for ( j=0; j<N; j++ ) { 
    76105//                      Smp ( i,j ) = GamRNG(); 
    77106//              } 
    78107//      } 
    79 //  
     108// 
    80109//      return Smp; 
    81110// } 
     
    121150 
    122151        switch ( method ) { 
    123         case MULTINOMIAL: 
    124                 u ( n - 1 ) = pow ( UniRNG.sample(), 1.0 / n ); 
    125  
    126                 for ( i = n - 2;i >= 0;i-- ) { 
    127                         u ( i ) = u ( i + 1 ) * pow ( UniRNG.sample(), 1.0 / ( i + 1 ) ); 
    128                 } 
    129  
    130                 break; 
    131  
    132         case STRATIFIED: 
    133  
    134                 for ( i = 0;i < n;i++ ) { 
    135                         u ( i ) = ( i + UniRNG.sample() ) / n; 
    136                 } 
    137  
    138                 break; 
    139  
    140         case SYSTEMATIC: 
    141                 u0 = UniRNG.sample(); 
    142  
    143                 for ( i = 0;i < n;i++ ) { 
    144                         u ( i ) = ( i + u0 ) / n; 
    145                 } 
    146  
    147                 break; 
    148  
    149         default: 
    150                 it_error ( "PF::resample(): Unknown resampling method" ); 
     152                case MULTINOMIAL: 
     153                        u ( n - 1 ) = pow ( UniRNG.sample(), 1.0 / n ); 
     154 
     155                        for ( i = n - 2;i >= 0;i-- ) { 
     156                                u ( i ) = u ( i + 1 ) * pow ( UniRNG.sample(), 1.0 / ( i + 1 ) ); 
     157                        } 
     158 
     159                        break; 
     160 
     161                case STRATIFIED: 
     162 
     163                        for ( i = 0;i < n;i++ ) { 
     164                                u ( i ) = ( i + UniRNG.sample() ) / n; 
     165                        } 
     166 
     167                        break; 
     168 
     169                case SYSTEMATIC: 
     170                        u0 = UniRNG.sample(); 
     171 
     172                        for ( i = 0;i < n;i++ ) { 
     173                                u ( i ) = ( i + u0 ) / n; 
     174                        } 
     175 
     176                        break; 
     177 
     178                default: 
     179                        it_error ( "PF::resample(): Unknown resampling method" ); 
    151180        } 
    152181 
     
    173202                        ind ( i ) = i; 
    174203                        N_babies ( i ) --; //this index was now replicated; 
    175                 } else { 
     204                } 
     205                else { 
    176206                        // test if the parent has been fully replicated 
    177207                        // if yes, find the next one 
     
    198228 
    199229void eEmp::set_parameters ( const vec &w0, epdf* epdf0 ) { 
    200 //it_assert_debug(rv==epdf0->rv(),"Wrong epdf0"); 
     230        //it_assert_debug(rv==epdf0->rv(),"Wrong epdf0"); 
    201231        w=w0; 
    202232        w/=sum ( w0 );//renormalize 
  • bdm/stat/libEF.h

    r162 r168  
    113113* \brief Gauss-inverse-Wishart density stored in LD form 
    114114 
    115 * More?... 
     115* For \f$p\f$-variate densities, given rv.count() should be \f$p\times\f$ V.rows(). 
     116* 
    116117*/ 
    117118class egiw : public eEF { 
     
    121122        //! Number of data records (degrees of freedom) of sufficient statistics 
    122123        double nu; 
    123 public: 
    124         //!Default constructor 
     124        //! Dimension of the output 
     125        int xdim; 
     126        //! Dimension of the regressor 
     127        int nPsi; 
     128public: 
     129        //!Default constructor, assuming 
    125130        egiw(RV rv, mat V0, double nu0): eEF(rv), V(V0), nu(nu0) { 
    126                 it_assert_debug(rv.count()==V.rows(),"Incompatible V0."); 
     131                xdim = rv.count()/V.rows(); 
     132                it_assert_debug(rv.count()==xdim*V.rows(),"Incompatible V0."); 
     133                nPsi = V.rows()-xdim; 
    127134        } 
    128135 
    129136        vec sample() const; 
    130137        vec mean() const; 
     138        //! In this instance, val= [theta, r]. For multivariate instances, it is stored columnwise val = [theta_1 theta_2 ... r_1 r_2 ] 
    131139        double evalpdflog ( const vec &val ) const; 
    132140        double lognc () const; 
  • bdm/stat/merger.h

    r165 r168  
    5353        //! sample from merged density 
    5454        //! weight w is a  
    55         vec sample(double &w, vec smp0=zeros ( rv.count()+rvc.count())){ 
     55/*      vec sample(double &w, const vec &smp0){ 
    5656                // result 
    5757                vec smp=smp0; 
    58                 vec cond=sample(condpdf); // Just like in samplecond, here it is not given! 
     58                vec cond=condpdf.sample(); // Just like in samplecond, here it is not given! 
    5959                // temporary 
    6060                mat smpi=zeros(rv.count()+rvc.count(), n); 
     
    6363                 
    6464                // Consider arithmetic mean as a proposal density 
    65                 ll(i) = 0; 
     65                ll = 0; 
    6666                for ( int i = ( n - 1 );i >= 0;i-- ) { 
    6767                        if ( rvcinds ( i ).length() > 0 ) { 
     
    9999                 
    100100                // copied from mprod.sample 
    101         }; 
     101        };*/ 
    102102//      project  
    103103        //! for future use 
  • tests/merger_test.cpp

    r165 r168  
    2626 
    2727        merger M(A); 
    28         eEmp res=M.merge(100); 
     28//      eEmp res=M.merge(100); 
    2929         
    30         cout << res.mean() << end; 
     30//      cout << res.mean() << endl; 
    3131        //Exit program: 
    3232        return 0;