Verified Commit 677c1e18 authored by Stéphane Adjemian's avatar Stéphane Adjemian
Browse files

Cosmetic changes.

parent 5a198932
...@@ -2,7 +2,7 @@ classdef dseries<handle % --*-- Unitary tests --*-- ...@@ -2,7 +2,7 @@ classdef dseries<handle % --*-- Unitary tests --*--
% Class for time series. % Class for time series.
% Copyright (C) 2013-2017 Dynare Team % Copyright (C) 2013-2018 Dynare Team
% %
% This code 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 % it under the terms of the GNU General Public License as published by
......
function B = subsref(A, S) % --*-- Unitary tests --*-- function r = subsref(o, S) % --*-- Unitary tests --*--
%@info: % Overloads the subsref method for dseries class.
%! @deftypefn {Function File} {@var{us} =} subsref (@var{ts},S) %
%! @anchor{@dseries/subsref} % INPUTS
%! @sp 1 % - o [dseries] Time series object instantiated by dseries.
%! Overloads the subsref method for the Dynare time series class (@ref{dseries}). % - S [struct] Matlab's structure array S with two fields, type and subs. The type field is
%! @sp 2 % a string containing '()', '{}', or '.', where '()' specifies integer
%! @strong{Inputs} % subscripts, '{}' specifies cell array subscripts, and '.' specifies
%! @sp 1 % subscripted structure fields. The subs field is a cell array or a string
%! @table @ @var % containing the actual subscripts (see matlab's documentation).
%! @item ts %
%! Dynare time series object instantiated by @ref{dseries}. % OUTPUTS
%! @item S % - r [dseries] Depending on the calling sequence `r` is a transformation of `o` obtained
%! Matlab's structure array S with two fields, type and subs. The type field is string containing '()', '@{@}', or '.', where '()' specifies % by applying a public method on `o`, or a dseries object built by extracting
%! integer subscripts, '@{@}' specifies cell array subscripts, and '.' specifies subscripted structure fields. The subs field is a cell array % a variable from `o`, or a dseries object containing a subsample.
%! or a string containing the actual subscripts (see matlab's documentation).
%! @end table % Copyright (C) 2011-2018 Dynare Team
%! @sp 1
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item us
%! Dynare time series object. Depending on the calling sequence @var{us} is a transformation of @var{ts} obtained by applying a public method on @var{ts},
%! or a dseries object built by extracting a variable from @var{ts}, or a dseries object containing a subsample of the all the variable in @var{ts}.
%! @end table
%! @sp 2
%! @strong{Example 1.} Let @var{ts} be a dseries object containing three variables named 'A1', 'A2' and 'A3'. Then the following syntax:
%! @example
%! us = ts.A1;
%! @end example
%!will create a new dseries object @var{us} containing the variable 'A1'.
%! @sp 1
%! @strong{Example 2.} Let @var{ts} be a dseries object. Then the following syntax:
%! @example
%! us = ts.log;
%! @end example
%!will create a new dseries object @var{us} containing all the variables of @var{ts} transformed by the neperian logarithm.
%! @sp 1
%! @strong{Example 3.} Let @var{ts} be a dseries object. The following syntax:
%! @example
%! us = ts(3:50);
%! @end example
%!will create a new dseries object @var{us} by selecting a subsample out of @var{ts}.
%! @end deftypefn
%@eod:
% Copyright (C) 2011-2017 Dynare Team
% %
% This file is part of Dynare. % This file is part of Dynare.
% %
...@@ -69,37 +39,37 @@ switch S(1).type ...@@ -69,37 +39,37 @@ switch S(1).type
if length(S)>1 && isequal(S(2).type,'()') && isempty(S(2).subs) if length(S)>1 && isequal(S(2).type,'()') && isempty(S(2).subs)
error(['dseries::subsref: ' S(1).subs ' is not a method but a member!']) error(['dseries::subsref: ' S(1).subs ' is not a method but a member!'])
end end
B = builtin('subsref', A, S(1)); r = builtin('subsref', o, S(1));
case 'nobs' case 'nobs'
% Returns the number of observations. % Returns the number of observations.
B = rows(A.data); r = rows(o.data);
case 'vobs' case 'vobs'
% Returns the number of variables. % Returns the number of variables.
B = columns(A.data); r = columns(o.data);
case 'init' case 'init'
% Returns a dates object (first date). % Returns a dates object (first date).
B = A.dates(1); r = o.dates(1);
case 'last' case 'last'
% Returns a dates object (last date). % Returns a dates object (last date).
B = A.dates(end); r = o.dates(end);
case 'freq' case 'freq'
% Returns an integer characterizing the data frequency (1, 4, 12 or 52) % Returns an integer characterizing the data frequency (1, 4, 12 or 52)
B = A.dates.freq; r = o.dates.freq;
case 'length' case 'length'
error(['dseries::subsref: we do not support the length operator on ' ... error(['dseries::subsref: we do not support the length operator on ' ...
'dseries. Please use ''nobs'' or ''vobs''']); 'dseries. Please use ''nobs'' or ''vobs''']);
case 'save' case 'save'
% Save dseries object on disk (default is a mat file). % Save dseries object on disk (default is a mat file).
B = NaN; r = NaN;
if isequal(length(S),2) if isequal(length(S),2)
if strcmp(S(2).type,'()') if strcmp(S(2).type,'()')
if isempty(S(2).subs) if isempty(S(2).subs)
save(A); save(o);
else else
if isempty(S(2).subs{1}) if isempty(S(2).subs{1})
save(A,'',S(2).subs{2}); save(o,'',S(2).subs{2});
else else
save(A,S(2).subs{:}); save(o,S(2).subs{:});
end end
end end
S = shiftS(S,1); S = shiftS(S,1);
...@@ -107,12 +77,12 @@ switch S(1).type ...@@ -107,12 +77,12 @@ switch S(1).type
error('dseries::subsref: Wrong syntax.') error('dseries::subsref: Wrong syntax.')
end end
elseif isequal(length(S),1) elseif isequal(length(S),1)
save(A); save(o);
else else
error('dseries::subsref: Call to save method must come in last position!') error('dseries::subsref: Call to save method must come in last position!')
end end
case 'struct' case 'struct'
B = dseries2struct(A); r = dseries2struct(o);
case {'baxter_king_filter', 'baxter_king_filter_', ... case {'baxter_king_filter', 'baxter_king_filter_', ...
'cumsum','cumsum_', ... 'cumsum','cumsum_', ...
'insert', ... 'insert', ...
...@@ -149,48 +119,48 @@ switch S(1).type ...@@ -149,48 +119,48 @@ switch S(1).type
'resetops', 'resettags'} 'resetops', 'resettags'}
if length(S)>1 && isequal(S(2).type,'()') if length(S)>1 && isequal(S(2).type,'()')
if isempty(S(2).subs) if isempty(S(2).subs)
B = feval(S(1).subs,A); r = feval(S(1).subs,o);
S = shiftS(S,1); S = shiftS(S,1);
else else
B = feval(S(1).subs,A,S(2).subs{:}); r = feval(S(1).subs,o,S(2).subs{:});
S = shiftS(S,1); S = shiftS(S,1);
end end
else else
B = feval(S(1).subs,A); r = feval(S(1).subs,o);
end end
case 'size' case 'size'
if isequal(length(S),2) && strcmp(S(2).type,'()') if isequal(length(S),2) && strcmp(S(2).type,'()')
if isempty(S(2).subs) if isempty(S(2).subs)
[x,y] = size(A); [x,y] = size(o);
B = [x, y]; r = [x, y];
else else
B = size(A,S(2).subs{1}); r = size(o,S(2).subs{1});
end end
S = shiftS(S,1); S = shiftS(S,1);
elseif isequal(length(S),1) elseif isequal(length(S),1)
[x,y] = size(A); [x,y] = size(o);
B = [x, y]; r = [x, y];
else else
error('dseries::subsref: Call to size method must come in last position!') error('dseries::subsref: Call to size method must come in last position!')
end end
case {'set_names','rename','rename_','tex_rename','tex_rename_', 'tag'} case {'set_names','rename','rename_','tex_rename','tex_rename_', 'tag'}
B = feval(S(1).subs,A,S(2).subs{:}); r = feval(S(1).subs,o,S(2).subs{:});
S = shiftS(S,1); S = shiftS(S,1);
case {'disp'} case {'disp'}
feval(S(1).subs,A); feval(S(1).subs,o);
return return
otherwise % Extract a sub-object by selecting one variable. otherwise % Extract a sub-object by selecting one variable.
ndx = find(strcmp(S(1).subs,A.name)); ndx = find(strcmp(S(1).subs,o.name));
if ~isempty(ndx) if ~isempty(ndx)
B = dseries(); r = dseries();
B.data = A.data(:,ndx); r.data = o.data(:,ndx);
B.name = A.name(ndx); r.name = o.name(ndx);
B.tex = A.tex(ndx); r.tex = o.tex(ndx);
B.dates = A.dates; r.dates = o.dates;
B.ops = A.ops(ndx); r.ops = o.ops(ndx);
tagnames = fieldnames(A.tags); tagnames = fieldnames(o.tags);
for i=1:length(tagnames) for i=1:length(tagnames)
B.tags.(tagnames{i}) = A.tags.(tagnames{i})(ndx); r.tags.(tagnames{i}) = o.tags.(tagnames{i})(ndx);
end end
else else
error('dseries::subsref: Unknown public method, public member or variable!') error('dseries::subsref: Unknown public method, public member or variable!')
...@@ -199,9 +169,9 @@ switch S(1).type ...@@ -199,9 +169,9 @@ switch S(1).type
case '()' case '()'
if ischar(S(1).subs{1}) && ~isdate(S(1).subs{1}) if ischar(S(1).subs{1}) && ~isdate(S(1).subs{1})
% If ts is an empty dseries object, populate this object by reading data in a file. % If ts is an empty dseries object, populate this object by reading data in a file.
if isempty(A) if isempty(o)
if exist(S(1).subs{1}, 'file') if exist(S(1).subs{1}, 'file')
B = dseries(S(1).subs{1}); r = dseries(S(1).subs{1});
else else
error('dseries::subsref: Cannot find file %s', S(1).subs{1}) error('dseries::subsref: Cannot find file %s', S(1).subs{1})
end end
...@@ -211,12 +181,12 @@ switch S(1).type ...@@ -211,12 +181,12 @@ switch S(1).type
elseif isscalar(S(1).subs{1}) && isnumeric(S(1).subs{1}) && isint(S(1).subs{1}) elseif isscalar(S(1).subs{1}) && isnumeric(S(1).subs{1}) && isint(S(1).subs{1})
% Input is also interpreted as a backward/forward operator % Input is also interpreted as a backward/forward operator
if S(1).subs{1}>0 if S(1).subs{1}>0
B = feval('lead', A, S(1).subs{1}); r = feval('lead', o, S(1).subs{1});
elseif S(1).subs{1}<0 elseif S(1).subs{1}<0
B = feval('lag', A, -S(1).subs{1}); r = feval('lag', o, -S(1).subs{1});
else else
% Do nothing. % Do nothing.
B = A; r = o;
end end
elseif isdates(S(1).subs{1}) || isdate(S(1).subs{1}) elseif isdates(S(1).subs{1}) || isdate(S(1).subs{1})
% Select observation(s) with date(s) % Select observation(s) with date(s)
...@@ -226,34 +196,34 @@ switch S(1).type ...@@ -226,34 +196,34 @@ switch S(1).type
Dates = S(1).subs{1}; Dates = S(1).subs{1};
end end
% Test if Dates is out of bounds % Test if Dates is out of bounds
if min(Dates)<min(A.dates) if min(Dates)<min(o.dates)
error(['dseries::subsref: Indices are out of bounds! Subsample cannot start before ' date2string(A.dates(1)) '.']) error(['dseries::subsref: Indices are out of bounds! Subsample cannot start before ' date2string(o.dates(1)) '.'])
end end
if max(Dates)>max(A.dates) if max(Dates)>max(o.dates)
error(['dseries::subsref: Indices are out of bounds! Subsample cannot end after ' date2string(A.dates(end)) '.']) error(['dseries::subsref: Indices are out of bounds! Subsample cannot end after ' date2string(o.dates(end)) '.'])
end end
% Extract a subsample using a dates object % Extract a subsample using a dates object
[~,tdx] = intersect(A.dates.time,Dates.time,'rows'); [~,tdx] = intersect(o.dates.time,Dates.time,'rows');
B = copy(A); r = copy(o);
B.data = B.data(tdx,:); r.data = r.data(tdx,:);
B.dates = B.dates(tdx); r.dates = r.dates(tdx);
elseif isnumeric(S(1).subs{1}) && isequal(ndims(S(1).subs{1}), 2) elseif isnumeric(S(1).subs{1}) && isequal(ndims(S(1).subs{1}), 2)
if isempty(A) if isempty(o)
% Populate an empty dseries object. % Populate an empty dseries object.
if isempty(A.dates) if isempty(o.dates)
B = copy(A); r = copy(o);
B.dates = dates('1Y'):dates('1Y')+(rows(S(1).subs{1})-1); r.dates = dates('1Y'):dates('1Y')+(rows(S(1).subs{1})-1);
B.data = S(1).subs{1}; r.data = S(1).subs{1};
B.name = default_name(columns(B.data)); r.name = default_name(columns(r.data));
B.tex = name2tex(B.name); r.tex = name2tex(r.name);
B.ops = cell(length(B.name), 1); r.ops = cell(length(r.name), 1);
else else
B = copy(A); r = copy(o);
B.dates = B.dates:B.dates+(rows(S(1).subs{1})-1); r.dates = r.dates:r.dates+(rows(S(1).subs{1})-1);
B.data = S(1).subs{1}; r.data = S(1).subs{1};
B.name = default_name(columns(B.data)); r.name = default_name(columns(r.data));
B.tex = name2tex(B.name); r.tex = name2tex(r.name);
B.ops = cell(length(B.name), 1); r.ops = cell(length(r.name), 1);
end end
else else
error('dseries::subsref: It is not possible to populate a non empty dseries object!'); error('dseries::subsref: It is not possible to populate a non empty dseries object!');
...@@ -263,21 +233,21 @@ switch S(1).type ...@@ -263,21 +233,21 @@ switch S(1).type
end end
case '{}' case '{}'
if iscellofchar(S(1).subs) if iscellofchar(S(1).subs)
B = extract(A,S(1).subs{:}); r = extract(o,S(1).subs{:});
elseif isequal(length(S(1).subs),1) && all(isint(S(1).subs{1})) elseif isequal(length(S(1).subs),1) && all(isint(S(1).subs{1}))
idx = S(1).subs{1}; idx = S(1).subs{1};
if max(idx)>size(A.data,2) || min(idx)<1 if max(idx)>size(o.data,2) || min(idx)<1
error('dseries::subsref: Indices are out of bounds!') error('dseries::subsref: Indices are out of bounds!')
end end
B = dseries(); r = dseries();
B.data = A.data(:,idx); r.data = o.data(:,idx);
B.name = A.name(idx); r.name = o.name(idx);
B.tex = A.tex(idx); r.tex = o.tex(idx);
B.dates = A.dates; r.dates = o.dates;
B.ops = A.ops(idx); r.ops = o.ops(idx);
tagnames = fieldnames(A.tags); tagnames = fieldnames(o.tags);
for i=1:length(tagnames) for i=1:length(tagnames)
B.tags.(tagnames{i}) = A.tags.(tagnames{i})(idx); r.tags.(tagnames{i}) = o.tags.(tagnames{i})(idx);
end end
else else
error('dseries::subsref: What the Hell are you tryin'' to do?!') error('dseries::subsref: What the Hell are you tryin'' to do?!')
...@@ -288,7 +258,7 @@ end ...@@ -288,7 +258,7 @@ end
S = shiftS(S,1); S = shiftS(S,1);
if ~isempty(S) if ~isempty(S)
B = subsref(B, S); r = subsref(r, S);
end end
return return
......
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