2017-03-24 11:23:45 +01:00
|
|
|
function []=graph_decomp_detail(z,shock_names,endo_names,i_var,initial_date,DynareModel,DynareOptions)
|
2017-01-08 23:02:11 +01:00
|
|
|
%function []=graph_decomp_detail(z,shock_names,endo_names,i_var,initial_date,DynareModel,DynareOptions)
|
|
|
|
% Plots the results from the shock_decomposition command
|
2017-05-16 15:10:20 +02:00
|
|
|
%
|
2017-01-08 23:02:11 +01:00
|
|
|
% Inputs
|
|
|
|
% z [n_var*(nshock+2)*nperiods] shock decomposition array, see shock_decomposition.m for details
|
|
|
|
% shock_names [endo_nbr*string length] shock names from M_.exo_names
|
|
|
|
% endo_names [exo_nbr*string length] variable names from M_.endo_names
|
|
|
|
% i_var [n_var*1] vector indices of requested variables in M_.endo_names and z
|
|
|
|
% initial_date [dseries object] first period of decomposition to plot
|
|
|
|
% DynareModel [structure] Dynare model structure
|
|
|
|
% DynareOptions [structure] Dynare options structure
|
|
|
|
|
2017-10-10 10:05:59 +02:00
|
|
|
% Copyright (C) 2010-2018 Dynare Team
|
2017-01-08 23:02:11 +01:00
|
|
|
%
|
|
|
|
% 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 <http://www.gnu.org/licenses/>.
|
|
|
|
|
2017-01-19 16:55:10 +01:00
|
|
|
GraphDirectoryName = CheckPath('graphs',DynareModel.dname);
|
2017-01-08 23:02:11 +01:00
|
|
|
% interactive = 0;
|
2017-01-09 18:53:10 +01:00
|
|
|
fig_mode='';
|
2017-01-08 23:02:11 +01:00
|
|
|
fig_mode1='';
|
2017-03-21 17:17:31 +01:00
|
|
|
% fig_name='';
|
2017-01-08 23:02:11 +01:00
|
|
|
% screen_shocks=0;
|
2017-03-22 11:30:35 +01:00
|
|
|
use_shock_groups = DynareOptions.plot_shock_decomp.use_shock_groups;
|
2017-01-08 23:02:11 +01:00
|
|
|
if use_shock_groups
|
|
|
|
shock_groups = DynareModel.shock_groups.(use_shock_groups);
|
|
|
|
shock_ind = fieldnames(shock_groups);
|
|
|
|
end
|
|
|
|
|
|
|
|
% number of components equals number of shocks + 1 (initial conditions)
|
|
|
|
comp_nbr = size(z,2)-1;
|
|
|
|
|
2017-03-22 11:30:35 +01:00
|
|
|
opts_decomp = DynareOptions.plot_shock_decomp;
|
2017-01-08 23:02:11 +01:00
|
|
|
|
|
|
|
interactive = opts_decomp.interactive;
|
2017-01-19 16:55:10 +01:00
|
|
|
if ~isempty(opts_decomp.type)
|
|
|
|
fig_mode = opts_decomp.type;
|
2017-01-08 23:02:11 +01:00
|
|
|
fig_mode1 = ['_' fig_mode];
|
|
|
|
fig_mode = [fig_mode '_'];
|
|
|
|
end
|
2017-01-15 18:40:06 +01:00
|
|
|
screen_shocks = opts_decomp.screen_shocks;
|
2017-05-16 12:42:01 +02:00
|
|
|
if DynareOptions.plot_shock_decomp.use_shock_groups | comp_nbr<=18
|
2017-01-08 23:02:11 +01:00
|
|
|
screen_shocks=0;
|
|
|
|
end
|
2017-03-21 17:17:31 +01:00
|
|
|
fig_name_long = opts_decomp.fig_name;
|
|
|
|
% fig_name = ['_' fig_name];
|
2017-01-19 16:55:10 +01:00
|
|
|
|
2017-01-08 23:02:11 +01:00
|
|
|
if screen_shocks
|
2017-05-16 15:10:20 +02:00
|
|
|
% fig_name1 = [fig_name1 '_screen'];
|
2017-03-21 17:17:31 +01:00
|
|
|
fig_name_long = [fig_name_long ' SCREEN'];
|
2017-01-08 23:02:11 +01:00
|
|
|
end
|
2017-02-01 08:32:03 +01:00
|
|
|
|
2017-03-21 17:17:31 +01:00
|
|
|
fig_name=strrep(fig_name_long, '(given ', '');
|
|
|
|
fig_name=strrep(fig_name, '(vintage ', '');
|
|
|
|
fig_name=regexprep(fig_name, ' ', '_');
|
|
|
|
fig_name=strrep(fig_name, '.', '');
|
|
|
|
fig_name=strrep(fig_name, '-', '');
|
|
|
|
fig_name=strrep(fig_name, ')', '');
|
|
|
|
fig_name=strrep(fig_name, '(', '');
|
|
|
|
% fig_name1 = [fig_name];
|
|
|
|
% fig_name = [fig_name '_'];
|
2017-01-08 23:02:11 +01:00
|
|
|
|
|
|
|
gend = size(z,3);
|
|
|
|
if isempty(initial_date)
|
|
|
|
x = 0:gend;
|
|
|
|
freq = 1;
|
|
|
|
else
|
|
|
|
freq = initial_date.freq;
|
|
|
|
initial_period = initial_date.time(1) + (initial_date.time(2)-1)/freq;
|
|
|
|
x = initial_period-1/freq:(1/freq):initial_period+(gend-1)/freq;
|
|
|
|
end
|
|
|
|
|
2017-05-16 15:10:20 +02:00
|
|
|
ind_yrs = find(floor(x)==x);
|
2017-01-08 23:02:11 +01:00
|
|
|
dind_tick = 1;
|
2017-05-16 12:42:01 +02:00
|
|
|
if floor(length(ind_yrs)/3)
|
2017-01-08 23:02:11 +01:00
|
|
|
dind_tick = floor(length(ind_yrs)/3);
|
|
|
|
xind_tick = x(ind_yrs(1)):dind_tick:x(ind_yrs(end))+(length(ind_yrs)-(dind_tick*3+1));
|
|
|
|
else
|
|
|
|
xind_tick = x(ind_yrs(1)):dind_tick:x(ind_yrs(end))+(length(ind_yrs)-(dind_tick+1));
|
|
|
|
end
|
|
|
|
% xind_tick = floor(x(1))-floor(dind_tick/2):dind_tick:ceil(x(end))+ceil(dind_tick/2);
|
|
|
|
if abs(floor(x(1))-xind_tick(1))-abs(ceil(x(end))-xind_tick(end))>1
|
|
|
|
xind_tick = xind_tick-1;
|
|
|
|
end
|
|
|
|
if abs(floor(x(1))-xind_tick(1))-abs(ceil(x(end))-xind_tick(end))<-1
|
|
|
|
xind_tick = xind_tick+1;
|
|
|
|
end
|
2017-11-13 12:18:51 +01:00
|
|
|
if length(xind_tick)==gend,
|
|
|
|
xind_tick = x(2:end);
|
|
|
|
end
|
2017-01-08 23:02:11 +01:00
|
|
|
% xind_tick = [x(ind_yrs(1))-floor(dind_tick/2):dind_tick:x(ind_yrs(end))+floor(dind_tick/2)]+1;
|
|
|
|
% xind_tick = x(ind_yrs(1))-1:dind_tick:x(ind_yrs(end))+1;
|
|
|
|
% xind_tick = x(ind_yrs(1))-1:dind_tick:x(ind_yrs(end))+dind_tick;
|
|
|
|
|
|
|
|
nvar = length(i_var);
|
|
|
|
|
|
|
|
%% write LaTeX-Header
|
2017-03-22 11:30:35 +01:00
|
|
|
if DynareOptions.TeX && any(strcmp('eps',cellstr(DynareOptions.plot_shock_decomp.graph_format)))
|
2017-03-21 17:17:31 +01:00
|
|
|
fidTeX = fopen([GraphDirectoryName, filesep, DynareModel.fname '_shock_decomp' fig_mode1 fig_name '_detail.tex'],'w');
|
2017-01-08 23:02:11 +01:00
|
|
|
fprintf(fidTeX,'%% TeX eps-loader file generated by Dynare''s graph_decomp_detail.m.\n');
|
|
|
|
fprintf(fidTeX,['%% ' datestr(now,0) '\n']);
|
|
|
|
fprintf(fidTeX,' \n');
|
|
|
|
end
|
|
|
|
|
2017-05-16 12:42:01 +02:00
|
|
|
if opts_decomp.vintage && opts_decomp.realtime>1
|
2017-01-25 16:10:51 +01:00
|
|
|
preamble_txt = 'Shock decomposition';
|
|
|
|
else
|
|
|
|
preamble_txt = 'Historical shock decomposition';
|
|
|
|
end
|
|
|
|
|
2017-01-08 23:02:11 +01:00
|
|
|
ncol=3;
|
|
|
|
nrow=ceil(comp_nbr/ncol);
|
|
|
|
ntotrow = nrow;
|
|
|
|
nrow = min(ntotrow, 6);
|
|
|
|
nfigs = ceil(ntotrow/nrow);
|
|
|
|
labels = char(char(shock_names),'Initial values');
|
2017-05-16 12:42:01 +02:00
|
|
|
if ~(screen_shocks && comp_nbr>18)
|
2017-01-08 23:02:11 +01:00
|
|
|
screen_shocks=0;
|
|
|
|
end
|
|
|
|
comp_nbr0=comp_nbr;
|
|
|
|
%%plot decomposition
|
|
|
|
for j=1:nvar
|
|
|
|
z1 = squeeze(z(i_var(j),:,:));
|
|
|
|
if screen_shocks,
|
|
|
|
[junk, isort] = sort(mean(abs(z1(1:end-2,:)')), 'descend');
|
2017-10-10 10:05:59 +02:00
|
|
|
labels = char(char(shock_names(isort(1:16))),'Others', 'Initial values');
|
2017-01-08 23:02:11 +01:00
|
|
|
zres = sum(z1(isort(17:end),:),1);
|
|
|
|
z1 = [z1(isort(1:16),:); zres; z1(comp_nbr0:end,:)];
|
|
|
|
comp_nbr=18;
|
|
|
|
nfigs=1;
|
|
|
|
end
|
|
|
|
xmin = x(1);
|
|
|
|
xmin = min(xmin, xind_tick(1));
|
|
|
|
xmax = x(end)+1/freq;
|
|
|
|
xmax = max(xmax, xind_tick(end));
|
|
|
|
ix = z1(1:comp_nbr,:) > 0;
|
|
|
|
ymax = max(sum(z1(1:comp_nbr,:).*ix))*1.1;
|
|
|
|
ix = z1(1:comp_nbr,:) < 0;
|
|
|
|
ymin = min(sum(z1(1:comp_nbr,:).*ix))*1.1;
|
|
|
|
if ymax-ymin < 1e-6
|
|
|
|
continue
|
|
|
|
end
|
|
|
|
for jf = 1:nfigs
|
2017-10-10 10:05:59 +02:00
|
|
|
fhandle = dyn_figure(DynareOptions.plot_shock_decomp.nodisplay,'Name',[preamble_txt fig_name_long strrep(fig_mode1, '_', ' ') ': ' endo_names{i_var(j)} ' (detail).'],'position',[200 100 650 850], 'PaperPositionMode', 'auto','PaperOrientation','portrait','renderermode','auto');
|
2017-05-16 15:10:20 +02:00
|
|
|
a0=zeros(1,4);
|
|
|
|
a0(3)=inf;
|
|
|
|
a0(4)=-inf;
|
|
|
|
for ic=1+nrow*ncol*(jf-1):min(nrow*ncol*jf,comp_nbr)
|
|
|
|
i = ic-nrow*ncol*(jf-1);
|
|
|
|
zz = z1(ic,:);
|
|
|
|
zz(2,:)=z1(end,:)-zz;
|
|
|
|
ipos=zz>0;
|
|
|
|
ineg=zz<0;
|
|
|
|
hax = subplot(nrow,ncol,i); set(gca,'box','on')
|
2017-11-13 12:18:51 +01:00
|
|
|
hbar = bar(x(2:end),(zz.*ipos)','stacked','FaceColor','flat');
|
|
|
|
if ~matlab_ver_less_than('9.3.0')
|
|
|
|
% make bar obey colormap under MATLAB R2017b
|
|
|
|
for k = 1:2
|
|
|
|
hbar(k).CData = k;
|
|
|
|
end
|
|
|
|
end
|
2017-05-16 15:10:20 +02:00
|
|
|
set(hbar,'edgecolor','flat');
|
|
|
|
hold on,
|
2017-11-13 12:18:51 +01:00
|
|
|
hbar = bar(x(2:end),(zz.*ineg)','stacked','FaceColor','flat');
|
|
|
|
if ~matlab_ver_less_than('9.3.0')
|
|
|
|
% make bar obey colormap under MATLAB R2017b
|
|
|
|
for k = 1:2
|
|
|
|
hbar(k).CData = k;
|
|
|
|
end
|
|
|
|
end
|
2017-05-16 15:10:20 +02:00
|
|
|
set(hbar,'edgecolor','flat');
|
|
|
|
title(deblank(labels(ic,:)),'Interpreter','none'),
|
|
|
|
axis tight;
|
|
|
|
a=axis;
|
|
|
|
set(gca,'Xtick',xind_tick)
|
|
|
|
set(gca,'xlim',[xmin xmax])
|
|
|
|
a0(3)=min(a(3),a0(3));
|
|
|
|
a0(4)=max(a(4),a0(4));
|
|
|
|
set(gca,'ylim',a0(3:4))
|
|
|
|
hold on, h1=plot(x(2:end),z1(end,:),'k-','LineWidth',2);
|
|
|
|
if interactive & (~isoctave & use_shock_groups)
|
|
|
|
mydata.fig_name = DynareOptions.plot_shock_decomp.fig_name(2:end);
|
|
|
|
mydata.use_shock_groups = DynareOptions.plot_shock_decomp.use_shock_groups;
|
|
|
|
mydata.shock_group = shock_groups.(shock_ind{ic});
|
2018-01-17 19:28:44 +01:00
|
|
|
mydata.shock_decomp = DynareOptions.shock_decomp;
|
|
|
|
mydata.plot_shock_decomp = DynareOptions.plot_shock_decomp;
|
|
|
|
mydata.first_obs = DynareOptions.first_obs;
|
|
|
|
mydata.nobs = DynareOptions.nobs;
|
2017-10-10 10:05:59 +02:00
|
|
|
if ~isempty(mydata.shock_group.shocks)
|
2017-05-16 15:10:20 +02:00
|
|
|
c = uicontextmenu;
|
|
|
|
hax.UIContextMenu=c;
|
|
|
|
browse_menu = uimenu(c,'Label','Browse group');
|
2018-01-17 19:28:44 +01:00
|
|
|
expand_menu = uimenu(c,'Label','Expand group','Callback',['expand_group(''' mydata.plot_shock_decomp.use_shock_groups ''',''' deblank(mydata.plot_shock_decomp.orig_varlist{j}) ''',' int2str(ic) ')']);
|
2017-05-16 15:10:20 +02:00
|
|
|
set(expand_menu,'UserData',mydata,'Tag',['group' int2str(ic)]);
|
|
|
|
for jmember = mydata.shock_group.shocks
|
|
|
|
uimenu('parent',browse_menu,'Label',char(jmember))
|
|
|
|
end
|
2017-01-08 23:02:11 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-05-16 15:10:20 +02:00
|
|
|
for isub=1:i
|
|
|
|
subplot(nrow,ncol,isub),
|
|
|
|
set(gca,'ylim',a0(3:4))
|
|
|
|
end
|
|
|
|
|
|
|
|
% make legend
|
|
|
|
axes('Position',[0.1 0.01 0.8 0.02],'units','normalized');
|
|
|
|
axis([0 1 0 1]);
|
|
|
|
axis off;
|
|
|
|
hold on;
|
|
|
|
x1 = 0;
|
|
|
|
width = 1/2;
|
|
|
|
mylabels = {'Individual contrib.','Residual contrib.'};
|
|
|
|
|
|
|
|
for i=1:2
|
|
|
|
% for i=1:comp_nbr
|
|
|
|
hl = fill([x1 x1 x1+0.3*width x1+0.3*width],[0 1 1 0],i);
|
|
|
|
hold on
|
|
|
|
ht = text(x1+0.4*width,0.3,mylabels{i},'Interpreter','none');
|
|
|
|
hold on
|
|
|
|
x1 = x1 + width;
|
|
|
|
end
|
2017-11-13 12:18:51 +01:00
|
|
|
colormap([0.15 0.15 0.15;0.85 0.85 0.85]),
|
2017-05-16 15:10:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
if nfigs>1
|
|
|
|
suffix = ['_detail_' int2str(jf)];
|
|
|
|
else
|
|
|
|
suffix = ['_detail'];
|
|
|
|
end
|
2017-10-10 10:05:59 +02:00
|
|
|
dyn_saveas(fhandle,[GraphDirectoryName, filesep, DynareModel.fname, ...
|
|
|
|
'_shock_decomposition_', endo_names{i_var(j)}, fig_mode1,fig_name suffix],DynareOptions.plot_shock_decomp.nodisplay,DynareOptions.plot_shock_decomp.graph_format);
|
2017-05-16 15:10:20 +02:00
|
|
|
if DynareOptions.TeX && any(strcmp('eps',cellstr(DynareOptions.plot_shock_decomp.graph_format)))
|
|
|
|
fprintf(fidTeX,'\\begin{figure}[H]\n');
|
|
|
|
fprintf(fidTeX,'\\centering \n');
|
2017-10-10 10:05:59 +02:00
|
|
|
fprintf(fidTeX,'\\includegraphics[width=0.8\\textwidth]{%s/graphs/%s_shock_decomposition_%s}\n',DynareModel.fname,DynareModel.fname,[endo_names{i_var(j)} fig_mode1 fig_name suffix]);
|
|
|
|
fprintf(fidTeX,'\\label{Fig:shock_decomp_detail:%s}\n',[fig_mode endo_names{i_var(j)} fig_name suffix]);
|
|
|
|
fprintf(fidTeX,['\\caption{' preamble_txt fig_name_long strrep(fig_mode1, '_', ' ') ': $ %s $ (detail).}\n'], DynareModel.endo_names_tex{i_var(j)});
|
2017-05-16 15:10:20 +02:00
|
|
|
fprintf(fidTeX,'\\end{figure}\n');
|
|
|
|
fprintf(fidTeX,' \n');
|
|
|
|
end
|
2017-01-08 23:02:11 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
%% write LaTeX-Footer
|
2017-03-22 11:30:35 +01:00
|
|
|
if DynareOptions.TeX && any(strcmp('eps',cellstr(DynareOptions.plot_shock_decomp.graph_format)))
|
2017-01-08 23:02:11 +01:00
|
|
|
fprintf(fidTeX,' \n');
|
|
|
|
fprintf(fidTeX,'%% End of TeX file.\n');
|
|
|
|
fclose(fidTeX);
|
|
|
|
end
|