Changeset 168
- Timestamp:
- 09/18/08 19:54:09 (16 years ago)
- Files:
-
- 15 modified
Legend:
- Unmodified
- Added
- Removed
-
CMakeLists.txt
r161 r168 1 # Compulsory line 2 cmake_minimum_required(VERSION 2.6) 3 1 4 # The name of our project is "BDM". CMakeLists files in this project can 2 5 # refer to the root source directory of the project as ${BDM_SOURCE_DIR} and -
bdm/CMakeLists.txt
r162 r168 3 3 SET(BdmMath math/libDC.cpp math/libDC.h math/chmat.cpp math/chmat.h) 4 4 SET(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 )5 SET(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) 6 6 SET(BdmUI userinfo.cpp userinfo.h) 7 7 -
bdm/estim/arx.h
r145 r168 50 50 public: 51 51 //! 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;}; 53 53 //! 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;} 55 55 //! Returns sufficient statistics 56 56 void get_parameters(mat &V0, double &nu0){V0=est._V().to_mat(); nu0=est._nu();} -
bdm/math/chmat.cpp
r108 r168 17 17 Ch = R ( 0, Ch.rows()-1, 0, Ch.cols()-1 ); 18 18 }; 19 mat chmat::to_mat() {mat F=Ch.T() *Ch;return F;};19 mat chmat::to_mat() const {mat F=Ch.T() *Ch;return F;}; 20 20 void chmat::mult_sym ( const mat &C ) { 21 21 it_error ( "not implemented" ); -
bdm/math/chmat.h
r108 r168 32 32 33 33 void opupdt ( const vec &v, double w ); 34 mat to_mat() ;34 mat to_mat() const; 35 35 void mult_sym ( const mat &C ); 36 36 void mult_sym ( const mat &C , chmat &U ) const; -
bdm/math/libDC.cpp
r162 r168 7 7 8 8 void fsqmat::opupdt ( const vec &v, double w ) {M+=outer_product ( v,v*w );}; 9 mat fsqmat::to_mat() {return M;};9 mat fsqmat::to_mat() const {return M;}; 10 10 void fsqmat::mult_sym ( const mat &C) {M=C *M*C.T();}; 11 11 void fsqmat::mult_sym_t ( const mat &C) {M=C.T() *M*C;}; … … 78 78 } 79 79 80 mat ldmat::to_mat() {80 mat ldmat::to_mat() const { 81 81 int dim = D.length(); 82 82 mat V( dim, dim ); -
bdm/math/libDC.h
r98 r168 44 44 */ 45 45 46 virtual mat to_mat() =0;46 virtual mat to_mat() const =0; 47 47 48 48 /*! \brief Inplace symmetric multiplication by a SQUARE matrix \f$C\f$, i.e. \f$V = C*V*C'\f$ … … 115 115 public: 116 116 void opupdt ( const vec &v, double w ); 117 mat to_mat() ;117 mat to_mat() const; 118 118 void mult_sym ( const mat &C); 119 119 void mult_sym_t ( const mat &C); … … 198 198 199 199 void opupdt ( const vec &v, double w ); 200 mat to_mat() ;200 mat to_mat() const; 201 201 void mult_sym ( const mat &C); 202 202 void mult_sym_t ( const mat &C); -
bdm/stat/emix.cpp
r165 r168 4 4 5 5 void emix::set_parameters ( const vec &w0, const Array<epdf*> &Coms0 ) { 6 w = w0 ;6 w = w0/sum(w0); 7 7 int i; 8 8 for ( i=0;i<w.length();i++ ) { … … 38 38 }; 39 39 40 independent = true;40 // independent = true; 41 41 //test rvc of mpdfs and fill rvinds 42 42 for ( i = 0;i < n;i++ ) { … … 48 48 rvcinds ( i ) = mpdfs ( i )->_rvc().dataind ( rvc ); 49 49 // 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;}*/ 52 52 } 53 53 }; -
bdm/stat/emix.h
r165 r168 82 82 //! Indeces of rvc in common rvc 83 83 Array<ivec> rvcinds; 84 //! Indicate independence of its factors85 bool independent;84 // //! Indicate independence of its factors 85 // bool independent; 86 86 // //! Indicate internal creation of mpdfs which must be destroyed 87 87 // bool intermpdfs; 88 88 public: 89 /*!\brief Constructor from list of mFacs, 89 /*!\brief Constructor from list of mFacs, 90 90 Additional parameter overlap is left for future use. Do not set to true for mprod. 91 91 */ 92 mprod ( Array<mpdf*> mFacs, bool overlap=false ); 92 mprod ( Array<mpdf*> mFacs, bool overlap=false ); 93 93 94 94 double evalpdflog ( const vec &val ) const { … … 122 122 set_subvector ( smp,rvinds ( i ), smpi ); 123 123 // add ith likelihood contribution 124 ll+=epdfs (i)->evalpdflog(smpi);124 ll+=epdfs ( i )->evalpdflog ( smpi ); 125 125 } 126 126 return smp; 127 127 } 128 128 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 ) ) );} 131 131 return Smp; 132 132 } 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 // }150 133 151 134 ~mprod() {}; 152 135 }; 136 137 //! Product of independent epdfs. For dependent pdfs, use mprod. 138 class eprod: public epdf { 139 protected: 140 //! Components (epdfs) 141 Array<epdf*> epdfs; 142 //! Array of indeces 143 Array<ivec> rvinds; 144 public: 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 153 179 154 180 /*! \brief Mixture of mpdfs with constant weights, all mpdfs are of equal type -
bdm/stat/libBM.cpp
r165 r168 47 47 } 48 48 49 RV::RV() : tsize ( 0 ), len ( 0 ), ids ( 0 ), names ( 0 ), sizes ( 0 ), times ( 0 ) {};49 RV::RV() : tsize ( 0 ), len ( 0 ), ids ( 0 ), sizes ( 0 ), times ( 0 ),names ( 0 ) {}; 50 50 51 51 bool RV::add ( const RV &rv2 ) { -
bdm/stat/libBM.h
r165 r168 265 265 //!Logarithm of marginalized data likelihood. 266 266 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. 268 268 bool evalll; 269 269 public: … … 288 288 //!access function 289 289 double _ll() const {return ll;} 290 //!access function 291 void set_evalll(bool evl0){evalll=evl0;} 290 292 }; 291 293 -
bdm/stat/libEF.cpp
r162 r168 13 13 14 14 vec 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 19 double 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 41 double egiw::lognc() const { 32 42 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; 38 58 } 39 59 … … 41 61 const mat &L= V._L(); 42 62 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 ) ); 43 67 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 } 52 81 return m; 53 82 } … … 59 88 for ( i=0; i<rv.count(); i++ ) { 60 89 GamRNG.setup ( alpha ( i ),beta ( i ) ); 61 90 #pragma omp critical 62 91 smp ( i ) = GamRNG(); 63 92 } … … 69 98 // mat Smp ( rv.count(),N ); 70 99 // int i,j; 71 // 100 // 72 101 // for ( i=0; i<rv.count(); i++ ) { 73 102 // GamRNG.setup ( alpha ( i ),beta ( i ) ); 74 // 103 // 75 104 // for ( j=0; j<N; j++ ) { 76 105 // Smp ( i,j ) = GamRNG(); 77 106 // } 78 107 // } 79 // 108 // 80 109 // return Smp; 81 110 // } … … 121 150 122 151 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" ); 151 180 } 152 181 … … 173 202 ind ( i ) = i; 174 203 N_babies ( i ) --; //this index was now replicated; 175 } else { 204 } 205 else { 176 206 // test if the parent has been fully replicated 177 207 // if yes, find the next one … … 198 228 199 229 void 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"); 201 231 w=w0; 202 232 w/=sum ( w0 );//renormalize -
bdm/stat/libEF.h
r162 r168 113 113 * \brief Gauss-inverse-Wishart density stored in LD form 114 114 115 * More?... 115 * For \f$p\f$-variate densities, given rv.count() should be \f$p\times\f$ V.rows(). 116 * 116 117 */ 117 118 class egiw : public eEF { … … 121 122 //! Number of data records (degrees of freedom) of sufficient statistics 122 123 double nu; 123 public: 124 //!Default constructor 124 //! Dimension of the output 125 int xdim; 126 //! Dimension of the regressor 127 int nPsi; 128 public: 129 //!Default constructor, assuming 125 130 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; 127 134 } 128 135 129 136 vec sample() const; 130 137 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 ] 131 139 double evalpdflog ( const vec &val ) const; 132 140 double lognc () const; -
bdm/stat/merger.h
r165 r168 53 53 //! sample from merged density 54 54 //! weight w is a 55 vec sample(double &w, vec smp0=zeros ( rv.count()+rvc.count())){55 /* vec sample(double &w, const vec &smp0){ 56 56 // result 57 57 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! 59 59 // temporary 60 60 mat smpi=zeros(rv.count()+rvc.count(), n); … … 63 63 64 64 // Consider arithmetic mean as a proposal density 65 ll (i)= 0;65 ll = 0; 66 66 for ( int i = ( n - 1 );i >= 0;i-- ) { 67 67 if ( rvcinds ( i ).length() > 0 ) { … … 99 99 100 100 // copied from mprod.sample 101 }; 101 };*/ 102 102 // project 103 103 //! for future use -
tests/merger_test.cpp
r165 r168 26 26 27 27 merger M(A); 28 eEmp res=M.merge(100);28 // eEmp res=M.merge(100); 29 29 30 cout << res.mean() << end;30 // cout << res.mean() << endl; 31 31 //Exit program: 32 32 return 0;