method_of_moments: adapt comparison and display scripts for irf_matching

covariance-quadratic-approximation
Willi Mutschler 2023-10-09 22:36:08 +02:00
parent f210ffa3ae
commit 8a29933c6e
No known key found for this signature in database
GPG Key ID: 91E724BF17A73F6D
4 changed files with 208 additions and 81 deletions

View File

@ -1,75 +0,0 @@
function display_comparison_moments(M_, options_mom_, data_moments, model_moments)
% display_comparison_moments(M_, options_mom_, data_moments, model_moments)
% -------------------------------------------------------------------------
% Displays and saves to disk the comparison of the data moments and the model moments
% -------------------------------------------------------------------------
% INPUTS
% M_: [structure] model information
% options_mom_: [structure] method of moments options
% data_moments: [vector] data moments
% model_moments: [vector] model moments
% -------------------------------------------------------------------------
% OUTPUT
% No output, just displays and saves to disk the comparison of the data moments and the model moments
% -------------------------------------------------------------------------
% This function is called by
% o mom.run
% -------------------------------------------------------------------------
% This function calls
% o dyn_latex_table
% o dyntable
% o cellofchararraymaxlength
% -------------------------------------------------------------------------
% Copyright © 2023 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
titl = ['Comparison of matched data moments and model moments (',options_mom_.mom.mom_method,')'];
headers = {'Moment','Data','Model'};
for jm = 1:size(M_.matched_moments,1)
lables_tmp = 'E[';
lables_tmp_tex = 'E \left[ ';
for jvar = 1:length(M_.matched_moments{jm,1})
lables_tmp = [lables_tmp M_.endo_names{M_.matched_moments{jm,1}(jvar)}];
lables_tmp_tex = [lables_tmp_tex, '{', M_.endo_names_tex{M_.matched_moments{jm,1}(jvar)}, '}'];
if M_.matched_moments{jm,2}(jvar) ~= 0
lables_tmp = [lables_tmp, '(', num2str(M_.matched_moments{jm,2}(jvar)), ')'];
lables_tmp_tex = [lables_tmp_tex, '_{t', num2str(M_.matched_moments{jm,2}(jvar)), '}'];
else
lables_tmp_tex = [lables_tmp_tex, '_{t}'];
end
if M_.matched_moments{jm,3}(jvar) > 1
lables_tmp = [lables_tmp, '^', num2str(M_.matched_moments{jm,3}(jvar))];
lables_tmp_tex = [lables_tmp_tex, '^{', num2str(M_.matched_moments{jm,3}(jvar)) '}'];
end
if jvar == length(M_.matched_moments{jm,1})
lables_tmp = [lables_tmp, ']'];
lables_tmp_tex = [lables_tmp_tex, ' \right]'];
else
lables_tmp = [lables_tmp, '*'];
lables_tmp_tex = [lables_tmp_tex, ' \times '];
end
end
labels{jm,1} = lables_tmp;
labels_TeX{jm,1} = lables_tmp_tex;
end
data_mat = [data_moments model_moments];
dyntable(options_mom_, titl, headers, labels, data_mat, cellofchararraymaxlength(labels)+2, 10, 7);
if options_mom_.TeX
dyn_latex_table(M_, options_mom_, titl, ['comparison_moments_', options_mom_.mom.mom_method], headers, labels_TeX, data_mat, cellofchararraymaxlength(labels)+2, 10, 7);
end

View File

@ -0,0 +1,84 @@
function display_comparison_moments_irfs(M_, options_mom_, data_moments, model_moments)
% display_comparison_moments_irfs(M_, options_mom_, data_moments, model_moments)
% -------------------------------------------------------------------------
% Displays and saves to disk the comparison of the data moments/irfs and the model moments/irfs
% -------------------------------------------------------------------------
% INPUTS
% M_: [structure] model information
% options_mom_: [structure] method of moments options
% data_moments: [vector] data moments
% model_moments: [vector] model moments
% -------------------------------------------------------------------------
% OUTPUT
% No output, just displays and saves to disk the comparison of the data moments and the model moments
% -------------------------------------------------------------------------
% This function is called by
% o mom.run
% -------------------------------------------------------------------------
% This function calls
% o dyn_latex_table
% o dyntable
% o cellofchararraymaxlength
% -------------------------------------------------------------------------
% Copyright © 2023 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
if strcmp(options_mom_.mom.mom_method,'IRF_MATCHING')
titl = upper('Comparison of matched data irfs and model irfs');
headers = {'Irf','Data','Model'};
for jm = 1:size(M_.matched_irfs,1)
labels{jm,1} = [M_.endo_names{M_.matched_irfs{jm,1}(1)} ' ' M_.exo_names{M_.matched_irfs{jm,1}(2)} ' (' num2str(M_.matched_irfs{jm,1}(3)) ')'];
labels_TeX{jm,1} = [M_.endo_names_tex{M_.matched_irfs{jm,1}(1)} ' ' M_.exo_names_tex{M_.matched_irfs{jm,1}(2)} ' (' num2str(M_.matched_irfs{jm,1}(3)) ')'];
end
else
titl = ['Comparison of matched data moments and model moments (',options_mom_.mom.mom_method,')'];
headers = {'Moment','Data','Model'};
for jm = 1:size(M_.matched_moments,1)
lables_tmp = 'E[';
lables_tmp_tex = 'E \left[ ';
for jvar = 1:length(M_.matched_moments{jm,1})
lables_tmp = [lables_tmp M_.endo_names{M_.matched_moments{jm,1}(jvar)}];
lables_tmp_tex = [lables_tmp_tex, '{', M_.endo_names_tex{M_.matched_moments{jm,1}(jvar)}, '}'];
if M_.matched_moments{jm,2}(jvar) ~= 0
lables_tmp = [lables_tmp, '(', num2str(M_.matched_moments{jm,2}(jvar)), ')'];
lables_tmp_tex = [lables_tmp_tex, '_{t', num2str(M_.matched_moments{jm,2}(jvar)), '}'];
else
lables_tmp_tex = [lables_tmp_tex, '_{t}'];
end
if M_.matched_moments{jm,3}(jvar) > 1
lables_tmp = [lables_tmp, '^', num2str(M_.matched_moments{jm,3}(jvar))];
lables_tmp_tex = [lables_tmp_tex, '^{', num2str(M_.matched_moments{jm,3}(jvar)) '}'];
end
if jvar == length(M_.matched_moments{jm,1})
lables_tmp = [lables_tmp, ']'];
lables_tmp_tex = [lables_tmp_tex, ' \right]'];
else
lables_tmp = [lables_tmp, '*'];
lables_tmp_tex = [lables_tmp_tex, ' \times '];
end
end
labels{jm,1} = lables_tmp;
labels_TeX{jm,1} = lables_tmp_tex;
end
end
data_mat = [data_moments model_moments];
dyntable(options_mom_, titl, headers, labels, data_mat, cellofchararraymaxlength(labels)+2, 10, 7);
if options_mom_.TeX
dyn_latex_table(M_, options_mom_, titl, ['comparison_moments_', options_mom_.mom.mom_method], headers, labels_TeX, data_mat, cellofchararraymaxlength(labels)+2, 10, 7);
end

View File

@ -0,0 +1,118 @@
function graph_comparison_irfs(matched_irfs,irf_model_varobs,varobs_id,irfHorizon,relative_irf,endo_names,exo_names,exo_names_tex,dname,fname,graph_format,TeX,nodisplay,figures_textwidth)
% graph_comparison_irfs(matched_irfs,irf_model_varobs,varobs_id,irfHorizon,relative_irf,endo_names,exo_names,exo_names_tex,dname,fname,graph_format,TeX,nodisplay,figures_textwidth)
% -------------------------------------------------------------------------
% Plots and saves to disk the comparison of the selected data irfs and corresponding model irfs
% -------------------------------------------------------------------------
% INPUTS
% matched_irfs: [matrix] information on matched data irfs
% irf_model_varobs: [matrix] model irfs for observable variables
% varobs_id: [vector] index for observable variables in endo_names
% irfHorizon: [scalar] maximum horizon of irfs
% relative_irf: [boolean] if true, plots normalized irfs
% endo_names: [cell] names of endogenous variables
% exo_names: [cell] names of exogenous variables
% exo_names_tex: [cell] names of exogenous variables in latex
% dname: [string] name of the directory where to save the graphs
% fname: [string] name of the mod file
% graph_format: [cell] format of the graphs
% TeX: [boolean] if true, uses latex for plots
% nodisplay: [boolean] if true, does not display the graphs
% figures_textwidth: [scalar] textwidth used in plots
% -------------------------------------------------------------------------
% OUTPUT
% No output, just displays and saves to disk the graphs
% -------------------------------------------------------------------------
% This function is called by
% o mom.run
% -------------------------------------------------------------------------
% This function calls
% o dyn_figure
% o dyn_saveas
% o remove_fractional_xticks
% o CheckPath
% o pltorg
% -------------------------------------------------------------------------
% Copyright © 2023 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
graphDirectoryName = CheckPath('graphs',dname);
latexDirectoryName = CheckPath('latex',dname);
if TeX && any(strcmp('eps',cellstr(graph_format)))
fidTeX = fopen([latexDirectoryName filesep fname '_irf_matching_plot.tex'],'w');
fprintf(fidTeX,'%% TeX eps-loader file generated by mom.run.m (Dynare).\n');
fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
fprintf(fidTeX,' \n');
end
shockEntries = cellfun(@(x) x(2), matched_irfs(:, 1));
uniqueShockEntries = unique(shockEntries);
for jexo = uniqueShockEntries'
entriesDataIrfs = find(cellfun(@(x) x(2) == jexo, matched_irfs(:, 1)));
uniqueVariables = unique(cellfun(@(x) x(1), matched_irfs(entriesDataIrfs,1)));
[nbplt,nr,nc,lr,lc,nstar] = pltorg(length(uniqueVariables));
fig = 0;
for jvar = 1:length(uniqueVariables)
if jvar==1 || ~( (fig-1)*nstar<jvar && jvar<=fig*nstar )
fig = fig+1;
fig_irf = dyn_figure(nodisplay,'Name',['Irf matching shock to ' exo_names{jexo} ' figure ' int2str(fig)]);
end
plt = jvar-(fig-1)*nstar;
dataIrfRows = find(cellfun(@(x) x(1) == uniqueVariables(jvar) && x(2) == jexo, matched_irfs(:, 1)));
dataIrfPeriods = cellfun(@(x) x(3), matched_irfs(dataIrfRows,1));
dataIrfValues = cell2mat(matched_irfs(dataIrfRows,2));
if nbplt>1 && fig==nbplt
subplot(lr,lc,plt);
else
subplot(nr,nc,plt);
end
plt_data = plot(dataIrfPeriods,dataIrfValues,'x','linewidth',2);
hold on
plt_model = plot(1:irfHorizon, irf_model_varobs(:,varobs_id==uniqueVariables(jvar),jexo),'-k','linewidth',1);
hold on
plot([1 irfHorizon],[0 0],'-r','linewidth',0.5);
hold off
xlim([1 irfHorizon]);
remove_fractional_xticks
if TeX
title(['$' endo_names{uniqueVariables(jvar)} '$'],'Interpreter','latex');
else
title(endo_names{uniqueVariables(jvar)},'Interpreter','none');
end
if (plt==nstar) || jvar==length(uniqueVariables)
% Adding a legend at the bottom
axes('Position',[0, 0, 1, 1],'Visible','off');
lgd = legend([plt_data,plt_model],{'Data', 'Model'}, 'Location', 'southeast','NumColumns',2);
lgd.Position = [0.405 0.025 lgd.Position(3) lgd.Position(4)];
dyn_saveas(fig_irf,[graphDirectoryName filesep fname '_matched_irf_' exo_names{jexo} int2str(fig)],nodisplay,graph_format);
if TeX && any(strcmp('eps',cellstr(graph_format)))
fprintf(fidTeX,'\\begin{figure}[H]\n');
fprintf(fidTeX,'\\centering \n');
fprintf(fidTeX,'\\includegraphics[width=%2.2f\\textwidth]{%s_matched_irf_%s%s}\n',figures_textwidth*min(plt/nc,1),[graphDirectoryName filesep fname],exo_names{jexo},int2str(fig));
if relative_irf
fprintf(fidTeX,'\\caption{Relative impulse response functions (orthogonalized shock to $%s$).}', exo_names_tex{jexo});
else
fprintf(fidTeX,'\\caption{Impulse response functions (orthogonalized shock to $%s$).}', exo_names_tex{jexo});
end
fprintf(fidTeX,'\\label{Fig:IRF:%s:%s}\n', exo_names{jexo},int2str(fig));
fprintf(fidTeX,'\\end{figure}\n');
fprintf(fidTeX,' \n');
end
end
end
end

View File

@ -50,7 +50,7 @@ LaTeXtitle=strrep(table_title,' ','_');
tstath = abs(xparam1)./stdh;
header_width = row_header_width(M_, estim_params_, bayestopt_);
if strcmp(field_name,'posterior')
if contains(field_name,'posterior')
tit1 = sprintf('%-*s %10s %8s %7s %6s %6s\n', header_width, ' ', 'prior mean', ...
'mode', 's.d.', 'prior', 'pstdev');
else
@ -62,7 +62,7 @@ if np
disp(tit1)
for i=1:np
name = bayestopt_.name{ip};
if strcmp(field_name,'posterior')
if contains(field_name,'posterior')
fprintf('%-*s %10.4f %8.4f %7.4f %6s %6.4f \n', ...
header_width,name, ...
bayestopt_.p1(ip),xparam1(ip),stdh(ip), ...
@ -85,7 +85,7 @@ if nvx
for i=1:nvx
k = estim_params_.var_exo(i,1);
name = M_.exo_names{k};
if strcmp(field_name,'posterior')
if contains(field_name,'posterior')
fprintf('%-*s %10.4f %8.4f %7.4f %6s %6.4f \n', ...
header_width, name, bayestopt_.p1(ip), xparam1(ip), ...
stdh(ip), pnames{bayestopt_.pshape(ip)+1}, ...
@ -106,7 +106,7 @@ if nvn
ip = nvx+1;
for i=1:nvn
name = options_.varobs{estim_params_.nvn_observable_correspondence(i,1)};
if strcmp(field_name,'posterior')
if contains(field_name,'posterior')
fprintf('%-*s %10.4f %8.4f %7.4f %6s %6.4f \n', ...
header_width, name, bayestopt_.p1(ip), ...
xparam1(ip), stdh(ip), ...
@ -132,7 +132,7 @@ if ncx
k2 = estim_params_.corrx(i,2);
name = sprintf('%s,%s', M_.exo_names{k1}, M_.exo_names{k2});
NAME = sprintf('%s_%s', M_.exo_names{k1}, M_.exo_names{k2});
if strcmp(field_name, 'posterior')
if contains(field_name,'posterior')
fprintf('%-*s %10.4f %8.4f %7.4f %6s %6.4f \n', ...
header_width, name, bayestopt_.p1(ip), xparam1(ip), stdh(ip), ...
pnames{bayestopt_.pshape(ip)+1}, bayestopt_.p2(ip));
@ -158,7 +158,7 @@ if ncn
k2 = estim_params_.corrn(i,2);
name = sprintf('%s,%s', M_.endo_names{k1}, M_.endo_names{k2});
NAME = sprintf('%s_%s', M_.endo_names{k1}, M_.endo_names{k2});
if strcmp(field_name,'posterior')
if contains(field_name,'posterior')
fprintf('%-*s %10.4f %8.4f %7.4f %6s %6.4f \n', ...
header_width, name, bayestopt_.p1(ip), xparam1(ip), stdh(ip), ...
pnames{bayestopt_.pshape(ip)+1}, bayestopt_.p2(ip));