0001 function PosteriorSmoother(Y,gend, type)
0002
0003 global options_ estim_params_ oo_ M_
0004
0005 nvx = estim_params_.nvx;
0006 nvn = estim_params_.nvn;
0007 ncx = estim_params_.ncx;
0008 ncn = estim_params_.ncn;
0009 np = estim_params_.np ;
0010 npar = nvx+nvn+ncx+ncn+np;
0011 offset = npar-np;
0012 naK = length(options_.filter_step_ahead);
0013
0014 MaxNumberOfPlotPerFigure = 4;
0015 MaxNumberOfBytes=options_.MaxNumberOfBytes;
0016 endo_nbr=M_.endo_nbr;
0017 exo_nbr=M_.exo_nbr;
0018 nvobs = size(options_.varobs,1);
0019 nn = sqrt(MaxNumberOfPlotPerFigure);
0020
0021 CheckPath('Plots/');
0022 DirectoryName = CheckPath('metropolis');
0023 load([ DirectoryName '/' M_.fname '_mh_history'])
0024 FirstMhFile = record.KeepedDraws.FirstMhFile;
0025 FirstLine = record.KeepedDraws.FirstLine;
0026 TotalNumberOfMhFiles = sum(record.MhDraws(:,2)); LastMhFile = TotalNumberOfMhFiles;
0027 TotalNumberOfMhDraws = sum(record.MhDraws(:,1));
0028 NumberOfDraws = TotalNumberOfMhDraws-floor(options_.mh_drop*TotalNumberOfMhDraws);
0029 clear record;
0030 MAX_nruns = ceil(options_.MaxNumberOfBytes/(npar+2)/8);
0031 MAX_nsmoo = ceil(MaxNumberOfBytes/((endo_nbr)*gend)/8);
0032 MAX_ninno = ceil(MaxNumberOfBytes/(exo_nbr*gend)/8);
0033 MAX_nerro = ceil(MaxNumberOfBytes/(size(options_.varobs,1)*gend)/8);
0034 MAX_naK = ceil(MaxNumberOfBytes/(size(options_.varobs,1)*length(options_.filter_step_ahead)*gend)/8);
0035
0036 B = round(0.25*NumberOfDraws);
0037
0038 varlist = options_.varlist;
0039 if isempty(varlist)
0040 varlist = M_.endo_names;
0041 SelecVariables = transpose(1:M_.endo_nbr);
0042 nvar = M_.endo_nbr;
0043 else
0044 nvar = size(varlist,1);
0045 SelecVariables = [];
0046 for i=1:nvar
0047 if ~isempty(strmatch(varlist(i,:),M_.endo_names,'exact'))
0048 SelecVariables = [SelecVariables;strmatch(varlist(i,:),M_.endo_names,'exact')];
0049 end
0050 end
0051 end
0052
0053 irun1 = 1;
0054 irun2 = 1;
0055 irun3 = 1;
0056 irun4 = 1;
0057 irun5 = 1;
0058 ifil1 = 1;
0059 ifil2 = 1;
0060 ifil3 = 1;
0061 ifil4 = 1;
0062 ifil5 = 1;
0063 h = waitbar(0,'Bayesian smoother...');
0064 if B <= MAX_nruns
0065 stock_param = zeros(B, npar);
0066 stock_logpo = zeros(B,1);
0067 stock_ys = zeros(B,endo_nbr);
0068 else
0069 stock_param = zeros(MAX_nruns, npar);
0070 stock_logpo = zeros(MAX_nruns,1);
0071 stock_ys = zeros(MAX_nruns,endo_nbr);
0072 end
0073 if options_.smoother
0074 if B <= MAX_nsmoo
0075 stock_smooth = zeros(endo_nbr,gend,B);
0076 else
0077 stock_smooth = zeros(endo_nbr,gend,MAX_nsmoo);
0078 end
0079 if B <= MAX_ninno
0080 stock_innov = zeros(exo_nbr,gend,B);
0081 else
0082 stock_innov = zeros(exo_nbr,gend,MAX_ninno);
0083 end
0084 if nvn & B <= MAX_nerro
0085 stock_error = zeros(nvobs,gend,B);
0086 else nvn & B > MAX_nerro
0087 stock_error = zeros(nvobs,gend,MAX_nerro);
0088 end
0089 end
0090 if options_.filter_step_ahead ~= 0
0091 if B <= MAX_naK
0092 stock_filter = zeros(naK,endo_nbr,gend+options_.filter_step_ahead(end),B);
0093 else
0094 stock_filter = zeros(naK,endo_nbr,gend+options_.filter_step_ahead(end),MAX_naK);
0095 end
0096 end
0097 for b=1:B
0098
0099 [deep, logpo] = GetOneDraw(type);
0100 set_all_parameters(deep);
0101 dr = resol(oo_.steady_state,0);
0102 [alphahat,etahat,epsilonhat,ahat,SteadyState,trend_coeff,aK] = DsgeSmoother(deep,gend,Y);
0103
0104 stock_smooth(:,:,irun1) = alphahat(1:endo_nbr,:);
0105 if nvx
0106 stock_innov(:,:,irun2) = etahat;
0107 end
0108 if nvn
0109 stock_error(:,:,irun3) = epsilonhat;
0110 end
0111 if naK
0112 stock_filter(:,:,:,irun4) = aK(options_.filter_step_ahead,1:endo_nbr,:);
0113 end
0114 stock_param(irun5,:) = deep;
0115 stock_logpo(irun5,1) = logpo;
0116 stock_ys(irun5,:) = SteadyState';
0117
0118 irun1 = irun1 + 1;
0119 irun2 = irun2 + 1;
0120 irun3 = irun3 + 1;
0121 irun4 = irun4 + 1;
0122 irun5 = irun5 + 1;
0123
0124 if irun1 > MAX_nsmoo | b == B
0125 if b == B
0126 stock_smooth = stock_smooth(:,:,1:irun1-1);
0127 end
0128 stock = stock_smooth;
0129 save([DirectoryName '/' M_.fname '_smooth' int2str(ifil1)],'stock');
0130 ifil1 = ifil1 + 1;
0131 irun1 = 1;
0132 end
0133
0134 if nvx & (irun2 > MAX_ninno | b == B)
0135 if b == B
0136 stock_innov = stock_innov(:,:,1:irun2-1);
0137 end
0138 stock = stock_innov;
0139 save([DirectoryName '/' M_.fname '_inno' int2str(ifil2)],'stock');
0140 ifil2 = ifil2 + 1;
0141 irun2 = 1;
0142 end
0143
0144 if nvn & (irun3 > MAX_error | b == B)
0145 if b == B
0146 stock_error = stock_error(:,:,1:irun3-1);
0147 end
0148 stock = stock_error;
0149 save([DirectoryName '/' M_.fname '_error' int2str(ifil3)],'stock');
0150 ifil3 = ifil3 + 1;
0151 irun3 = 1;
0152 end
0153
0154 if naK & (irun4 > MAX_naK | b == B)
0155 if b == B
0156 stock_filter = stock_filter(:,:,:,1:irun4-1);
0157 end
0158 stock = stock_filter;
0159 save([DirectoryName '/' M_.fname '_filter' int2str(ifil4)],'stock');
0160 ifil4 = ifil4 + 1;
0161 irun4 = 1;
0162 end
0163
0164 if irun5 > MAX_nruns | b == B
0165 if b == B
0166 stock_param = stock_param(1:irun5-1,:);
0167 stock_logpo = stock_logpo(1:irun5-1,1);
0168 stock_ys = stock_ys(1:irun5-1,:);
0169 end
0170 stock = stock_param;
0171 save([DirectoryName '/' M_.fname '_param' int2str(ifil5)],'stock','stock_logpo','stock_ys');
0172 ifil5 = ifil5 + 1;
0173 irun5 = 1;
0174 end
0175
0176 waitbar(b/B,h);
0177 end
0178 close(h)
0179
0180 stock_gend=gend;
0181 stock_data=Y;
0182 save([DirectoryName '/' M_.fname '_data'],'stock_gend','stock_data');