2014-12-13 12:18:45 +01:00
|
|
|
|
function oo_recursive_=dynare_estimation(var_list,dname)
|
2015-04-27 11:07:52 +02:00
|
|
|
|
% function dynare_estimation(var_list, dname)
|
2009-01-22 22:34:15 +01:00
|
|
|
|
% runs the estimation of the model
|
2011-09-17 15:38:49 +02:00
|
|
|
|
%
|
2009-01-22 22:34:15 +01:00
|
|
|
|
% INPUTS
|
|
|
|
|
% var_list: selected endogenous variables vector
|
2015-04-27 11:07:52 +02:00
|
|
|
|
% dname: alternative directory name
|
2011-09-17 15:38:49 +02:00
|
|
|
|
%
|
2009-01-22 22:34:15 +01:00
|
|
|
|
% OUTPUTS
|
2014-12-13 12:18:45 +01:00
|
|
|
|
% oo_recursive_: cell array containing the results structures from recursive estimation
|
2009-01-22 22:34:15 +01:00
|
|
|
|
%
|
|
|
|
|
% SPECIAL REQUIREMENTS
|
|
|
|
|
% none
|
|
|
|
|
|
2021-01-19 14:52:27 +01:00
|
|
|
|
% Copyright (C) 2003-2021 Dynare Team
|
2009-01-22 22:34:15 +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
|
2021-06-09 17:33:48 +02:00
|
|
|
|
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2009-01-22 22:34:15 +01:00
|
|
|
|
|
2016-10-05 19:04:13 +02:00
|
|
|
|
global options_ oo_ M_ dataset_ dataset_info
|
2014-12-13 12:18:45 +01:00
|
|
|
|
|
|
|
|
|
oo_recursive_={};
|
2017-02-16 14:48:40 +01:00
|
|
|
|
mode_file0 = options_.mode_file; % store mode_file set by the user
|
2017-05-16 15:10:20 +02:00
|
|
|
|
% Test if the order of approximation is nonzero (the preprocessor tests if order is non negative).
|
2013-11-28 10:54:15 +01:00
|
|
|
|
if isequal(options_.order,0)
|
|
|
|
|
error('Estimation:: The order of the Taylor approximation cannot be 0!')
|
|
|
|
|
end
|
|
|
|
|
|
2013-11-28 10:57:54 +01:00
|
|
|
|
% Decide if a DSGE or DSGE-VAR has to be estimated.
|
2009-03-21 23:36:18 +01:00
|
|
|
|
if ~isempty(strmatch('dsge_prior_weight',M_.param_names))
|
2010-06-24 12:53:10 +02:00
|
|
|
|
options_.dsge_var = 1;
|
2009-03-21 23:36:18 +01:00
|
|
|
|
end
|
|
|
|
|
|
2021-01-19 14:52:27 +01:00
|
|
|
|
% The TeX option crashes MATLAB R2014a run with "-nodisplay" option
|
|
|
|
|
% (as is done from the testsuite).
|
|
|
|
|
% Since we can’t directly test whether "-nodisplay" has been passed,
|
|
|
|
|
% we test for the "TOP_TEST_DIR" environment variable, which is set
|
|
|
|
|
% by the testsuite.
|
|
|
|
|
% Note that it was not tested whether the crash happens with more
|
|
|
|
|
% recent MATLAB versions, so when OLD_MATLAB_VERSION is increased,
|
|
|
|
|
% one should make a test before removing this workaround.
|
|
|
|
|
if options_.TeX && ~isoctave && matlab_ver_less_than('8.4') && ~isempty(getenv('TOP_TEST_DIR'))
|
|
|
|
|
warning('Disabling TeX option due to a bug in MATLAB R2014a with -nodisplay')
|
|
|
|
|
options_.TeX = false;
|
|
|
|
|
end
|
|
|
|
|
|
2009-01-22 22:34:15 +01:00
|
|
|
|
var_list = check_list_of_variables(options_, M_, var_list);
|
|
|
|
|
options_.varlist = var_list;
|
|
|
|
|
|
2017-05-16 15:10:20 +02:00
|
|
|
|
nobs = sort(options_.nobs);
|
|
|
|
|
first_obs = sort(options_.first_obs);
|
2015-08-24 21:43:28 +02:00
|
|
|
|
|
2009-01-22 22:34:15 +01:00
|
|
|
|
nnobs = length(nobs);
|
2015-08-24 21:43:28 +02:00
|
|
|
|
nfirstobs = length(first_obs);
|
|
|
|
|
|
|
|
|
|
if nnobs~=1 && nfirstobs~=1
|
|
|
|
|
error('You cannot simultaneously do rolling window and recursive estimation')
|
|
|
|
|
end
|
2017-05-16 15:10:20 +02:00
|
|
|
|
|
2009-01-22 22:34:15 +01:00
|
|
|
|
horizon = options_.forecast;
|
|
|
|
|
|
2017-03-07 13:37:56 +01:00
|
|
|
|
if nargin<2 || ~exist('dname','var') || isempty(dname)
|
2015-04-27 11:50:25 +02:00
|
|
|
|
dname = options_.dirname;
|
2011-09-17 15:38:49 +02:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
M_.dname = dname;
|
|
|
|
|
|
2013-10-27 10:56:11 +01:00
|
|
|
|
if (isnumeric(options_.mode_compute) && options_.mode_compute && options_.analytic_derivation) ... %no user supplied function
|
|
|
|
|
|| (~isnumeric(options_.mode_compute) && options_.analytic_derivation) % user supplied function
|
2012-04-27 15:57:58 +02:00
|
|
|
|
analytic_derivation0=options_.analytic_derivation;
|
|
|
|
|
options_.analytic_derivation=1;
|
|
|
|
|
end
|
2014-01-30 08:53:58 +01:00
|
|
|
|
|
|
|
|
|
if options_.logged_steady_state
|
|
|
|
|
oo_.dr.ys=exp(oo_.dr.ys);
|
|
|
|
|
oo_.steady_state=exp(oo_.steady_state);
|
2015-03-07 12:36:58 +01:00
|
|
|
|
options_.logged_steady_state=0;
|
2014-01-30 08:53:58 +01:00
|
|
|
|
end
|
|
|
|
|
|
2012-04-27 15:57:58 +02:00
|
|
|
|
|
2015-08-24 21:43:28 +02:00
|
|
|
|
if nnobs>1 || nfirstobs > 1
|
|
|
|
|
for i=1:max(nnobs,nfirstobs)
|
|
|
|
|
if nnobs>1
|
|
|
|
|
options_.nobs = nobs(i);
|
|
|
|
|
M_.dname = [dname '_' int2str(nobs(i))];
|
2017-05-16 12:42:01 +02:00
|
|
|
|
elseif nfirstobs>1
|
2017-05-16 15:10:20 +02:00
|
|
|
|
options_.first_obs=first_obs(i);
|
2015-08-24 21:43:28 +02:00
|
|
|
|
M_.dname = [dname '_' int2str(first_obs(i))];
|
|
|
|
|
end
|
2011-10-21 22:06:09 +02:00
|
|
|
|
dynare_estimation_1(var_list,M_.dname);
|
2016-09-27 15:11:42 +02:00
|
|
|
|
if isequal(i,1) && options_.mode_compute ~= 0
|
2021-07-22 17:46:08 +02:00
|
|
|
|
options_.mode_file = [M_.dname filesep 'Output' filesep M_.fname '_mode'];
|
2012-09-06 14:13:29 +02:00
|
|
|
|
end
|
|
|
|
|
if options_.recursive_estimation_restart
|
|
|
|
|
for j=1:options_.recursive_estimation_restart
|
|
|
|
|
dynare_estimation_1(var_list,M_.dname);
|
|
|
|
|
end
|
|
|
|
|
end
|
2015-08-24 21:43:28 +02:00
|
|
|
|
if nnobs>1
|
|
|
|
|
oo_recursive_{nobs(i)} = oo_;
|
2017-05-16 12:42:01 +02:00
|
|
|
|
elseif nfirstobs>1
|
2015-08-24 21:43:28 +02:00
|
|
|
|
oo_recursive_{first_obs(i)} = oo_;
|
|
|
|
|
end
|
2009-01-22 22:34:15 +01:00
|
|
|
|
end
|
|
|
|
|
else
|
2011-09-17 15:38:49 +02:00
|
|
|
|
dynare_estimation_1(var_list,dname);
|
2009-01-22 22:34:15 +01:00
|
|
|
|
end
|
|
|
|
|
|
2017-05-16 12:42:01 +02:00
|
|
|
|
if isnumeric(options_.mode_compute) && options_.mode_compute && options_.analytic_derivation
|
2012-04-27 15:57:58 +02:00
|
|
|
|
options_.analytic_derivation=analytic_derivation0;
|
|
|
|
|
end
|
|
|
|
|
|
2011-02-10 15:54:23 +01:00
|
|
|
|
if nnobs > 1 && horizon > 0
|
2009-01-22 22:34:15 +01:00
|
|
|
|
mh_replic = options_.mh_replic;
|
|
|
|
|
|
|
|
|
|
endo_names = M_.endo_names;
|
2013-05-21 16:38:17 +02:00
|
|
|
|
n_varobs = length(options_.varobs);
|
2011-09-17 15:38:49 +02:00
|
|
|
|
|
2009-01-22 22:34:15 +01:00
|
|
|
|
if isempty(var_list)
|
|
|
|
|
var_list = endo_names;
|
2017-10-10 10:05:59 +02:00
|
|
|
|
nvar = length(endo_names);
|
2009-01-22 22:34:15 +01:00
|
|
|
|
SelecVariables = transpose(1:nvar);
|
|
|
|
|
else
|
2017-10-10 10:05:59 +02:00
|
|
|
|
nvar = length(var_list);
|
2009-01-22 22:34:15 +01:00
|
|
|
|
SelecVariables = [];
|
|
|
|
|
for i=1:nvar
|
2017-10-10 10:05:59 +02:00
|
|
|
|
if ~isempty(strmatch(var_list{i}, endo_names, 'exact'))
|
|
|
|
|
SelecVariables = [SelecVariables; strmatch(var_list{i}, endo_names, 'exact')];
|
2009-01-22 22:34:15 +01:00
|
|
|
|
else
|
2017-10-10 10:05:59 +02:00
|
|
|
|
error(['Estimation:: ' var_list{i} ' isn''t an endogenous variable'])
|
2011-09-17 15:38:49 +02:00
|
|
|
|
end
|
2009-01-22 22:34:15 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
IdObs = zeros(n_varobs,1);
|
|
|
|
|
for j=1:n_varobs
|
2013-05-21 16:38:17 +02:00
|
|
|
|
iobs = strmatch(options_.varobs{j},var_list,'exact');
|
2009-01-22 22:34:15 +01:00
|
|
|
|
if ~isempty(iobs)
|
|
|
|
|
IdObs(j,1) = iobs;
|
2011-09-17 15:38:49 +02:00
|
|
|
|
end
|
|
|
|
|
end
|
2017-05-16 15:10:20 +02:00
|
|
|
|
|
2015-04-11 08:56:59 +02:00
|
|
|
|
gend = dataset_.nobs;
|
2013-06-09 10:55:52 +02:00
|
|
|
|
time_offset=min(3,gend-1); %for observables, plot 3 previous periods unless data is shorter
|
|
|
|
|
k = time_offset+min(nobs(end)-nobs(1)+horizon, ...
|
2017-05-16 15:10:20 +02:00
|
|
|
|
size(dataset_.data,1)-nobs(1));
|
2016-10-05 19:04:13 +02:00
|
|
|
|
data2 = dataset_info.rawdata(end-k+1:end,:);
|
2020-03-03 11:43:38 +01:00
|
|
|
|
[~,nr,nc,~,~,nstar] = pltorg(nvar);
|
2009-01-22 22:34:15 +01:00
|
|
|
|
m = 1;
|
2013-06-09 10:55:52 +02:00
|
|
|
|
plot_index=0;
|
2020-12-16 22:38:22 +01:00
|
|
|
|
OutputDirectoryName = CheckPath('graphs',M_.dname);
|
2017-10-10 10:05:59 +02:00
|
|
|
|
for i = 1:length(var_list)
|
2009-01-22 22:34:15 +01:00
|
|
|
|
if mod(i,nstar) == 1
|
2013-06-09 10:55:52 +02:00
|
|
|
|
plot_index=plot_index+1;
|
2017-03-22 16:54:11 +01:00
|
|
|
|
hfig = dyn_figure(options_.nodisplay,'Name',['Out of sample forecasts (',num2str(plot_index),')']);
|
2009-01-22 22:34:15 +01:00
|
|
|
|
m = 1;
|
|
|
|
|
end
|
|
|
|
|
subplot(nr,nc,m)
|
|
|
|
|
hold on
|
|
|
|
|
if any(i==IdObs)
|
|
|
|
|
k2 = find(i==IdObs);
|
|
|
|
|
offsetx = 3;
|
2013-06-09 10:55:52 +02:00
|
|
|
|
plot(nobs(1)-offsetx+1:nobs(1)-offsetx+k,data2(end-k+1:end,k2)','-k','linewidth',2);
|
2009-01-22 22:34:15 +01:00
|
|
|
|
else
|
|
|
|
|
offsetx = 0;
|
2011-09-17 15:38:49 +02:00
|
|
|
|
end
|
2017-10-10 10:05:59 +02:00
|
|
|
|
vname = var_list{i};
|
2009-01-22 22:34:15 +01:00
|
|
|
|
for j=1:nnobs
|
|
|
|
|
if mh_replic > 0
|
2014-11-11 11:42:19 +01:00
|
|
|
|
oo_.RecursiveForecast.Mean.(vname)(j,:) = ...
|
2017-05-16 15:10:20 +02:00
|
|
|
|
oo_recursive_{nobs(j)}.MeanForecast.Mean.(vname);
|
2014-11-11 11:42:19 +01:00
|
|
|
|
oo_.RecursiveForecast.HPDinf.(vname)(j,:) = ...
|
2017-05-16 15:10:20 +02:00
|
|
|
|
oo_recursive_{nobs(j)}.MeanForecast.HPDinf.(vname);
|
2014-11-11 11:42:19 +01:00
|
|
|
|
oo_.RecursiveForecast.HPDsup.(vname)(j,:) = ...
|
2017-05-16 15:10:20 +02:00
|
|
|
|
oo_recursive_{nobs(j)}.MeanForecast.HPDsup.(vname);
|
2014-11-11 11:42:19 +01:00
|
|
|
|
oo_.RecursiveForecast.HPDTotalinf.(vname)(j,:) = ...
|
2017-05-16 15:10:20 +02:00
|
|
|
|
oo_recursive_{nobs(j)}.PointForecast.HPDinf.(vname);
|
2014-11-11 11:42:19 +01:00
|
|
|
|
oo_.RecursiveForecast.HPDTotalsup.(vname)(j,:) = ...
|
2017-05-16 15:10:20 +02:00
|
|
|
|
oo_recursive_{nobs(j)}.PointForecast.HPDsup.(vname);
|
2009-01-22 22:34:15 +01:00
|
|
|
|
else
|
2014-11-11 11:42:19 +01:00
|
|
|
|
oo_.RecursiveForecast.Mean.(vname)(j,:) =...
|
2017-05-16 15:10:20 +02:00
|
|
|
|
oo_recursive_{nobs(j)}.forecast.Mean.(vname);
|
2014-11-11 11:42:19 +01:00
|
|
|
|
oo_.RecursiveForecast.HPDinf.(vname)(j,:) =...
|
2017-05-16 15:10:20 +02:00
|
|
|
|
oo_recursive_{nobs(j)}.forecast.HPDinf.(vname);
|
2014-11-11 11:42:19 +01:00
|
|
|
|
oo_.RecursiveForecast.HPDsup.(vname)(j,:) =...
|
2017-05-16 15:10:20 +02:00
|
|
|
|
oo_recursive_{nobs(j)}.forecast.HPDsup.(vname);
|
|
|
|
|
end
|
2013-06-09 10:55:52 +02:00
|
|
|
|
x = nobs(1)+nobs(j)-nobs(1)+(1:horizon);
|
|
|
|
|
|
2014-11-11 11:42:19 +01:00
|
|
|
|
y = oo_.RecursiveForecast.Mean.(vname)(j,:);
|
|
|
|
|
y1 = oo_.RecursiveForecast.HPDinf.(vname)(j,:);
|
|
|
|
|
y2 = oo_.RecursiveForecast.HPDsup.(vname)(j,:);
|
2009-01-22 22:34:15 +01:00
|
|
|
|
plot(x,y,'-b','linewidth',2)
|
|
|
|
|
plot(x,y1,'--g', ...
|
|
|
|
|
'linewidth',1.5)
|
|
|
|
|
plot(x,y2,'--g', ...
|
|
|
|
|
'linewidth',1.5)
|
|
|
|
|
if mh_replic
|
2014-11-11 11:42:19 +01:00
|
|
|
|
y3 = oo_.RecursiveForecast.HPDTotalinf.(vname)(j,:);
|
|
|
|
|
y4 = oo_.RecursiveForecast.HPDTotalsup.(vname)(j,:);
|
2009-01-22 22:34:15 +01:00
|
|
|
|
plot(x,y3,'--r', ...
|
|
|
|
|
'linewidth',1.5)
|
|
|
|
|
plot(x,y4,'--r','linewidth',1.5)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
box on
|
|
|
|
|
title(vname,'Interpreter','none')
|
|
|
|
|
hold off
|
2013-06-09 10:55:52 +02:00
|
|
|
|
xlim([nobs(1)-offsetx nobs(end)+horizon])
|
2009-01-22 22:34:15 +01:00
|
|
|
|
m = m + 1;
|
2017-10-10 10:05:59 +02:00
|
|
|
|
if mod(i+1,nstar) == 1 || i==length(var_list)
|
2020-12-16 22:38:22 +01:00
|
|
|
|
dyn_saveas(hfig,[M_.dname,filesep,'graphs',filesep M_.fname '_RecursiveForecasts_' int2str(plot_index)],options_.nodisplay,options_.graph_format);
|
2013-06-09 10:55:52 +02:00
|
|
|
|
end
|
2009-01-22 22:34:15 +01:00
|
|
|
|
end
|
|
|
|
|
end
|
2017-05-16 15:10:20 +02:00
|
|
|
|
options_.mode_file = mode_file0;
|
2017-02-16 14:48:40 +01:00
|
|
|
|
%reset stored mode-file to user defined one (and in case it was only set by the recursive estimation)
|
2020-03-03 11:45:38 +01:00
|
|
|
|
oo_.gui.ran_estimation = true;
|
|
|
|
|
|
2021-09-17 15:18:39 +02:00
|
|
|
|
if isoctave && isfield(options_, 'prior_restrictions') && ...
|
|
|
|
|
isfield(options_.prior_restrictions, 'routine')
|
|
|
|
|
% Octave crashes if it tries to save function handles (to the _results.mat file)
|
|
|
|
|
% See https://savannah.gnu.org/bugs/?43215
|
|
|
|
|
options_.prior_restrictions.routine = [];
|
|
|
|
|
end
|