Closes #1761 (Fixes for histval_initval_file)

time-shift
Stéphane Adjemian (Charybdis) 2020-09-23 12:19:18 +02:00
commit a40f30cada
Signed by: stepan
GPG Key ID: 295C1FE89E17EB3C
5 changed files with 211 additions and 109 deletions

View File

@ -30,29 +30,6 @@ function [endo_histval, exo_histval, exo_det_histval] = histvalf(M, options)
% 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 <http://www.gnu.org/licenses/>. % along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if ~isfield(options, 'nobs') || isempty(options.nobs)
options.nobs = M.orig_maximum_lag;
end
if ~isfield(options, 'first_obs') || isempty(options.first_obs)
if isfield(options, 'first_simulation_period')
options.first_obs = options.first_simulation_period ...
- options.nobs;
else
options.first_obs = 1;
end
elseif isfield(options, 'first_simulation_period')
nobs = options.first_simulation_period - opions_.first_obs;
if options.nobs ~= nobs
error(sprintf(['HISTVALF: first_obs = %d and', ...
' first_simulation_period = %d', ...
' don''t provide for the number of' ...
' lags in the model.'], ...
options.first_obs, ...
options.first_simulation_period))
end
end
series = histvalf_initvalf('HISTVAL', M, options); series = histvalf_initvalf('HISTVAL', M, options);
% capture the difference between stochastic and % capture the difference between stochastic and
% perfect foresight setup % perfect foresight setup

View File

@ -129,95 +129,143 @@ nobs0 = series.nobs;
first_obs_ispresent = false; first_obs_ispresent = false;
last_obs_ispresent = false; last_obs_ispresent = false;
if isfield(options, 'first_obs')
i = options.first_obs; first_obs = periods(1);
if i < 1 if isfield(options, 'first_obs') && ~isempty(options.first_obs)
error([caller, '_FILE: the first requested period is before available', ... if options.first_obs < 1
' data.']) error([caller, '_FILE: first_obs must be a positive number'])
elseif i > nobs0 elseif options.first_obs > nobs0
error([caller, '_FILE: the first requested period is after available', ... error(sprintf([caller, '_FILE: first_obs = %d is larger than the number', ...
' data.']) ' of observations in the data file (%d)'], ...
end options.first_obs, nobs0))
first_obs = periods(i); elseif isfield(options, 'first_simulation_period')
if nobs > 0 if options.first_obs == options.first_simulation_period ...
last_obs = first_obs + nobs - 1; - M.orig_maximum_lag
last_obs_ispresent = true; first_obs = periods(options.first_obs);
end else
first_obs_ispresent = true; error(sprintf([caller, '_FILE: first_obs = %d and', ...
elseif isfield(options, 'firstobs') ' first_simulation_period = %d have values', ...
first_obs = options.firstobs; ' inconsistent with a maximum lag of %d periods'], ...
if nobs > 0 options.first_obs, options.first_simulation_period, ...
last_obs = first_obs + nobs - 1; M.orig_maximum_lag))
last_obs_ispresent = true; end
elseif isfield(options, 'firstsimulationperiod')
if periods(options.first_obs) == options.firstsimulationperiod ...
- M.orig_maximum_lag
first_obs = periods(options.first_obs);
else
error(sprintf([caller, '_FILE: first_obs = %d and', ...
' first_simulation_period = %s have values', ...
' inconsistent with a maximum lag of %d periods'], ...
options.first_obs, options.firstsimulationperiod, ...
M.orig_maximum_lag))
end
else
first_obs = periods(options.first_obs);
end end
first_obs_ispresent = true; first_obs_ispresent = true;
end end
if last_obs_ispresent if isfield(options, 'firstobs') && ~isempty(options.firstobs)
if isfield(options, 'last_obs') if isfield(options, 'first_simulation_period')
i = options.last_obs; if options.firstobs == periods(options.first_simulation_period) ...
if i < 1 - M.orig_maximum_lag
error([caller, '_FILE: the last requested period is before available', ... first_obs = options.firstobs;
' data.']) else
elseif i > nobs0 error(sprintf([caller, '_FILE: first_obs = %s and', ...
error([caller, '_FILE: the last requested period is after available', ... ' first_simulation_period = %d have values', ...
' data.']) ' inconsistent with a maximum lag of %d periods'], ...
options.firstobs, options.first_simulation_period, ...
M.orig_maximum_lag))
end end
if last_obs ~= periods(i) elseif isfield(options, 'firstsimulationperiod')
error([caller, '_FILE: FIST_OBS, LAST_OBS and NOBS contain', ... if options.firstobs == options.firstsimulationperiod ...
' inconsistent information. Use only two of these', ... - M.orig_maximum_lag
' options.']) first_obs = options.firstobs;
end else
elseif isfield(options, 'lastobs') error(sprintf([caller, '_FILE: firstobs = %s and', ...
if last_obs ~= options.lastobs ' first_simulation_period = %s have values', ...
error([caller, '_FILE: FIST_OBS, LAST_OBS and NOBS contain', ... ' inconsistent with a maximum lag of %d periods'], ...
' inconsistent information. Use only two of these', ... options.firstobs, options.firstsimulationperiod, ...
' options.']) M.orig_maximum_lag))
end end
else
first_obs = options.firstobs;
end end
elseif isfield(options, 'last_obs') first_obs_ispresent = true;
i = options.last_obs;
if i < 1
error([caller, '_FILE: the last requested period is before available', ...
' data.'])
elseif i > nobs0
error([caller, '_FILE: the last requested period is after available', ...
' data.'])
end
last_obs = periods(i);
if nobs > 0
first_obs = last_obs - nobs + 1;
first_obs_ispresent = true;
end
last_obs_ispresent = true;
elseif isfield(options, 'lastobs')
last_obs = options.lastobs;
if nobs > 0
first_obs = last_obs - nobs + 1;
first_obs_ispresent = true;
end
last_obs_ispresent = true;
end end
if ~first_obs_ispresent if ~first_obs_ispresent
first_obs = periods(1); if isfield(options, 'first_simulation_period')
end if options.first_simulation_period < M.orig_maximum_lag
error(sprintf([caller, '_FILE: first_simulation_period = %d', ...
if ~last_obs_ispresent 'must be larger than the maximum lag (%d)'], ...
if nobs > 0 options.first_simulation_period, M.orig_maximum_lag))
last_obs = first_obs + nobs - 1; elseif options.first_simulation_period > nobs0
else error(sprintf([caller, '_FILE: first_simulations_period = %d', ...
last_obs = periods(end); ' is larger than the number of observations in', ...
' the data file (%d)'], ...
options.first_obs, nobs0))
else
first_obs = periods(options.first_simulation_period) - ...
M.orig_maximum_lag;
end
first_obs_ispresent = true;
elseif isfield(options, 'firstsimulationperiod')
first_obs = options.firstsimulationperiod - ...
M.orig_maximum_lag;
first_obs_ispresent = true;
end end
end end
if first_obs < series.init if isfield(options, 'last_obs')
error([caller, '_FILE: the first requested period is before available', ... if options.last_obs > nobs0
' data.']) error(sprintf([caller, '_FILE: last_obs = %d is larger than the number', ...
elseif last_obs > series.last ' of observations in the dataset (%d)'], ...
error([caller, '_FILE: the last requested period is after available', ... options.last_obs, nobs0))
' data.']) elseif first_obs_ispresent
if nobs > 0 && (periods(options.last_obs) ~= first_obs + nobs - 1)
error([caller, '_FILE: FIST_OBS, LAST_OBS and NOBS contain', ...
' inconsistent information. Use only two of these', ...
' options.'])
else
last_obs = periods(options.last_obs);
end
else
last_obs = periods(options.last_obs);
if nobs > 0
first_obs = last_obs - nobs + 1;
else
first_obs = periods(1);
end
end
elseif isfield(options, 'lastobs')
if options.lastobs > series.last
error(sprintf([caller, '_FILE: last_obs = %s is larger than the number', ...
' of observations in the dataset (%s)'], ...
options.lastobs, series.last))
elseif first_obs_ispresent
if nobs > 0 && (options.lastobs ~= first_obs + nobs - 1)
error([caller, '_FILE: FIST_OBS, LAST_OBS and NOBS contain', ...
' inconsistent information. Use only two of these', ...
' options.'])
else
last_obs = options.lastobs;
end
else
last_obs = options.last_obs;
if nobs > 0
first_obs = last_obs - nobs + 1;
else
first_obs = periods(1);
end
end
elseif nobs > 0
last_obs = first_obs + nobs - 1;
else else
series = series(first_obs:last_obs); last_obs = series.last;
end end
series = series(first_obs:last_obs);

View File

@ -1,4 +1,4 @@
function failed_tests = my_assert(failed_tests, success, test_name) function failed_tests = my_assert(failed_tests, success, test_name)
if ~success if ~success
failed_tests = cat(1, test_failed, test_name); failed_tests = cat(1, failed_tests, test_name);
end end

View File

@ -24,6 +24,12 @@ model;
end; end;
initval_file(series = ds); initval_file(series = ds);
if oo_.initval_series.dates(1) ~= dates('1Y');
error("Wrong initial date in oo_.initval_series");
end;
if oo_.initval_series{'x'}.data(6) ~= 0.9;
error("Wrond value for x");
end;
perfect_foresight_setup(periods=200); perfect_foresight_setup(periods=200);
perfect_foresight_solver(maxit=100); perfect_foresight_solver(maxit=100);
@ -42,6 +48,13 @@ data1(8, 6) = 0.9; //shock to x in period 2
ds1 = dseries(data1, '1Y', {'c', 'cmav', 'k', 'z_backward', 'z_forward', 'x'}); ds1 = dseries(data1, '1Y', {'c', 'cmav', 'k', 'z_backward', 'z_forward', 'x'});
initval_file(series = ds1, first_obs = 3, last_obs = 210, nobs = 208); initval_file(series = ds1, first_obs = 3, last_obs = 210, nobs = 208);
if oo_.initval_series.dates(1) ~= dates('3Y');
error("Wrong initial date in oo_.initval_series");
end;
if oo_.initval_series{'x'}.data(6) ~= 0.9;
error("Wrond value for x");
end;
perfect_foresight_setup(periods=200); perfect_foresight_setup(periods=200);
perfect_foresight_solver(maxit=100); perfect_foresight_solver(maxit=100);
@ -54,3 +67,67 @@ base_results=load('sim_exo_lead_lag_results.mat');
if max(max(abs(base_results.oo_.endo_simul(1:5,:) - oo_.endo_simul(1:5,:)))) > 1e-8 if max(max(abs(base_results.oo_.endo_simul(1:5,:) - oo_.endo_simul(1:5,:)))) > 1e-8
error('Simulation with leads and lags doesn''t match the one with auxiliary variables') error('Simulation with leads and lags doesn''t match the one with auxiliary variables')
end end
initval_file(series = ds1, first_obs = 3Y, last_obs = 210Y, nobs = 208);
if oo_.initval_series.dates(1) ~= dates('3Y');
error("Wrong initial date in oo_.initval_series");
end;
if oo_.initval_series{'x'}.data(6) ~= 0.9;
error("Wrond value for x");
end;
perfect_foresight_setup(periods=200);
perfect_foresight_solver(maxit=100);
if ~oo_.deterministic_simulation.status
error('Perfect foresight simulation failed');
end
base_results=load('sim_exo_lead_lag_results.mat');
if max(max(abs(base_results.oo_.endo_simul(1:5,:) - oo_.endo_simul(1:5,:)))) > 1e-8
error('Simulation with leads and lags doesn''t match the one with auxiliary variables')
end
initval_file(series = ds1, first_simulation_period = 7);
if oo_.initval_series.dates(1) ~= dates('3Y');
error("Wrong initial date in oo_.initval_series");
end;
if oo_.initval_series{'x'}.data(6) ~= 0.9;
error("Wrond value for x");
end;
perfect_foresight_setup(periods=200);
perfect_foresight_solver(maxit=100);
if ~oo_.deterministic_simulation.status
error('Perfect foresight simulation failed');
end
base_results=load('sim_exo_lead_lag_results.mat');
if max(max(abs(base_results.oo_.endo_simul(1:5,:) - oo_.endo_simul(1:5,:)))) > 1e-8
error('Simulation with leads and lags doesn''t match the one with auxiliary variables')
end
initval_file(series = ds1, first_simulation_period = 7Y);
if oo_.initval_series.dates(1) ~= dates('3Y');
error("Wrong initial date in oo_.initval_series");
end;
if oo_.initval_series{'x'}.data(6) ~= 0.9;
error("Wrond value for x");
end;
perfect_foresight_setup(periods=200);
perfect_foresight_solver(maxit=100);
if ~oo_.deterministic_simulation.status
error('Perfect foresight simulation failed');
end
base_results=load('sim_exo_lead_lag_results.mat');
if max(max(abs(base_results.oo_.endo_simul(1:5,:) - oo_.endo_simul(1:5,:)))) > 1e-8
error('Simulation with leads and lags doesn''t match the one with auxiliary variables')
end

View File

@ -16,6 +16,7 @@ M.endo_names = {'Variable_1','Variable_2','Variable_3'};
M.exo_nbr = 1; M.exo_nbr = 1;
M.exo_names = {'Variable_4'}; M.exo_names = {'Variable_4'};
M.exo_det_nbr = 0; M.exo_det_nbr = 0;
M.orig_maximum_lag = 2;
caller = 'INITVAL'; caller = 'INITVAL';
@ -88,9 +89,9 @@ try
ds1 = histvalf_initvalf(caller, M, options); ds1 = histvalf_initvalf(caller, M, options);
error('This test didn''t catch the error') error('This test didn''t catch the error')
catch me catch me
if strcmp(me.message, ['INITVAL_FILE: FIST_OBS, LAST_OBS and NOBS contain', ... if ~strcmp(me.message, strcat('INITVAL_FILE: FIST_OBS, LAST_OBS and NOBS contain', ...
' inconsistent information. Use only two of these', ... ' inconsistent information. Use only two of these', ...
' options.']) == false ' options.'))
failed_tests = cat(1, failed_tests, 'Wrong nobs error message' ); failed_tests = cat(1, failed_tests, 'Wrong nobs error message' );
end end
end end
@ -98,14 +99,13 @@ num_tests = num_tests + 1;
options = struct(); options = struct();
options.series = ds; options.series = ds;
options.first_obs = -1; options.first_obs = 0;
try try
ds1 = histvalf_initvalf(caller, M, options); ds1 = histvalf_initvalf(caller, M, options);
error('This test didn''t catch the error') error('This test didn''t catch the error')
catch me catch me
if strcmp(me.message, [caller, '_FILE: the first requested period is', ... if ~strcmp(me.message, strcat(caller, '_FILE: first_obs must be a positive number'))
' before available data.']) == false
failed_tests = cat(1, failed_tests, ... failed_tests = cat(1, failed_tests, ...
'Wrong first period error message'); 'Wrong first period error message');
end end
@ -120,8 +120,8 @@ try
ds1 = histvalf_initvalf(caller, M, options); ds1 = histvalf_initvalf(caller, M, options);
error('This test didn''t catch the error') error('This test didn''t catch the error')
catch me catch me
if strcmp(me.message, [caller, '_FILE: the last requested period is', ... if ~strcmp(me.message, strcat(caller, '_FILE: last_obs = 11 is larger than the number', ...
' after available data.']) == false ' of observations in the dataset (10)'))
failed_tests = cat(1, failed_tests, ... failed_tests = cat(1, failed_tests, ...
'Wrong last period error message'); 'Wrong last period error message');
end end