Added the possibility to pass a range of dates to the dseries constructor.

time-shift
Stéphane Adjemian (Scylla) 2014-03-22 11:23:46 +01:00
parent 41295ad602
commit 9712f4abe6
2 changed files with 96 additions and 17 deletions

View File

@ -9552,8 +9552,9 @@ A @code{nobs} by @code{vobs} array of doubles, the data.
@deftypefn {dseries} dseries ()
@deftypefnx {dseries} dseries (@var{INITIAL_DATE})
@deftypefnx {dseries} dseries (@var{RANGE_OF_DATES})
Instantiates an empty @dseries object, with, if defined, an initial date given by the single element @dates object @var{INITIAL_DATE} (the frequency is then set accordingly).
Instantiates an empty @dseries object, with, if defined, an initial date given by the single element @dates object @var{INITIAL_DATE} or the first element of the @dates object @var{RANGE_OF_DATES} (the frequency is then set accordingly).
@end deftypefn
@ -9579,8 +9580,9 @@ If a @file{.mat} file is used instead, it should provide the same informations.
@sp 1
@deftypefn {dseries} dseries (@var{DATA_MATRIX}[, @var{INITIAL_DATE}[, @var{LIST_OF_NAMES}[, @var{LIST_OF_TEX_NAMES}]]])
@deftypefn {dseries} dseries (@var{DATA_MATRIX}[, @var{RANGE_OF_DATES}[, @var{LIST_OF_NAMES}[, @var{LIST_OF_TEX_NAMES}]]])
If the data is not read from a file, it can be provided via a @math{T}x@math{N} matrix as the first argument to @code{dseries}' constructor, with @math{T} representing the number of observations on @math{N} variables. The optional second argument, @var{INITIAL_DATE}, can be either a @dates object representing the period of the first observation or a string which would be used to instantiate a @dates object. Its default value is @code{dates('1Y')}. The optional third argument, @var{LIST_OF_NAMES}, is a @math{N} by @math{1} cell of strings with one entry for each variable name. The default name associated with column @code{i} of @var{DATA_MATRIX} is @code{Variable_i}. The final argument, @var{LIST_OF_TEX_NAMES}, is a @math{N} by @math{1} cell of strings composed of the @LaTeX{} names associated with the variables. The default @LaTeX{} name associated with column @code{i} of @var{DATA_MATRIX} is @code{Variable\_i}.
If the data is not read from a file, it can be provided via a @math{T}x@math{N} matrix as the first argument to @code{dseries}' constructor, with @math{T} representing the number of observations on @math{N} variables. The optional second argument, @var{INITIAL_DATE}, can be either a @dates object representing the period of the first observation or a string which would be used to instantiate a @dates object. Its default value is @code{dates('1Y')}. The optional third argument, @var{LIST_OF_NAMES}, is a @math{N} by @math{1} cell of strings with one entry for each variable name. The default name associated with column @code{i} of @var{DATA_MATRIX} is @code{Variable_i}. The final argument, @var{LIST_OF_TEX_NAMES}, is a @math{N} by @math{1} cell of strings composed of the @LaTeX{} names associated with the variables. The default @LaTeX{} name associated with column @code{i} of @var{DATA_MATRIX} is @code{Variable\_i}. If the optional second input argument is a range of dates, @dates object @var{RANGE_OF_DATES}, the number of rows in the first argument must match the number of elements @var{RANGE_OF_DATES} or be equal to one (in which case the single observation is replicated).
@end deftypefn

View File

@ -103,7 +103,10 @@ switch nargin
ts.init = varargin{1};
ts.freq = varargin{1}.freq;
otherwise
error(['dseries::dseries: Input ' inputname(1) ' (identified as a dates object) must have only one element!'])
% A range of dates is passed to the constructor
ts.dates = varargin{1};
ts.init = varargin{1}(1);
ts.freq = varargin{1}.freq;
end
return
elseif ischar(varargin{1})
@ -173,19 +176,37 @@ switch nargin
if isempty(b)
ts.freq = 1;
ts.init = dates(1,1);
else
if isdate(b)% Weekly, Monthly, Quaterly or Annual data.
ts.init = dates(b);
ts.freq = ts.init.freq;
elseif isdates(b) && isequal(length(b),1)
ts.freq = b.freq;
ts.init = b;
elseif isnumeric(b) && isscalar(b) && isint(b) % Yearly data.
ts.freq = 1;
ts.init = dates([num2str(b) 'Y']);
else
error('dseries::dseries: Wrong calling sequence!');
elseif (isdates(b) && isequal(length(b),1))
ts.freq = b.freq;
ts.init = b;
elseif isdate(b)% Weekly, Monthly, Quaterly or Annual data (string).
ts.init = dates(b);
ts.freq = ts.init.freq;
elseif (isnumeric(b) && isscalar(b) && isint(b)) % Yearly data.
ts.freq = 1;
ts.init = dates([num2str(b) 'Y']);
elseif isdates(b) % Range of dates
ts.freq = b.freq;
ts.init = b(1);
if ts.nobs>1 && ~isequal(b.ndat,ts.nobs)
message = 'dseries::dseries: If second input is a range, its number of elements must match ';
message = char(message, ' the number of rows in the first input, unless the first input');
message = char(message, ' has only one row.');
skipline()
disp(message);
error(' ');
elseif isequal(ts.nobs, 1)
ts.data = repmat(ts.data,b.ndat,1);
ts.nobs = b.ndat;
end
ts.dates = b;
elseif (isnumeric(b) && isint(b)) % Range of yearly dates.
message = 'dseries::dseries: Not implemented! If you need to define a range of years';
message = char(message, ' you have to pass a dates object as the second input argument.');
disp(message)
error(' ')
else
error('dseries::dseries: Wrong calling sequence!');
end
% Get the names of the variables.
if ~isempty(c)
@ -214,7 +235,9 @@ switch nargin
error('dseries::dseries: Can''t instantiate the class, wrong calling sequence!')
end
ts.dates = ts.init:ts.init+(ts.nobs-1);
if isempty(ts.dates)
ts.dates = ts.init:ts.init+(ts.nobs-1);
end
%@test:1
%$ % Test if we can instantiate an empty dseries object.
@ -464,4 +487,58 @@ ts.dates = ts.init:ts.init+(ts.nobs-1);
%$ end
%$
%$ T = all(t);
%@eof:12
%@eof:12
%@test:13
%$ t = zeros(6,1);
%$
%$ try
%$ ts = dseries(transpose(1:4),dates('1990Q1'):dates('1990Q4'));
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if length(t)>1
%$ t(2) = dyn_assert(ts.freq,4);
%$ t(3) = dyn_assert(ts.init.freq,4);
%$ t(4) = dyn_assert(ts.init.time,[1990, 1]);
%$ t(5) = dyn_assert(ts.vobs,1);
%$ t(6) = dyn_assert(ts.nobs,4);
%$ end
%$
%$ T = all(t);
%@eof:13
%@test:14
%$ t = zeros(7,1);
%$
%$ try
%$ ts = dseries([1, 2],dates('1990Q1'):dates('1990Q4'));
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if length(t)>1
%$ t(2) = dyn_assert(ts.freq,4);
%$ t(3) = dyn_assert(ts.init.freq,4);
%$ t(4) = dyn_assert(ts.init.time,[1990, 1]);
%$ t(5) = dyn_assert(ts.vobs,2);
%$ t(6) = dyn_assert(ts.nobs,4);
%$ t(7) = dyn_assert(ts.data, [ones(4,1), 2*ones(4,1)]);
%$ end
%$
%$ T = all(t);
%@eof:14
%@test:15
%$ try
%$ ts = dseries([1; 2],dates('1990Q1'):dates('1990Q4'));
%$ t = 0;
%$ catch
%$ t = 1;
%$ end
%$
%$ T = all(t);
%@eof:15