Added X13 (CENSUS-ARIMA) class.

parent 8c2ef8fb
function basename = print(o, basename)
% Prints spc file.
% Copyright (C) 2017 Dynare Team
%
% 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/>.
if nargin<2 || isempty(basename)
basename = randomstring(10);
end
fid = fopen(sprintf('%s.spc', basename), 'w');
fprintf(fid, '# File created on %s by Dynare.\n\n', datetime());
% Write SERIES block
fprintf(fid, 'series {\n');
fprintf(fid, ' title = "%s"\n', o.y.name{1});
printstart(fid, o.y.init);
fprintf(fid, ' period = %i\n', o.y.init.freq);
fprintf(fid, ' data = %s', sprintf(data2txt(o.y.data)));
fprintf(fid, '}\n\n');
% Write TRANSFORM block
if ~all(cellfun(@isempty, struct2cell(o.transform)))
optionnames = fieldnames(o.transform);
fprintf(fid, 'transform {\n');
for i=1:length(optionnames)
if ~isempty(o.transform.(optionnames{i}))
printoption(fid, optionnames{i}, o.transform.(optionnames{i}));
end
end
fprintf(fid, '}\n\n');
end
% Write REGRESSION block
if ~all(cellfun(@isempty, struct2cell(o.regression)))
optionnames = fieldnames(o.regression);
fprintf(fid, 'regression {\n');
for i=1:length(optionnames)
if ~isempty(o.regression.(optionnames{i}))
if isequal(optionnames{i}, 'user') % Write needed data to a file.
% Determine the set of needed data
conditionningvariables = strsplit(o.regression.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:regression: 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.regression.user);
% Print data statement.
fprintf(fid, ' data = %s\n', sprintf(data2txt(x.data)));
elseif isequal(optionnames{i}, 'start')
if ischar(o.regression.start)
if isdate(o.regression.start)
PERIOD = dates(o.regression.start)
else
error('x13:regression: Option start cannot be interpreted as a date!')
end
elseif isdates(o.regression.start)
PERIOD = o.regression.start;
else
error('x13:regression: Option start cannot be interpreted as a date!')
end
printstart(fid, PERIOD);
else
printoption(fid, optionnames{i}, o.regression.(optionnames{i}));
end
end
end
if isempty(o.regression.start)
fprintf(fid, ' start = %i.%i\n', o.x.init.year, o.x.init.subperiod);
end
fprintf(fid, '}\n\n');
end
% Write ARIMA block
if ~all(cellfun(@isempty, struct2cell(o.arima)))
optionnames = fieldnames(o.arima);
fprintf(fid, 'arima {\n');
for i=1:length(optionnames)
if ~isempty(o.arima.(optionnames{i}))
printoption(fid, optionnames{i}, o.arima.(optionnames{i}));
end
end
fprintf(fid, '}\n\n');
end
% Write OUTLIER block
if ~all(cellfun(@isempty, struct2cell(o.outlier)))
optionnames = fieldnames(o.outlier);
fprintf(fid, 'outlier {\n');
for i=1:length(optionnames)
if ~isempty(o.outlier.(optionnames{i}))
printoption(fid, optionnames{i}, o.outlier.(optionnames{i}));
end
end
fprintf(fid, '}\n\n');
end
% Write FORECAST block
if ~all(cellfun(@isempty, struct2cell(o.forecast)))
optionnames = fieldnames(o.forecast);
fprintf(fid, 'forecast {\n');
for i=1:length(optionnames)
if ~isempty(o.forecast.(optionnames{i}))
printoption(fid, optionnames{i}, o.forecast.(optionnames{i}));
end
end
fprintf(fid, '}\n\n');
end
% Write ESTIMATE block
if ~all(cellfun(@isempty, struct2cell(o.estimate)))
optionnames = fieldnames(o.estimate);
fprintf(fid, 'estimate {\n');
for i=1:length(optionnames)
if ~isempty(o.estimate.(optionnames{i}))
printoption(fid, optionnames{i}, o.estimate.(optionnames{i}));
end
end
fprintf(fid, '}\n\n');
end
% Write CHECK block
if ~all(cellfun(@isempty, struct2cell(o.check)))
optionnames = fieldnames(o.check);
fprintf(fid, 'check {\n');
for i=1:length(optionnames)
if ~isempty(o.check.(optionnames{i}))
printoption(fid, optionnames{i}, o.check.(optionnames{i}));
end
end
fprintf(fid, '}\n\n');
end
% Write X11 block
if ~all(cellfun(@isempty, struct2cell(o.x11)))
optionnames = fieldnames(o.x11);
fprintf(fid, 'x11 {\n');
for i=1:length(optionnames)
if ~isempty(o.x11.(optionnames{i}))
printoption(fid, optionnames{i}, o.x11.(optionnames{i}));
end
end
fprintf(fid, '}\n\n');
end
\ No newline at end of file
function run(o, basename)
% Runs x13 program and saves results.
% Copyright (C) 2017 Dynare Team
%
% 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/>.
% Print spc file.
basename = o.print();
% Run spc file.
system(sprintf('%s %s', select_x13_binary(), basename));
o.results.name = basename; % Base name of the generated files.
% Save results related to the REGRESSION command
if ~all(cellfun(@isempty, struct2cell(o.regression)))
if ~isempty(o.regression.save)
savedoutput = strsplit(o.regression.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 X11 command
if ~all(cellfun(@isempty, struct2cell(o.x11)))
if ~isempty(o.x11.save)
savedoutput = strsplit(o.x11.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 FORECAST command
if ~all(cellfun(@isempty, struct2cell(o.forecast)))
if ~isempty(o.forecast.save)
savedoutput = strsplit(o.forecast.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})));
name = strsplit(tmp.textdata{1},'\t');
name = name(2:end);
data = tmp.data(:,2:end);
o.results.(savedoutput{i}) = dseries(data, lastobservedperiod(o.y)+1, name);
end
end
end
end
% Save main generated output file.
o.results.out = fileread(sprintf('%s.out', basename))
% Delete all generated files.
delete([basename '.*']);
\ No newline at end of file
function val = subsasgn(val, idx, rhs) % --*-- Unitary tests --*--
% Copyright (C) 2017 Dynare Team
%
% 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 dates 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.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
error('Members of x13 class are private')
function o = subsref(o, S) % --*-- Unitary tests --*--
% 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
% 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/>.
switch S(1).type
case '.'
switch S(1).subs
case {'arima','regression','transform','outlier', 'forecast', 'check', 'x11', 'estimate'}
if isequal(length(S), 1)
% Just print the member.
disp(o.(S(1).subs))
else
if isequal(S(2).type,'()')
if isempty(S(2).subs)
% Reset the member to its default (empty).
o.(S(1).subs) = setdefaultmember(S(1).subs);
else
% Set options.
if mod(length(S(2).subs), 2)
error('x13:%s: Wrong calling sequence, number of input arguments has to be even!', S(1).subs)
end
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});
else
disp(sprintf('Option %s is not available in block %s!', S(2).subs{i}, S(1).subs))
end
end
end
else
error('x13:%s: Wrong calling sequence!', S(1).subs)
end
end
case {'print', 'run'}
if isequal(length(S), 1)
feval(S(1).subs, o);
%print(o);
elseif isequal(length(S), 2)
if isequal(S(2).type,'()')
if isempty(S(2).subs)
feval(S(1).subs, o);
%print(o);
else
feval(S(1).subs, o, S(2).subs{:});
%print(o, S(2).subs{1});
end
else
error('x13:: Wrong calling sequence!')
end
else
error('x13:: I expect no more than two input arguments!')
end
case 'results'
o = o.results;
otherwise
error('x13:: I do not understand what you are asking for!')
end
otherwise
error('x13:: I do not understand what you are asking for!')
end
\ No newline at end of file
classdef x13<handle % --*-- Unitary tests --*--
% Class for X13 toolbox.
% Copyright (C) 2017 Dynare Team
%
% 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 dates 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.
%
% You should have received a copy of the GNU General Public License
% 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.
regression = []; % Regression model.
estimate = []; % Estimation options.
transform = []; % Transform command applied to y.
outlier = []; % Outlier command.
forecast = []; % Forecast command.
check = []; % Check command.
x11 = []; % X11 cmmand
results = []; % Estimation results
end
methods
function o = x13(y, x)
% Constructor for the x13 class.
%
% INPUTS
% - y [dseries] Data.
%
% OUPUTS
% - o [x13] Empty object except for the data.
if ~nargin
o.y = dseries();
o.x = dseries();
o.arima = setdefaultmember('arima');
o.regression = setdefaultmember('regression');
o.estimate = setdefaultmember('estimate');
o.transform = setdefaultmember('transform');
o.outlier = setdefaultmember('outlier');
o.forecast = setdefaultmember('forecast');
o.check = setdefaultmember('check');
o.x11 = setdefaultmember('x11');
o.results = struct();
return
end
if isdseries(y)
if isequal(y.vobs, 1)
o.y = y;
else
error('x13:: Wrong input argument (a dseries object with a single variable is expected)!')
end
else
error('x13:: Wrong input argument (a dseries object is expected)!')
end
if nargin>1
if isdseries(x)
o.x = x;
else
error('x13:: Wrong input argument (a dseries object is expected)!')
end
end
% Initialize other members (they are empty initially and must be set by calling methods)
o.arima = setdefaultmember('arima');
o.regression = setdefaultmember('regression');
o.estimate = setdefaultmember('estimate');
o.transform = setdefaultmember('transform');
o.outlier = setdefaultmember('outlier');
o.forecast = setdefaultmember('forecast');
o.check = setdefaultmember('check');
o.x11 = setdefaultmember('x11');
o.results = struct();
end
end
end
\ No newline at end of file
function b = isoption(command, option)
% Copyright (C) 2017 Dynare Team
%
% 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 dates 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.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
switch command
case 'arima'
b = ismember(option, {'ar', 'ma', 'model', 'print', 'save', 'title'});
case 'regression'
b = ismember(option, {'aicdiff', 'aictest', 'chi2test', 'chi2testcv', ...
'print', 'save', 'pvaictest', 'savelog', 'start', ...
'tlimit', 'user', 'usertype', 'variables', 'b', ...
'centeruser', 'eastermeans', 'noapply', 'tcrate'});
case 'transform'
b = ismember(option, {'adjust', 'aicdiff', 'function', 'mode', 'name', ...
'power', 'precision', 'print', 'save', 'savelog', ...
'start', 'title', 'type'});
case 'estimate'
b = ismember(option, {'exact', 'maxiter', 'outofsample', 'print', 'save', 'savelog', 'tol', 'file', 'fix'});
case 'outlier'
b = ismember(option, {'critical', 'lsrun', 'method', 'print', 'save', ...
'savelog', 'span', 'types', 'almost', 'tcrate'});
case 'forecast'
b = ismember(option, {'exclude', 'lognormal', 'maxback', 'maxlead', 'print', 'save', 'probability'});
case 'check'
b = ismember(option, {'maxlag', 'print', 'save', 'qtype', 'savelog'});
case 'x11'
b = ismember(option, {'appendbcst', 'appendfcst', 'final', 'mode', 'print', 'save', ...
'savelog', 'seasonalma', 'sigmalim', 'title', 'trendma', 'type', ...
'calendarsigma', 'centerseasonal', 'keepholiday', 'print1stpass', ...
'sfshort', 'sigmavec', 'trendic', 'true7term'});
otherwise
error('x13:isoption: Unknown block!')
end
function printoption(fid, optname, optvalue)
% Copyright (C) 2017 Dynare Team
%
% 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 dates 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.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if ischar(optvalue)
fprintf(fid, ' %s = %s\n', optname, optvalue);
elseif isreal(optvalue)
if isequal(numel(optvalue), 1)
if isint(optvalue)
fprintf(fid, ' %s = %i\n', optname, optvalue);
else
fprintf(fid, ' %s = %f\n', optname, optvalue);
end
else
if isvector(optvalue)
str = '(';
for i=1:length(optvalue)
if isint(optvalue(i))
str = sprintf('%s %i', str, optvalue(i));
elseif isreal(optvalue(i))
str = sprintf('%s %f', str, optvalue(i));
else
error('This option value type is not implemented!');
end
str = sprintf('%s%s', str, ')');
end
else
error('This option value type is not implemented!');
end
end
end
\ No newline at end of file
function printstart(fid, period)
% Copyright (C) 2017 Dynare Team
%
% 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 dates 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.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
if ~ismember(period.freq, [1 4 12])
error('x13:printstart: Only monthly, quaterly or annual data are allowed (option start)!')
end
switch period.freq
case 12
ListOfMonths = {'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'};
fprintf(fid, ' start = %i.%s\n', period.year, ListOfMonths{period.subperiod});
case 4
fprintf(fid, ' start = %i.%i\n', period.year, period.subperiod);
case 1
fprintf(fid, ' start = %i\n', period.year);
otherwise
error('x13:regression: This is a bug! Please contact the authors.')
end
\ No newline at end of file
function s = setdefaultmember(name)
% Set members of X13 object to default values (empty).
% Copyright (C) 2017 Dynare Team
%
% 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 dates 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.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
switch name
case 'arima'
s = struct('ar', [], 'ma', [], 'model', [], 'print', [], 'save', [], 'title', []);
case 'regression'
s = struct('aicdiff', [], 'aictest', [], 'chi2test', [], 'chi2testcv', [], ...
'print', [], 'save', [], 'pvaictest', [], 'savelog', [], 'start', [], ...
'tlimit', [], 'user', [], 'usertype', [], 'variables', [], 'b', [], ...
'centeruser', [], 'eastermeans', [], 'noapply', [], 'tcrate', []);
case 'estimate'
s = struct('exact', [], 'maxiter', [], 'outofsample', [], 'print', [], 'save', [], 'savelog', ...
[], 'tol', [], 'file', [], 'fix', []);
case 'transform'
s = struct('adjust', [], 'aicdiff', [], 'function', [], 'mode', [], 'name', [], ...
'power', [], 'precision', [], 'print', [], 'save', [], 'savelog', [], ...
'start', [], 'title', [], 'type', []);
case 'outlier'
s= struct('critical', [], 'lsrun', [], 'method', [], 'print', [], 'save', [], ...
'savelog', [], 'span', [], 'types', [], 'almost', [], 'tcrate', []);
case 'forecast'
s = struct('exclude', [], 'lognormal', [], 'maxback', [], 'maxlead', [], 'print', [], ...
'save', [], 'probability', []);
case 'check'
s = struct('maxlag', [], 'print', [], 'save', [], 'qtype', [], 'savelog', []);
case 'x11'
s = struct('appendbcst', [], 'appendfcst', [], 'final', [], 'mode', [], 'print', [], 'save', [], ...
'savelog', [], 'seasonalma', [], 'sigmalim', [], 'title', [], 'trendma', [], 'type', [], ...
'calendarsigma', [], 'centerseasonal', [], 'keepholiday', [], 'print1stpass', [], ...
'sfshort', [], 'sigmavec', [], 'trendic', [], 'true7term', []);
otherwise
error('x13:setdefaultmember: Unknown member!')
end
function command = setoption(command, optname, optvalue)
% Copyright (C) 2017 Dynare Team
%
% 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 dates 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.
%
% 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(command, optname)
command.(optname) = optvalue;
else
disp(sprintf('Option %s is unknown!', optname))
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment