From 489e6684e9cb600031d29f3d71e2d88a73e2d535 Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Fri, 17 Sep 2021 13:53:18 +0200 Subject: [PATCH 1/3] smoother2histval.m: fix initialization and dimensions Closes https://git.dynare.org/Dynare/dynare/-/issues/1775 --- matlab/smoother2histval.m | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/matlab/smoother2histval.m b/matlab/smoother2histval.m index 7e90e41ef..29791be1d 100644 --- a/matlab/smoother2histval.m +++ b/matlab/smoother2histval.m @@ -24,7 +24,7 @@ function smoother2histval(opts) % % The function also uses the value of option_.parameter_set -% Copyright (C) 2014-2018 Dynare Team +% Copyright (C) 2014-2021 Dynare Team % % This file is part of Dynare. % @@ -49,7 +49,6 @@ if ~isfield(opts, 'infile') end smoothedvars = oo_.SmoothedVariables; smoothedshocks = oo_.SmoothedShocks; - steady_state = oo_.steady_state; else S = load(opts.infile); if ~isfield(S, 'oo_') || ~isfield(S.oo_, 'SmoothedVariables') @@ -57,7 +56,6 @@ else end smoothedvars = S.oo_.SmoothedVariables; smoothedshocks = S.oo_.SmoothedShocks; - steady_state = S.oo_.steady_state; end % Hack to determine if oo_.SmoothedVariables was computed after a Metropolis @@ -79,9 +77,9 @@ end if post_metropolis tmp = fieldnames(smoothedvars.Mean); if length(tmp)~=M_.endo_nbr - warning(['You are using smoother2histval although smoothed values have not'... + warning(['You are using smoother2histval although smoothed values have not '... 'been computed for all endogenous and auxiliary variables.'... - 'The value of these variables will be set to 0.']) + 'The value of these variables will be set to their steady state.']) end tmpexo = fieldnames(smoothedshocks.Mean); else @@ -94,7 +92,6 @@ if isempty(options_.parameter_set) if post_metropolis smoothedvars = smoothedvars.Mean; smoothedshocks = smoothedshocks.Mean; - steady_state = zeros(size(steady_state)); end else switch options_.parameter_set @@ -112,14 +109,12 @@ else end smoothedvars = smoothedvars.Mean; smoothedshocks = smoothedshocks.Mean; - steady_state = zeros(size(steady_state)); case 'posterior_median' if ~post_metropolis error('Option parameter_set=posterior_median is not consistent with computed smoothed values.') end smoothedvars = smoothedvars.Median; smoothedshocks = smoothedshocks.Median; - steady_state = zeros(size(steady_state)); otherwise error([ 'Option parameter_set=' options_.parameter_set ' unsupported.' ]) end @@ -171,20 +166,27 @@ if ~isfield(opts, 'outfile') M_.endo_histval = repmat(oo_.steady_state, 1, M_.maximum_lag); else % Output to a file + data = zeros(M_.maximum_endo_lag, length(invars)); + for i=1:length(outvars) + j = strmatch(outvars{i}, M_.endo_names, 'exact'); + if ~isempty(j) + data(:,i)=oo_.steady_state(j); + end + end o = dseries(); end % Handle all endogenous variables to be copied -data = zeros(M_.orig_maximum_endo_lag, length(invars)); -k = M_.orig_maximum_endo_lag - M_.maximum_endo_lag + 1: M_.orig_maximum_lag; for i = 1:length(invars) - if isempty(strmatch(invars{i}, M_.endo_names, 'exact')) - % Skip exogenous - continue + if ~isempty(strmatch(invars{i}, M_.endo_names, 'exact')) + s = smoothedvars.(invars{i}); + elseif ~isempty(strmatch(invars{i}, M_.exo_names, 'exact')) + s = smoothedshocks.(invars{i}); + else + error('smoother2histval: unknown input variable') end - s = smoothedvars.(invars{i}); - j = strmatch(invars{i}, M_.endo_names, 'exact'); - v = s((period-M_.orig_maximum_endo_lag+1):period);% + steady_state(j); + + v = s((period-M_.maximum_lag+1):period); if ~isfield(opts, 'outfile') j = strmatch(outvars{i}, M_.endo_names, 'exact'); if isempty(j) From 62e80b0c07d14539f5bc515a3664131d1640926f Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Fri, 17 Sep 2021 14:42:44 +0200 Subject: [PATCH 2/3] smoother2histval.m: also set M_.exo_histval --- matlab/smoother2histval.m | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/matlab/smoother2histval.m b/matlab/smoother2histval.m index 29791be1d..c7502012e 100644 --- a/matlab/smoother2histval.m +++ b/matlab/smoother2histval.m @@ -188,11 +188,16 @@ for i = 1:length(invars) v = s((period-M_.maximum_lag+1):period); if ~isfield(opts, 'outfile') - j = strmatch(outvars{i}, M_.endo_names, 'exact'); - if isempty(j) - error(['smoother2histval: output variable ' outvars{i} ' does not exist.']) - else - M_.endo_histval(j, :) = v(k); + j_endo = strmatch(outvars{i}, M_.endo_names, 'exact'); + if ~isempty(j_endo) + M_.endo_histval(j_endo, :) = v; + end + j_exo = strmatch(outvars{i}, M_.exo_names, 'exact'); + if ~isempty(j_exo) + M_.exo_histval(j_exo, :) = v; + end + if isempty(j_endo) && isempty(j_exo) + error(['smoother2histval: output variable ' outvars{i} ' does not exist.']) end else data(:, i) = v'; From 2dd6510051304f2c534ad4d10bdd0b7d88ebdfaf Mon Sep 17 00:00:00 2001 From: Johannes Pfeifer Date: Fri, 17 Sep 2021 14:43:50 +0200 Subject: [PATCH 3/3] smoother2histval.m: undo logging Closes https://git.dynare.org/Dynare/dynare/-/issues/1407 --- matlab/smoother2histval.m | 6 +++++- tests/smoother2histval/fs2000_smooth_stoch_simul.mod | 5 +++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/matlab/smoother2histval.m b/matlab/smoother2histval.m index c7502012e..463931ab7 100644 --- a/matlab/smoother2histval.m +++ b/matlab/smoother2histval.m @@ -179,7 +179,11 @@ end % Handle all endogenous variables to be copied for i = 1:length(invars) if ~isempty(strmatch(invars{i}, M_.endo_names, 'exact')) - s = smoothedvars.(invars{i}); + if oo_.Smoother.loglinear + s = exp(smoothedvars.(invars{i})); + else + s = smoothedvars.(invars{i}); + end elseif ~isempty(strmatch(invars{i}, M_.exo_names, 'exact')) s = smoothedshocks.(invars{i}); else diff --git a/tests/smoother2histval/fs2000_smooth_stoch_simul.mod b/tests/smoother2histval/fs2000_smooth_stoch_simul.mod index 91bc25ef6..63a7bb398 100644 --- a/tests/smoother2histval/fs2000_smooth_stoch_simul.mod +++ b/tests/smoother2histval/fs2000_smooth_stoch_simul.mod @@ -82,10 +82,11 @@ varobs gp_obs gy_obs; options_.solve_tolf = 1e-12; -estimation(order=1,datafile=fsdat_simul,nobs=192,mh_replic=1500,mh_nblocks=1,mh_jscale=0.8,smoother,consider_all_endogenous); - +estimation(order=1,loglinear,datafile=fsdat_simul,nobs=192,mh_replic=2,mh_nblocks=1,mh_jscale=0.8,smoother,consider_all_endogenous_and_auxiliary); +steady; smoother2histval(period = 5); +options_.loglinear=0; stoch_simul(nomoments); forecast;