2019-12-27 18:58:32 +01:00
|
|
|
function series = histvalf_initvalf(caller, M, options)
|
|
|
|
% function initvalf(M)
|
|
|
|
%
|
|
|
|
% handles options for histvalf_initvalf() and initvalf()
|
|
|
|
%
|
|
|
|
% INPUTS
|
|
|
|
% caller: string, name of calling function
|
|
|
|
% M: model structure
|
|
|
|
% options: options specific to initivalf
|
|
|
|
%
|
|
|
|
% OUTPUTS
|
|
|
|
% series: dseries containing selected data from a file or a dseries
|
|
|
|
%
|
|
|
|
|
2021-04-20 10:44:18 +02:00
|
|
|
% Copyright (C) 2003-2021 Dynare Team
|
2019-12-27 18:58:32 +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/>.
|
|
|
|
|
|
|
|
|
|
|
|
% dseries
|
|
|
|
if isfield(options, 'series')
|
2021-04-20 10:44:18 +02:00
|
|
|
series = evalin('base', options.series);
|
2019-12-27 18:58:32 +01:00
|
|
|
dseries_ispresent = true;
|
|
|
|
else
|
|
|
|
dseries_ispresent = false;
|
|
|
|
end
|
|
|
|
|
|
|
|
% file
|
|
|
|
datafile = '';
|
|
|
|
if isfield(options, 'filename')
|
2020-09-23 13:56:08 +02:00
|
|
|
warning('%s_FILE: option FILENAME is deprecated, please use option DATAFILE', caller)
|
2019-12-27 18:58:32 +01:00
|
|
|
if dseries_ispresent
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: you can''t use option FILENAME and option SERIES at the same time', caller)
|
2019-12-27 18:58:32 +01:00
|
|
|
end
|
|
|
|
if isfield(options, 'datafile')
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: you can''t use option DATAFILE and option FILENAME at the same time', caller)
|
2019-12-27 18:58:32 +01:00
|
|
|
end
|
|
|
|
datafile = options.filename;
|
|
|
|
end
|
|
|
|
|
|
|
|
if isfield(options, 'datafile')
|
|
|
|
if dseries_ispresent
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: you can''t use option DATAFILE and option SERIES at the same time', caller)
|
2019-12-27 18:58:32 +01:00
|
|
|
end
|
|
|
|
datafile = options.datafile;
|
|
|
|
end
|
|
|
|
|
|
|
|
if datafile
|
|
|
|
[directory,basename,extension] = fileparts(datafile);
|
|
|
|
% Auto-detect extension if not provided
|
|
|
|
if isempty(extension)
|
|
|
|
if exist([basename '.m'],'file')
|
|
|
|
extension = '.m';
|
|
|
|
elseif exist([basename '.mat'],'file')
|
|
|
|
extension = '.mat';
|
|
|
|
elseif exist([basename '.xls'],'file')
|
|
|
|
extension = '.xls';
|
|
|
|
elseif exist([basename '.xlsx'],'file')
|
|
|
|
extension = '.xlsx';
|
|
|
|
else
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: Can''t find datafile: %s.{m,mat,xls,xlsx}', caller, basename);
|
2019-12-27 18:58:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
fullname = [basename extension];
|
|
|
|
series = dseries(fullname);
|
|
|
|
end
|
|
|
|
|
|
|
|
% checking that all variable are present
|
|
|
|
error_flag = false;
|
|
|
|
for i = 1:M.orig_endo_nbr
|
|
|
|
if ~series.exist(M.endo_names{i})
|
2020-09-23 13:56:08 +02:00
|
|
|
dprintf('%s_FILE: endogenous variable %s is missing', caller, M.endo_names{i})
|
2019-12-27 18:58:32 +01:00
|
|
|
error_flag = true;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for i = 1:M.exo_nbr
|
|
|
|
if ~series.exist(M.exo_names{i})
|
2020-09-23 13:56:08 +02:00
|
|
|
dprintf('%s_FILE: exogenous variable %s is missing', caller, M.exo_names{i})
|
2019-12-27 18:58:32 +01:00
|
|
|
error_flag = true;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
for i = 1:M.exo_det_nbr
|
|
|
|
if ~series.exist(M.exo_det_names{i})
|
2020-09-23 13:56:08 +02:00
|
|
|
dprintf('%s_FILE: exo_det variable %s is missing', caller, M.exo_det_names{i})
|
2019-12-27 18:58:32 +01:00
|
|
|
error_flag = true;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if error_flag
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: some variables are missing', caller)
|
2019-12-27 18:58:32 +01:00
|
|
|
end
|
|
|
|
|
2020-09-23 13:56:08 +02:00
|
|
|
if exist(sprintf('+%s/dynamic_set_auxiliary_series.m', M.fname), 'file')
|
2019-12-27 18:58:32 +01:00
|
|
|
series = feval(sprintf('%s.dynamic_set_auxiliary_series', M.fname), series, M.params);
|
|
|
|
end
|
|
|
|
|
|
|
|
% selecting observations
|
|
|
|
if isfield(options, 'nobs')
|
|
|
|
nobs = options.nobs;
|
|
|
|
else
|
|
|
|
nobs = 0;
|
|
|
|
end
|
|
|
|
|
|
|
|
periods = series.dates;
|
|
|
|
nobs0 = series.nobs;
|
|
|
|
|
|
|
|
first_obs_ispresent = false;
|
|
|
|
last_obs_ispresent = false;
|
2020-07-15 22:03:48 +02:00
|
|
|
|
2020-09-19 19:17:42 +02:00
|
|
|
first_obs = periods(1);
|
|
|
|
if isfield(options, 'first_obs') && ~isempty(options.first_obs)
|
|
|
|
if options.first_obs < 1
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: first_obs must be a positive number', caller)
|
2020-09-19 19:17:42 +02:00
|
|
|
elseif options.first_obs > nobs0
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: first_obs = %d is larger than the number of observations in the data file (%d)', ...
|
|
|
|
caller, options.first_obs, nobs0)
|
2020-09-19 19:17:42 +02:00
|
|
|
elseif isfield(options, 'first_simulation_period')
|
2020-09-23 13:56:08 +02:00
|
|
|
if options.first_obs == options.first_simulation_period - M.orig_maximum_lag
|
2020-09-19 19:17:42 +02:00
|
|
|
first_obs = periods(options.first_obs);
|
|
|
|
else
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: first_obs = %d and first_simulation_period = %d have values inconsistent with a maximum lag of %d periods', ...
|
|
|
|
caller, options.first_obs, options.first_simulation_period, M.orig_maximum_lag)
|
2020-09-19 19:17:42 +02:00
|
|
|
end
|
2020-08-28 15:46:56 +02:00
|
|
|
elseif isfield(options, 'firstsimulationperiod')
|
2020-09-23 13:56:08 +02:00
|
|
|
if periods(options.first_obs) == options.firstsimulationperiod - M.orig_maximum_lag
|
2020-09-19 19:17:42 +02:00
|
|
|
first_obs = periods(options.first_obs);
|
|
|
|
else
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: first_obs = %d and first_simulation_period = %s have values inconsistent with a maximum lag of %d periods', ...
|
|
|
|
caller, options.first_obs, options.firstsimulationperiod, M.orig_maximum_lag)
|
2020-09-19 19:17:42 +02:00
|
|
|
end
|
|
|
|
else
|
2020-09-20 11:11:26 +02:00
|
|
|
first_obs = periods(options.first_obs);
|
2020-07-15 22:03:48 +02:00
|
|
|
end
|
2020-09-19 19:17:42 +02:00
|
|
|
first_obs_ispresent = true;
|
2020-07-15 22:03:48 +02:00
|
|
|
end
|
|
|
|
|
2020-09-19 19:17:42 +02:00
|
|
|
if isfield(options, 'firstobs') && ~isempty(options.firstobs)
|
|
|
|
if isfield(options, 'first_simulation_period')
|
2020-09-23 13:56:08 +02:00
|
|
|
if options.firstobs == periods(options.first_simulation_period) - M.orig_maximum_lag
|
2020-09-19 19:17:42 +02:00
|
|
|
first_obs = options.firstobs;
|
|
|
|
else
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: first_obs = %s and first_simulation_period = %d have values inconsistent with a maximum lag of %d periods', ...
|
|
|
|
caller, options.firstobs, options.first_simulation_period, M.orig_maximum_lag)
|
2020-09-19 19:17:42 +02:00
|
|
|
end
|
|
|
|
elseif isfield(options, 'firstsimulationperiod')
|
2020-09-23 13:56:08 +02:00
|
|
|
if options.firstobs == options.firstsimulationperiod - M.orig_maximum_lag
|
2020-09-19 19:17:42 +02:00
|
|
|
first_obs = options.firstobs;
|
|
|
|
else
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: firstobs = %s and first_simulation_period = %s have values inconsistent with a maximum lag of %d periods', ...
|
|
|
|
caller, options.firstobs, options.firstsimulationperiod, M.orig_maximum_lag)
|
2020-09-19 19:17:42 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
first_obs = options.firstobs;
|
2019-12-27 18:58:32 +01:00
|
|
|
end
|
|
|
|
first_obs_ispresent = true;
|
|
|
|
end
|
|
|
|
|
2020-09-19 19:17:42 +02:00
|
|
|
if ~first_obs_ispresent
|
|
|
|
if isfield(options, 'first_simulation_period')
|
2020-09-20 16:52:31 +02:00
|
|
|
if options.first_simulation_period < M.orig_maximum_lag
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: first_simulation_period = %d must be larger than the maximum lag (%d)', ...
|
|
|
|
caller, options.first_simulation_period, M.orig_maximum_lag)
|
2020-09-19 19:17:42 +02:00
|
|
|
elseif options.first_simulation_period > nobs0
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: first_simulations_period = %d is larger than the number of observations in the data file (%d)', ...
|
|
|
|
caller, options.first_obs, nobs0)
|
2020-09-19 19:17:42 +02:00
|
|
|
else
|
2020-09-23 13:56:08 +02:00
|
|
|
first_obs = periods(options.first_simulation_period) - M.orig_maximum_lag;
|
2019-12-27 18:58:32 +01:00
|
|
|
end
|
2020-09-19 19:17:42 +02:00
|
|
|
first_obs_ispresent = true;
|
|
|
|
elseif isfield(options, 'firstsimulationperiod')
|
2020-09-23 13:56:08 +02:00
|
|
|
first_obs = options.firstsimulationperiod - M.orig_maximum_lag;
|
2020-09-19 19:17:42 +02:00
|
|
|
first_obs_ispresent = true;
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if isfield(options, 'last_obs')
|
|
|
|
if options.last_obs > nobs0
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: last_obs = %d is larger than the number of observations in the dataset (%d)', ...
|
|
|
|
caller, options.last_obs, nobs0)
|
2020-09-19 19:17:42 +02:00
|
|
|
elseif first_obs_ispresent
|
|
|
|
if nobs > 0 && (periods(options.last_obs) ~= first_obs + nobs - 1)
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: FIST_OBS, LAST_OBS and NOBS contain inconsistent information. Use only two of these options.', caller)
|
2020-09-19 19:17:42 +02:00
|
|
|
else
|
2020-09-20 11:11:26 +02:00
|
|
|
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);
|
2020-09-19 19:17:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
elseif isfield(options, 'lastobs')
|
|
|
|
if options.lastobs > series.last
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: last_obs = %s is larger than the number of observations in the dataset (%s)', ...
|
|
|
|
caller, options.lastobs, series.last)
|
2020-09-19 19:17:42 +02:00
|
|
|
elseif first_obs_ispresent
|
|
|
|
if nobs > 0 && (options.lastobs ~= first_obs + nobs - 1)
|
2020-09-23 13:56:08 +02:00
|
|
|
error('%s_FILE: FIST_OBS, LAST_OBS and NOBS contain inconsistent information. Use only two of these options.', caller)
|
2020-09-19 19:17:42 +02:00
|
|
|
else
|
2020-09-20 16:52:31 +02:00
|
|
|
last_obs = options.lastobs;
|
2020-09-20 11:11:26 +02:00
|
|
|
end
|
|
|
|
else
|
|
|
|
last_obs = options.last_obs;
|
|
|
|
if nobs > 0
|
|
|
|
first_obs = last_obs - nobs + 1;
|
|
|
|
else
|
|
|
|
first_obs = periods(1);
|
2020-09-19 19:17:42 +02:00
|
|
|
end
|
2019-12-27 18:58:32 +01:00
|
|
|
end
|
2020-09-19 19:17:42 +02:00
|
|
|
elseif nobs > 0
|
2020-09-20 11:11:26 +02:00
|
|
|
last_obs = first_obs + nobs - 1;
|
2020-09-19 19:17:42 +02:00
|
|
|
else
|
2020-09-20 11:11:26 +02:00
|
|
|
last_obs = series.last;
|
2019-12-27 18:58:32 +01:00
|
|
|
end
|
|
|
|
|
2021-04-20 10:44:18 +02:00
|
|
|
series = series(first_obs:last_obs);
|