Changeset 737 for library/bdm/stat/exp_family.cpp
- Timestamp:
- 11/25/09 12:14:38 (15 years ago)
- Files:
-
- 1 modified
Legend:
- Unmodified
- Added
- Removed
-
library/bdm/stat/exp_family.cpp
r730 r737 12 12 using std::cout; 13 13 14 15 16 void BMEF::bayes ( const vec &yt, const vec &cond ) {14 /////////// 15 16 void BMEF::bayes ( const vec &yt, const vec &cond ) { 17 17 this->bayes_weighted ( yt, cond, 1.0 ); 18 18 }; 19 19 20 void egiw::set_parameters ( int dimx0, ldmat V0, double nu0) {20 void egiw::set_parameters ( int dimx0, ldmat V0, double nu0 ) { 21 21 dimx = dimx0; 22 22 nPsi = V0.rows() - dimx; 23 dim = dimx * ( dimx + nPsi); // size(R) + size(Theta)24 23 dim = dimx * ( dimx + nPsi ); // size(R) + size(Theta) 24 25 25 V = V0; 26 if ( nu0 < 0) {26 if ( nu0 < 0 ) { 27 27 nu = 0.1 + nPsi + 2 * dimx + 2; // +2 assures finite expected value of R 28 28 // terms before that are sufficient for finite normalization … … 35 35 mat M; 36 36 chmat R; 37 sample_mat (M,R);38 39 return concat ( cvectorize(M),cvectorize(R.to_mat()));40 } 41 42 mat egiw::sample_mat (int n) const {37 sample_mat ( M, R ); 38 39 return concat ( cvectorize ( M ), cvectorize ( R.to_mat() ) ); 40 } 41 42 mat egiw::sample_mat ( int n ) const { 43 43 // TODO - correct approach - convert to product of norm * Wishart 44 44 mat M; 45 45 ldmat Vz; 46 46 ldmat Lam; 47 factorize (M,Vz,Lam);48 49 chmat ChLam (Lam.to_mat());47 factorize ( M, Vz, Lam ); 48 49 chmat ChLam ( Lam.to_mat() ); 50 50 chmat iChLam; 51 ChLam.inv (iChLam);52 53 eWishartCh Omega; //inverse Wishart, result is R, 54 Omega.set_parameters (iChLam,nu-2*nPsi-dimx); // 2*nPsi is there to match numercial simulations - check if analytically correct55 51 ChLam.inv ( iChLam ); 52 53 eWishartCh Omega; //inverse Wishart, result is R, 54 Omega.set_parameters ( iChLam, nu - 2*nPsi - dimx ); // 2*nPsi is there to match numercial simulations - check if analytically correct 55 56 56 mat OmChi; 57 mat Z (M.rows(),M.cols());57 mat Z ( M.rows(), M.cols() ); 58 58 59 59 mat Mi; 60 60 mat RChiT; 61 mat tmp (dimension(), n);62 for ( int i=0; i<n;i++){63 OmChi =Omega.sample_mat();64 RChiT =inv(OmChi);65 Z =randn(M.rows(), M.cols());66 Mi = M + RChiT * Z * inv (Vz._L().T() *diag(sqrt(Vz._D())));67 68 tmp.set_col (i,concat (cvectorize(Mi),cvectorize(RChiT*RChiT.T())));61 mat tmp ( dimension(), n ); 62 for ( int i = 0; i < n; i++ ) { 63 OmChi = Omega.sample_mat(); 64 RChiT = inv ( OmChi ); 65 Z = randn ( M.rows(), M.cols() ); 66 Mi = M + RChiT * Z * inv ( Vz._L().T() * diag ( sqrt ( Vz._D() ) ) ); 67 68 tmp.set_col ( i, concat ( cvectorize ( Mi ), cvectorize ( RChiT*RChiT.T() ) ) ); 69 69 } 70 70 return tmp; 71 71 } 72 72 73 void egiw::sample_mat (mat &Mi, chmat &Ri)const{74 73 void egiw::sample_mat ( mat &Mi, chmat &Ri ) const { 74 75 75 // TODO - correct approach - convert to product of norm * Wishart 76 76 mat M; 77 77 ldmat Vz; 78 78 ldmat Lam; 79 factorize (M,Vz,Lam);80 79 factorize ( M, Vz, Lam ); 80 81 81 chmat Ch; 82 Ch.setCh (Lam._L()*diag(sqrt(Lam._D())));82 Ch.setCh ( Lam._L() *diag ( sqrt ( Lam._D() ) ) ); 83 83 chmat iCh; 84 Ch.inv (iCh);85 86 eWishartCh Omega; //inverse Wishart, result is R, 87 Omega.set_parameters (iCh,nu-2*nPsi-dimx); // 2*nPsi is there to match numercial simulations - check if analytically correct88 84 Ch.inv ( iCh ); 85 86 eWishartCh Omega; //inverse Wishart, result is R, 87 Omega.set_parameters ( iCh, nu - 2*nPsi - dimx ); // 2*nPsi is there to match numercial simulations - check if analytically correct 88 89 89 chmat Omi; 90 Omi.setCh (Omega.sample_mat());91 92 mat Z =randn(M.rows(), M.cols());93 Mi = M + Omi._Ch() * Z * inv(Vz._L()*diag(sqrt(Vz._D())));94 Omi.inv (Ri);90 Omi.setCh ( Omega.sample_mat() ); 91 92 mat Z = randn ( M.rows(), M.cols() ); 93 Mi = M + Omi._Ch() * Z * inv ( Vz._L() * diag ( sqrt ( Vz._D() ) ) ); 94 Omi.inv ( Ri ); 95 95 } 96 96 … … 140 140 141 141 // bdm_assert_debug ( ( ( -nkG - nkW ) > -Inf ) && ( ( -nkG - nkW ) < Inf ), "ARX improper" ); 142 if ( -nkG - nkW==Inf){143 cout << "??" << endl;142 if ( -nkG - nkW == Inf ) { 143 cout << "??" << endl; 144 144 } 145 145 return -nkG - nkW; … … 162 162 } 163 163 164 void egiw::factorize (mat &M, ldmat &Vz, ldmat &Lam) const{164 void egiw::factorize ( mat &M, ldmat &Vz, ldmat &Lam ) const { 165 165 const mat &L = V._L(); 166 166 const vec &D = V._D(); 167 167 int end = L.rows() - 1; 168 169 Vz =ldmat(L ( dimx, end, dimx, end ), D(dimx,end));170 mat iLsub = ltuinv ( Vz._L() );168 169 Vz = ldmat ( L ( dimx, end, dimx, end ), D ( dimx, end ) ); 170 mat iLsub = ltuinv ( Vz._L() ); 171 171 // set mean value 172 172 mat Lpsi = L ( dimx, end, 0, dimx - 1 ); 173 173 M = iLsub * Lpsi; 174 175 Lam = ldmat ( L (0, dimx-1, 0, dimx-1 ), D (0, dimx-1 ) ); //exp val of R176 if (1){ // test with Peterka177 mat VF=V.to_mat();178 mat Vf=VF(0,dimx-1,0, dimx-1);179 mat Vzf = VF(dimx,end,0,dimx-1);180 mat VZ = VF(dimx,end,dimx,end);181 182 mat Lam2 = Vf-Vzf.T()*inv(VZ)*Vzf;183 174 175 Lam = ldmat ( L ( 0, dimx - 1, 0, dimx - 1 ), D ( 0, dimx - 1 ) ); //exp val of R 176 if ( 1 ) { // test with Peterka 177 mat VF = V.to_mat(); 178 mat Vf = VF ( 0, dimx - 1, 0, dimx - 1 ); 179 mat Vzf = VF ( dimx, end, 0, dimx - 1 ); 180 mat VZ = VF ( dimx, end, dimx, end ); 181 182 mat Lam2 = Vf - Vzf.T() * inv ( VZ ) * Vzf; 183 } 184 184 } 185 185 … … 193 193 // mat Dsub = diag ( D ( 1, end ) ); 194 194 195 ldmat LD (inv(Lsub).T(), 1.0/D(1,end));195 ldmat LD ( inv ( Lsub ).T(), 1.0 / D ( 1, end ) ); 196 196 return LD; 197 197 … … 217 217 mat R; 218 218 mean_mat ( M, R ); 219 return concat ( cvectorize ( M),cvectorize( R ) );219 return concat ( cvectorize ( M ), cvectorize ( R ) ); 220 220 } 221 221 … … 229 229 ldmat itmp ( l ); 230 230 tmp.inv ( itmp ); 231 231 232 232 // following Wikipedia notation 233 // m=nu-nPsi-dimx-1, p=dimx 234 double mp1p =nu-nPsi-2*dimx; // m-p+1235 double mp1m =mp1p-2;// m-p-1236 233 // m=nu-nPsi-dimx-1, p=dimx 234 double mp1p = nu - nPsi - 2 * dimx; // m-p+1 235 double mp1m = mp1p - 2; // m-p-1 236 237 237 if ( dimx == 1 ) { 238 238 double cove = V._D() ( 0 ) / mp1m ; … … 240 240 vec var ( l ); 241 241 var.set_subvector ( 0, diag ( itmp.to_mat() ) *cove ); 242 var ( l - 1 ) = cove * cove / ( mp1m-2);242 var ( l - 1 ) = cove * cove / ( mp1m - 2 ); 243 243 return var; 244 244 } else { 245 ldmat Vll ( V, linspace(0,dimx-1)); // top-left part of V246 mat Y =Vll.to_mat();247 mat varY (Y.rows(), Y.cols());248 249 double denom = ( mp1p-1)*mp1m*mp1m*(mp1m-2); // (m-p)(m-p-1)^2(m-p-3)250 251 int i, j;252 for ( i =0; i<Y.rows(); i++){253 for ( j =0; j<Y.cols(); j++){254 varY (i,j) = (mp1p*Y(i,j)*Y(i,j) + mp1m * Y(i,i)* Y(j,j)) /denom;245 ldmat Vll ( V, linspace ( 0, dimx - 1 ) ); // top-left part of V 246 mat Y = Vll.to_mat(); 247 mat varY ( Y.rows(), Y.cols() ); 248 249 double denom = ( mp1p - 1 ) * mp1m * mp1m * ( mp1m - 2 ); // (m-p)(m-p-1)^2(m-p-3) 250 251 int i, j; 252 for ( i = 0; i < Y.rows(); i++ ) { 253 for ( j = 0; j < Y.cols(); j++ ) { 254 varY ( i, j ) = ( mp1p * Y ( i, j ) * Y ( i, j ) + mp1m * Y ( i, i ) * Y ( j, j ) ) / denom; 255 255 } 256 256 } 257 vec mean_dR = diag (Y)/mp1m; // corresponds to cove258 vec var_th =diag ( itmp.to_mat() );259 vec var_Th ( mean_dR.length() *var_th.length() );257 vec mean_dR = diag ( Y ) / mp1m; // corresponds to cove 258 vec var_th = diag ( itmp.to_mat() ); 259 vec var_Th ( mean_dR.length() *var_th.length() ); 260 260 // diagonal of diag(mean_dR) \kron diag(var_th) 261 for ( int i=0; i<mean_dR.length(); i++){262 var_Th.set_subvector (i*var_th.length(), var_th*mean_dR(i));263 } 264 265 return concat (var_Th, cvectorize(varY));261 for ( int i = 0; i < mean_dR.length(); i++ ) { 262 var_Th.set_subvector ( i*var_th.length(), var_th*mean_dR ( i ) ); 263 } 264 265 return concat ( var_Th, cvectorize ( varY ) ); 266 266 } 267 267 }