2012-08-24 15:03:35 +02:00
function []= graph_decomp ( z,shock_names,endo_names,i_var,initial_date,DynareModel,DynareOptions)
2015-07-24 10:08:43 +02:00
%function []=graph_decomp(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
%
2015-07-24 10:08:43 +02: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
2009-12-16 18:17:34 +01:00
2017-10-10 10:05:59 +02:00
% Copyright (C) 2010-2018 Dynare Team
2009-12-16 18:17:34 +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
2021-06-09 17:33:48 +02:00
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
2009-12-16 18:17:34 +01:00
2019-01-17 11:37:41 +01:00
if ~ DynareOptions . plot_shock_decomp . expand
GraphDirectoryName = CheckPath ( ' graphs' , DynareModel . dname ) ;
end
2017-03-22 11:30:35 +01:00
new_colormap = DynareOptions . plot_shock_decomp . colormap ;
2016-04-14 21:22:31 +02:00
2009-12-16 18:17:34 +01:00
% number of components equals number of shocks + 1 (initial conditions)
comp_nbr = size ( z , 2 ) - 1 ;
2017-01-08 22:37:39 +01:00
SteadyState = [ ] ;
2017-01-09 18:53:10 +01:00
fig_mode = ' ' ;
fig_mode1 = ' ' ;
2017-03-21 17:17:31 +01:00
% fig_name='';
2017-01-08 22:37:39 +01:00
% screen_shocks=0;
2017-03-22 11:30:35 +01:00
opts_decomp = DynareOptions . plot_shock_decomp ;
2017-01-08 23:03:33 +01:00
if isfield ( opts_decomp , ' steady_state' )
SteadyState = opts_decomp . steady_state ;
end
2017-01-19 16:55:10 +01:00
if ~ isempty ( opts_decomp . type )
fig_mode = opts_decomp . type ;
2017-01-08 23:03:33 +01:00
fig_mode1 = [ ' _' fig_mode ] ;
fig_mode = [ fig_mode ' _' ] ;
end
2019-12-05 14:41:06 +01:00
2019-12-05 16:50:18 +01:00
if isfield ( opts_decomp , ' init_cond_decomp' )
init_cond_decomp = opts_decomp . init_cond_decomp ;
else
init_cond_decomp = 0 ;
end
2019-12-05 14:41:06 +01:00
2017-03-21 17:17:31 +01:00
fig_name_long = opts_decomp . fig_name ;
2017-01-19 16:55:10 +01:00
2017-03-22 11:30:35 +01:00
use_shock_groups = DynareOptions . plot_shock_decomp . use_shock_groups ;
2017-01-15 18:40:06 +01:00
screen_shocks = opts_decomp . screen_shocks ;
2018-07-04 12:33:51 +02:00
if ~ isempty ( use_shock_groups ) || comp_nbr < = 18
2017-01-08 23:03:33 +01:00
screen_shocks = 0 ;
end
2017-01-17 17:56:45 +01:00
if use_shock_groups
shock_groups = DynareModel . shock_groups . ( use_shock_groups ) ;
shock_ind = fieldnames ( shock_groups ) ;
end
2017-01-08 23:03:33 +01:00
if screen_shocks
2017-03-21 17:17:31 +01:00
fig_name_long = [ fig_name_long ' SCREEN' ] ;
2017-01-08 23:03:33 +01:00
end
2017-01-19 16:55:10 +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 , ' (' , ' ' ) ;
2017-01-19 16:55:10 +01:00
2017-01-17 17:56:45 +01:00
interactive = opts_decomp . interactive ;
2017-01-08 22:37:39 +01:00
2009-12-16 18:17:34 +01:00
gend = size ( z , 3 ) ;
2016-08-21 11:11:19 +02:00
if isempty ( initial_date )
x = 0 : gend ;
2017-03-17 12:04:03 +01:00
freq = 1 ;
2016-08-21 11:11:19 +02:00
else
freq = initial_date . freq ;
2021-01-15 20:42:21 +01:00
initial_period = double ( initial_date ) ;
2016-08-21 11:11:19 +02:00
x = initial_period - 1 / freq : ( 1 / freq ) : initial_period + ( gend - 1 ) / freq ;
end
2009-12-16 18:17:34 +01:00
2010-03-23 06:38:39 +01:00
nvar = length ( i_var ) ;
2015-05-12 08:05:06 +02:00
%% write LaTeX-Header
2019-01-17 11:37:41 +01:00
if DynareOptions . TeX && any ( strcmp ( ' eps' , cellstr ( DynareOptions . plot_shock_decomp . graph_format ) ) ) && ~ DynareOptions . plot_shock_decomp . expand
2017-03-21 17:17:31 +01:00
fidTeX = fopen ( [ GraphDirectoryName , filesep , DynareModel . fname ' _shock_decomp' fig_mode1 fig_name ' .tex' ] , ' w' ) ;
2015-05-12 08:05:06 +02:00
fprintf ( fidTeX , ' %% TeX eps-loader file generated by Dynare' ' s graph_decomp.m.\n' ) ;
fprintf ( fidTeX , [ ' %% ' datestr ( now , 0 ) ' \n' ] ) ;
fprintf ( fidTeX , ' \n' ) ;
end
2019-12-05 16:50:18 +01:00
if init_cond_decomp
preamble_txt = ' Initial condition decomposition' ;
preamble_figname = ' _initval_decomposition_' ;
2017-01-25 16:10:51 +01:00
else
2019-12-05 16:50:18 +01:00
preamble_figname = ' _shock_decomposition_' ;
if opts_decomp . vintage && opts_decomp . realtime > 1
preamble_txt = ' Shock decomposition' ;
else
preamble_txt = ' Historical shock decomposition' ;
end
2017-01-25 16:10:51 +01:00
end
2017-05-16 12:42:01 +02:00
if ~ ( screen_shocks && comp_nbr > 18 )
2017-01-08 22:37:39 +01:00
screen_shocks = 0 ;
end
comp_nbr0 = comp_nbr ;
2015-05-12 08:05:06 +02:00
%%plot decomposition
2009-12-16 18:17:34 +01:00
for j = 1 : nvar
z1 = squeeze ( z ( i_var ( j ) , : , : ) ) ;
2017-05-16 12:42:01 +02:00
if screen_shocks
2018-11-13 17:58:42 +01:00
[ ~ , 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 22:37:39 +01:00
zres = sum ( z1 ( isort ( 17 : end ) , : ) , 1 ) ;
z1 = [ z1 ( isort ( 1 : 16 ) , : ) ; zres ; z1 ( comp_nbr0 : end , : ) ] ;
comp_nbr = 18 ;
func = @ ( x ) colorspace ( ' RGB->Lab' , x ) ;
new_colormap = distinguishable_colors ( size ( z1 , 1 ) - 1 , ' w' , func ) ;
new_colormap ( end , : ) = [ 0.7 0.7 0.7 ] ;
else
labels = char ( char ( shock_names ) , ' Initial values' ) ;
end
2009-12-16 18:17:34 +01:00
xmin = x ( 1 ) ;
2017-01-08 22:37:39 +01:00
xmax = x ( end ) + 1 / freq ;
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 ;
2009-12-16 18:17:34 +01:00
if ymax - ymin < 1e-6
continue
end
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 ) } ' .' ] , ' PaperPositionMode' , ' auto' , ' PaperOrientation' , ' landscape' , ' renderermode' , ' auto' ) ;
2017-01-08 22:37:39 +01:00
set ( fhandle , ' position' , [ 50 50 1500 750 ] )
ax = axes ( ' Position' , [ 0.1 0.1 0.6 0.8 ] , ' box' , ' on' ) ;
2017-05-16 15:10:20 +02:00
% plot(ax,x(2:end),z1(end,:),'k-','LineWidth',2)
% axis(ax,[xmin xmax ymin ymax]);
2019-06-05 23:17:38 +02:00
if strcmp ( ' aoa' , DynareOptions . plot_shock_decomp . type )
bgap = 0.15 ;
else
bgap = 0 ;
2019-12-20 16:28:06 +01:00
end
2009-12-16 18:17:34 +01:00
hold on ;
for i = 1 : gend
i_1 = i - 1 ;
yp = 0 ;
ym = 0 ;
2012-08-24 15:03:35 +02:00
for k = 1 : comp_nbr
2009-12-16 18:17:34 +01:00
zz = z1 ( k , i ) ;
if zz > 0
2019-06-05 23:17:38 +02:00
fill ( [ x ( i ) + bgap x ( i ) + bgap x ( i + 1 ) - bgap x ( i + 1 ) - bgap ] + ( 1 / freq / 2 ) , [ yp yp + zz yp + zz yp ] , k ) ;
2009-12-16 18:17:34 +01:00
yp = yp + zz ;
else
2019-06-05 23:17:38 +02:00
fill ( [ x ( i ) + bgap x ( i ) + bgap x ( i + 1 ) - bgap x ( i + 1 ) - bgap ] + ( 1 / freq / 2 ) , [ ym ym + zz ym + zz ym ] , k ) ;
2009-12-16 18:17:34 +01:00
ym = ym + zz ;
end
hold on ;
end
end
plot ( ax , x ( 2 : end ) , z1 ( end , : ) , ' k-' , ' LineWidth' , 2 )
2017-01-08 22:37:39 +01:00
if ~ isempty ( SteadyState )
2017-02-09 19:29:19 +01:00
plot ( ax , [ xmin xmax ] , [ 0 0 ] , ' --' , ' linewidth' , 1 , ' color' , [ 0.7 0.7 0.7 ] )
2017-05-16 12:42:01 +02:00
if ymin + SteadyState ( i_var ( j ) ) < 0 && ymax + SteadyState ( i_var ( j ) ) > 0
2017-01-08 22:37:39 +01:00
plot ( ax , [ xmin xmax ] , SteadyState ( i_var ( j ) ) * [ - 1 - 1 ] , ' k--' , ' linewidth' , 1 )
ytick = get ( ax , ' ytick' ) ;
ytick1 = ytick - SteadyState ( i_var ( j ) ) ;
ind1 = min ( find ( ytick1 > = ymin ) ) ;
ind2 = max ( find ( ytick1 < = ymax ) ) ;
dytick = ytick ( 2 ) - ytick ( 1 ) ;
2017-05-16 12:42:01 +02:00
if ind1 > 1
2017-01-08 22:37:39 +01:00
ytick1 = [ ytick1 ( ind1 : end ) ytick1 ( end ) + dytick : dytick : ymax ] ;
elseif ind2 < length ( ytick )
ytick1 = [ sort ( ytick1 ( 1 ) - dytick : - dytick : ymin ) ytick1 ( 1 : ind2 ) ] ;
end
set ( ax , ' ytick' , ytick1 ) ,
2017-02-07 09:15:48 +01:00
else
ytick1 = get ( ax , ' ytick' ) ;
2017-01-08 22:37:39 +01:00
end
ylabel = ytick1 ' + SteadyState ( i_var ( j ) ) ;
ylabel ( abs ( ylabel ) < eps ) = 0 ;
set ( ax , ' yticklabel' , num2str ( ylabel , ' %4.2g' ) )
end
set ( ax , ' xlim' , [ xmin xmax ] ) ;
2009-12-16 18:17:34 +01:00
hold off ;
2017-05-16 15:10:20 +02:00
2009-12-16 18:17:34 +01:00
axes ( ' Position' , [ 0.75 0.1 0.2 0.8 ] ) ;
axis ( [ 0 1 0 1 ] ) ;
axis off ;
hold on ;
y1 = 0 ;
height = 1 / comp_nbr ;
2012-08-24 15:03:35 +02:00
2017-01-08 22:37:39 +01:00
for i = comp_nbr : - 1 : 1
2017-05-16 15:10:20 +02:00
% for i=1:comp_nbr
2017-01-17 17:56:45 +01:00
hl = fill ( [ 0 0 0.2 0.2 ] , [ y1 y1 + 0.7 * height y1 + 0.7 * height y1 ] , i ) ;
2009-12-16 18:17:34 +01:00
hold on
2017-01-17 17:56:45 +01:00
ht = text ( 0.3 , y1 + 0.3 * height , labels ( i , : ) , ' Interpreter' , ' none' ) ;
2009-12-16 18:17:34 +01:00
hold on
2018-07-04 12:33:51 +02:00
if interactive && ( ~ isoctave && ~ isempty ( use_shock_groups ) )
2017-03-22 11:30:35 +01:00
mydata . fig_name = DynareOptions . plot_shock_decomp . fig_name ( 2 : end ) ;
mydata . use_shock_groups = DynareOptions . plot_shock_decomp . use_shock_groups ;
2017-01-17 17:56:45 +01:00
mydata . shock_group = shock_groups . ( shock_ind { i } ) ;
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 ;
2019-01-17 11:37:41 +01:00
mydata . plot_shock_decomp . zfull = DynareOptions . plot_shock_decomp . zfull ( i_var ( j ) , : , : ) ;
2019-12-12 16:45:57 +01:00
mydata . endo_names = endo_names ( i_var ( j ) ) ;
mydata . endo_names_tex = DynareModel . endo_names_tex ( i_var ( j ) ) ;
mydata . exo_names = DynareModel . exo_names ;
2018-01-17 19:28:44 +01:00
if ~ isempty ( mydata . shock_group . shocks )
2017-01-17 17:56:45 +01:00
c = uicontextmenu ;
hl . UIContextMenu = c ;
browse_menu = uimenu ( c , ' Label' , ' Browse group' ) ;
2019-06-05 23:17:38 +02:00
expand_menu = uimenu ( c , ' Label' , ' Expand group' , ' Callback' , [ ' expand_group(' ' ' mydata . plot_shock_decomp . use_shock_groups ' ' ' ,' ' ' mydata . plot_shock_decomp . orig_varlist { j } ' ' ' ,' int2str ( i ) ' )' ] ) ;
2017-01-17 17:56:45 +01:00
set ( expand_menu , ' UserData' , mydata , ' Tag' , [ ' group' int2str ( i ) ] ) ;
2020-11-04 14:34:46 +01:00
save_expand2xls_menu = uimenu ( c , ' Label' , ' Export group to xls' , ' Callback' , [ ' expand_group(' ' ' mydata . plot_shock_decomp . use_shock_groups ' ' ' ,' ' ' mydata . plot_shock_decomp . orig_varlist { j } ' ' ' ,' int2str ( i ) ' , 1)' ] ) ;
2020-10-29 18:27:24 +01:00
set ( save_expand2xls_menu , ' Tag' , [ ' xls_group' int2str ( i ) ] ) ;
2017-01-17 17:56:45 +01:00
for jmember = mydata . shock_group . shocks
uimenu ( ' parent' , browse_menu , ' Label' , char ( jmember ) )
end
ht . UIContextMenu = c ;
end
end
2009-12-16 18:17:34 +01:00
y1 = y1 + height ;
end
2012-08-24 15:03:35 +02:00
2016-04-14 21:22:31 +02:00
if ~ isempty ( new_colormap )
colormap ( new_colormap )
end
2009-12-16 18:17:34 +01:00
hold off
2019-01-17 11:37:41 +01:00
if ~ DynareOptions . plot_shock_decomp . expand
2019-12-20 16:28:06 +01:00
2019-12-05 16:50:18 +01:00
dyn_saveas ( fhandle , [ GraphDirectoryName , filesep , DynareModel . fname , preamble_figname , endo_names { i_var ( j ) } , fig_mode1 , fig_name ] , DynareOptions . plot_shock_decomp . nodisplay , DynareOptions . plot_shock_decomp . graph_format ) ;
2019-01-17 11:37:41 +01:00
if DynareOptions . TeX && any ( strcmp ( ' eps' , cellstr ( DynareOptions . plot_shock_decomp . graph_format ) ) )
fprintf ( fidTeX , ' \\begin{figure}[H]\n' ) ;
fprintf ( fidTeX , ' \\centering \n' ) ;
2019-12-05 16:50:18 +01:00
fprintf ( fidTeX , ' \\includegraphics[width=0.8\\textwidth]{%s/graphs/%s%s}\n' , DynareModel . fname , DynareModel . fname , [ preamble_figname endo_names { i_var ( j ) } fig_mode1 fig_name ] ) ;
2019-01-17 11:37:41 +01:00
fprintf ( fidTeX , ' \\label{Fig:shock_decomp:%s}\n' , [ fig_mode endo_names { i_var ( j ) } fig_name ] ) ;
fprintf ( fidTeX , [ ' \\caption{' preamble_txt fig_name_long strrep ( fig_mode1 , ' _' , ' ' ) ' : $ %s $.}\n' ] , DynareModel . endo_names_tex { i_var ( j ) } ) ;
fprintf ( fidTeX , ' \\end{figure}\n' ) ;
fprintf ( fidTeX , ' \n' ) ;
end
else
2019-12-12 16:45:57 +01:00
if ~ isempty ( DynareOptions . plot_shock_decomp . filepath )
dyn_saveas ( fhandle , [ DynareOptions . plot_shock_decomp . filepath , filesep , DynareModel . fname , preamble_figname , endo_names { i_var ( j ) } , fig_mode1 , fig_name ] , DynareOptions . plot_shock_decomp . nodisplay , DynareOptions . plot_shock_decomp . graph_format ) ;
end
2017-05-16 15:10:20 +02:00
end
2019-12-20 16:28:06 +01:00
2015-05-12 08:05:06 +02:00
end
%% write LaTeX-Footer
2019-01-17 11:37:41 +01:00
if DynareOptions . TeX && any ( strcmp ( ' eps' , cellstr ( DynareOptions . plot_shock_decomp . graph_format ) ) ) && ~ DynareOptions . plot_shock_decomp . expand
2015-05-12 08:05:06 +02:00
fprintf ( fidTeX , ' \n' ) ;
fprintf ( fidTeX , ' %% End of TeX file.\n' ) ;
fclose ( fidTeX ) ;
end