0001 function PosteriorIRF(type)
0002
0003 global options_ estim_params_ oo_ M_
0004 nvx = estim_params_.nvx;
0005 nvn = estim_params_.nvn;
0006 ncx = estim_params_.ncx;
0007 ncn = estim_params_.ncn;
0008 np = estim_params_.np ;
0009 npar = nvx+nvn+ncx+ncn+np;
0010 offset = npar-np;
0011
0012 MaxNumberOfPlotPerFigure = 9;
0013 nn = sqrt(MaxNumberOfPlotPerFigure);
0014 DirectoryName = CheckPath('Output');
0015 if strcmpi(type,'posterior')
0016 MhDirectoryName = CheckPath('metropolis');
0017 else
0018 MhDirectoryName = CheckPath('prior');
0019 end
0020 MAX_nirfs = ceil(options_.MaxNumberOfBytes/(options_.irf*length(oo_.steady_state)*M_.exo_nbr)/8)+50;
0021 MAX_nruns = ceil(options_.MaxNumberOfBytes/(npar+2)/8);
0022
0023 if strcmpi(type,'posterior')
0024 load([ MhDirectoryName '/' M_.fname '_mh_history'])
0025 TotalNumberOfMhDraws = sum(record.MhDraws(:,1));
0026 NumberOfDraws = TotalNumberOfMhDraws-floor(options_.mh_drop*TotalNumberOfMhDraws);
0027 else
0028 NumberOfDraws = 500;
0029 end
0030 B = min([round(.5*NumberOfDraws),500]); options_.B = B;
0031 try delete([MhDirectoryName '\' M_.fname '_IRFs*']);
0032 catch disp('No _IRFs files to be deleted!')
0033 end
0034 irun = 0;
0035 irun2 = 0;
0036 NumberOfIRFfiles = 1;
0037 ifil2 = 1;
0038 if strcmpi(type,'posterior')
0039 h = waitbar(0,'Bayesian (posterior) IRFs...');
0040 else
0041 h = waitbar(0,'Bayesian (prior) IRFs...');
0042 end
0043 if B <= MAX_nruns
0044 stock_param = zeros(B, npar);
0045 else
0046 stock_param = zeros(MAX_nruns, npar);
0047 end
0048 if B >= MAX_nirfs
0049 stock_irf = zeros(options_.irf,M_.endo_nbr,M_.exo_nbr,MAX_nirfs);
0050 else
0051 stock_irf = zeros(options_.irf,M_.endo_nbr,M_.exo_nbr,B);
0052 end
0053 for b=1:B
0054 irun = irun+1;
0055 irun2 = irun2+1;
0056 deep = GetOneDraw(type);
0057 stock_param(irun2,:) = deep;
0058 set_parameters(deep);
0059 dr = resol(oo_.steady_state,0);
0060 SS(M_.exo_names_orig_ord,M_.exo_names_orig_ord) = M_.Sigma_e+1e-14*eye(M_.exo_nbr);
0061 SS = transpose(chol(SS));
0062 for i = 1:M_.exo_nbr
0063 if SS(i,i) > 1e-13
0064 y=irf(dr,SS(M_.exo_names_orig_ord,i), options_.irf, options_.drop,options_.replic,options_.order);
0065 if options_.relative_irf
0066 y = 100*y/cs(i,i);
0067 end
0068 for j = 1:M_.endo_nbr
0069 if max(y(j,:)) - min(y(j,:)) > 1e-10
0070 stock_irf(:,j,i,irun) = transpose(y(j,:));
0071 end
0072 end
0073 end
0074 end
0075 if irun == MAX_nirfs | irun == B | b == B
0076 if b == B
0077 stock_irf = stock_irf(:,:,:,1:irun);
0078 end
0079 save([MhDirectoryName '/' M_.fname '_irf' int2str(NumberOfIRFfiles)],'stock_irf');
0080 NumberOfIRFfiles = NumberOfIRFfiles+1;
0081 irun = 0;
0082 end
0083 if irun2 > MAX_nruns | b == B
0084 if b == B
0085 stock_param = stock_param(1:irun2,:);
0086 end
0087 stock = stock_param;
0088 save([MhDirectoryName '/' M_.fname '_param_irf' int2str(ifil2)],'stock');
0089 ifil2 = ifil2 + 1;
0090 irun2 = 1;
0091 end
0092 waitbar(b/B,h);
0093 end
0094 NumberOfIRFfiles = NumberOfIRFfiles-1;
0095 ifil2 = ifil2-1;
0096 close(h);
0097
0098 ReshapeMatFiles('irf')
0099
0100 varlist = options_.varlist;
0101 if isempty(varlist)
0102 varlist = M_.endo_names;
0103 SelecVariables = transpose(1:M_.endo_nbr);
0104 nvar = M_.endo_nbr;
0105 else
0106 nvar = size(varlist,1);
0107 SelecVariables = [];
0108 for i=1:nvar
0109 if ~isempty(strmatch(varlist(i,:),M_.endo_names,'exact'))
0110 SelecVariables = [SelecVariables;strmatch(varlist(i,:),M_.endo_names,'exact')];
0111 end
0112 end
0113 end
0114
0115 MeanIRF = zeros(options_.irf,nvar,M_.exo_nbr);
0116 MedianIRF = zeros(options_.irf,nvar,M_.exo_nbr);
0117 StdIRF = zeros(options_.irf,nvar,M_.exo_nbr);
0118 DistribIRF = zeros(options_.irf,9,nvar,M_.exo_nbr);
0119 HPDIRF = zeros(options_.irf,2,nvar,M_.exo_nbr);
0120
0121 if options_.TeX
0122 varlist_TeX = [];
0123 for i=1:nvar
0124 varlist_TeX = strvcat(varlist_TeX,M_.endo_names_tex(SelecVariables(i),:));
0125 end
0126 end
0127
0128 fprintf('MH: Posterior IRFs...\n');
0129 tit(M_.exo_names_orig_ord,:) = M_.exo_names;
0130 kdx = 0;
0131 for file = 1:NumberOfIRFfiles
0132 load([MhDirectoryName '/' M_.fname '_IRFs' int2str(file)]);
0133 for i = 1:M_.exo_nbr
0134 for j = 1:nvar
0135 for k = 1:size(STOCK_IRF,1)
0136 kk = k+kdx;
0137 [MeanIRF(kk,j,i),MedianIRF(kk,j,i),VarIRF(kk,j,i),HPDIRF(kk,:,j,i),DistribIRF(kk,:,j,i)] = ...
0138 posterior_moments(squeeze(STOCK_IRF(k,SelecVariables(j),i,:)),0);
0139 end
0140 end
0141 end
0142 kdx = kdx + size(STOCK_IRF,1);
0143 end
0144 clear STOCK_IRF;
0145
0146 for i = 1:M_.exo_nbr
0147 for j = 1:nvar
0148 name = [deblank(M_.endo_names(SelecVariables(j),:)) '_' deblank(tit(i,:))];
0149 eval(['oo_.PosteriorIRF.Mean.' name ' = MeanIRF(:,j,i);']);
0150 eval(['oo_.PosteriorIRF.Median.' name ' = MedianIRF(:,j,i);']);
0151 eval(['oo_.PosteriorIRF.Var.' name ' = VarIRF(:,j,i);']);
0152 eval(['oo_.PosteriorIRF.Distribution.' name ' = DistribIRF(:,:,j,i);']);
0153 eval(['oo_.PosteriorIRF.HPDinf.' name ' = HPDIRF(:,1,j,i);']);
0154 eval(['oo_.PosteriorIRF.HPDsup.' name ' = HPDIRF(:,2,j,i);']);
0155 end
0156 end
0157
0158
0159
0160 if options_.TeX
0161 fidTeX = fopen([DirectoryName '/' M_.fname '_BayesianIRF.TeX'],'w');
0162 fprintf(fidTeX,'%% TeX eps-loader file generated by PosteriorIRF.m (Dynare).\n');
0163 fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
0164 fprintf(fidTeX,' \n');
0165 titTeX(M_.exo_names_orig_ord,:) = M_.exo_names_tex;
0166 end
0167
0168 subplotnum = 0;
0169 for i=1:M_.exo_nbr
0170 NAMES = [];
0171 if options_.TeX
0172 TEXNAMES = [];
0173 end
0174 figunumber = 0;
0175 for j=1:nvar
0176 if max(abs(MeanIRF(:,j,i))) > 10^(-6)
0177 subplotnum = subplotnum+1;
0178 if options_.nograph
0179 if subplotnum == 1 & options_.relative_irf
0180 hh = figure('Name',['Relative response to orthogonalized shock to ' tit(i,:)],'Visible','off');
0181 elseif subplotnum == 1 & ~options_.relative_irf
0182 hh = figure('Name',['Orthogonalized shock to ' tit(i,:)],'Visible','off');
0183 end
0184 else
0185 if subplotnum == 1 & options_.relative_irf
0186 hh = figure('Name',['Relative response to orthogonalized shock to ' tit(i,:)]);
0187 elseif subplotnum == 1 & ~options_.relative_irf
0188 hh = figure('Name',['Orthogonalized shock to ' tit(i,:)]);
0189 end
0190 end
0191 set(0,'CurrentFigure',hh)
0192 subplot(nn,nn,subplotnum);
0193 plot([1 options_.irf],[0 0],'-r','linewidth',0.5);
0194 hold on
0195 for k = 1:9
0196 plot(1:options_.irf,DistribIRF(:,k,j,i),'-g','linewidth',0.5)
0197 end
0198 plot(1:options_.irf,MeanIRF(:,j,i),'-k','linewidth',1)
0199 xlim([1 options_.irf]);
0200 hold off
0201 name = deblank(varlist(j,:));
0202 NAMES = strvcat(NAMES,name);
0203 if options_.TeX
0204 texname = deblank(varlist_TeX(j,:));
0205 TEXNAMES = strvcat(TEXNAMES,['$' texname '$']);
0206 end
0207 title(name,'Interpreter','none')
0208 end
0209 if subplotnum == MaxNumberOfPlotPerFigure | (j == nvar & subplotnum>0)
0210 figunumber = figunumber+1;
0211 set(hh,'visible','on')
0212 eval(['print -depsc2 ' DirectoryName '/' M_.fname '_Bayesian_IRF_' deblank(tit(i,:)) '_' int2str(figunumber)]);
0213 eval(['print -dpdf ' DirectoryName '/' M_.fname '_Bayesian_IRF_' deblank(tit(i,:)) '_' int2str(figunumber)]);
0214 saveas(hh,[DirectoryName '/' M_.fname '_Bayesian_IRF_' deblank(tit(i,:)) '_' int2str(figunumber) '.fig']);
0215 set(hh,'visible','off')
0216 if options_.nograph, close(hh), end
0217 if options_.TeX
0218 fprintf(fidTeX,'\\begin{figure}[H]\n');
0219 for jj = 1:size(TEXNAMES,1)
0220 fprintf(fidTeX,['\\psfrag{%s}[1][][0.5][0]{%s}\n'],deblank(NAMES(jj,:)),deblank(TEXNAMES(jj,:)));
0221 end
0222 fprintf(fidTeX,'\\centering \n');
0223 fprintf(fidTeX,'\\includegraphics[scale=0.5]{%s_Bayesian_IRF_%s}\n',M_.fname,deblank(tit(i,:)));
0224 if options_.relative_irf
0225 fprintf(fidTeX,['\\caption{Bayesian relative IRF.}']);
0226 else
0227 fprintf(fidTeX,'\\caption{Bayesian IRF.}');
0228 end
0229 fprintf(fidTeX,'\\label{Fig:BayesianIRF:%s}\n',deblank(tit(i,:)));
0230 fprintf(fidTeX,'\\end{figure}\n');
0231 fprintf(fidTeX,' \n');
0232 end
0233 subplotnum = 0;
0234 end
0235 end
0236 end
0237
0238 if options_.TeX
0239 fprintf(fidTeX,'%% End of TeX file.\n');
0240 fclose(fidTeX);
0241 end
0242 fprintf('MH: Posterior IRFs, done!\n');