From b425351404c5fa9b2f4d5c5effe66ac7e3113535 Mon Sep 17 00:00:00 2001 From: adjemian Date: Sun, 25 Sep 2005 14:10:41 +0000 Subject: [PATCH] Added posterior densities git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@461 ac1d8469-bf42-47a9-8791-bf33cf982152 --- matlab/GetPosteriorParametersStatistics.m | 6 +- matlab/McMCDiagnostics.m | 4 +- matlab/PlotPosteriorDistributions.m | 122 ++++++++++++++++++++++ matlab/draw_prior_density.m | 2 +- matlab/dynare_estimation.m | 2 +- matlab/get_the_name.m | 14 ++- 6 files changed, 133 insertions(+), 17 deletions(-) create mode 100644 matlab/PlotPosteriorDistributions.m diff --git a/matlab/GetPosteriorParametersStatistics.m b/matlab/GetPosteriorParametersStatistics.m index c35036644..3dd82ffac 100644 --- a/matlab/GetPosteriorParametersStatistics.m +++ b/matlab/GetPosteriorParametersStatistics.m @@ -155,8 +155,4 @@ if ncn eval(['oo_.posterior_density.measurement_errors_corr.' NAME ' = post_density;']); ip = ip+1; end -end - - - - +end \ No newline at end of file diff --git a/matlab/McMCDiagnostics.m b/matlab/McMCDiagnostics.m index 33aaf0fe7..279a56183 100644 --- a/matlab/McMCDiagnostics.m +++ b/matlab/McMCDiagnostics.m @@ -1,5 +1,5 @@ function McmcDiagnostic -% stephane.adjemian.cepremap.cnrs.fr [06-04-2005] +% stephane.adjemian@ens.fr [06-04-2005] global options_ estim_params_ M_ @@ -23,7 +23,7 @@ NumberOfDraws = PastDraws(1); Origin = 1000; StepSize = ceil((NumberOfDraws-Origin)/100);% So that the computational time does not -ALPHA = 0.2; % increase too much with the number of simulations. +ALPHA = 0.2; % increase too much with the number of simulations. time = 1:NumberOfDraws; xx = Origin:StepSize:NumberOfDraws; NumberOfLines = length(xx); diff --git a/matlab/PlotPosteriorDistributions.m b/matlab/PlotPosteriorDistributions.m new file mode 100644 index 000000000..c2bea24fd --- /dev/null +++ b/matlab/PlotPosteriorDistributions.m @@ -0,0 +1,122 @@ +function PlotPosteriorDistributions() +% stephane.adjemian@ens.fr [09-09-2005] +global estim_params_ M_ options_ bayestopt_ oo_ + +DirectoryName = CheckPath('Plots\Densities') + +TeX = options_.TeX; +nblck = options_.mh_nblck; +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; + +MaxNumberOfPlotPerFigure = 9;% The square root must be an integer! +nn = sqrt(MaxNumberOfPlotPerFigure); + +figurename = 'Priors and posteriors'; + +if TeX + fidTeX = fopen([DirectoryName '\' M_.fname '_PriorsAndPosteriors.TeX'],'w'); + fprintf(fidTeX,'%% TeX eps-loader file generated by PlotPosteriorDistributions.m (Dynare).\n'); + fprintf(fidTeX,['%% ' datestr(now,0) '\n']); + fprintf(fidTeX,' \n'); +end + +figunumber = 0; +subplotnum = 0; +for i=1:npar + subplotnum = subplotnum+1; + if subplotnum == 1 + figunumber = figunumber+1; + hfig = figure('Name',figurename); + end + if subplotnum == 1 + if TeX + TeXNAMES = []; + end + NAMES = []; + end + [nam,texnam] = get_the_name(i,TeX); + NAMES = strvcat(NAMES,nam); + if TeX + TeXNAMES = strvcat(TeXNAMES,texnam); + end + [x2,f2,abscissa,dens,binf2,bsup2] = draw_prior_density(i); + top2 = max(f2); + if i <= nvx + name = deblank(M_.exo_names(estim_params_.var_exo(i,1),:)); + eval(['x1 = oo_.posterior_density.shocks_std.' name '(:,1);']) + eval(['f1 = oo_.posterior_density.shocks_std.' name '(:,2);']) + eval(['pmode = oo_.posterior_mode.shocks_std.' name ';']) + elseif i <= nvx+nvn + name = deblank(options_.varobs(estim_params_.var_endo(i-nvx,1),:)); + eval(['x1 = oo_.posterior_density.measurement_errors_std.' name '(:,1);']) + eval(['f1 = oo_.posterior_density.measurement_errors_std.' name '(:,2);']) + eval(['pmode = oo_.posterior_mode.measurement_errors_std.' name ';']) + elseif i <= nvx+nvn+ncx + j = i - (nvx+nvn) + k1 = estim_params_.corrx(j,1); + k2 = estim_params_.corrx(j,2); + name = [deblank(M_.exo_names(k1,:)) '_' deblank(M_.exo_names(k2,:))]; + eval(['x1 = oo_.posterior_density.shocks_corr.' name '(:,1);']) + eval(['f1 = oo_.posterior_density.shocks_corr.' name '(:,2);']) + eval(['pmode = oo_.posterior_mode.shocks_corr.' name ';']) + elseif i <= nvx+nvn+ncx+ncn + j = i - (nvx+nvn+ncx); + k1 = estim_params_.corrn(j,1); + k2 = estim_params_.corrn(j,2); + name = [deblank(M_.endo_names(k1,:)) '_' deblank(M_.endo_names(k2,:))]; + eval(['x1 = oo_.posterior_density.measurement_errors_corr.' name '(:,1);']) + eval(['f1 = oo_.posterior_density.measurement_errors_corr.' name '(:,2);']) + eval(['pmode = oo_.posterior_mode.measurement_errors_corr.' name ';']) + else + j = i - (nvx+nvn+ncx+ncn); + name = deblank(M_.param_names(estim_params_.param_vals(j,1),:)); + eval(['x1 = oo_.posterior_density.' name '(:,1);']) + eval(['f1 = oo_.posterior_density.' name '(:,2);']) + eval(['pmode = oo_.posterior_mode.parameters.' name ';']) + end + top1 = max(f1); + top0 = max([top1;top2]); + binf1 = x1(1); + bsup1 = x1(end); + borneinf = min(binf1,binf2); + bornesup = max(bsup1,bsup2); + subplot(nn,nn,subplotnum) + hh = plot(x2,f2,'-k','linewidth',2); + set(hh,'color',[0.7 0.7 0.7]); + hold on; + plot(x1,f1,'-k','linewidth',2); + plot( [pmode pmode], [0.0 1.1*top0], '--g', 'linewidth', 2); + box on; + axis([borneinf bornesup 0 1.1*top0]); + title(nam,'Interpreter','none'); + hold off; + drawnow + if subplotnum == MaxNumberOfPlotPerFigure | i == npar; + eval(['print -depsc2 ' DirectoryName '\' M_.fname '_PriorsAndPosteriors' int2str(figunumber)]); + eval(['print -dpdf ' DirectoryName '\' M_.fname '_PriorsAndPosteriors' int2str(figunumber)]); + saveas(hfig,[DirectoryName '\' M_.fname '_PriorsAndPosteriors' int2str(figunumber) '.fig']); + if TeX + fprintf(fidTeX,'\\begin{figure}[H]\n'); + for j = 1:size(NAMES,1) + fprintf(fidTeX,'\\psfrag{%s}[1][][0.5][0]{%s}\n',deblank(NAMES(j,:)),deblank(TeXNAMES(j,:))); + end + fprintf(fidTeX,'\\centering\n'); + fprintf(fidTeX,'\\includegraphics[scale=0.5]{%s_PriorsAndPosteriors%s}\n',M_.fname,int2str(figunumber)); + fprintf(fidTeX,'\\caption{Priors and posteriors.}'); + fprintf(fidTeX,'\\label{Fig:PriorsAndPosteriors:%s}\n',int2str(figunumber)); + fprintf(fidTeX,'\\end{figure}\n'); + fprintf(fidTeX,' \n'); + if i == npar + fprintf(fidTeX,'%% End of TeX file.\n'); + fclose(fidTeX); + end + end + if options_.nograph, close(hfig), end + subplotnum = 0; + end +end \ No newline at end of file diff --git a/matlab/draw_prior_density.m b/matlab/draw_prior_density.m index 0bc0b5c00..125efaacc 100644 --- a/matlab/draw_prior_density.m +++ b/matlab/draw_prior_density.m @@ -1,5 +1,5 @@ function [x,f,abscissa,dens,binf,bsup] = draw_prior_density(indx); -% stephane.adjemian@cepremap.cnrs.fr [07-15-2004] +% stephane.adjemian@ens.fr [07-15-2004] global bayestopt_ diff --git a/matlab/dynare_estimation.m b/matlab/dynare_estimation.m index 5d93fa608..cc56bf783 100644 --- a/matlab/dynare_estimation.m +++ b/matlab/dynare_estimation.m @@ -649,7 +649,7 @@ if (any(bayestopt_.pshape >0 ) & options_.mh_replic) | (any(bayestopt_.pshape > marginal = marginal_density; %% GetPosteriorParametersStatistics(); - + PlotPosteriorDistributions(); return end diff --git a/matlab/get_the_name.m b/matlab/get_the_name.m index 5e73fd5d5..c4c3ca152 100644 --- a/matlab/get_the_name.m +++ b/matlab/get_the_name.m @@ -8,7 +8,7 @@ texnam = []; nvx = estim_params_.nvx; nvn = estim_params_.nvn; ncx = estim_params_.ncx; -ncx = estim_params_.ncn; +ncn = estim_params_.ncn; if k <= nvx vname = deblank(M_.exo_names(estim_params_.var_exo(k,1),:)); @@ -24,8 +24,8 @@ elseif k <= (nvx+nvn) tname = deblank(options_.TeX_varobs(estim_params_.var_endo(k-estim_params_.nvx,1),:)); texnam = ['$ SE_{' tname '} $']; end -elseif k <= (estim_params_.nvx+estim_params_.nvn+estim_params_.ncx) - jj = k - (estim_params_.nvx+estim_params_.nvn); +elseif k <= (nvx+nvn+ncx) + jj = k - (nvx+nvn); k1 = estim_params_.corrx(jj,1); k2 = estim_params_.corrx(jj,2); vname = [deblank(M_.exo_names(k1,:)) '_' deblank(M_.exo_names(k2,:))]; @@ -34,9 +34,8 @@ elseif k <= (estim_params_.nvx+estim_params_.nvn+estim_params_.ncx) tname = [deblank(M_.exo_names_tex(k1,:)) ',' deblank(M_.exo_names_tex(k2,:))]; texnam = ['$ CC_{' tname '} $']; end -elseif k <= (estim_params_.nvx+estim_params_.nvn+estim_params_.ncx+ ... - estim_params_.ncn) - jj = k - (estim_params_.nvx+estim_params_.nvn+estim_params_.ncx); +elseif k <= (nvx+nvn+ncx+ncn) + jj = k - (nvx+nvn+ncx); k1 = estim_params_.corrn(jj,1); k2 = estim_params_.corrn(jj,2); vname = [deblank(M_.endo_names(k1,:)) '_' deblank(M_.endo_names(k2,:))]; @@ -46,8 +45,7 @@ elseif k <= (estim_params_.nvx+estim_params_.nvn+estim_params_.ncx+ ... texnam =['$ CC_{' tname '} $']; end else - jj = k - (estim_params_.nvx+estim_params_.nvn+estim_params_.ncx+ ... - estim_params_.ncn); + jj = k - (nvx+nvn+ncx+ncn); jj1 = estim_params_.param_vals(jj,1); nam = deblank(M_.param_names(jj1,:)); if TeX