2014-04-03 15:05:20 +02:00
|
|
|
function smoother2histval(opts)
|
2014-04-04 17:22:09 +02:00
|
|
|
% This function takes values from oo_.SmoothedVariables (and possibly
|
|
|
|
% oo_.SmoothedShocks) and copies them into M_.histval.
|
2017-05-16 15:10:20 +02:00
|
|
|
%
|
2014-04-03 15:05:20 +02:00
|
|
|
% Optional fields in 'opts' structure:
|
2014-03-25 19:01:36 +01:00
|
|
|
% infile: An optional *_results MAT file created by Dynare.
|
2017-05-16 15:10:20 +02:00
|
|
|
% If present, oo_.Smoothed{Variables,Shocks} are read from
|
2014-04-04 17:22:09 +02:00
|
|
|
% there. Otherwise, they are read from the global workspace.
|
2014-04-03 15:05:20 +02:00
|
|
|
% invars: An optional char or cell array listing variables to read in
|
2014-03-25 19:01:36 +01:00
|
|
|
% oo_.SmoothedVariables. If absent, all the endogenous
|
|
|
|
% variables present in oo_.SmoothedVariables are used.
|
2014-02-21 18:33:51 +01:00
|
|
|
% period: An optional period number to use as the starting point
|
|
|
|
% for subsequent simulations. It should be between 1 and
|
|
|
|
% the number of observations that were used to produce the
|
|
|
|
% smoothed values. If absent, the last observation is used.
|
2014-03-25 19:01:36 +01:00
|
|
|
% outfile: An optional MAT file in which to save the histval structure.
|
|
|
|
% If absent, the output will be written in M_.endo_histval
|
2014-04-03 15:05:20 +02:00
|
|
|
% outvars: An optional char or cell array listing variables to be written in
|
2014-03-25 19:01:36 +01:00
|
|
|
% outfile or M_.endo_histval. This cell must be of same
|
|
|
|
% length than invars, and there is a mapping between the input
|
|
|
|
% variable at the i-th position in invars, and the output
|
|
|
|
% variable at the i-th position in outvars. If absent, then
|
|
|
|
% taken as equal to invars.
|
|
|
|
%
|
|
|
|
% The function also uses the value of option_.parameter_set
|
2014-02-21 18:33:51 +01:00
|
|
|
|
2017-10-10 10:05:59 +02:00
|
|
|
% Copyright (C) 2014-2018 Dynare Team
|
2014-02-21 18:33:51 +01:00
|
|
|
%
|
|
|
|
% 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_ oo_
|
|
|
|
|
2014-04-03 15:05:20 +02:00
|
|
|
if ~isfield(opts, 'infile')
|
2014-02-21 18:33:51 +01:00
|
|
|
if ~isfield(oo_, 'SmoothedVariables')
|
|
|
|
error('Could not find smoothed variables; did you set the "smoother" option?')
|
|
|
|
end
|
2014-04-04 17:22:09 +02:00
|
|
|
smoothedvars = oo_.SmoothedVariables;
|
|
|
|
smoothedshocks = oo_.SmoothedShocks;
|
2014-09-12 15:35:00 +02:00
|
|
|
steady_state = oo_.steady_state;
|
2014-02-21 18:33:51 +01:00
|
|
|
else
|
2014-04-03 15:05:20 +02:00
|
|
|
S = load(opts.infile);
|
2014-02-21 18:33:51 +01:00
|
|
|
if ~isfield(S, 'oo_') || ~isfield(S.oo_, 'SmoothedVariables')
|
|
|
|
error('Could not find smoothed variables in file; is this a Dynare results file, and did you set the "smoother" option when producing it?')
|
|
|
|
end
|
2014-04-04 17:22:09 +02:00
|
|
|
smoothedvars = S.oo_.SmoothedVariables;
|
|
|
|
smoothedshocks = S.oo_.SmoothedShocks;
|
2014-09-12 15:35:00 +02:00
|
|
|
steady_state = S.oo_.steady_state;
|
2014-02-21 18:33:51 +01:00
|
|
|
end
|
|
|
|
|
2014-03-25 19:01:36 +01:00
|
|
|
% Hack to determine if oo_.SmoothedVariables was computed after a Metropolis
|
2014-04-07 14:58:11 +02:00
|
|
|
tmp = fieldnames(smoothedvars);
|
2019-07-25 20:28:22 +02:00
|
|
|
if isstruct(smoothedvars.(tmp{1}))
|
2014-03-25 19:01:36 +01:00
|
|
|
post_metropolis = 1;
|
2019-07-25 20:28:22 +02:00
|
|
|
if ~ isstruct(smoothedvars.(tmp{end}))
|
2017-02-15 22:26:23 +01:00
|
|
|
% point and metropolis results are simultaneously present
|
|
|
|
post_metropolis = 2;
|
|
|
|
end
|
2017-05-16 15:10:20 +02:00
|
|
|
|
2019-07-25 20:28:22 +02:00
|
|
|
elseif isstruct(smoothedvars.(tmp{end}))
|
2017-02-15 22:26:23 +01:00
|
|
|
% point and metropolis results are simultaneously present
|
|
|
|
post_metropolis = 2;
|
2014-03-25 19:01:36 +01:00
|
|
|
else
|
|
|
|
post_metropolis = 0;
|
|
|
|
end
|
|
|
|
|
2017-02-16 15:46:40 +01:00
|
|
|
if post_metropolis
|
|
|
|
tmp = fieldnames(smoothedvars.Mean);
|
|
|
|
tmpexo = fieldnames(smoothedshocks.Mean);
|
|
|
|
else
|
|
|
|
tmp = fieldnames(smoothedvars);
|
|
|
|
tmpexo = fieldnames(smoothedshocks);
|
|
|
|
end
|
|
|
|
|
2014-03-25 19:01:36 +01:00
|
|
|
% If post-Metropolis, select the parameter set
|
|
|
|
if isempty(options_.parameter_set)
|
|
|
|
if post_metropolis
|
2014-04-04 17:22:09 +02:00
|
|
|
smoothedvars = smoothedvars.Mean;
|
|
|
|
smoothedshocks = smoothedshocks.Mean;
|
2014-09-12 15:35:00 +02:00
|
|
|
steady_state = zeros(size(steady_state));
|
2014-03-25 19:01:36 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
switch options_.parameter_set
|
|
|
|
case 'calibration'
|
2017-02-15 22:26:23 +01:00
|
|
|
if post_metropolis == 1
|
2014-03-25 19:01:36 +01:00
|
|
|
error('Option parameter_set=calibration is not consistent with computed smoothed values.')
|
|
|
|
end
|
|
|
|
case 'posterior_mode'
|
2017-02-15 22:26:23 +01:00
|
|
|
if post_metropolis == 1
|
2014-03-25 19:01:36 +01:00
|
|
|
error('Option parameter_set=posterior_mode is not consistent with computed smoothed values.')
|
|
|
|
end
|
|
|
|
case 'posterior_mean'
|
|
|
|
if ~post_metropolis
|
|
|
|
error('Option parameter_set=posterior_mean is not consistent with computed smoothed values.')
|
|
|
|
end
|
2014-04-04 17:22:09 +02:00
|
|
|
smoothedvars = smoothedvars.Mean;
|
|
|
|
smoothedshocks = smoothedshocks.Mean;
|
2014-09-12 15:35:00 +02:00
|
|
|
steady_state = zeros(size(steady_state));
|
2014-03-25 19:01:36 +01:00
|
|
|
case 'posterior_median'
|
|
|
|
if ~post_metropolis
|
|
|
|
error('Option parameter_set=posterior_median is not consistent with computed smoothed values.')
|
|
|
|
end
|
2014-04-04 17:22:09 +02:00
|
|
|
smoothedvars = smoothedvars.Median;
|
|
|
|
smoothedshocks = smoothedshocks.Median;
|
2014-09-12 15:35:00 +02:00
|
|
|
steady_state = zeros(size(steady_state));
|
2014-03-25 19:01:36 +01:00
|
|
|
otherwise
|
|
|
|
error([ 'Option parameter_set=' options_.parameter_set ' unsupported.' ])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
% Determine number of periods
|
2019-07-25 20:28:22 +02:00
|
|
|
n = size(smoothedvars.(tmp{1}));
|
2014-02-21 18:33:51 +01:00
|
|
|
|
|
|
|
if n < M_.maximum_endo_lag
|
|
|
|
error('Not enough observations to create initial conditions')
|
|
|
|
end
|
|
|
|
|
2014-04-03 15:05:20 +02:00
|
|
|
if isfield(opts, 'invars')
|
|
|
|
invars = opts.invars;
|
|
|
|
if ischar(invars)
|
|
|
|
invars = cellstr(invars);
|
|
|
|
end
|
|
|
|
else
|
2017-02-16 15:46:40 +01:00
|
|
|
invars = [tmp; tmpexo];
|
2014-03-25 19:01:36 +01:00
|
|
|
end
|
|
|
|
|
2014-04-03 15:05:20 +02:00
|
|
|
if isfield(opts, 'period')
|
|
|
|
period = opts.period;
|
2014-02-21 18:33:51 +01:00
|
|
|
if period > n
|
|
|
|
error('The period that you indicated is beyond the data sample')
|
|
|
|
end
|
|
|
|
if period < M_.maximum_endo_lag
|
|
|
|
error('The period that you indicated is too small to construct initial conditions')
|
|
|
|
end
|
2014-04-03 15:05:20 +02:00
|
|
|
else
|
|
|
|
period = n;
|
2014-02-21 18:33:51 +01:00
|
|
|
end
|
|
|
|
|
2014-04-03 15:05:20 +02:00
|
|
|
if isfield(opts, 'outvars')
|
|
|
|
outvars = opts.outvars;
|
|
|
|
if ischar(outvars)
|
|
|
|
outvars = cellstr(outvars);
|
|
|
|
end
|
2014-02-21 18:33:51 +01:00
|
|
|
if length(invars) ~= length(outvars)
|
|
|
|
error('The number of input and output variables is not the same')
|
|
|
|
end
|
2014-04-03 15:05:20 +02:00
|
|
|
else
|
|
|
|
outvars = invars;
|
2014-02-21 18:33:51 +01:00
|
|
|
end
|
|
|
|
|
2014-04-03 15:05:20 +02:00
|
|
|
% Initialize outputs
|
|
|
|
if ~isfield(opts, 'outfile')
|
|
|
|
% Output to M_.endo_histval
|
2019-12-27 18:58:32 +01:00
|
|
|
M_.endo_histval = repmat(oo_.steady_state, 1, M_.maximum_lag);
|
2014-04-03 15:05:20 +02:00
|
|
|
else
|
|
|
|
% Output to a file
|
2019-12-27 18:58:32 +01:00
|
|
|
o = dseries();
|
2014-04-03 15:05:20 +02:00
|
|
|
end
|
2014-02-21 18:33:51 +01:00
|
|
|
|
2014-04-04 17:22:09 +02:00
|
|
|
% Handle all endogenous variables to be copied
|
2019-12-27 18:58:32 +01:00
|
|
|
data = zeros(M_.orig_maximum_endo_lag, length(invars));
|
|
|
|
k = M_.orig_maximum_endo_lag - M_.maximum_endo_lag + 1: M_.orig_maximum_lag
|
2014-02-21 18:33:51 +01:00
|
|
|
for i = 1:length(invars)
|
2021-01-07 21:43:31 +01:00
|
|
|
if isempty(strmatch(invars{i}, M_.endo_names, 'exact'))
|
2014-04-04 17:22:09 +02:00
|
|
|
% Skip exogenous
|
|
|
|
continue
|
|
|
|
end
|
2019-07-25 20:28:22 +02:00
|
|
|
s = smoothedvars.(invars{i});
|
2014-09-12 15:35:00 +02:00
|
|
|
j = strmatch(invars{i}, M_.endo_names, 'exact');
|
2019-12-27 18:58:32 +01:00
|
|
|
v = s((period-M_.orig_maximum_endo_lag+1):period);% + steady_state(j);
|
2014-04-03 15:05:20 +02:00
|
|
|
if ~isfield(opts, 'outfile')
|
|
|
|
j = strmatch(outvars{i}, M_.endo_names, 'exact');
|
|
|
|
if isempty(j)
|
2014-03-25 19:01:36 +01:00
|
|
|
error(['smoother2histval: output variable ' outvars{i} ' does not exist.'])
|
2014-04-03 15:05:20 +02:00
|
|
|
else
|
2019-12-27 18:58:32 +01:00
|
|
|
M_.endo_histval(j, :) = v(k);
|
2014-03-25 19:01:36 +01:00
|
|
|
end
|
|
|
|
else
|
2019-12-27 18:58:32 +01:00
|
|
|
data(:, i) = v';
|
2014-02-21 18:33:51 +01:00
|
|
|
end
|
2014-03-25 19:01:36 +01:00
|
|
|
end
|
2019-12-27 18:58:32 +01:00
|
|
|
if isfield(opts, 'outfile')
|
|
|
|
o = dseries(data, '1Y', invars);
|
2014-04-03 15:05:20 +02:00
|
|
|
end
|
|
|
|
|
2019-12-27 18:58:32 +01:00
|
|
|
% $$$ % Handle auxiliary variables for lags (both on endogenous and exogenous)
|
|
|
|
% $$$ for i = 1:length(M_.aux_vars)
|
|
|
|
% $$$ if ~ ismember(M_.endo_names{M_.aux_vars(i).endo_index},invars)
|
|
|
|
% $$$ if M_.aux_vars(i).type ~= 1 && M_.aux_vars(i).type ~= 3
|
|
|
|
% $$$ continue
|
|
|
|
% $$$ end
|
|
|
|
% $$$ if M_.aux_vars(i).type == 1
|
|
|
|
% $$$ % Endogenous
|
|
|
|
% $$$ orig_var = M_.endo_names{M_.aux_vars(i).orig_index};
|
|
|
|
% $$$ else
|
|
|
|
% $$$ % Exogenous
|
|
|
|
% $$$ orig_var = M_.exo_names{M_.aux_vars(i).orig_index};
|
|
|
|
% $$$ end
|
|
|
|
% $$$ [m, k] = ismember(orig_var, outvars);
|
|
|
|
% $$$ if m
|
|
|
|
% $$$ if ~isempty(strmatch(invars{k}, M_.endo_names))
|
|
|
|
% $$$ s = smoothedvars.(invars{k});
|
|
|
|
% $$$ else
|
|
|
|
% $$$ s = smoothedshocks.(invars{k});
|
|
|
|
% $$$ end
|
|
|
|
% $$$ l = M_.aux_vars(i).orig_lead_lag;
|
|
|
|
% $$$ if period-M_.maximum_endo_lag+1+l < 1
|
|
|
|
% $$$ error('The period that you indicated is too small to construct initial conditions')
|
|
|
|
% $$$ end
|
|
|
|
% $$$ j = M_.aux_vars(i).endo_index;
|
|
|
|
% $$$ v = s((period-M_.maximum_endo_lag+1+l):(period+l)); %+steady_state(j);
|
|
|
|
% $$$ if ~isfield(opts, 'outfile')
|
|
|
|
% $$$ M_.endo_histval(j, :) = v;
|
|
|
|
% $$$ else
|
|
|
|
% $$$ % When saving to a file, x(-2) is in the variable called "x_l2"
|
|
|
|
% $$$ lead_lag = num2str(l);
|
|
|
|
% $$$ lead_lag = regexprep(lead_lag, '-', 'l');
|
|
|
|
% $$$ o.([ orig_var '_' lead_lag ]) = v;
|
|
|
|
% $$$ end
|
|
|
|
% $$$ end
|
|
|
|
% $$$ end
|
|
|
|
% $$$ end
|
|
|
|
|
2014-04-03 15:05:20 +02:00
|
|
|
% Finalize output
|
|
|
|
if isfield(opts, 'outfile')
|
2019-12-27 18:58:32 +01:00
|
|
|
[dir, fname, ext] = fileparts(opts.outfile);
|
|
|
|
if ~strcmp(ext,'.mat') && ~isempty(ext)
|
|
|
|
error(['smoother2hisvtval: if outfile has an extension, it must ' ...
|
|
|
|
'be .mat'])
|
|
|
|
end
|
|
|
|
o.save([dir fname]);
|
2014-02-21 18:33:51 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|