1function oo_ = PlotPosteriorDistributions(estim_params_, M_, options_, bayestopt_, oo_)
2
3% function PlotPosteriorDistributions()
4% plots posterior distributions
5%
6% INPUTS
7%    estim_params_   [structure]
8%    M_              [structure]
9%    options_        [structure]
10%    bayestopt_      [structure]
11%    oo_             [structure]
12%
13% OUTPUTS
14%    oo_             [structure]
15%
16% SPECIAL REQUIREMENTS
17%    none
18
19% Copyright (C) 2005-2018 Dynare Team
20%
21% This file is part of Dynare.
22%
23% Dynare is free software: you can redistribute it and/or modify
24% it under the terms of the GNU General Public License as published by
25% the Free Software Foundation, either version 3 of the License, or
26% (at your option) any later version.
27%
28% Dynare is distributed in the hope that it will be useful,
29% but WITHOUT ANY WARRANTY; without even the implied warranty of
30% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31% GNU General Public License for more details.
32%
33% You should have received a copy of the GNU General Public License
34% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
35
36OutputDirectoryName = CheckPath('Output',M_.dname);
37
38TeX     = options_.TeX;
39nblck   = options_.mh_nblck;
40nvx     = estim_params_.nvx;
41nvn     = estim_params_.nvn;
42ncx     = estim_params_.ncx;
43ncn     = estim_params_.ncn;
44np      = estim_params_.np ;
45npar    = nvx+nvn+ncx+ncn+np;
46
47MaxNumberOfPlotPerFigure = 9;% The square root must be an integer!
48nn = sqrt(MaxNumberOfPlotPerFigure);
49
50figurename = 'Priors and posteriors';
51
52if TeX && any(strcmp('eps',cellstr(options_.graph_format)))
53    fidTeX = fopen([OutputDirectoryName '/' M_.fname '_PriorsAndPosteriors.tex'],'w');
54    fprintf(fidTeX,'%% TeX eps-loader file generated by PlotPosteriorDistributions.m (Dynare).\n');
55    fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
56    fprintf(fidTeX,' \n');
57end
58
59figunumber = 0;
60subplotnum = 0;
61
62for i=1:npar
63    subplotnum = subplotnum+1;
64    if subplotnum == 1
65        figunumber = figunumber+1;
66        hfig=dyn_figure(options_.nodisplay, 'Name', figurename);
67    end
68    [nam,texnam] = get_the_name(i, TeX, M_, estim_params_, options_);
69    if subplotnum == 1
70        NAMES = {nam};
71        if TeX
72            TeXNAMES = {texnam};
73        end
74    else
75        NAMES = [NAMES; {nam}];
76        if TeX
77            TeXNAMES = [TeXNAMES; {texnam}];
78        end
79    end
80    [x2, f2, abscissa, dens, binf2, bsup2] = draw_prior_density(i, bayestopt_);
81    top2 = max(f2);
82    if i <= nvx
83        name = M_.exo_names{estim_params_.var_exo(i,1)};
84        x1 = oo_.posterior_density.shocks_std.(name)(:,1);
85        f1 = oo_.posterior_density.shocks_std.(name)(:,2);
86        oo_.prior_density.shocks_std.(name)(:,1) = x2;
87        oo_.prior_density.shocks_std.(name)(:,2) = f2;
88        if ~options_.mh_posterior_mode_estimation
89            pmod = oo_.posterior_mode.shocks_std.(name);
90        end
91    elseif i <= nvx+nvn
92        name = options_.varobs{estim_params_.nvn_observable_correspondence(i-nvx,1)};
93        x1 = oo_.posterior_density.measurement_errors_std.(name)(:,1);
94        f1 = oo_.posterior_density.measurement_errors_std.(name)(:,2);
95        oo_.prior_density.measurement_errors_std.(name)(:,1) = x2;
96        oo_.prior_density.measurement_errors_std.(name)(:,2) = f2;
97        if ~options_.mh_posterior_mode_estimation
98            pmod = oo_.posterior_mode.measurement_errors_std.(name);
99        end
100    elseif i <= nvx+nvn+ncx
101        j = i - (nvx+nvn);
102        k1 = estim_params_.corrx(j,1);
103        k2 = estim_params_.corrx(j,2);
104        name = sprintf('%s_%s', M_.exo_names{k1}, M_.exo_names{k2});
105        x1 = oo_.posterior_density.shocks_corr.(name)(:,1);
106        f1 = oo_.posterior_density.shocks_corr.(name)(:,2);
107        oo_.prior_density.shocks_corr.(name)(:,1) = x2;
108        oo_.prior_density.shocks_corr.(name)(:,2) = f2;
109        if ~options_.mh_posterior_mode_estimation
110            pmod = oo_.posterior_mode.shocks_corr.(name);
111        end
112    elseif i <= nvx+nvn+ncx+ncn
113        j = i - (nvx+nvn+ncx);
114        k1 = estim_params_.corrn(j,1);
115        k2 = estim_params_.corrn(j,2);
116        name = sprintf('%s_%s', M_.endo_names{k1}, M_.endo_names{k2});
117        x1 = oo_.posterior_density.measurement_errors_corr.(name)(:,1);
118        f1 = oo_.posterior_density.measurement_errors_corr.(name)(:,2);
119        oo_.prior_density.measurement_errors_corr.(name)(:,1) = x2;
120        oo_.prior_density.measurement_errors_corr.(name)(:,2) = f2;
121        if ~options_.mh_posterior_mode_estimation
122            pmod = oo_.posterior_mode.measurement_errors_corr.(name);
123        end
124    else
125        j = i - (nvx+nvn+ncx+ncn);
126        name = M_.param_names{estim_params_.param_vals(j,1)};
127        x1 = oo_.posterior_density.parameters.(name)(:,1);
128        f1 = oo_.posterior_density.parameters.(name)(:,2);
129        oo_.prior_density.parameters.(name)(:,1) = x2;
130        oo_.prior_density.parameters.(name)(:,2) = f2;
131        if ~options_.mh_posterior_mode_estimation
132            pmod = oo_.posterior_mode.parameters.(name);
133        end
134    end
135    top1 = max(f1);
136    top0 = max([top1; top2]);
137    binf1 = x1(1);
138    bsup1 = x1(end);
139    borneinf = min(binf1, binf2);
140    bornesup = max(bsup1, bsup2);
141    subplot(nn, nn, subplotnum)
142    hh = plot(x2, f2, '-k', 'linewidth', 2);
143    set(hh, 'color', [0.7 0.7 0.7]);
144    hold on;
145    plot(x1, f1, '-k', 'linewidth', 2);
146    if ~options_.mh_posterior_mode_estimation
147        plot([pmod pmod], [0.0 1.1*top0], '--g', 'linewidth', 2);
148    end
149    box on
150    axis([borneinf bornesup 0 1.1*top0])
151    title(nam, 'Interpreter', 'none')
152    hold off
153    drawnow
154    if subplotnum == MaxNumberOfPlotPerFigure || i == npar
155        dyn_saveas(hfig,[OutputDirectoryName '/' M_.fname '_PriorsAndPosteriors' int2str(figunumber)], options_.nodisplay, options_.graph_format);
156        if TeX && any(strcmp('eps', cellstr(options_.graph_format)))
157            fprintf(fidTeX, '\\begin{figure}[H]\n');
158            for j = 1:size(NAMES, 1)
159                fprintf(fidTeX, '\\psfrag{%s}[1][][0.5][0]{%s}\n', NAMES{j}, TeXNAMES{j});
160            end
161            fprintf(fidTeX, '\\centering\n');
162            fprintf(fidTeX, '\\includegraphics[width=%2.2f\\textwidth]{%s/%s_PriorsAndPosteriors%s}\n', ...
163                    options_.figures.textwidth*min(subplotnum/nn,1), OutputDirectoryName, M_.fname, int2str(figunumber));
164            fprintf(fidTeX,'\\caption{Priors and posteriors.}');
165            fprintf(fidTeX,'\\label{Fig:PriorsAndPosteriors:%s}\n', int2str(figunumber));
166            fprintf(fidTeX,'\\end{figure}\n');
167            fprintf(fidTeX,' \n');
168            if i == npar
169                fprintf(fidTeX,'%% End of TeX file.\n');
170                fclose(fidTeX);
171            end
172        end
173        subplotnum = 0;
174    end
175end