Home > matlab > ReshapeMatFiles.m

ReshapeMatFiles

PURPOSE ^

function ReshapeMatFiles(type, type2)

SYNOPSIS ^

function ReshapeMatFiles(type, type2)

DESCRIPTION ^

 function ReshapeMatFiles(type, type2)
 Reshapes and sorts (along the mcmc simulations) the mat files generated by DYNARE.
 4D-arrays are splitted along the first dimension.
 3D-arrays are splitted along the second dimension.

 INPUTS:
   type:            statistics type in the repertory:
                      dgse
                      irf_bvardsge
                      smooth
                      filter
                      error
                      innov
                      forcst
                      forcst1
   type2:           analysis type:
                      posterior
                      gsa
                      prior
    
 OUTPUTS:
    none              

 SPECIAL REQUIREMENTS
    none

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 function ReshapeMatFiles(type, type2)
0002 % function ReshapeMatFiles(type, type2)
0003 % Reshapes and sorts (along the mcmc simulations) the mat files generated by DYNARE.
0004 % 4D-arrays are splitted along the first dimension.
0005 % 3D-arrays are splitted along the second dimension.
0006 %
0007 % INPUTS:
0008 %   type:            statistics type in the repertory:
0009 %                      dgse
0010 %                      irf_bvardsge
0011 %                      smooth
0012 %                      filter
0013 %                      error
0014 %                      innov
0015 %                      forcst
0016 %                      forcst1
0017 %   type2:           analysis type:
0018 %                      posterior
0019 %                      gsa
0020 %                      prior
0021 %
0022 % OUTPUTS:
0023 %    none
0024 %
0025 % SPECIAL REQUIREMENTS
0026 %    none
0027 
0028 % Copyright (C) 2003-2010 Dynare Team
0029 %
0030 % This file is part of Dynare.
0031 %
0032 % Dynare is free software: you can redistribute it and/or modify
0033 % it under the terms of the GNU General Public License as published by
0034 % the Free Software Foundation, either version 3 of the License, or
0035 % (at your option) any later version.
0036 %
0037 % Dynare is distributed in the hope that it will be useful,
0038 % but WITHOUT ANY WARRANTY; without even the implied warranty of
0039 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0040 % GNU General Public License for more details.
0041 %
0042 % You should have received a copy of the GNU General Public License
0043 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
0044 
0045 global M_ options_
0046 
0047 if nargin==1, 
0048     MhDirectoryName = [ CheckPath('metropolis',M_.dname) filesep ];
0049 else
0050     if strcmpi(type2,'posterior')
0051         MhDirectoryName = [CheckPath('metropolis',M_.dname) filesep ];
0052     elseif strcmpi(type2,'gsa')
0053         if options_.opt_gsa.morris==1,
0054             MhDirectoryName = [CheckPath('gsa/screen',M_.dname) filesep ];
0055         elseif options_.opt_gsa.morris==2,
0056             MhDirectoryName = [CheckPath('gsa/identif',M_.dname) filesep ];
0057         elseif options_.opt_gsa.pprior
0058             MhDirectoryName = [CheckPath(['gsa' filesep 'prior'],M_.dname) filesep ];
0059         else
0060             MhDirectoryName = [CheckPath(['gsa' filesep 'mc'],M_.dname) filesep ];
0061         end
0062     else
0063         MhDirectoryName = [CheckPath('prior',M_.dname) filesep ];
0064     end  
0065 end
0066 switch type
0067   case 'irf_dsge'
0068     CAPtype  = 'IRF_DSGE';
0069     TYPEsize = [ options_.irf , size(options_.varlist,1) , M_.exo_nbr ];
0070     TYPEarray = 4;    
0071   case 'irf_bvardsge'
0072     CAPtype  = 'IRF_BVARDSGE';
0073     TYPEsize = [ options_.irf , size(options_.varobs,1) , M_.exo_nbr ];
0074     TYPEarray = 4;      
0075   case 'smooth'
0076     CAPtype  = 'SMOOTH';
0077     TYPEsize = [ M_.endo_nbr , options_.nobs ];
0078     TYPEarray = 3;
0079   case 'filter'
0080     CAPtype = 'FILTER';
0081     TYPEsize = [ M_.endo_nbr , options_.nobs + 1 ];% TO BE CHECKED!
0082     TYPEarray = 3;
0083   case 'error'
0084     CAPtype = 'ERROR';
0085     TYPEsize = [ size(options_.varobs,1) , options_.nobs ];
0086     TYPEarray = 3;
0087   case 'innov'
0088     CAPtype = 'INNOV';
0089     TYPEsize = [ M_.exo_nbr , options_.nobs ];
0090     TYPEarray = 3;
0091   case 'forcst'
0092     CAPtype = 'FORCST';
0093     TYPEsize = [ M_.endo_nbr , options_.forecast ];
0094     TYPEarray = 3;
0095   case 'forcst1'
0096     CAPtype = 'FORCST1';
0097     TYPEsize = [ M_.endo_nbr , options_.forecast ];
0098     TYPEarray = 3;
0099   otherwise
0100     disp('ReshapeMatFiles :: Unknown argument!')
0101     return
0102 end
0103 
0104 TYPEfiles = dir([MhDirectoryName M_.fname '_' type '*.mat']);
0105 NumberOfTYPEfiles = length(TYPEfiles);
0106 B = options_.B;
0107 
0108 switch TYPEarray
0109   case 4
0110     if NumberOfTYPEfiles > 1
0111         NumberOfPeriodsPerTYPEfiles = ceil(TYPEsize(1)/NumberOfTYPEfiles);
0112         foffset = NumberOfTYPEfiles-floor(TYPEsize(1)/NumberOfPeriodsPerTYPEfiles);
0113         reste = TYPEsize(1)-NumberOfPeriodsPerTYPEfiles*(NumberOfTYPEfiles-foffset);
0114         idx = 0;
0115         jdx = 0;
0116         for f1=1:NumberOfTYPEfiles-foffset
0117             eval(['STOCK_' CAPtype ' = zeros(NumberOfPeriodsPerTYPEfiles,TYPEsize(2),TYPEsize(3),B);'])
0118             for f2 = 1:NumberOfTYPEfiles
0119                 load([MhDirectoryName M_.fname '_' type int2str(f2) '.mat']);
0120                 eval(['STOCK_' CAPtype '(:,:,1:+size(stock_' type ',3),idx+1:idx+size(stock_' type ',4))=stock_' ...
0121                       type '(jdx+1:jdx+NumberOfPeriodsPerTYPEfiles,:,:,:);'])
0122                 eval(['idx = idx + size(stock_' type ',4);'])
0123             end
0124             %eval(['STOCK_' CAPtype ' = sort(STOCK_' CAPtype ',4);'])
0125             save([MhDirectoryName M_.fname '_' CAPtype 's' int2str(f1) '.mat'],['STOCK_' CAPtype]);
0126             jdx = jdx + NumberOfPeriodsPerTYPEfiles;
0127             idx = 0;
0128         end
0129         if reste
0130             eval(['STOCK_' CAPtype ' = zeros(reste,TYPEsize(2),TYPEsize(3),B);'])
0131             for f2 = 1:NumberOfTYPEfiles
0132                 load([MhDirectoryName M_.fname '_' type int2str(f2) '.mat']);
0133                 eval(['STOCK_' CAPtype '(:,:,:,idx+1:idx+size(stock_' type ',4))=stock_' type '(jdx+1:jdx+reste,:,:,:);'])
0134                 eval(['idx = idx + size(stock_' type ',4);'])
0135             end
0136             %eval(['STOCK_' CAPtype ' = sort(STOCK_' CAPtype ',4);'])
0137             save([MhDirectoryName M_.fname '_' CAPtype 's' int2str(NumberOfTYPEfiles-foffset+1) '.mat'],['STOCK_' CAPtype]);  
0138         end
0139     else
0140         load([MhDirectoryName M_.fname '_' type '1.mat']);
0141         %eval(['STOCK_' CAPtype ' = sort(stock_' type ',4);'])
0142         eval(['STOCK_' CAPtype ' = stock_' type ';'])
0143         save([MhDirectoryName M_.fname '_' CAPtype 's' int2str(1) '.mat'],['STOCK_' CAPtype ]);
0144     end
0145     % Original file format may be useful in some cases...
0146     % for file = 1:NumberOfTYPEfiles
0147     %  delete([MhDirectoryName M_.fname '_' type int2str(file) '.mat'])
0148     % end
0149   case 3
0150     if NumberOfTYPEfiles>1
0151         NumberOfPeriodsPerTYPEfiles = ceil( TYPEsize(2)/NumberOfTYPEfiles );
0152         reste = TYPEsize(2)-NumberOfPeriodsPerTYPEfiles*(NumberOfTYPEfiles-1);
0153         idx = 0;
0154         jdx = 0;
0155         for f1=1:NumberOfTYPEfiles-1
0156             eval(['STOCK_' CAPtype ' = zeros(TYPEsize(1),NumberOfPeriodsPerTYPEfiles,B);'])
0157             for f2 = 1:NumberOfTYPEfiles
0158                 load([MhDirectoryName M_.fname '_' type int2str(f2) '.mat']);
0159                 eval(['STOCK_' CAPtype '(:,:,idx+1:idx+size(stock_ ' type ',3))=stock_' type '(:,jdx+1:jdx+NumberOfPeriodsPerTYPEfiles,:);'])
0160                 eval(['idx = idx + size(stock_' type ',3);'])
0161             end
0162             %eval(['STOCK_' CAPtype ' = sort(STOCK_' CAPtype ',3);'])
0163             save([MhDirectoryName M_.fname '_' CAPtype 's' int2str(f1) '.mat'],['STOCK_' CAPtype]);
0164             jdx = jdx + NumberOfPeriodsPerTYPEfiles;
0165             idx = 0;
0166         end
0167         eval(['STOCK_' CAPtype ' = zeros(TYPEsize(1),reste,B);'])
0168         for f2 = 1:NumberOfTYPEfiles
0169             load([MhDirectoryName M_.fname '_' type int2str(f2) '.mat']);
0170             eval(['STOCK_' CAPtype '(:,:,idx+1:idx+size(stock_' type ',3))=stock_' type '(:,jdx+1:jdx+reste,:);'])
0171             eval(['idx = idx + size(stock_' type ',3);'])
0172         end
0173         %eval(['STOCK_' CAPtype ' = sort(STOCK_' CAPtype ',3);'])
0174         save([MhDirectoryName M_.fname '_' CAPtype 's' int2str(NumberOfTYPEfiles) '.mat'],['STOCK_' CAPtype]);
0175     else
0176         load([MhDirectoryName M_.fname '_' type '1.mat']);
0177         %eval(['STOCK_' CAPtype ' = sort(stock_' type ',3);'])
0178         eval(['STOCK_' CAPtype ' = stock_' type ';'])
0179         save([MhDirectoryName M_.fname '_' CAPtype 's' int2str(1) '.mat'],['STOCK_' CAPtype ]);      
0180     end
0181     % Original file format may be useful in some cases...
0182     % for file = 1:NumberOfTYPEfiles
0183     %   delete([MhDirectoryName M_.fname '_' type  int2str(file) '.mat'])
0184     % end
0185 end

Generated on Mon 21-May-2012 02:42:43 by m2html © 2005