Merge non linear shock decomposition for backward models.

Closes #2.
time-shift
Stéphane Adjemian (Charybdis) 2020-09-29 18:17:25 +02:00
commit 80bfbc0736
Signed by: stepan
GPG Key ID: 295C1FE89E17EB3C
3 changed files with 260 additions and 0 deletions

View File

@ -0,0 +1,113 @@
function decomposition = shock_decomposition_backward(simulations, initialconditions, shocklist, endograph, use_shock_groups)
% Computes and possibly plots the shock decomposition of a backward (possibly nonlinear)
% model simulation.
%
% Inputs:
% - simulations dseries object as returned by simul_backward_model
% - initialconditions dseries object as passed to simul_backward_model
% - shocklist a cell array listing the (names of the) shocks whose contribution should be
% computed. The order matters: the contribution of a shock appearing at index
% i is computed as the difference between the simulation where all shocks ≥i+1
% are zero and the simulation where all shocks ≥i are zero. It is also
% possible to put in this list shock groups, as defined in a shock_groups
% block
% - endograph an optional cell array listing the (names of the) endogenous variables for
% which a shock decomposition graph should be created
% - use_shock_groups an optional string giving the name of the shock_groups block to be used to
% resolve shock groups listed in shocklist. If not given, 'default' is used.
%
% Output:
% - decomposition a 3D array of size endo_nbr×nshocks×nperiods where nshocks=length(shocklist)
% and nperiods is the number of simulation periods (i.e. excluding the initial
% conditions)
% Copyright © 2020 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 <http://www.gnu.org/licenses/>.
global M_ options_
narginchk(3, 5);
if nargin < 4
endograph = {};
end
if nargin < 5
use_shock_groups = 'default';
end
% Extract matrix of innovations from the whole simulation paths
simdates = simulations.dates(initialconditions.nobs+1:end);
innovations = simulations{M_.exo_names{:}}(simdates);
% Number of simulation periods
nperiods = simulations.nobs - initialconditions.nobs;
decomposition = NaN(M_.endo_nbr, length(shocklist), nperiods);
% Add auxiliary variables to simulation paths
if exist(sprintf('+%s/dynamic_set_auxiliary_series', M_.fname), 'file')
simulations = feval(sprintf('%s.dynamic_set_auxiliary_series', M_.fname), simulations, M_.params);
end
for i = length(shocklist):-1:1
% Zero the innovation(s) corresponding to this shock or shock group
if ismember(shocklist{i}, M_.exo_names)
innovations{shocklist{i}}(simdates) = zeros(nperiods, 1);
else % This is a shock group
if ~ismember(use_shock_groups, fieldnames(M_.shock_groups))
error(['Unknown shock_groups block: ' use_shock_groups])
end
groups = fieldnames(M_.shock_groups.(use_shock_groups));
shocks_in_group = [];
for j = 1:length(groups)
if strcmp(shocklist{i}, M_.shock_groups.(use_shock_groups).(groups{j}).label)
shocks_in_group = M_.shock_groups.(use_shock_groups).(groups{j}).shocks;
break
end
end
if isempty(shocks_in_group)
error(['Unknown shock group: ' shocklist{i}])
end
for j = 1:length(shocks_in_group)
innovations{shocks_in_group{j}}(simdates) = zeros(nperiods, 1);
end
end
% Compute simulation with the current shock or shock group removed
simulations_new = simul_backward_model(initialconditions, nperiods, innovations);
if exist(sprintf('+%s/dynamic_set_auxiliary_series', M_.fname), 'file')
simulations_new = feval(sprintf('%s.dynamic_set_auxiliary_series', M_.fname), simulations_new, M_.params);
end
% Compute the contribution of the current shock or shock group
contribution = simulations{M_.endo_names{:}}.data(initialconditions.nobs+1:end, :) ...
- simulations_new{M_.endo_names{:}}.data(initialconditions.nobs+1:end, :);
decomposition(:, i, :) = contribution';
simulations = simulations_new;
end
% Plot the decomposition
for i = 1:length(endograph)
h = dyn_figure(options_.plot_shock_decomp.nodisplay, 'Name', [ 'Shock decomposition for ' endograph{i}]);
endoidx = find(strcmp(endograph{i}, M_.endo_names));
bar(double(simdates), squeeze(decomposition(endoidx, :, :))', 'stacked')
legend(shocklist)
end
end

View File

@ -338,6 +338,7 @@ MODFILES = \
shock_decomposition/fs2000_est_varlist.mod \
shock_decomposition/fs2000_cal_groups.mod \
shock_decomposition/ls2003_plot.mod \
shock_decomposition/shock_decomposition_backward.mod \
stochastic_purely_forward/stochastic_purely_forward.mod \
stochastic_purely_forward/stochastic_purely_forward_with_static.mod \
forecast/Hansen_exo_det_forecast.mod \

View File

@ -0,0 +1,146 @@
// --+ options: json=compute, stochastic +--
/* This file tests the matlab/backward/shock_decomposition_backward.m routine,
used for shock decomposition of nonlinear backward models */
var x1 x2 x3 x1bar x2bar z y x u v s;
varexo ex1
ex2
ex1bar (used='estimationonly')
ex2bar (used='estimationonly')
ez
ey
ex
eu
ev
es;
parameters
rho_1 rho_2 rho_3 rho_4
a_x1_0 a_x1_1 a_x1_2 a_x1_x2_1 a_x1_x2_2
a_x2_0 a_x2_1 a_x2_2 a_x2_x1_1 a_x2_x1_2
e_c_m c_z_1 c_z_2 c_z_dx2 c_z_u c_z_dv c_z_s cx cy beta
lambda
px3;
rho_1 = .9;
rho_2 = -.2;
rho_3 = .4;
rho_4 = -.3;
a_x1_0 = -.9;
a_x1_1 = .4;
a_x1_2 = .3;
a_x1_x2_1 = .1;
a_x1_x2_2 = .2;
a_x2_0 = -.9;
a_x2_1 = .2;
a_x2_2 = -.1;
a_x2_x1_1 = -.1;
a_x2_x1_2 = .2;
beta = .2;
e_c_m = .5;
c_z_1 = .2;
c_z_2 = -.1;
c_z_dx2 = .3;
c_z_u = .3;
c_z_dv = .4;
c_z_s = -.2;
cx = 1.0;
cy = 1.0;
lambda = 0.5; // Share of optimizing agents.
px3 = -.1;
trend_component_model(model_name=toto, eqtags=['eq:x1', 'eq:x2', 'eq:x1bar', 'eq:x2bar'], targets=['eq:x1bar', 'eq:x2bar']);
pac_model(auxiliary_model_name=toto, discount=beta, model_name=pacman);
model;
[name='eq:s']
s = .3*s(-1) - .1*s(-2) + es;
[name='eq:diff(v)']
diff(v) = .5*diff(v(-1)) + ev;
[name='eq:u']
u = .5*u(-1) - .2*u(-2) + eu;
[name='eq:y']
y = rho_1*y(-1) + rho_2*y(-2) + ey;
[name='eq:x']
x = rho_3*x(-1) + rho_4*x(-2) + ex;
[name='eq:x1']
diff(x1) = a_x1_0*(x1(-1)-x1bar(-1)) + a_x1_1*diff(x1(-1)) + a_x1_2*diff(x1(-2)) + a_x1_x2_1*diff(x2(-1)) + a_x1_x2_2*diff(x2(-2)) + ex1;
[name='eq:x2']
diff(x2) = a_x2_0*(x2(-1)-x2bar(-1)) + a_x2_1*diff(x1(-1)) + a_x2_2*diff(x1(-2)) + a_x2_x1_1*diff(x2(-1)) + a_x2_x1_2*diff(x2(-2)) + ex2;
[name='eq:x3']
x3 = px3*x + y ;
[name='eq:x1bar']
x1bar = x1bar(-1) + ex1bar;
[name='eq:x2bar']
x2bar = x2bar(-1) + ex2bar;
[name='zpac']
diff(z) = lambda*(e_c_m*(x1(-1)-z(-1)) + c_z_1*diff(z(-1)) + c_z_2*diff(z(-2)) + pac_expectation(pacman) + c_z_s*s + c_z_dv*diff(v) ) + (1-lambda)*( cy*y + cx*x) + c_z_u*u + c_z_dx2*diff(x2) + ez;
end;
shocks;
var ex1 = 1.0;
var ex2 = 1.0;
var ex1bar = 1.0;
var ex2bar = 1.0;
var ez = 1.0;
var ey = 0.1;
var ex = 0.1;
var eu = 0.05;
var ev = 0.05;
var es = 0.07;
end;
shock_groups;
g1 = ex, ey, ez;
g2 = eu, ev;
end;
// Initialize the PAC model (build the Companion VAR representation for the auxiliary model).
pac.initialize('pacman');
// Update the parameters of the PAC expectation model (h0 and h1 vectors).
pac.update.expectation('pacman');
// Set initial conditions to zero for non logged variables, and one for logged variables
init = zeros(10, M_.endo_nbr+M_.exo_nbr);
initialconditions = dseries(init, 2000Q1, vertcat(M_.endo_names,M_.exo_names));
// Simulate the model for 500 periods
TrueData = simul_backward_model(initialconditions, 500);
decomposition = shock_decomposition_backward(TrueData, initialconditions, { 'g1', 'g2', 'es' }, { 'z', 'y', 'u'});
% Verify that y is only influenced by g1 (which contains ey)
y_idx = find(strcmp(M_.endo_names, 'y'));
if ~(all(all(decomposition(y_idx, 2:3, :) == 0)) && all(all(decomposition(y_idx, 1, :) ~= 0)))
error('Wrong decomposition for y')
end
% Verify that u is only influenced by g2 (which contains eu)
u_idx = find(strcmp(M_.endo_names, 'u'));
if ~(all(all(decomposition(u_idx, [1 3], :) == 0)) && all(all(decomposition(u_idx, 2, :) ~= 0)))
error('Wrong decomposition for u')
end