conditional_forecast: remove globals and move to namespace

covariance-quadratic-approximation
Johannes Pfeifer 2023-12-07 21:15:18 +01:00 committed by Sébastien Villemot
parent fd0d93ba13
commit 75cd1042c8
No known key found for this signature in database
GPG Key ID: 2CECE9350ECEBE4A
5 changed files with 24 additions and 26 deletions

View File

@ -1,5 +1,5 @@
function [forcs, e] = mcforecast3(cL, H, mcValue, shocks, forcs, T, R, mv, mu) function [forcs, e] = get_shock_paths(cL, H, mcValue, shocks, forcs, T, R, mv, mu)
% [forcs, e] = mcforecast3(cL, H, mcValue, shocks, forcs, T, R, mv, mu) % [forcs, e] = get_shock_paths(cL, H, mcValue, shocks, forcs, T, R, mv, mu)
% Computes the shock values for constrained forecasts necessary to keep % Computes the shock values for constrained forecasts necessary to keep
% endogenous variables at their constrained paths % endogenous variables at their constrained paths
% %

View File

@ -1,4 +1,5 @@
function plot_icforecast(Variables,periods,options_,oo_) function plot(Variables,periods,options_,oo_)
% plot(Variables,periods,options_,oo_)
% Build plots for the conditional forecasts. % Build plots for the conditional forecasts.
% %
% INPUTS % INPUTS
@ -11,7 +12,7 @@ function plot_icforecast(Variables,periods,options_,oo_)
% None. % None.
% %
% SPECIAL REQUIREMENTS % SPECIAL REQUIREMENTS
% This routine has to be called after imcforecast.m. % This routine has to be called after conditional_forecasts.run
% Copyright © 2006-2023 Dynare Team % Copyright © 2006-2023 Dynare Team
% %
@ -44,7 +45,7 @@ else
end end
if periods>forecast_periods if periods>forecast_periods
fprintf('\nplot_icforecast:: Number of periods for plotting exceeds forecast horizon. Setting periods to %d.\n',forecast_periods-1) fprintf('\nconditional_forecasts.plot:: Number of periods for plotting exceeds forecast horizon. Setting periods to %d.\n',forecast_periods-1)
periods=forecast_periods; periods=forecast_periods;
end end

View File

@ -1,11 +1,16 @@
function imcforecast(constrained_paths, constrained_vars, options_cond_fcst) function forecasts=run(M_,options_,oo_,bayestopt_,estim_params_,constrained_paths, constrained_vars, options_cond_fcst)
% run(constrained_paths, constrained_vars, options_cond_fcst)
% Computes conditional forecasts. % Computes conditional forecasts.
% %
% INPUTS % INPUTS
% - constrained_paths [double] m*p array, where m is the number of constrained endogenous variables and p is the number of constrained periods. % M_ [structure] describing the model
% - constrained_vars [integer] m*1 array, indices in M_.endo_names of the constrained variables. % options_ [structure] describing the options
% - options_cond_fcst [structure] containing the options. The fields are: % oo_: [structure] storing the results
% bayestopt_ [structure] describing the priors
% estim_params_ [structure] characterizing parameters to be estimated
% - constrained_paths [double] m*p array, where m is the number of constrained endogenous variables and p is the number of constrained periods.
% - constrained_vars [integer] m*1 array, indices in M_.endo_names of the constrained variables.
% - options_cond_fcst [structure] containing the options. The fields are:
% %
% + replic [integer] scalar, number of monte carlo simulations. % + replic [integer] scalar, number of monte carlo simulations.
% + parameter_set [char] values of the estimated parameters: % + parameter_set [char] values of the estimated parameters:
@ -19,16 +24,16 @@ function imcforecast(constrained_paths, constrained_vars, options_cond_fcst)
% + conf_sig [double] scalar in [0,1], probability mass covered by the confidence bands. % + conf_sig [double] scalar in [0,1], probability mass covered by the confidence bands.
% %
% OUTPUTS % OUTPUTS
% None. % - forecasts [structure] results of the conditional forecast exercise
% %
% SPECIAL REQUIREMENTS % SPECIAL REQUIREMENTS
% This routine has to be called after an estimation statement or an estimated_params block. % This routine has to be called after an estimation statement or an estimated_params block.
% %
% REMARKS % REMARKS
% [1] Results are stored in oo_.conditional_forecast. % [1] Results are stored in oo_.conditional_forecast.
% [2] Use the function plot_icforecast to plot the results. % [2] Use the function conditional_forecasts.plot to plot the results.
% Copyright © 2006-2020 Dynare Team % Copyright © 2006-2023 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
@ -45,8 +50,6 @@ function imcforecast(constrained_paths, constrained_vars, options_cond_fcst)
% 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/>.
global options_ oo_ M_ bayestopt_ estim_params_
if ~isfield(options_cond_fcst,'parameter_set') || isempty(options_cond_fcst.parameter_set) if ~isfield(options_cond_fcst,'parameter_set') || isempty(options_cond_fcst.parameter_set)
if isfield(oo_,'posterior_mode') if isfield(oo_,'posterior_mode')
options_cond_fcst.parameter_set = 'posterior_mode'; options_cond_fcst.parameter_set = 'posterior_mode';
@ -123,7 +126,6 @@ if estimated_model
missing_value = dataset_info.missing.state; missing_value = dataset_info.missing.state;
%store qz_criterium %store qz_criterium
qz_criterium_old=options_.qz_criterium;
options_=select_qz_criterium_value(options_); options_=select_qz_criterium_value(options_);
options_smoothed_state_uncertainty_old = options_.smoothed_state_uncertainty; options_smoothed_state_uncertainty_old = options_.smoothed_state_uncertainty;
[atT, ~, ~, ~,ys, ~, ~, ~, ~, ~, ~, ~, ~, ~,oo_,bayestopt_] = ... [atT, ~, ~, ~,ys, ~, ~, ~, ~, ~, ~, ~, ~, ~,oo_,bayestopt_] = ...
@ -155,7 +157,6 @@ if estimated_model
trend = constant(oo_.dr.order_var,:); trend = constant(oo_.dr.order_var,:);
InitState(:,1) = atT(:,end); InitState(:,1) = atT(:,end);
else else
qz_criterium_old=options_.qz_criterium;
if isempty(options_.qz_criterium) if isempty(options_.qz_criterium)
options_.qz_criterium = 1+1e-6; options_.qz_criterium = 1+1e-6;
end end
@ -246,7 +247,7 @@ FORCS1_shocks = zeros(n1,cL,options_cond_fcst.replic);
for b=1:options_cond_fcst.replic %conditional forecast using cL set to constrained values for b=1:options_cond_fcst.replic %conditional forecast using cL set to constrained values
shocks = sQ*randn(ExoSize,options_cond_fcst.periods); shocks = sQ*randn(ExoSize,options_cond_fcst.periods);
shocks(controlled_varexo,:) = zeros(n1, options_cond_fcst.periods); shocks(controlled_varexo,:) = zeros(n1, options_cond_fcst.periods);
[FORCS1(:,:,b), FORCS1_shocks(:,:,b)] = mcforecast3(cL,options_cond_fcst.periods,constrained_paths,shocks,FORCS1(:,:,b),T,R,mv, mu); [FORCS1(:,:,b), FORCS1_shocks(:,:,b)] = conditional_forecasts.get_shock_paths(cL,options_cond_fcst.periods,constrained_paths,shocks,FORCS1(:,:,b),T,R,mv, mu);
FORCS1(:,:,b)=FORCS1(:,:,b)+trend; %add trend FORCS1(:,:,b)=FORCS1(:,:,b)+trend; %add trend
end end
if max(max(max(abs(bsxfun(@minus,FORCS1(constrained_vars,1+1:1+cL,:),trend(constrained_vars,1:cL)+constrained_paths)))))>1e-4 if max(max(max(abs(bsxfun(@minus,FORCS1(constrained_vars,1+1:1+cL,:),trend(constrained_vars,1:cL)+constrained_paths)))))>1e-4
@ -292,7 +293,7 @@ FORCS2(:,1,:) = repmat(InitState,1,options_cond_fcst.replic); %set initial stead
for b=1:options_cond_fcst.replic %conditional forecast using cL set to 0 for b=1:options_cond_fcst.replic %conditional forecast using cL set to 0
shocks = sQ*randn(ExoSize,options_cond_fcst.periods); shocks = sQ*randn(ExoSize,options_cond_fcst.periods);
shocks(controlled_varexo,:) = zeros(n1, options_cond_fcst.periods); shocks(controlled_varexo,:) = zeros(n1, options_cond_fcst.periods);
FORCS2(:,:,b) = mcforecast3(0,options_cond_fcst.periods,constrained_paths,shocks,FORCS2(:,:,b),T,R,mv, mu)+trend; FORCS2(:,:,b) = conditional_forecasts.get_shock_paths(0,options_cond_fcst.periods,constrained_paths,shocks,FORCS2(:,:,b),T,R,mv, mu)+trend;
end end
mFORCS2 = mean(FORCS2,3); mFORCS2 = mean(FORCS2,3);
@ -307,8 +308,4 @@ for i = 1:EndoSize
forecasts.uncond.ci.(M_.endo_names{oo_.dr.order_var(i)}) = [tmp(t1,:)' ,tmp(t2,:)' ]'; forecasts.uncond.ci.(M_.endo_names{oo_.dr.order_var(i)}) = [tmp(t1,:)' ,tmp(t2,:)' ]';
end end
forecasts.graph.title = graph_title; forecasts.graph.title = graph_title;
forecasts.graph.fname = M_.fname; forecasts.graph.fname = M_.fname;
%reset qz_criterium
options_.qz_criterium=qz_criterium_old;
oo_.conditional_forecast = forecasts;

@ -1 +1 @@
Subproject commit cf45c77343c50cddf89ab848b4300932e3bdf673 Subproject commit 3dadac8f191dfa1dd660442d5bc4526c4c218149

View File

@ -59,7 +59,7 @@ var e_a; stderr 0.014;
var e_m; stderr 0.005; var e_m; stderr 0.005;
end; end;
steady; steady(tolx=1e-10,tolf=1e-12);
check; check;