Show
Ignore:
Timestamp:
11/25/09 12:14:38 (15 years ago)
Author:
mido
Message:

ASTYLER RUN OVER THE WHOLE LIBRARY, JUPEE

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • library/bdm/stat/exp_family.cpp

    r730 r737  
    1212using std::cout; 
    1313 
    14         /////////// 
    15  
    16 void BMEF::bayes( const vec &yt, const vec &cond ) { 
     14/////////// 
     15 
     16void BMEF::bayes ( const vec &yt, const vec &cond ) { 
    1717        this->bayes_weighted ( yt, cond, 1.0 ); 
    1818}; 
    1919 
    20 void egiw::set_parameters (int dimx0, ldmat V0, double nu0) { 
     20void egiw::set_parameters ( int dimx0, ldmat V0, double nu0 ) { 
    2121        dimx = dimx0; 
    2222        nPsi = V0.rows() - dimx; 
    23         dim = dimx * (dimx + nPsi); // size(R) + size(Theta) 
    24          
     23        dim = dimx * ( dimx + nPsi ); // size(R) + size(Theta) 
     24 
    2525        V = V0; 
    26         if (nu0 < 0) { 
     26        if ( nu0 < 0 ) { 
    2727                nu = 0.1 + nPsi + 2 * dimx + 2; // +2 assures finite expected value of R 
    2828                // terms before that are sufficient for finite normalization 
     
    3535        mat M; 
    3636        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 
     42mat egiw::sample_mat ( int n ) const { 
    4343        // TODO - correct approach - convert to product of norm * Wishart 
    4444        mat M; 
    4545        ldmat Vz; 
    4646        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() ); 
    5050        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 correct 
    55          
     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 
    5656        mat OmChi; 
    57         mat Z(M.rows(),M.cols()); 
     57        mat Z ( M.rows(), M.cols() ); 
    5858 
    5959        mat Mi; 
    6060        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() ) ) ); 
    6969        } 
    7070        return tmp; 
    7171} 
    7272 
    73 void egiw::sample_mat(mat &Mi, chmat &Ri)const{ 
    74          
     73void egiw::sample_mat ( mat &Mi, chmat &Ri ) const { 
     74 
    7575        // TODO - correct approach - convert to product of norm * Wishart 
    7676        mat M; 
    7777        ldmat Vz; 
    7878        ldmat Lam; 
    79         factorize(M,Vz,Lam); 
    80          
     79        factorize ( M, Vz, Lam ); 
     80 
    8181        chmat Ch; 
    82         Ch.setCh(Lam._L()*diag(sqrt(Lam._D()))); 
     82        Ch.setCh ( Lam._L() *diag ( sqrt ( Lam._D() ) ) ); 
    8383        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 correct 
    88          
     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 
    8989        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 ); 
    9595} 
    9696 
     
    140140 
    141141//      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; 
    144144        } 
    145145        return -nkG - nkW; 
     
    162162} 
    163163 
    164 void egiw::factorize(mat &M, ldmat &Vz, ldmat &Lam) const{       
     164void egiw::factorize ( mat &M, ldmat &Vz, ldmat &Lam ) const { 
    165165        const mat &L = V._L(); 
    166166        const vec &D = V._D(); 
    167167        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() ); 
    171171        // set mean value 
    172172        mat Lpsi = L ( dimx, end, 0, dimx - 1 ); 
    173173        M = iLsub * Lpsi; 
    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          } 
     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        } 
    184184} 
    185185 
     
    193193//              mat Dsub = diag ( D ( 1, end ) ); 
    194194 
    195                 ldmat LD(inv(Lsub).T(), 1.0/D(1,end)); 
     195                ldmat LD ( inv ( Lsub ).T(), 1.0 / D ( 1, end ) ); 
    196196                return LD; 
    197197 
     
    217217                mat R; 
    218218                mean_mat ( M, R ); 
    219                 return concat( cvectorize (  M),cvectorize( R ) ); 
     219                return concat ( cvectorize ( M ), cvectorize ( R ) ); 
    220220        } 
    221221 
     
    229229        ldmat itmp ( l ); 
    230230        tmp.inv ( itmp ); 
    231          
     231 
    232232        // following Wikipedia notation 
    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          
     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 
    237237        if ( dimx == 1 ) { 
    238238                double cove = V._D() ( 0 ) / mp1m ; 
     
    240240                vec var ( l ); 
    241241                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 ); 
    243243                return var; 
    244244        } else { 
    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; 
     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; 
    255255                        } 
    256256                } 
    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() ); 
     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() ); 
    260260                // 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 ) ); 
    266266        } 
    267267}