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
% 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);
% capture the difference between stochastic and
% perfect foresight setup

View File

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

View File

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

View File

@ -24,6 +24,12 @@ model;
end;
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_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'});
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_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
error('Simulation with leads and lags doesn''t match the one with auxiliary variables')
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_names = {'Variable_4'};
M.exo_det_nbr = 0;
M.orig_maximum_lag = 2;
caller = 'INITVAL';
@ -88,9 +89,9 @@ try
ds1 = histvalf_initvalf(caller, M, options);
error('This test didn''t catch the error')
catch me
if strcmp(me.message, ['INITVAL_FILE: FIST_OBS, LAST_OBS and NOBS contain', ...
' inconsistent information. Use only two of these', ...
' options.']) == false
if ~strcmp(me.message, strcat('INITVAL_FILE: FIST_OBS, LAST_OBS and NOBS contain', ...
' inconsistent information. Use only two of these', ...
' options.'))
failed_tests = cat(1, failed_tests, 'Wrong nobs error message' );
end
end
@ -98,14 +99,13 @@ num_tests = num_tests + 1;
options = struct();
options.series = ds;
options.first_obs = -1;
options.first_obs = 0;
try
ds1 = histvalf_initvalf(caller, M, options);
error('This test didn''t catch the error')
catch me
if strcmp(me.message, [caller, '_FILE: the first requested period is', ...
' before available data.']) == false
if ~strcmp(me.message, strcat(caller, '_FILE: first_obs must be a positive number'))
failed_tests = cat(1, failed_tests, ...
'Wrong first period error message');
end
@ -120,8 +120,8 @@ try
ds1 = histvalf_initvalf(caller, M, options);
error('This test didn''t catch the error')
catch me
if strcmp(me.message, [caller, '_FILE: the last requested period is', ...
' after available data.']) == false
if ~strcmp(me.message, strcat(caller, '_FILE: last_obs = 11 is larger than the number', ...
' of observations in the dataset (10)'))
failed_tests = cat(1, failed_tests, ...
'Wrong last period error message');
end