Changeset 12
- Timestamp:
- 01/25/08 10:53:47 (17 years ago)
- Files:
-
- 6 modified
Legend:
- Unmodified
- Added
- Removed
-
Makefile
r11 r12 2 2 CPPFLAGS=-g 3 3 4 all: test PF4 all: testSmp 5 5 6 6 test: test0 … … 13 13 testKF: testKF.o libKF.o libBM.o libDC.o itpp_ext.o 14 14 testPF: testPF.o libPF.o libBM.o libDC.o itpp_ext.o 15 16 itpp_ext.o: itpp_ext.cpp itpp_ext.h 15 testSmp: testSmp.o libEF.o libBM.o libDC.o itpp_ext.o -
libBM.cpp
r8 r12 9 9 void RV::init( ivec in_ids, Array<std::string> in_names, ivec in_sizes, ivec in_times, ivec in_obs ) { 10 10 // 11 int i; 11 12 len = in_ids.length(); 12 13 //PRUDENT_MODE … … 25 26 times = in_times; 26 27 obs = in_obs; 28 size = 0; 29 for(i=0;i<len;i++){size+=sizes(i);} 27 30 }; 28 31 … … 32 35 33 36 RV::RV () {}; 34 35 inline int RV::length () {return len;};36 37 37 38 RV::RV ( ivec in_ids ) { … … 48 49 } 49 50 50 RV RV:: rvsubselect(ivec ind){51 RV RV::subselect(ivec ind){ 51 52 return RV(ids(ind), names(to_Arr(ind)), sizes(ind), times(ind), obs(ind)); 52 53 } -
libBM.h
r8 r12 25 25 */ 26 26 class RV { 27 int len;27 int size,len; 28 28 ivec ids; 29 29 ivec sizes; … … 46 46 47 47 //! Return length (number of scalars) of the RV. 48 int length(); 48 int count() {return size;} 49 //TODO why not inline and later?? 50 49 51 //! Find indexes of another rv in self 50 ivec rvfind(RV rv2);52 ivec find(RV rv2); 51 53 //! Add (concat) another variable to the current one 52 RV rvadd(RV rv2);54 RV add(RV rv2); 53 55 //! Subtract another variable from the current one 54 RV rvsubt(RV rv2);56 RV subt(RV rv2); 55 57 //! Select only variables at indeces ind 56 RV rvsubselect(ivec ind);58 RV subselect(ivec ind); 57 59 //! Select only variables at indeces ind 58 60 RV operator()(ivec ind); … … 110 112 }; 111 113 114 115 112 116 #endif // BM_H -
libDC.cpp
r8 r12 18 18 D = exD; 19 19 L = exL; 20 dim = exD.length(); 20 21 } 21 22 … … 23 24 vec D ; 24 25 mat L; 26 dim = 0; 25 27 } 26 28 … … 28 30 //TODO check if correct!! Based on heuristic observation of lu() 29 31 30 intdim = V.cols();32 dim = V.cols(); 31 33 it_assert_debug( dim == V.rows(),"ldmat::ldmat matrix V is not square!" ); 32 34 33 mat U( dim,dim ); 34 35 L = V; //Allocate space for L 36 ivec p = ivec( dim ); //not clear why? 37 38 lu( V,L,U,p ); 39 40 //Now, if V is symmetric, L is what we seek and D is on diagonal of U 41 D = diag( U ); 42 43 //check if V was symmetric 44 //TODO How? norm of L-U'? 45 //it_assert_debug(); 35 46 36 } 47 37 … … 62 52 } 63 53 64 std::ostream &operator<< ( std::ostream &os, sqmat &sq ) { 65 os << sq.to_mat() << endl; 54 std::ostream &operator<< ( std::ostream &os, ldmat &ld ) { 55 os << "L:" << ld.L << endl; 56 os << "D:" << ld.D << endl; 66 57 } 67 58 … … 85 76 } 86 77 } 87 return V; 78 mat V2 = L.transpose()*diag( D )*L; 79 return V2; 88 80 } 89 81 … … 177 169 } 178 170 179 ldmat& ldmat::operator *= (double x){ 180 int i; 181 for(i=0;i<D.length();i++){D(i)*=x;}; 182 } 183 171 ldmat& ldmat::operator *= ( double x ) { 172 int i; 173 for ( i=0;i<D.length();i++ ){D( i )*=x;}; 174 } 175 176 vec ldmat::sqrt_mult( vec &x ) { 177 int i,j; 178 vec res( dim ); 179 double sum; 180 for ( i=0;i<dim;i++ ) {//for each element of result 181 res( i ) = 0.0; 182 for ( j=i;j<dim;j++ ) {//sum D(j)*L(:,i).*x 183 res( i ) += sqrt( D( j ) )*L( j,i )*x( j ); 184 } 185 } 186 vec res2 = L.transpose()*diag( sqrt( D ) )*x; 187 return res2; 188 } 189 190 void ldmat::ldform( mat &A,vec &D0 ) { 191 int m = A.rows(); 192 int n = A.cols(); 193 int mn = (m<n) ? m :n ; 194 195 it_assert_debug( A.cols()==dim,"ldmat::ldform A is not compatible" ); 196 it_assert_debug( D.length()==A.rows(),"ldmat::ldform Vector D must have the length as row count of A" ); 197 198 L=concat_vertical( zeros( n,n ), diag( sqrt( D0 ) )*A ); 199 D=zeros( n+m ); 200 201 //unnecessary big L and D will be made smaller at the end of file 202 203 vec w=zeros( n ); 204 vec v; 205 double sum, beta, pom; 206 207 int cc=0; 208 int i=n; // +1 in .m 209 int ii,j,jj; 210 while (( i> ( n-mn+1-cc ) )&&( i>1 ) ) { 211 i--; 212 sum = 0.0; 213 v.set_size( m+i-( n-cc ) ); //prepare v 214 for ( ii=n-cc;ii<m+i;i++ ) { 215 sum+= L( ii,i )*L( ii,i ); 216 v( ii )=L( ii,i ); //assign v 217 } 218 if ( L( m+i,i )==0 ) { 219 beta = sqrt( sum ); 220 } else { 221 beta = L( m+i,i )+sign( L( m+i,i ) )*sqrt( sum ) ; 222 } 223 if ( std::fabs( beta )<eps ) { 224 cc++; 225 L.set_row( n+1-cc, L.get_row( m+i ) ); 226 L.set_row( m+i,0 ); 227 D( m+i )=0; L( m+i,i )=1; 228 L.set_submatrix( n+1-cc,m+i,i,i,0 ); 229 continue; 230 } 231 232 sum-=v( v.length()-1 )*v( v.length()-1 ); // 233 sum/=beta*beta; 234 sum++; 235 236 v/=beta; 237 v( v.length()-1 )=1; 238 239 pom=-2/sum; 240 for ( j=i;i>=0;i-- ) { 241 w( j )=0.0; 242 for ( ii=n-cc;ii<m+i;ii++ ) { 243 w( j )+= v( ii )*L( ii,j ); 244 } 245 w( j )*=pom; 246 } 247 248 for ( ii=n-cc;ii<m+i;ii++ ) { 249 for ( jj=0;jj<i-1;jj++ ) { 250 L( ii,jj )+= v( ii )*w( jj ); 251 } 252 } 253 for ( ii=n-cc;ii<m+i;ii++ ) { 254 L( ii,i )= 0; 255 } 256 L( m+i,i )+=w( i ); 257 258 D( m+i )=L( m+i,i )*L( m+i,i ); 259 for ( ii=0;ii<i;ii++ ) { 260 L( m+i,ii )/=L( m+i,i ); 261 } 262 } 263 if ( i>0 ) { 264 for ( ii=0;ii<i-1;ii++ ) { 265 jj = D.length()-1-n+ii; 266 L.set_row(jj,0); 267 L(jj,jj)=1; 268 } 269 } 270 271 //cut-out L and D; 272 L.del_rows(0,m-1); 273 D.del(0,m-1); 274 } 184 275 185 276 //////// Auxiliary Functions … … 255 346 *kr = 0.0; 256 347 if ( *Df < -threshold ) { 257 it_warning( "Problem in dydr: subraction of dyad results in negative definitness. Likely mistake in calling function." );} 348 it_warning( "Problem in dydr: subraction of dyad results in negative definitness. Likely mistake in calling function." ); 349 } 258 350 *Df = 0.0; 259 351 } -
libDC.h
r8 r12 52 52 53 53 /*! 54 \brief Multiplies square root of $V$ by vector $x$. 55 56 Used e.g. in generating normal samples. 57 */ 58 virtual vec sqrt_mult(vec &v) =0; 59 60 /*! 54 61 \brief Evaluates quadratic form $x= v'*V*v$; 55 62 … … 59 66 // //! easy version of the 60 67 // sqmat inv(); 61 62 friend std::ostream &operator<< ( std::ostream &os, sqmat &sq );63 68 64 69 //! Clearing matrix so that it corresponds to zeros. … … 71 76 virtual int rows() =0; 72 77 78 protected: 79 int dim; 73 80 }; 74 81 … … 86 93 void clear(); 87 94 95 88 96 //! Constructor 89 97 fsqmat(const mat &M); … … 95 103 */ 96 104 virtual void inv(fsqmat* Inv); 105 106 97 107 }; 98 108 … … 118 128 int cols(); 119 129 int rows(); 130 vec sqrt_mult(vec &v); 120 131 121 132 /*! \brief Matrix inversion preserving the chosen form. … … 133 144 void mult_sym( const mat &C, ldmat &U, bool trans=false ); 134 145 146 /*! \brief Transforms general $A'D0A$ into pure $L'DL$ 147 148 The new decomposition fullfills: $A'*diag(D)*A = self.L'*diag(self.D)*self.L$ 149 150 @param A general matrix 151 @param D0 general vector 152 153 */ 154 void ldform( mat &A, vec &D0 ); 155 135 156 ldmat& operator += (const ldmat &ldA); 136 157 ldmat& operator -= (const ldmat &ldA); 137 158 ldmat& operator *= (double x); 138 159 160 friend std::ostream &operator<< ( std::ostream &os, ldmat &sq ); 161 139 162 protected: 140 163 vec D; -
libEF.h
r8 r12 15 15 16 16 #include <itpp/itbase.h> 17 #include "libDC.h" 18 #include "libBM.h" 17 19 //#include <std> 18 20 … … 24 26 * More?... 25 27 */ 26 class eEF : public epdf {28 class eEF : public epdf { 27 29 28 30 public: 29 virtual void tupdate( double phi, mat &vbar, double nubar ) {};30 virtual void dupdate( mat &v,double nu=1.0 ) {};31 virtual void tupdate( double phi, mat &vbar, double nubar ) {}; 32 virtual void dupdate( mat &v,double nu=1.0 ) {}; 31 33 }; 32 34 33 class mEF : public mpdf {35 class mEF : public mpdf { 34 36 35 37 public: … … 44 46 template<class sq_T> 45 47 class enorm : public eEF { 48 int dim; 46 49 vec mu; 47 50 sq_T R; … … 52 55 void dupdate( mat &v,double nu=1.0 ); 53 56 vec sample(); 57 mat sample(int N); 54 58 double eval( const vec &val ); 55 59 Normal_RNG RNG; 56 60 }; 57 61 … … 76 80 template<class sq_T> 77 81 enorm<sq_T>::enorm( RV &rv, vec &mu0, sq_T &R0 ) { 82 dim = rv.count(); 78 83 mu = mu0; 79 84 R = R0; … … 92 97 template<class sq_T> 93 98 vec enorm<sq_T>::sample() { 94 // 99 vec x( dim ); 100 RNG.sample_vector( dim,x ); 101 vec smp = R.sqrt_mult( x ); 102 103 smp += mu; 104 return smp; 105 }; 106 107 template<class sq_T> 108 mat enorm<sq_T>::sample( int N ) { 109 mat X( dim,N ); 110 vec x( dim ); 111 vec pom; 112 int i; 113 for ( i=0;i<N;i++ ) { 114 RNG.sample_vector( dim,x ); 115 pom = R.sqrt_mult( x ); 116 pom +=mu; 117 X.set_col( i, pom); 118 } 119 return X; 95 120 }; 96 121 … … 106 131 template<class sq_T> 107 132 mlnorm<sq_T>::mlnorm( RV &rv,RV &rvc, mat &A, sq_T &R ) { 108 int dim = rv. length();133 int dim = rv.count(); 109 134 vec mu( dim ); 110 135 … … 116 141 this->condition( cond ); 117 142 vec smp = epdf.sample(); 118 lik = epdf.eval( smp);143 lik = epdf.eval( smp ); 119 144 return smp; 120 145 } … … 123 148 mat mlnorm<sq_T>::samplecond( vec &cond, vec &lik, int n ) { 124 149 int i; 125 int dim = rv. length();150 int dim = rv.count(); 126 151 mat Smp( dim,n ); 127 vec smp( dim );152 vec smp( dim ); 128 153 this->condition( cond ); 129 154 for ( i=0; i<dim; i++ ) { 130 155 smp = epdf.sample(); 131 lik( i) = epdf.eval(smp);132 Smp.set_col( i ,smp );156 lik( i ) = epdf.eval( smp ); 157 Smp.set_col( i ,smp ); 133 158 } 134 159 return Smp; … … 136 161 137 162 template<class sq_T> 138 void mlnorm<sq_T>::condition( vec &cond) { 163 void mlnorm<sq_T>::condition( vec &cond ) { 164 epdf.mu = A*cond; 165 //R is already assigned; 139 166 } 140 167