Commit 1bd72c96 authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Completed X13 interface.

All commands except series and composite.
parent 6f915005
*~
.DS_Store
externals/*
src/git.info
src/git.last-commit-hash
\ No newline at end of file
......@@ -216,4 +216,178 @@ if ismember('x11', o.commands)
fprintf(fid, '}\n\n');
end
% Write FORCE block
if ismember('force', o.commands)
fprintf(fid, 'force {');
if ~all(cellfun(@isempty, struct2cell(o.force)))
fprintf(fid, '\n');
optionnames = fieldnames(o.force);
for i=1:length(optionnames)
if ~isempty(o.force.(optionnames{i}))
printoption(fid, optionnames{i}, o.force.(optionnames{i}));
end
end
end
fprintf(fid, '}\n\n');
end
% Write HISTORY block
if ismember('history', o.commands)
fprintf(fid, 'history {');
if ~all(cellfun(@isempty, struct2cell(o.history)))
fprintf(fid, '\n');
optionnames = fieldnames(o.history);
for i=1:length(optionnames)
if ~isempty(o.history.(optionnames{i}))
printoption(fid, optionnames{i}, o.history.(optionnames{i}));
end
end
end
fprintf(fid, '}\n\n');
end
% Write METADATA block
if ismember('metadata', o.commands)
fprintf(fid, 'metadata {');
if ~all(cellfun(@isempty, struct2cell(o.metadata)))
fprintf(fid, '\n');
optionnames = fieldnames(o.metadata);
for i=1:length(optionnames)
if ~isempty(o.metadata.(optionnames{i}))
printoption(fid, optionnames{i}, o.metadata.(optionnames{i}));
end
end
end
fprintf(fid, '}\n\n');
end
% Write IDENTIFY block
if ismember('identify', o.commands)
fprintf(fid, 'identify {');
if ~all(cellfun(@isempty, struct2cell(o.identify)))
fprintf(fid, '\n');
optionnames = fieldnames(o.identify);
for i=1:length(optionnames)
if ~isempty(o.identify.(optionnames{i}))
printoption(fid, optionnames{i}, o.identify.(optionnames{i}));
end
end
end
fprintf(fid, '}\n\n');
end
% Write PICKMDL block
if ismember('pickmdl', o.commands)
fprintf(fid, 'pickmdl {');
if ~all(cellfun(@isempty, struct2cell(o.pickmdl)))
fprintf(fid, '\n');
optionnames = fieldnames(o.pickmdl);
for i=1:length(optionnames)
if ~isempty(o.pickmdl.(optionnames{i}))
printoption(fid, optionnames{i}, o.pickmdl.(optionnames{i}));
end
end
end
fprintf(fid, '}\n\n');
end
% Write SEATS block
if ismember('seats', o.commands)
fprintf(fid, 'seats {');
if ~all(cellfun(@isempty, struct2cell(o.seats)))
fprintf(fid, '\n');
optionnames = fieldnames(o.seats);
for i=1:length(optionnames)
if ~isempty(o.seats.(optionnames{i}))
printoption(fid, optionnames{i}, o.seats.(optionnames{i}));
end
end
end
fprintf(fid, '}\n\n');
end
% Write SLIDINGSPANS block
if ismember('slidingspans', o.commands)
fprintf(fid, 'slidingspans {');
if ~all(cellfun(@isempty, struct2cell(o.slidingspans)))
fprintf(fid, '\n');
optionnames = fieldnames(o.slidingspans);
for i=1:length(optionnames)
if ~isempty(o.slidingspans.(optionnames{i}))
printoption(fid, optionnames{i}, o.slidingspans.(optionnames{i}));
end
end
end
fprintf(fid, '}\n\n');
end
% Write SPECTRUM block
if ismember('spectrum', o.commands)
fprintf(fid, 'spectrum {');
if ~all(cellfun(@isempty, struct2cell(o.spectrum)))
fprintf(fid, '\n');
optionnames = fieldnames(o.spectrum);
for i=1:length(optionnames)
if ~isempty(o.spectrum.(optionnames{i}))
printoption(fid, optionnames{i}, o.spectrum.(optionnames{i}));
end
end
end
fprintf(fid, '}\n\n');
end
% Write X11REGRESSION block
if ismember('x11regression', o.commands)
fprintf(fid, 'x11regression {');
if ~all(cellfun(@isempty, struct2cell(o.x11regression)))
optionnames = fieldnames(o.x11regression);
fprintf(fid, '\n');
for i=1:length(optionnames)
if ~isempty(o.x11regression.(optionnames{i}))
if isequal(optionnames{i}, 'user') % Write needed data to a file.
% Determine the set of needed data
conditionningvariables = strsplit(o.x11regression.user, {',' , '(' , ')' , ' '});
conditionningvariables = conditionningvariables(~cellfun(@isempty,conditionningvariables));
% Check that these data are available.
for i=1:length(conditionningvariables)
if ~ismember(conditionningvariables{i}, o.x.name)
fclose(fid);
error('x13:x11regression: Variable %s is unkonwn', conditionningvariables{i})
end
end
% Select the data.
if length(conditionningvariables)<vobs(o.x)
x = o.x{conditionningvariables{:}};
else
x= o.x;
end
% Print user statement.
fprintf(fid, ' user = %s\n', o.x11regression.user);
% Print data statement.
fprintf(fid, ' data = %s\n', sprintf(data2txt(x.data)));
elseif isequal(optionnames{i}, 'start')
if ischar(o.x11regression.start)
if isdate(o.x11regression.start)
PERIOD = dates(o.x11regression.start);
else
error('x13:x11regression: Option start cannot be interpreted as a date!')
end
elseif isdates(o.x11regression.start)
PERIOD = o.x11regression.start;
else
error('x13:x11regression: Option start cannot be interpreted as a date!')
end
printstart(fid, PERIOD);
else
printoption(fid, optionnames{i}, o.x11regression.(optionnames{i}));
end
end
end
if ~isempty(o.x) && isempty(o.x11regression.start)
fprintf(fid, ' start = %i.%i\n', year(o.x.init), subperiod(o.x.init));
end
end
fprintf(fid, '}\n\n');
end
fclose(fid);
\ No newline at end of file
......@@ -74,5 +74,288 @@ if ~all(cellfun(@isempty, struct2cell(o.forecast)))
end
end
% Save results related to the TRANSFORM command
if ~all(cellfun(@isempty, struct2cell(o.transform)))
if ~isempty(o.transform.save)
savedoutput = strsplit(o.transform.save, {',' , '(' , ')' , ' '});
savedoutput = savedoutput(~cellfun('isempty', savedoutput));
for i=1:length(savedoutput)
if exist(sprintf('%s.%s', basename, lower(savedoutput{i})))
tmp = importdata(sprintf('%s.%s', basename, lower(savedoutput{i})));
data = tmp.data;
o.results.(savedoutput{i}) = dseries(data(:,2), o.y.init, savedoutput{i});
end
end
end
end
% Save results related to the OUTLIER command
if ~all(cellfun(@isempty, struct2cell(o.outlier)))
if ~isempty(o.outlier.save)
savedoutput = strsplit(o.outlier.save, {',' , '(' , ')' , ' '});
savedoutput = savedoutput(~cellfun('isempty', savedoutput));
for i=1:length(savedoutput)
if exist(sprintf('%s.%s', basename, lower(savedoutput{i})))
tmp = importdata(sprintf('%s.%s', basename, lower(savedoutput{i})));
data = tmp.data;
if lower(savedoutput{i}) == 'fts'
header = strjoin(tmp.textdata(1));
header = strsplit(header, {'\t'});
header = header(1,2:end);
o.results.(savedoutput{i}) = dseries(data(:,2:end), o.y.init, regexprep(header,'\(|\)',''));
elseif lower(savedoutput{i}) == 'oit'
header = strjoin(tmp.textdata(1,1));
header = strsplit(header, {'\t'});
header = header(1,4:end);
info = tmp.textdata(3:end,3);
for j = 1:numel(info)
flag = strjoin(info(j));
o.results.(savedoutput{i}).(['outlier_' num2str(j)]).type = flag(1:2);
o.results.(savedoutput{i}).(['outlier_' num2str(j)]).date(1,1) = str2double(flag(3:strfind(flag,'.')-1));
o.results.(savedoutput{i}).(['outlier_' num2str(j)]).date(1,2) = str2double(flag(strfind(flag,'.')+1:end));
o.results.(savedoutput{i}).(['outlier_' num2str(j)]).(header{1}) = data(j,1);% medrmse
o.results.(savedoutput{i}).(['outlier_' num2str(j)]).(header{2}) = data(j,2);% rmse
o.results.(savedoutput{i}).(['outlier_' num2str(j)]).(header{3}) = data(j,3);% t-stat
end
end
end
end
end
end
% Save results related to the SLIDINGSPANS command
if ~all(cellfun(@isempty, struct2cell(o.slidingspans)))
if ~isempty(o.slidingspans.save)
savedoutput = strsplit(o.slidingspans.save, {',' , '(' , ')' , ' '});
savedoutput = savedoutput(~cellfun('isempty', savedoutput));
for i=1:length(savedoutput)
if exist(sprintf('%s.%s', basename, lower(savedoutput{i})))
tmp = importdata(sprintf('%s.%s', basename, lower(savedoutput{i})));
data = tmp.data;
header = strjoin(tmp.textdata(1));
header = strsplit(header, {'\t'});
header = header(1,2:end);
% In this case, the initial date is not the start of the series provided
startdate = num2str(data(1,1));
startdate = dates(o.y.dates.freq,str2double(startdate(1:4)),str2double(startdate(5:6)));
o.results.(savedoutput{i}) = dseries(data(:,2:end),startdate, regexprep(header,'%','pct'));
end
end
end
end
% Save results related to the IDENTIFY command
if ~all(cellfun(@isempty, struct2cell(o.identify)))
if ~isempty(o.identify.save)
savedoutput = strsplit(o.identify.save, {',' , '(' , ')' , ' '});
savedoutput = savedoutput(~cellfun('isempty', savedoutput));
for i=1:length(savedoutput)
if exist(sprintf('%s.%s', basename, lower(savedoutput{i})))
tmp = importdata(sprintf('%s.%s', basename, lower(savedoutput{i})));
data = tmp.data;
header = strjoin(tmp.textdata(3,1));
header = strsplit(header, {'\t'});
header = header(1,2:end);
for j = 1:length(data)
for k = 1:numel(header)
o.results.(savedoutput{i}).(['lag' num2str(j)]).(strjoin(regexprep(header(k),'(\.||\-)',''))) = data(j,k+1);
end
end
end
end
end
end
% Save results related to the CHECK command
if ~all(cellfun(@isempty, struct2cell(o.check)))
if ~isempty(o.check.save)
savedoutput = strsplit(o.check.save, {',' , '(' , ')' , ' '});
savedoutput = savedoutput(~cellfun('isempty', savedoutput));
for i=1:length(savedoutput)
if exist(sprintf('%s.%s', basename, lower(savedoutput{i})))
% if "ac2" is selected and output is treated as a table/struct, there will be a
% dimension error. As long as there is no fix, ac2 is saved as text.
if lower(savedoutput{i}) == 'ac2'
o.results.(savedoutput{i}) = fileread(sprintf('%s.ac2', basename));
else
tmp = importdata(sprintf('%s.%s', basename, lower(savedoutput{i})));
data = tmp.data;
header = strjoin(tmp.textdata(1,1));
header = strsplit(header, {'\t'});
header = header(1,2:end);
for j = 1:length(data)
for k = 1:numel(header)
o.results.(savedoutput{i}).(['lag' num2str(j)]).(strjoin(regexprep(header(k),'(\.||\-)',''))) = data(j,k+1);
end
end
end
end
end
end
end
% Save results related to the FORCE command
if ~all(cellfun(@isempty, struct2cell(o.force)))
if ~isempty(o.force.save)
savedoutput = strsplit(o.force.save, {',' , '(' , ')' , ' '});
savedoutput = savedoutput(~cellfun('isempty', savedoutput));
for i=1:length(savedoutput)
if exist(sprintf('%s.%s', basename, lower(savedoutput{i})))
tmp = importdata(sprintf('%s.%s', basename, lower(savedoutput{i})));
data = tmp.data;
o.results.(savedoutput{i}) = dseries(data(:,2), o.y.init, savedoutput{i});
end
end
end
end
% Save results related to the SPECTRUM command
if ~all(cellfun(@isempty, struct2cell(o.spectrum)))
if ~isempty(o.spectrum.save)
savedoutput = strsplit(o.spectrum.save, {',' , '(' , ')' , ' '});
savedoutput = savedoutput(~cellfun('isempty', savedoutput));
for i=1:length(savedoutput)
if exist(sprintf('%s.%s', basename, lower(savedoutput{i})))
tmp = importdata(sprintf('%s.%s', basename, lower(savedoutput{i})));
data = tmp.data;
header = strjoin(tmp.textdata(1,1));
header = strsplit(header, {'\t'});
header = regexprep(header(2:end),'10','Ten');
for j = 1:numel(header)
o.results.(savedoutput{i}).(strjoin(regexprep(header(j),'(\*||\(||\))',''))) = data(:,j+1);
end
end
end
end
end
% Save results related to the SEATS command
if ~all(cellfun(@isempty, struct2cell(o.seats)))
if ~isempty(o.seats.save)
savedoutput = strsplit(o.seats.save, {',' , '(' , ')' , ' '});
savedoutput = savedoutput(~cellfun('isempty', savedoutput));
for i=1:length(savedoutput)
if exist(sprintf('%s.%s', basename, lower(savedoutput{i})))
tmp = importdata(sprintf('%s.%s', basename, lower(savedoutput{i})));
data = tmp.data;
o.results.(savedoutput{i}) = dseries(data(:,2), o.y.init, savedoutput{i});
end
end
end
o.results.tbs = fileread(sprintf('%s.tbs', basename));
end
% Save results related to the X11REGRESSION command
if ~all(cellfun(@isempty, struct2cell(o.x11regression)))
if ~isempty(o.x11regression.save)
savedoutput = strsplit(o.x11regression.save, {',' , '(' , ')' , ' '});
savedoutput = savedoutput(~cellfun('isempty', savedoutput));
for i=1:length(savedoutput)
if exist(sprintf('%s.%s', basename, lower(savedoutput{i})))
if lower(savedoutput{i}) == 'xrc'
o.results.out = fileread(sprintf('%s.xrc', basename));
else
tmp = importdata(sprintf('%s.%s', basename, lower(savedoutput{i})));
data = tmp.data;
o.results.(savedoutput{i}) = dseries(data(:,2), o.y.init, savedoutput{i});
end
end
end
end
end
% Save results related to the ESTIMATE command
if ~all(cellfun(@isempty, struct2cell(o.estimate)))
if ~isempty(o.estimate.save)
savedoutput = strsplit(o.estimate.save, {',' , '(' , ')' , ' '});
savedoutput = savedoutput(~cellfun('isempty', savedoutput));
for i = 1:length(savedoutput)
if exist(sprintf('%s.%s', basename, lower(savedoutput{i})))
% The .est file cannot be read straightforwardly using
% importdata, so it is treated separately:
if lower(savedoutput{i}) == 'est'
fid = fopen(sprintf('%s.%s', basename, lower(savedoutput{i})));
fid2 = fopen(sprintf('%s.estx', basename), 'w');
r = 1;
s = 0;
while s<=1
a = fgetl(fid);
if a ~= -1
output{r,1} = a;
fprintf(fid2,[a '\n']);
else
s = s+1;
end
r = r+1;
end
o.results.(savedoutput{i}) = fileread(sprintf('%s.estx', basename));
else
tmp = importdata(sprintf('%s.%s', basename, lower(savedoutput{i})));
data = tmp.data;
end
if lower(savedoutput{i}) == 'lks'
for j = 1:numel(tmp.textdata)
o.results.(savedoutput{i}).(tmp.textdata{j}) = data(j);
end
elseif lower(savedoutput{i}) == 'ref'
header = strjoin(tmp.textdata(1,1));
header = strsplit(header, {'\t'});
header = regexprep(header(1,2:end),'( ||\-)','');
for j = 1:numel(header)
o.results.(savedoutput{i}).(header{j}) = dseries(data(:,j+1), o.y.init, header(j));
end
elseif lower(savedoutput{i}) == 'rrs'
o.results.(savedoutput{i}) = dseries(data(:,2), o.y.init, savedoutput{i});
elseif lower(savedoutput{i}) == 'rsd'
o.results.(savedoutput{i}) = dseries(data(:,2), o.y.init, savedoutput{i});
elseif lower(savedoutput{i}) == 'rcm'
o.results.(savedoutput{i}) = fileread(sprintf('%s.rcm', basename));
elseif lower(savedoutput{i}) == 'acm'
o.results.(savedoutput{i}) = fileread(sprintf('%s.acm', basename));
elseif lower(savedoutput{i}) == 'rts'
header = strjoin(tmp.textdata(1,1));
header = strsplit(header, {'\t'});
info = tmp.textdata(3:end,1:2);
for j = 1:rows(data)
for k = 1:columns(info)
o.results.(savedoutput{i}).(['root_' num2str(j)]).(header{k}) = strjoin(info(j,k));
end
for k = 1:columns(data)
o.results.(savedoutput{i}).(['root_' num2str(j)]).(header{k+columns(info)}) = data(j,k);
end
end
elseif lower(savedoutput{i}) == 'itr'
data = data(:,3:end);
header = strjoin(tmp.textdata(1,1));
header = strsplit(header, {'\t'});
header = header(3:end-1);
for j = 1:rows(data)
for k = 1:columns(header)
o.results.(savedoutput{i}).(['iter_' num2str(j)]).(header{k}) = data(j,k);
end
end
elseif lower(savedoutput{i}) == 'mdl'
o.results.(savedoutput{i}) = fileread(sprintf('%s.mdl', basename));
end
end
end
end
end
% Save results related to the HISTORY command
if ~all(cellfun(@isempty, struct2cell(o.history)))
if ~isempty(o.history.save)
savedoutput = strsplit(o.history.save, {',' , '(' , ')' , ' '});
savedoutput = savedoutput(~cellfun('isempty', savedoutput));
for i=1:length(savedoutput)
if exist(sprintf('%s.%s', basename, lower(savedoutput{i})))
% Prov
o.results.(savedoutput{i}) =fileread(sprintf('%s.%s', basename,lower(savedoutput{i})));
end
end
end
end
% Save main generated output file.
o.results.out = fileread(sprintf('%s.out', basename));
\ No newline at end of file
function val = subsasgn(val, idx, rhs) % --*-- Unitary tests --*--
function val = subsasgn(val, idx, rhs)
% Copyright (C) 2017 Dynare Team
%
......
function o = subsref(o, S) % --*-- Unitary tests --*--
function o = subsref(o, S)
% Overloads the subsref method.
% Copyright (C) 2017 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% This code 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,
% Dynare dseries submodule 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.
......@@ -22,24 +20,24 @@ function o = subsref(o, S) % --*-- Unitary tests --*--
switch S(1).type
case '.'
switch S(1).subs
case {'arima','automdl','regression','transform','outlier', 'forecast', 'check', 'x11', 'estimate'}
case {'x','y'}
if isequal(length(S), 1)
o = builtin('subsref', o, S(1));
else
if isequal(S(2).type,'.')
o = builtin('subsref', o.(S(1).subs), S(2));
end
end
case {'commands'}
o = builtin('subsref', o, S(1));
case {'arima','automdl','regression','transform','outlier', 'forecast', 'check', 'x11', 'estimate','composite',...
'force','history','metadata','identify','pickmdl','seats','slidingspans','spectrum','x11regression'}
if isequal(length(S), 1)
% Just print the member.
disp(o.(S(1).subs))
o = builtin('subsref', o, S(1));
else
if isequal(S(2).type,'()')
if ~ismember(S(1).subs, o.commands)
switch S(1).subs
case 'arima'
if ismember('automdl', o.commands)
error('x13:arima: ARIMA command is not compatible with AUTOMDL command!')
end
case 'automdl'
if ismember('arima', o.commands)
error('x13:automdl: AUTOMDL command is not compatible with ARIMA command!')
end
otherwise
end
checkcommandcompatibility(o, S(1).subs);
o.commands(end+1) = {S(1).subs};
end
if isempty(S(2).subs)
......@@ -53,11 +51,14 @@ switch S(1).type
for i=1:2:length(S(2).subs)
if isoption(S(1).subs, S(2).subs{i})
o.(S(1).subs) = setoption(o.(S(1).subs), S(2).subs{i}, S(2).subs{i+1});
checkoptioncompatibility(o);
else
disp(sprintf('Option %s is not available in block %s!', S(2).subs{i}, S(1).subs))
end
end
end
elseif isequal(S(2).type,'.')
o = builtin('subsref', o.(S(1).subs), S(2));
else
error('x13:%s: Wrong calling sequence!', S(1).subs)
end
......@@ -80,7 +81,13 @@ switch S(1).type
end
case 'results'
% Returns a structure with all the results.
o = o.results;
if isequal(length(S), 1)
o = builtin('subsref', o, S(1));
else
if isequal(S(2).type,'.')
o = builtin('subsref', o.(S(1).subs), S(2));
end
end
otherwise
error('x13:: I do not understand what you are asking for!')
end
......
classdef x13<handle % --*-- Unitary tests --*--
classdef x13<handle
% Class for X13 toolbox.
......@@ -18,30 +18,39 @@ classdef x13<handle % --*-- Unitary tests --*--
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
properties
y = []; % dseries object with a single variable.
x = []; % dseries object with an arbitrary number of variables (to be used in the REGRESSION block).
arima = []; % ARIMA model.
automdl = []; % ARIMA model selection.
regression = []; % Regression model.
estimate = []; % Estimation options.
transform = []; % Transform command applied to y.