Add error flag (new output) to simul_backward_model routine.

silicon
Stéphane Adjemian (Ryûk) 2023-03-16 21:53:13 +01:00
parent b8c70eba3b
commit 51ffe77fb7
Signed by: stepan
GPG Key ID: 295C1FE89E17EB3C
8 changed files with 68 additions and 36 deletions

View File

@ -10,7 +10,7 @@ function forecasts = backward_model_forecast(initialcondition, listofvariables,
% OUTPUTS % OUTPUTS
% - forecast [dseries] % - forecast [dseries]
% Copyright © 2017-2018 Dynare Team % Copyright © 2017-2023 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -91,10 +91,15 @@ end
% Compute forecast without shock % Compute forecast without shock
if options_.linear if options_.linear
ysim__0 = simul_backward_linear_model_(initialcondition, periods, DynareOptions, DynareModel, DynareOutput, innovations, nx, ny1, iy1, jdx, model_dynamic); [ysim__0, errorflag] = simul_backward_linear_model_(initialcondition, periods, DynareOptions, DynareModel, DynareOutput, innovations, nx, ny1, iy1, jdx, model_dynamic);
else else
ysim__0 = simul_backward_nonlinear_model_(initialcondition, periods, DynareOptions, DynareModel, DynareOutput, innovations, iy1, model_dynamic); [ysim__0, errorflag] = simul_backward_nonlinear_model_(initialcondition, periods, DynareOptions, DynareModel, DynareOutput, innovations, iy1, model_dynamic);
end end
if errorflag
error('Simulation failed.')
end
forecasts.pointforecast = dseries(transpose(ysim__0(idy,:)), initialcondition.init, listofvariables); forecasts.pointforecast = dseries(transpose(ysim__0(idy,:)), initialcondition.init, listofvariables);
% Set first period of forecast % Set first period of forecast
@ -106,9 +111,12 @@ if withuncertainty
for i=1:B for i=1:B
innovations = transpose(sigma*randn(M_.exo_nbr, periods)); innovations = transpose(sigma*randn(M_.exo_nbr, periods));
if options_.linear if options_.linear
[ysim__, xsim__] = simul_backward_linear_model_(initialcondition, periods, DynareOptions, DynareModel, DynareOutput, innovations, nx, ny1, iy1, jdx, model_dynamic); [ysim__, xsim__, errorflag] = simul_backward_linear_model_(initialcondition, periods, DynareOptions, DynareModel, DynareOutput, innovations, nx, ny1, iy1, jdx, model_dynamic);
else else
[ysim__, xsim__] = simul_backward_nonlinear_model_(initialcondition, periods, DynareOptions, DynareModel, DynareOutput, innovations, iy1, model_dynamic); [ysim__, xsim__, errorflag] = simul_backward_nonlinear_model_(initialcondition, periods, DynareOptions, DynareModel, DynareOutput, innovations, iy1, model_dynamic);
end
if errorflag
error('Simulation failed.')
end end
ArrayOfForecasts(:,:,i) = ysim__(idy,:); ArrayOfForecasts(:,:,i) = ysim__(idy,:);
end end
@ -120,4 +128,4 @@ if withuncertainty
ArrayOfForecasts = sort(ArrayOfForecasts, 3); ArrayOfForecasts = sort(ArrayOfForecasts, 3);
forecasts.lb = dseries(transpose(ArrayOfForecasts(:,:,round(0.025*B))), initialcondition.init, listofvariables); forecasts.lb = dseries(transpose(ArrayOfForecasts(:,:,round(0.025*B))), initialcondition.init, listofvariables);
forecasts.ub = dseries(transpose(ArrayOfForecasts(:,:,round(0.975*B))), initialcondition.init, listofvariables); forecasts.ub = dseries(transpose(ArrayOfForecasts(:,:,round(0.975*B))), initialcondition.init, listofvariables);
end end

View File

@ -19,7 +19,7 @@ function [deviations, baseline, irfs] = backward_model_irf(initialcondition, inn
% argument. % argument.
% - If second argument is not empty, periods must not be greater than innovationbaseline.nobs. % - If second argument is not empty, periods must not be greater than innovationbaseline.nobs.
% Copyright © 2017-2020 Dynare Team % Copyright © 2017-2023 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -163,9 +163,13 @@ irfs = struct();
% Baseline paths (get transition paths induced by the initial condition and % Baseline paths (get transition paths induced by the initial condition and
% baseline innovations). % baseline innovations).
if options_.linear if options_.linear
ysim__0 = simul_backward_linear_model_(initialcondition, periods, DynareOptions, DynareModel, DynareOutput, Innovations, nx, ny1, iy1, jdx, model_dynamic); [ysim__0, errorflag] = simul_backward_linear_model_(initialcondition, periods, DynareOptions, DynareModel, DynareOutput, Innovations, nx, ny1, iy1, jdx, model_dynamic);
else else
ysim__0 = simul_backward_nonlinear_model_(initialcondition, periods, DynareOptions, DynareModel, DynareOutput, Innovations, iy1, model_dynamic); [ysim__0, errorflag] = simul_backward_nonlinear_model_(initialcondition, periods, DynareOptions, DynareModel, DynareOutput, Innovations, iy1, model_dynamic);
end
if errorflag
error('Simulation failed. Cannot simulate baseline.')
end end
% Transform the endogenous variables. % Transform the endogenous variables.
@ -198,9 +202,13 @@ for i=1:length(listofshocks)
innovations(1,:) = innovations(1,:) + transpose(C(:,j)); innovations(1,:) = innovations(1,:) + transpose(C(:,j));
end end
if options_.linear if options_.linear
ysim__1 = simul_backward_linear_model_(initialcondition, periods, DynareOptions, DynareModel, DynareOutput, innovations, nx, ny1, iy1, jdx, model_dynamic); [ysim__1, errorflag] = simul_backward_linear_model_(initialcondition, periods, DynareOptions, DynareModel, DynareOutput, innovations, nx, ny1, iy1, jdx, model_dynamic);
else else
ysim__1 = simul_backward_nonlinear_model_(initialcondition, periods, DynareOptions, DynareModel, DynareOutput, innovations, iy1, model_dynamic); [ysim__1, errorflag] = simul_backward_nonlinear_model_(initialcondition, periods, DynareOptions, DynareModel, DynareOutput, innovations, iy1, model_dynamic);
end
if errorflag
warning('Simulation failed. Cannot compute IRF for %s.', listofshocks{i})
continue
end end
% Transform the endogenous variables % Transform the endogenous variables
if notransform if notransform
@ -230,4 +238,4 @@ end
if nargout>1 if nargout>1
baseline = dseries(transpose(endo_simul__0), initialcondition.init, endonames(1:M_.orig_endo_nbr), DynareModel.endo_names_tex(1:M_.orig_endo_nbr)); baseline = dseries(transpose(endo_simul__0), initialcondition.init, endonames(1:M_.orig_endo_nbr), DynareModel.endo_names_tex(1:M_.orig_endo_nbr));
baseline = merge(baseline, innovationbaseline); baseline = merge(baseline, innovationbaseline);
end end

View File

@ -1,4 +1,4 @@
function simulations = simul_backward_linear_model(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations) function [simulations, errorflag] = simul_backward_linear_model(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations)
% Simulates a stochastic linear backward looking model. % Simulates a stochastic linear backward looking model.
% %
@ -12,6 +12,7 @@ function simulations = simul_backward_linear_model(initialconditions, samplesize
% %
% OUTPUTS % OUTPUTS
% - DynareOutput [struct] Dynare's oo_ global structure. % - DynareOutput [struct] Dynare's oo_ global structure.
% - errorflag [logical] scalar, equal to false iff the simulation did not fail.
% %
% REMARKS % REMARKS
% [1] The innovations used for the simulation are saved in DynareOutput.exo_simul, and the resulting paths for the endogenous % [1] The innovations used for the simulation are saved in DynareOutput.exo_simul, and the resulting paths for the endogenous
@ -21,7 +22,7 @@ function simulations = simul_backward_linear_model(initialconditions, samplesize
% [3] If the first input argument is empty, the endogenous variables are initialized with 0, or if available with the informations % [3] If the first input argument is empty, the endogenous variables are initialized with 0, or if available with the informations
% provided thrtough the histval block. % provided thrtough the histval block.
% Copyright © 2012-2022 Dynare Team % Copyright © 2012-2023 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -53,6 +54,6 @@ end
[initialconditions, samplesize, innovations, DynareOptions, DynareModel, DynareOutput, endonames, exonames, nx, ny1, iy1, jdx, model_dynamic] = ... [initialconditions, samplesize, innovations, DynareOptions, DynareModel, DynareOutput, endonames, exonames, nx, ny1, iy1, jdx, model_dynamic] = ...
simul_backward_model_init(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations); simul_backward_model_init(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations);
[ysim, xsim] = simul_backward_linear_model_(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations, nx, ny1, iy1, jdx, model_dynamic); [ysim, xsim, errorflag] = simul_backward_linear_model_(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations, nx, ny1, iy1, jdx, model_dynamic);
simulations = [dseries(ysim', initialconditions.init, endonames(1:DynareModel.orig_endo_nbr)), dseries(xsim, initialconditions.init, exonames)]; simulations = [dseries(ysim', initialconditions.init, endonames(1:DynareModel.orig_endo_nbr)), dseries(xsim, initialconditions.init, exonames)];

View File

@ -1,4 +1,4 @@
function [ysim, xsim] = simul_backward_linear_model_(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations, nx, ny1, iy1, jdx, model_dynamic) function [ysim, xsim, errorflag] = simul_backward_linear_model_(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations, nx, ny1, iy1, jdx, model_dynamic)
% Simulates a stochastic linear backward looking model. % Simulates a stochastic linear backward looking model.
% %
@ -12,6 +12,7 @@ function [ysim, xsim] = simul_backward_linear_model_(initialconditions, samplesi
% %
% OUTPUTS % OUTPUTS
% - DynareOutput [struct] Dynare's oo_ global structure. % - DynareOutput [struct] Dynare's oo_ global structure.
% - errorflag [logical] scalar, equal to false iff the simulation did not fail.
% %
% REMARKS % REMARKS
% [1] The innovations used for the simulation are saved in DynareOutput.exo_simul, and the resulting paths for the endogenous % [1] The innovations used for the simulation are saved in DynareOutput.exo_simul, and the resulting paths for the endogenous
@ -21,7 +22,7 @@ function [ysim, xsim] = simul_backward_linear_model_(initialconditions, samplesi
% [3] If the first input argument is empty, the endogenous variables are initialized with 0, or if available with the informations % [3] If the first input argument is empty, the endogenous variables are initialized with 0, or if available with the informations
% provided thrtough the histval block. % provided thrtough the histval block.
% Copyright © 2017-2022 Dynare Team % Copyright © 2017-2023 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -38,6 +39,8 @@ function [ysim, xsim] = simul_backward_linear_model_(initialconditions, samplesi
% You should have received a copy of the GNU General Public License % You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <https://www.gnu.org/licenses/>. % along with Dynare. If not, see <https://www.gnu.org/licenses/>.
errorflag = false;
if ~isempty(innovations) if ~isempty(innovations)
DynareOutput.exo_simul(initialconditions.nobs+(1:samplesize),:) = innovations; DynareOutput.exo_simul(initialconditions.nobs+(1:samplesize),:) = innovations;
end end
@ -48,7 +51,15 @@ end
DynareModel.params, ... DynareModel.params, ...
DynareOutput.steady_state, DynareModel.orig_maximum_lag+1); DynareOutput.steady_state, DynareModel.orig_maximum_lag+1);
A0inv = inv(jacob(:,jdx)); try
A0inv = inv(jacob(:,jdx));
catch
errorflag = true;
ysim = [];
xsim = [];
return
end
A1 = jacob(:,nonzeros(DynareModel.lead_lag_incidence(1,:))); A1 = jacob(:,nonzeros(DynareModel.lead_lag_incidence(1,:)));
B = jacob(:,end-nx+1:end); B = jacob(:,end-nx+1:end);
@ -58,4 +69,4 @@ for it = initialconditions.nobs+(1:samplesize)
end end
ysim = DynareOutput.endo_simul(1:DynareModel.orig_endo_nbr,:); ysim = DynareOutput.endo_simul(1:DynareModel.orig_endo_nbr,:);
xsim = DynareOutput.exo_simul; xsim = DynareOutput.exo_simul;

View File

@ -1,4 +1,4 @@
function simulation = simul_backward_model(initialconditions, samplesize, innovations) function [simulation, errorflag] = simul_backward_model(initialconditions, samplesize, innovations)
% function simulation = simul_backward_model(initialconditions, samplesize, innovations) % function simulation = simul_backward_model(initialconditions, samplesize, innovations)
% Simulates a stochastic backward looking model (with arbitrary precision). % Simulates a stochastic backward looking model (with arbitrary precision).
% %
@ -9,6 +9,7 @@ function simulation = simul_backward_model(initialconditions, samplesize, innova
% %
% OUTPUTS % OUTPUTS
% - simulation [dseries] Simulated endogenous and exogenous variables. % - simulation [dseries] Simulated endogenous and exogenous variables.
% - errorflag [logical] scalar, equal to false iff the simulation did not fail.
% %
% REMARKS % REMARKS
% [1] The innovations used for the simulation are saved in DynareOutput.exo_simul, and the resulting paths for the endogenous % [1] The innovations used for the simulation are saved in DynareOutput.exo_simul, and the resulting paths for the endogenous
@ -18,7 +19,7 @@ function simulation = simul_backward_model(initialconditions, samplesize, innova
% [3] If the first input argument is empty, the endogenous variables are initialized with 0, or if available with the informations % [3] If the first input argument is empty, the endogenous variables are initialized with 0, or if available with the informations
% provided through the histval block. % provided through the histval block.
% Copyright © 2012-2022 Dynare Team % Copyright © 2012-2023 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -93,7 +94,7 @@ else
end end
if options_.linear if options_.linear
simulation = simul_backward_linear_model(initialconditions, samplesize, options_, M_, oo_, Innovations); [simulation, errorflag] = simul_backward_linear_model(initialconditions, samplesize, options_, M_, oo_, Innovations);
else else
simulation = simul_backward_nonlinear_model(initialconditions, samplesize, options_, M_, oo_, Innovations); [simulation, errorflag] = simul_backward_nonlinear_model(initialconditions, samplesize, options_, M_, oo_, Innovations);
end end

View File

@ -1,4 +1,4 @@
function simulations = simul_backward_nonlinear_model(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations) function [simulations, errorflag] = simul_backward_nonlinear_model(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations)
% function simulations = simul_backward_nonlinear_model(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations) % function simulations = simul_backward_nonlinear_model(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations)
% Simulates a stochastic non linear backward looking model with arbitrary precision (a deterministic solver is used). % Simulates a stochastic non linear backward looking model with arbitrary precision (a deterministic solver is used).
% %
@ -12,6 +12,7 @@ function simulations = simul_backward_nonlinear_model(initialconditions, samples
% %
% OUTPUTS % OUTPUTS
% - simulation [dseries] Simulated endogenous and exogenous variables. % - simulation [dseries] Simulated endogenous and exogenous variables.
% - errorflag [logical] scalar, equal to false iff the simulation did not fail.
% %
% REMARKS % REMARKS
% [1] The innovations used for the simulation are saved in DynareOutput.exo_simul, and the resulting paths for the endogenous % [1] The innovations used for the simulation are saved in DynareOutput.exo_simul, and the resulting paths for the endogenous
@ -21,7 +22,7 @@ function simulations = simul_backward_nonlinear_model(initialconditions, samples
% [3] If the first input argument is empty, the endogenous variables are initialized with 0, or if available with the informations % [3] If the first input argument is empty, the endogenous variables are initialized with 0, or if available with the informations
% provided thrtough the histval block. % provided thrtough the histval block.
% Copyright (©) 2012-2022 Dynare Team % Copyright (©) 2012-2023 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -53,6 +54,6 @@ end
[initialconditions, samplesize, innovations, DynareOptions, DynareModel, DynareOutput, endonames, exonames, ~, ~, iy1, ~, model_dynamic] = ... [initialconditions, samplesize, innovations, DynareOptions, DynareModel, DynareOutput, endonames, exonames, ~, ~, iy1, ~, model_dynamic] = ...
simul_backward_model_init(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations); simul_backward_model_init(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations);
[ysim, xsim] = simul_backward_nonlinear_model_(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations, iy1, model_dynamic); [ysim, xsim, errorflag] = simul_backward_nonlinear_model_(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations, iy1, model_dynamic);
simulations = [dseries(ysim', initialconditions.init, endonames(1:DynareModel.orig_endo_nbr)), dseries(xsim, initialconditions.init, exonames)]; simulations = [dseries(ysim', initialconditions.init, endonames(1:DynareModel.orig_endo_nbr)), dseries(xsim, initialconditions.init, exonames)];

View File

@ -1,4 +1,4 @@
function [ysim, xsim] = simul_backward_nonlinear_model_(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations, iy1, model_dynamic) function [ysim, xsim, errorflag] = simul_backward_nonlinear_model_(initialconditions, samplesize, DynareOptions, DynareModel, DynareOutput, innovations, iy1, model_dynamic)
% Simulates a stochastic non linear backward looking model with arbitrary precision (a deterministic solver is used). % Simulates a stochastic non linear backward looking model with arbitrary precision (a deterministic solver is used).
% %
@ -12,6 +12,7 @@ function [ysim, xsim] = simul_backward_nonlinear_model_(initialconditions, sampl
% %
% OUTPUTS % OUTPUTS
% - DynareOutput [struct] Dynare's oo_ global structure. % - DynareOutput [struct] Dynare's oo_ global structure.
% - errorflag [logical] scalar, equal to false iff the simulation did not fail.
% %
% REMARKS % REMARKS
% [1] The innovations used for the simulation are saved in DynareOutput.exo_simul, and the resulting paths for the endogenous % [1] The innovations used for the simulation are saved in DynareOutput.exo_simul, and the resulting paths for the endogenous
@ -21,7 +22,7 @@ function [ysim, xsim] = simul_backward_nonlinear_model_(initialconditions, sampl
% [3] If the first input argument is empty, the endogenous variables are initialized with 0, or if available with the informations % [3] If the first input argument is empty, the endogenous variables are initialized with 0, or if available with the informations
% provided thrtough the histval block. % provided thrtough the histval block.
% Copyright © 2017-2022 Dynare Team % Copyright © 2017-2023 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -100,6 +101,7 @@ for it = initialconditions.nobs+(1:samplesize)
end end
end end
catch Error catch Error
errorflag = true;
DynareOutput.endo_simul = DynareOutput.endo_simul(:, 1:it-1); DynareOutput.endo_simul = DynareOutput.endo_simul(:, 1:it-1);
dprintf('Newton failed on iteration i = %s.', num2str(it-initialconditions.nobs)); dprintf('Newton failed on iteration i = %s.', num2str(it-initialconditions.nobs));
ytm = DynareOutput.endo_simul(:,end); ytm = DynareOutput.endo_simul(:,end);

View File

@ -51,7 +51,7 @@ histval;
PhysicalCapitalStock(0) = 1; PhysicalCapitalStock(0) = 1;
end; end;
simulation__ = simul_backward_nonlinear_model([], 10, options_, M_, oo_); [simulation__, errorflag] = simul_backward_nonlinear_model([], 10, options_, M_, oo_);
initialcondition = dseries(simulation__.data(10,1:M_.endo_nbr), 2017Q1, M_.endo_names); initialcondition = dseries(simulation__.data(10,1:M_.endo_nbr), 2017Q1, M_.endo_names);
@ -73,11 +73,11 @@ listofvariables = {'Efficiency', 'Output'};
forecasts = backward_model_forecast(initialcondition, listofvariables, 16, true); forecasts = backward_model_forecast(initialcondition, listofvariables, 16, true);
/* REMARKS /* REMARKS
** **
** - The third argument is the number of periods for the forecast. Default is 8. ** - The third argument is the number of periods for the forecast. Default is 8.
** - The last argument is for computing (or not) the confidence bands. If false (default), only point forecast is produced. ** - The last argument is for computing (or not) the confidence bands. If false (default), only point forecast is produced.
** - forecasts is a structure, each field is a dseries object for the point forecast (ie forecasts without innovations in the future), ** - forecasts is a structure, each field is a dseries object for the point forecast (ie forecasts without innovations in the future),
** the mean forecast, the median forecast, the standard deviation of the predictive distribution and the lower/upper bounds of the ** the mean forecast, the median forecast, the standard deviation of the predictive distribution and the lower/upper bounds of the
** interval containing 95% of the predictive distribution. ** interval containing 95% of the predictive distribution.
*/ */
@ -89,7 +89,7 @@ plot(forecasts.ub.Output,'-r')
hold off hold off
/* REMARKS /* REMARKS
** **
** In this model there is no steady state (only a stable balanced growth paths), this explains the ** In this model there is no steady state (only a stable balanced growth paths), this explains the
** shape of the forecast for Output. ** shape of the forecast for Output.
*/ */