1function [oo_,M_] = shock_decomposition(M_,oo_,options_,varlist,bayestopt_,estim_params_)
2% function z = shock_decomposition(M_,oo_,options_,varlist)
3% Computes shocks contribution to a simulated trajectory. The field set is
4% oo_.shock_decomposition. It is a n_var by nshock+2 by nperiods array. The
5% first nshock columns store the respective shock contributions, column n+1
6% stores the role of the initial conditions, while column n+2 stores the
7% value of the smoothed variables.  Both the variables and shocks are stored
8% in the order of declaration, i.e. M_.endo_names and M_.exo_names, respectively.
9%
10% INPUTS
11%    M_:          [structure]  Definition of the model
12%    oo_:         [structure]  Storage of results
13%    options_:    [structure]  Options
14%    varlist:     [char]       List of variables
15%    bayestopt_:  [structure]  describing the priors
16%    estim_params_: [structure] characterizing parameters to be estimated
17%
18% OUTPUTS
19%    oo_:         [structure]  Storage of results
20%    M_:          [structure]  Definition of the model; makes sure that
21%                               M_.params is correctly updated
22%
23% SPECIAL REQUIREMENTS
24%    none
25
26% Copyright (C) 2009-2020 Dynare Team
27%
28% This file is part of Dynare.
29%
30% Dynare is free software: you can redistribute it and/or modify
31% it under the terms of the GNU General Public License as published by
32% the Free Software Foundation, either version 3 of the License, or
33% (at your option) any later version.
34%
35% Dynare is distributed in the hope that it will be useful,
36% but WITHOUT ANY WARRANTY; without even the implied warranty of
37% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38% GNU General Public License for more details.
39%
40% You should have received a copy of the GNU General Public License
41% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
42
43% indices of endogenous variables
44
45if isfield(oo_,'shock_decomposition_info') && isfield(oo_.shock_decomposition_info,'i_var')
46    if isfield (oo_,'realtime_conditional_shock_decomposition') ...
47            || isfield (oo_,'realtime_forecast_shock_decomposition') ...
48            || isfield (oo_,'realtime_shock_decomposition') ...
49            || isfield (oo_,'conditional_shock_decomposition') ...
50            || isfield (oo_,'initval_decomposition')
51        error('shock_decomposition::squeezed shock decompositions are already stored in oo_')
52    end
53end
54with_epilogue = options_.shock_decomp.with_epilogue;
55
56if isempty(varlist)
57    varlist = M_.endo_names(1:M_.orig_endo_nbr);
58end
59
60[~, ~,index_uniques] = varlist_indices(varlist, M_.endo_names);
61varlist = varlist(index_uniques);
62
63% number of variables
64endo_nbr = M_.endo_nbr;
65
66% number of shocks
67nshocks = M_.exo_nbr;
68
69% parameter set
70parameter_set = options_.parameter_set;
71if isempty(parameter_set)
72    if isfield(oo_,'posterior_mean')
73        parameter_set = 'posterior_mean';
74    elseif isfield(oo_,'mle_mode')
75        parameter_set = 'mle_mode';
76    elseif isfield(oo_,'posterior')
77        parameter_set = 'posterior_mode';
78    else
79        error(['shock_decomposition: option parameter_set is not specified ' ...
80               'and posterior mode is not available'])
81    end
82end
83
84
85options_.selected_variables_only = 0; %make sure all variables are stored
86options_.plot_priors=0;
87[oo_, M_, ~, ~, Smoothed_Variables_deviation_from_mean] = evaluate_smoother(parameter_set, varlist, M_, oo_, options_, bayestopt_, estim_params_);
88
89% reduced form
90dr = oo_.dr;
91
92% data reordering
93order_var = dr.order_var;
94inv_order_var = dr.inv_order_var;
95
96
97% coefficients
98A = dr.ghx;
99B = dr.ghu;
100
101% initialization
102gend = size(oo_.SmoothedShocks.(M_.exo_names{1}),1);
103epsilon=NaN(nshocks,gend);
104for i=1:nshocks
105    epsilon(i,:) = oo_.SmoothedShocks.(M_.exo_names{i});
106end
107
108z = zeros(endo_nbr,nshocks+2,gend);
109
110z(:,end,:) = Smoothed_Variables_deviation_from_mean;
111
112maximum_lag = M_.maximum_lag;
113
114k2 = dr.kstate(find(dr.kstate(:,2) <= maximum_lag+1),[1 2]);
115i_state = order_var(k2(:,1))+(min(i,maximum_lag)+1-k2(:,2))*M_.endo_nbr;
116for i=1:gend
117    if i > 1 && i <= maximum_lag+1
118        lags = min(i-1,maximum_lag):-1:1;
119    end
120
121    if i > 1
122        tempx = permute(z(:,1:nshocks,lags),[1 3 2]);
123        m = min(i-1,maximum_lag);
124        tempx = [reshape(tempx,endo_nbr*m,nshocks); zeros(endo_nbr*(maximum_lag-i+1),nshocks)];
125        z(:,1:nshocks,i) = A(inv_order_var,:)*tempx(i_state,:);
126        lags = lags+1;
127    end
128
129    if i > options_.shock_decomp.init_state
130        z(:,1:nshocks,i) = z(:,1:nshocks,i) + B(inv_order_var,:).*repmat(epsilon(:,i)',endo_nbr,1);
131    end
132    z(:,nshocks+1,i) = z(:,nshocks+2,i) - sum(z(:,1:nshocks,i),2);
133end
134
135if with_epilogue
136    [z, oo_.shock_decomposition_info.epilogue_steady_state] = epilogue_shock_decomposition(z, M_, oo_);
137end
138
139oo_.shock_decomposition = z;
140
141if ~options_.no_graph.shock_decomposition
142    oo_ = plot_shock_decomposition(M_,oo_,options_,varlist);
143end
144
145oo_.gui.ran_shock_decomposition = true;
146