dynare/matlab/PosteriorIRF.m

301 lines
11 KiB
Matlab

function PosteriorIRF()
% stephane.adjemian@ens.fr [09-25-2005]
global options_ estim_params_ oo_ M_
nvx = estim_params_.nvx;
nvn = estim_params_.nvn;
ncx = estim_params_.ncx;
ncn = estim_params_.ncn;
np = estim_params_.np ;
npar = nvx+nvn+ncx+ncn+np;
offset = npar-np;
DirectoryName = CheckPath('metropolis');
load([ DirectoryName '\' M_.fname '_mh_history'])
FirstMhFile = record.KeepedDraws.FirstMhFile;
FirstLine = record.KeepedDraws.FirstLine;
TotalNumberOfMhFiles = sum(record.MhDraws(:,2)); LastMhFile = TotalNumberOfMhFiles;
TotalNumberOfMhDraws = sum(record.MhDraws(:,1));
NumberOfDraws = TotalNumberOfMhDraws-floor(options_.mh_drop*TotalNumberOfMhDraws);
clear record;
MAX_nruns = ceil(options_.MaxNumberOfBytes/(npar+2)/8);
MAX_nirfs = ceil(options_.MaxNumberOfBytes/(options_.irf*length(oo_.steady_state)*M_.exo_nbr)/8);
B = round(0.25*NumberOfDraws);
if B <= MAX_nirfs
stock_irf = zeros(options_.irf,size(M_.endo_names,1),M_.exo_nbr,B);
elseif nvn & B > MAX_nirfs
stock_irf = zeros(options_.irf,size(M_.endo_names,1),M_.exo_nbr,MAX_nirfs);
end
irun = 0;
ifil = 1;
h = waitbar(0,'Bayesian IRFs...');
for b=1:B
irun = irun+1;
deep = GetOneDraw(NumberOfDraws,FirstMhFile,LastMhFile,FirstLine,MAX_nruns,DirectoryName);
M_.params(estim_params_.param_vals(:,1)) = deep(offset+1:end);
dr = resol(oo_.steady_state,0)
if nvx
ip = 1;
for i=1:nvx
k = estim_params_.var_exo(i,1);
M_.Sigma_e(k,k) = deep(ip)*deep(ip);
ip = ip+1;
end
end
if ncx
ip = nvx+nvn+1;
for i=1:ncx
k1 = estim_params_.corrx(i,1);
k2 = estim_params_.corrx(i,2);
M_.Sigma_e(k1,k2) = deep(ip)*sqrt(M_.Sigma_e(k1,k1)*M_.Sigma_e(k2,k2));
M_.Sigma_e(k2,k1) = M_.Sigma_e(k1,k2);
ip = ip+1;
end
end
SS(M_.exo_names_orig_ord,M_.exo_names_orig_ord) = M_.Sigma_e+1e-14*eye(M_.exo_nbr);
SS = transpose(chol(SS));
for i = 1:M_.exo_nbr
if SS(i,i) > 1e-13
y=irf(dr,SS(M_.exo_names_orig_ord,i), options_.irf, options_.drop,options_.replic,options_.order);
if options_.relative_irf
y = 100*y/cs(i,i);
end
for j = 1:size(M_.endo_names,1)
if max(y(j,:)) - min(y(j,:)) > 1e-10
stock_irf(:,j,i,b) = transpose(y(j,:));
end
end
end
end
if irun == MAX_nirfs | irun == B | i == B
if i == B
stock_irf = stock_irf(:,:,:,1:irun);
end
save([DirectoryName '\IRFs\' M_.fname '_irf' int2str(ifil)],'stock_irf');
ifil = ifil+1;
irun = 0;
end
waitbar(b/B,h);
end
ifil = ifil-1;
close(h)
MeanIRF = zeros(options_.irf,nvar,M_.exo_nbr);
MedianIRF = zeros(options_.irf,nvar,M_.exo_nbr);
StdIRF = zeros(options_.irf,nvar,M_.exo_nbr);
DistribIRF = zeros(options_.irf,9,nvar,M_.exo_nbr);
HPDIRF = zeros(options_.irf,2,nvar,M_.exo_nbr);
fprintf('MH: Posterior IRFs...\n');
tit(M_.exo_names_orig_ord,:) = M_.exo_names;
for i = 1:M_.exo_nbr
for j = 1:nvar
for k = 1:options_.irf
StartLine = 0;
tmp = zeros(B,1);
for file = 1:ifil
load([DirectoryName '\IRFs\' M_.fname '_irf' int2str(file)]);
DeProfundis = size(stock_irf,4);
tmp(StartLine+1:StartLine+DeProfundis) = squeeze(stock_irf(k,SelecVariables(j),i,:));
StartLine = StartLine+DeProfundis;
end
[MeanIRF(k,j,i),MedianIRF(k,j,i),VarIRF(k,j,i),HPDIRF(k,:,j,i),DistribIRF(k,:,j,i)] = posterior_moments(tmp,0);
disp([' Variable: ' deblank(M_.endo_names(SelecVariables(j),:)) ', orthogonalized shock to ' deblank(tit(i,:))])
end
end
end
clear stock_irf;
for i = 1:M_.exo_nbr
for j = 1:nvar
name = [deblank(M_.endo_names(SelecVariables(j),:)) '_' deblank(tit(i,:))];
eval(['oo_.PosteriorIRF.Mean.' name ' = MeanIRF(:,j,i);']);
eval(['oo_.PosteriorIRF.Median.' name ' = MedianIRF(:,j,i);']);
eval(['oo_.PosteriorIRF.Var.' name ' = VarIRF(:,j,i);']);
eval(['oo_.PosteriorIRF.Distribution.' name ' = DistribIRF(:,:,j,i);']);
eval(['oo_.PosteriorIRF.HPDinf.' name ' = HPDIRF(:,1,j,i);']);
eval(['oo_.PosteriorIRF.HPDsup.' name ' = HPDIRF(:,2,j,i);']);
end
end
return
%%
%% Finally i build the plots.
%%
if TeX
fidTeX = fopen([M_.fname '_BayesianIRF.TeX'],'w');
fprintf(fidTeX,'%% TeX eps-loader file generated by PosteriorIRF.m (Dynare).\n');
fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
fprintf(fidTeX,' \n');
end
if TeX; titTeX(M_.exo_name_orig_ord,:) = M_.exo_names_tex; end;
% $$$ for i=1:M_.exo_nbr
% $$$ number_of_plots_to_draw = 0;
% $$$ index = [];
% $$$ for j=1:nvar
% $$$ if MeanIRF(1,j,i)
% $$$ number_of_plots_to_draw = number_of_plots_to_draw + 1;
% $$$ index = cat(1,index,j);
% $$$ end
% $$$ end
% $$$ [nbplt,nr,nc,lr,lc,nstar] = pltorg(number_of_plots_to_draw);
% $$$ if nbplt == 1
% $$$ if options_.relative_irf
% $$$ hh = figure('Name',['Relative response to orthogonalized' ...
% $$$ ' shock to ' tit(i,:)]);
% $$$ else
% $$$ hh = figure('Name',['Orthogonalized shock to ' tit(i, ...
% $$$ :)]);
% $$$ end
% $$$ NAMES = [];
% $$$ if TeX; TEXNAMES = []; end;
% $$$ for j=1:number_of_plots_to_draw
% $$$ set(0,'CurrentFigure',hh)
% $$$ subplot(nr,nc,j);
% $$$ plot([1 options_.irf],[0 0],'-r','linewidth',0.5);
% $$$ hold on
% $$$ for k = 1:9
% $$$ plot(1:options_.irf,DistribIRF(:,index(j),i,k),'-g','linewidth',0.5)
% $$$ end
% $$$ plot(1:options_.irf,MeanIRF(:,index(j),i),'-k','linewidth',1)
% $$$ xlim([1 options_.irf]);
% $$$ hold off
% $$$ name = deblank(M_.endo_names(SelecVariables(index(j)),:));
% $$$ NAMES = strvcat(NAMES,name);
% $$$ if TeX
% $$$ texname = deblank(M_.endo_names_tex(SelecVariables(index(j)),:));
% $$$ TEXNAMES = strvcat(TEXNAMES,['$' texname '$']);
% $$$ end
% $$$ title(name,'Interpreter','none')
% $$$ end
% $$$ eval(['print -depsc2 ' M_.fname '_Bayesian_IRF_' deblank(tit(i,:))]);
% $$$ eval(['print -dpdf ' M_.fname '_Bayesian_IRF_' deblank(tit(i,:))]);
% $$$ saveas(hh,[M_.fname '_Bayesian_IRF_' deblank(tit(i,:)) '.fig']);
% $$$ if options_.nograph, close(hh), end
% $$$ if TeX
% $$$ fprintf(fidTeX,'\\begin{figure}[H]\n');
% $$$ for jj = 1:number_of_plots_to_draw
% $$$ fprintf(fidTeX,['\\psfrag{%s}[1][][0.5][0]{%s}\n'],deblank(NAMES(jj,:)),deblank(TEXNAMES(jj,:)));
% $$$ end
% $$$ fprintf(fidTeX,'\\centering \n');
% $$$ fprintf(fidTeX,'\\includegraphics[scale=0.5]{%s_Bayesian_IRF_%s}\n',M_.fname,deblank(tit(i,:)));
% $$$ if options_.relative_irf
% $$$ fprintf(fidTeX,['\\caption{Bayesian relative' ...
% $$$ ' IRF.}']);
% $$$ else
% $$$ fprintf(fidTeX,'\\caption{Bayesian IRF.}');
% $$$ end
% $$$ fprintf(fidTeX,'\\label{Fig:BayesianIRF:%s}\n',deblank(tit(i,:)));
% $$$ fprintf(fidTeX,'\\end{figure}\n');
% $$$ fprintf(fidTeX,' \n');
% $$$ end
% $$$ elseif nbplt > 1
% $$$ for fig = 1:nbplt-1
% $$$ if options_.relative_irf
% $$$ hh = figure('Name',['Relative response to orthogonalized' ...
% $$$ ' shock to ' tit(i,:) ' figure ' int2str(fig) '.']);
% $$$ else
% $$$ hh = figure('Name',['Orthogonalized shock to ' tit(i,:) ...
% $$$ ' figure ' int2str(fig) '.']);
% $$$ end
% $$$ NAMES = [];
% $$$ if TeX; TEXNAMES = []; end;
% $$$ for j=1:nstar
% $$$ jj = (fig-1)*nstar + j;
% $$$ subplot(nr,nc,j);
% $$$ plot([1 options_.irf],[0 0],'-r','linewidth',0.5);
% $$$ hold on
% $$$ for k = 1:9
% $$$ plot(1:options_.irf,DistribIRF(:,index(jj),i,k),'-g','linewidth',0.5)
% $$$ end
% $$$ plot(1:options_.irf,MeanIRF(:,index(jj),i),'-k','linewidth',1)
% $$$ xlim([1 options_.irf]);
% $$$ hold off
% $$$ name = deblank(M_.endo_names(SelecVariables(index(jj)),:));
% $$$ NAMES = strvcat(NAMES,name);
% $$$ if TeX
% $$$ texname = deblank(M_.endo_names_tex(SelecVariables(index(jj)),:));
% $$$ TEXNAMES = strvcat(TEXNAMES,['$' texname '$']);
% $$$ end
% $$$ title(name,'Interpreter','none')
% $$$ end
% $$$ eval(['print -depsc2 ' M_.fname '_Bayesian_IRF_' deblank(tit(i,:)) int2str(fig)]);
% $$$ eval(['print -dpdf ' M_.fname '_Bayesian_IRF_' deblank(tit(i,:)) int2str(fig)]);
% $$$ saveas(hh,[M_.fname '_Bayesian_IRF_' deblank(tit(i,:)) int2str(fig) '.fig']);
% $$$ if options_.nograph, close(hh), end
% $$$ if TeX
% $$$ fprintf(fidTeX,'\\begin{figure}[H]\n');
% $$$ for jj = 1:nstar
% $$$ fprintf(fidTeX,['\\psfrag{%s}[1][][0.5][0]{%s}\n'],deblank(NAMES(jj,:)),deblank(TEXNAMES(jj,:)));
% $$$ end
% $$$ fprintf(fidTeX,'\\centering \n');
% $$$ fprintf(fidTeX,'\\includegraphics[scale=0.5]{%s_Bayesian_IRF_%s%s}\n',M_.fname,deblank(tit(i,:)),int2str(fig));
% $$$ if options_.relative_irf == 1
% $$$ fprintf(fidTeX,['\\caption{Bayesian relative' ...
% $$$ ' IRF.}']);
% $$$ else
% $$$ fprintf(fidTeX,'\\caption{Bayesian IRF.}');
% $$$ end
% $$$ fprintf(fidTeX,'\\label{Fig:BayesianIRF:%s:%s}\n',deblank(tit(i,:)), int2str(fig));
% $$$ fprintf(fidTeX,'\\end{figure}\n');
% $$$ fprintf(fidTeX,' \n');
% $$$ end
% $$$ end
% $$$ hh = figure('Name',['Orthogonalized shock to ' tit(i,:) ' figure ' int2str(nbplt) '.']);
% $$$ NAMES = [];
% $$$ if TeX; TEXNAMES = []; end;
% $$$ for j=1:number_of_plots_to_draw -(nbplt-1)*nstar
% $$$ jj = (nbplt-1)*nstar + j;
% $$$ subplot(nr,nc,j);
% $$$ plot([1 options_.irf],[0 0],'-r','linewidth',0.5);
% $$$ hold on
% $$$ for k = 1:9
% $$$ plot(1:options_.irf,DistribIRF(:,index(jj),i,k),'-g','linewidth',0.5)
% $$$ end
% $$$ plot(1:options_.irf,MeanIRF(:,index(jj),i),'-k','linewidth',1)
% $$$ xlim([1 options_.irf]);
% $$$ hold off
% $$$ name = deblank(M_.endo_names(SelecVariables(index(jj)),:));
% $$$ NAMES = strvcat(NAMES,name);
% $$$ if TeX
% $$$ texname = deblank(M_.endo_names_tex(SelecVariables(index(jj)),:));
% $$$ TEXNAMES = strvcat(TEXNAMES,['$' texname '$']);
% $$$ end
% $$$ title(name,'Interpreter','none')
% $$$ end
% $$$ eval(['print -depsc2 ' M_.fname '_Bayesian_IRF_' deblank(tit(i,:)) int2str(nbplt)]);
% $$$ eval(['print -dpdf ' M_.fname '_Bayesian_IRF_' deblank(tit(i,:)) int2str(nbplt)]);
% $$$ saveas(hh,[M_.fname '_Bayesian_IRF_' deblank(tit(i,:)) int2str(nbplt) '.fig']);
% $$$ if options_.nograph, close(hh), end
% $$$ if TeX
% $$$ fprintf(fidTeX,'\\begin{figure}[H]\n');
% $$$ for jj = 1:nstar
% $$$ fprintf(fidTeX,['\\psfrag{%s}[1][][0.5][0]{%s}\n'],deblank(NAMES(jj,:)),deblank(TEXNAMES(jj,:)));
% $$$ end
% $$$ fprintf(fidTeX,'\\centering \n');
% $$$ fprintf(fidTeX,'\\includegraphics[scale=0.5]{%s_Bayesian_IRF_%s%s}\n',M_.fname,deblank(tit(i,:)),int2str(nbplt));
% $$$ fprintf(fidTeX,'\\caption{Bayesian IRF.}');
% $$$ fprintf(fidTeX,'\\label{Fig:BayesianIRF:%s:%s}\n',deblank(tit(i,:)), int2str(nbplt));
% $$$ fprintf(fidTeX,'\\end{figure}\n');
% $$$ fprintf(fidTeX,' \n');
% $$$ end
% $$$ else % nbplt = 0
% $$$ disp('There''s nothing to plot here!')
% $$$ end
% $$$ end
% $$$ if TeX
% $$$ fprintf(fidTeX,'%% End of TeX file.\n');
% $$$ fclose(fidTeX);
% $$$ end
% $$$ fprintf('MH: Posterior IRFs, done!\n');