2017-02-10 12:05:54 +01:00
function WriteShockDecomp2Excel ( z,shock_names,endo_names,i_var,initial_date,DynareModel,DynareOptions,opts_decomp)
%function WriteShockDecomp2Excel(z,shock_names,endo_names,i_var,initial_date,DynareModel,DynareOptions)
% Saves the results from the shock_decomposition command to xls
2017-05-16 15:10:20 +02:00
%
2017-02-10 12:05:54 +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) 2016-2018 Dynare Team
2017-02-10 12:05:54 +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/>.
SteadyState = [ ] ;
fig_mode = ' ' ;
fig_mode1 = ' ' ;
2017-03-21 17:17:31 +01:00
fig_name = ' ' ;
2017-02-10 12:05:54 +01:00
screen_shocks = 0 ;
2017-03-22 11:30:35 +01:00
use_shock_groups = DynareOptions . plot_shock_decomp . use_shock_groups ;
2017-02-10 12:05:54 +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-05-16 12:42:01 +02:00
if nargin == 8
2017-02-10 12:05:54 +01:00
if isfield ( opts_decomp , ' steady_state' )
SteadyState = opts_decomp . steady_state ;
end
if isfield ( opts_decomp , ' fig_mode' ) && ~ isempty ( opts_decomp . fig_mode )
fig_mode = opts_decomp . fig_mode ;
2017-05-16 15:10:20 +02:00
fig_mode1 = [ ' _' fig_mode ] ;
fig_mode = [ fig_mode ' _' ] ;
2017-02-10 12:05:54 +01:00
end
if isfield ( opts_decomp , ' screen_shocks' )
2017-03-22 14:01:34 +01:00
if use_shock_groups
2017-02-10 12:05:54 +01:00
screen_shocks = 0 ;
elseif comp_nbr > 18
screen_shocks = opts_decomp . screen_shocks ;
end
end
2017-03-21 17:17:31 +01:00
if isfield ( opts_decomp , ' fig_name' )
fig_name = opts_decomp . fig_name ;
2017-05-16 15:10:20 +02:00
% fig_name = ['_' fig_name];
fig_name1 = [ fig_name ] ;
fig_name = [ fig_name ' _' ] ;
2017-02-10 12:05:54 +01:00
end
if screen_shocks
2017-05-16 15:10:20 +02:00
fig_name1 = [ fig_name1 ' _screen' ] ;
fig_name = [ fig_name ' screen_' ] ;
2017-02-10 12:05:54 +01:00
end
2017-05-16 15:10:20 +02:00
end
2017-02-10 12:05:54 +01:00
gend = size ( z , 3 ) ;
if isempty ( initial_date )
x = 1 : gend ;
else
freq = initial_date . freq ;
initial_period = initial_date . time ( 1 ) + ( initial_date . time ( 2 ) - 1 ) / freq ;
x = initial_period : ( 1 / freq ) : initial_period + ( gend - 1 ) / freq ;
end
nvar = length ( i_var ) ;
labels = char ( char ( shock_names ) , ' Initial values' ) ;
2017-05-16 12:42:01 +02:00
if ~ ( screen_shocks && comp_nbr > 18 )
2017-02-10 12:05:54 +01:00
screen_shocks = 0 ;
end
comp_nbr0 = comp_nbr ;
%%plot decomposition
for j = 1 : nvar
d0 = { } ;
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-02-10 12:05:54 +01:00
zres = sum ( z1 ( isort ( 17 : end ) , : ) , 1 ) ;
z1 = [ z1 ( isort ( 1 : 16 ) , : ) ; zres ; z1 ( comp_nbr0 : end , : ) ] ;
comp_nbr = 18 ;
end
2017-05-16 15:10:20 +02:00
2017-02-10 12:05:54 +01:00
d0 ( 1 , : ) = [ { ' Decomposition' } cellstr ( labels ( 1 : comp_nbr , : ) ) ' { ' Smoot Var' } ] ;
d0 = [ d0 ; num2cell ( [ x ' z1 ' ] ) ] ;
LastRow = size ( d0 , 1 ) ;
if use_shock_groups
d0 ( LastRow + 2 , 1 ) = { ' Legend.' } ;
d0 ( LastRow + 2 , 2 ) = { ' Shocks include:' } ;
d0 ( LastRow + 3 : LastRow + 3 + comp_nbr - 1 , 1 ) = cellstr ( labels ( 1 : comp_nbr , : ) ) ;
2017-05-16 12:42:01 +02:00
for ic = 1 : comp_nbr
2017-02-10 12:05:54 +01:00
group_members = shock_groups . ( shock_ind { ic } ) . shocks ;
d0 ( LastRow + 2 + ic , 2 : 1 + length ( group_members ) ) = group_members ;
end
end
2017-05-16 15:10:20 +02:00
2017-02-10 12:05:54 +01:00
warning off
2019-09-10 10:47:13 +02:00
try
2017-10-10 10:05:59 +02:00
[ STATUS , MESSAGE ] = xlswrite ( [ DynareModel . fname , ' _shock_decomposition' , fig_mode , fig_name1 ] , d0 , endo_names { i_var ( j ) } ) ;
2019-09-10 10:47:13 +02:00
catch
if exist ( ' xlwrite.m' , ' file' )
[ STATUS ] = xlwrite ( [ DynareModel . fname , ' _shock_decomposition' , fig_mode , fig_name1 ] , d0 , endo_names { i_var ( j ) } ) ;
else
fprintf ( ' \nWriteShockDecomp2Excel: could not write Excel file. It seems Excel is not installed on your machine.\n' )
fprintf ( ' Please install the free xlwrite.m from https://mathworks.com/matlabcentral/fileexchange/38591-xlwrite-generate-xls-x-files-without-excel-on-mac-linux-win\n' )
end
2017-02-10 12:05:54 +01:00
end
warning on
clear d0
2017-05-16 15:10:20 +02:00
end