Pass dseries (saved in .mat or .m) to initval_file.

Allows to pass a dseries object saved on disk in a .mat file or .m to
initialize the paths for the endogenous variables and set the paths
for the exogenous variables. It is not required to pass the auxiliary
variables (automatically computed by initvalf routine), which is useful
if the baseline comes from another model. In this case, the
initval_file command or the datafile option of the
perfect_foresight_setup command sets the value of periods (deduced
from the number of observation in the dseries object and the number of
lags/leads in the model).
time-shift
Stéphane Adjemian (Charybdis) 2020-05-06 22:36:06 +02:00
parent 2566320834
commit e41080fede
Signed by: stepan
GPG Key ID: 295C1FE89E17EB3C
2 changed files with 56 additions and 44 deletions

View File

@ -34,7 +34,7 @@ global M_ oo_ options_
series_ = 1; series_ = 1;
[directory,basename,extension] = fileparts(fname_); [directory, basename, extension] = fileparts(fname_);
% Auto-detect extension if not provided % Auto-detect extension if not provided
if isempty(extension) if isempty(extension)
@ -53,58 +53,70 @@ end
fullname = [basename extension]; fullname = [basename extension];
if ~exist(fullname) if ~exist(fullname, 'file')
error(['Can''t find datafile: ' fullname ]); error(['Can''t find datafile: ' fullname ]);
end end
switch (extension)
case '.m'
eval(basename);
case '.mat'
load(basename);
case { '.xls', '.xlsx' }
[data_,names_v_]=xlsread(fullname); % Octave needs the extension explicitly
series_=0;
otherwise
error(['Unsupported extension for datafile: ' extension])
end
options_.initval_file = true; options_.initval_file = true;
oo_.endo_simul = []; oo_.endo_simul = [];
oo_.exo_simul = []; oo_.exo_simul = [];
for i_=1:length(M_.endo_names) if ismember(extension, {'.mat', '.m'}) && isdseries(fname_)
if series_ == 1 data = dseries(fname_);
x_ = eval(M_.endo_names{i_}); % Add auxiliary variables if any.
if size(x_,2)>size(x_,1) %oo_.endo_simul must be collection of row vectors data = feval(sprintf('%s.dynamic_set_auxiliary_series', M_.fname), data, M_.params);
oo_.endo_simul = [oo_.endo_simul; x_]; % Remove NaNs in the first periods.
else %transpose if column vector data = data(data.firstobservedperiod:data.lastobservedperiod);
% Fill endo_simul.
oo_.endo_simul = transpose(data{M_.endo_names{:}}.data);
% Fill exo_simul.
oo_.exo_simul = data{M_.exo_names{:}}.data;
% Update number of periods for the deterministic simulation.
options_.periods = data.nobs-M_.maximum_lag-M_.maximum_lead;
else
switch (extension)
case '.m'
eval(basename);
case '.mat'
load(basename);
case { '.xls', '.xlsx' }
[data_,names_v_]=xlsread(fullname); % Octave needs the extension explicitly
series_=0;
otherwise
error(['Unsupported extension for datafile: ' extension])
end
for i_=1:length(M_.endo_names)
if series_ == 1
x_ = eval(M_.endo_names{i_});
if size(x_,2)>size(x_,1) %oo_.endo_simul must be collection of row vectors
oo_.endo_simul = [oo_.endo_simul; x_];
else %transpose if column vector
oo_.endo_simul = [oo_.endo_simul; x_'];
end
else
k_ = strmatch(M_.endo_names{i_}, names_v_, 'exact');
if isempty(k_)
error(['INITVAL_FILE: ' M_.endo_names{i_} ' not found'])
end
x_ = data_(:,k_);
oo_.endo_simul = [oo_.endo_simul; x_']; oo_.endo_simul = [oo_.endo_simul; x_'];
end end
else
k_ = strmatch(M_.endo_names{i_}, names_v_, 'exact');
if isempty(k_)
error(['INITVAL_FILE: ' M_.endo_names{i_} ' not found'])
end
x_ = data_(:,k_);
oo_.endo_simul = [oo_.endo_simul; x_'];
end end
end for i_=1:length(M_.exo_names)
if series_ == 1
for i_=1:length(M_.exo_names) x_ = eval(M_.exo_names{i_});
if series_ == 1 if size(x_,2)>size(x_,1) %oo_.endo_simul must be collection of row vectors
x_ = eval(M_.exo_names{i_}); oo_.exo_simul = [oo_.exo_simul x_'];
if size(x_,2)>size(x_,1) %oo_.endo_simul must be collection of row vectors else %if column vector
oo_.exo_simul = [oo_.exo_simul x_']; oo_.exo_simul = [oo_.exo_simul x_];
else %if column vector end
else
k_ = strmatch(M_.exo_names{i_}, names_v_, 'exact');
if isempty(k_)
error(['INITVAL_FILE: ' M_.exo_names{i_} ' not found'])
end
x_ = data_(:,k_);
oo_.exo_simul = [oo_.exo_simul x_]; oo_.exo_simul = [oo_.exo_simul x_];
end end
else
k_ = strmatch(M_.exo_names{i_}, names_v_, 'exact');
if isempty(k_)
error(['INITVAL_FILE: ' M_.exo_names{i_} ' not found'])
end
x_ = data_(:,k_);
oo_.exo_simul = [oo_.exo_simul x_];
end end
end end

@ -1 +1 @@
Subproject commit 7629bc46ca918478b5dbd1b806be7bdbc32d1714 Subproject commit 56e06450e16c60299cbe0ad61519d74fb8cf020e