Added new class for unordered dates (not ready).
parent
01bfd371d2
commit
8c66d4b86a
|
@ -0,0 +1,198 @@
|
|||
function dd = dynDates(varargin)
|
||||
|
||||
%@info:
|
||||
%! @deftypefn {Function File} {@var{dd} =} dynDates (@var{a},@var{b},...)
|
||||
%! @anchor{dynDates}
|
||||
%! @sp 1
|
||||
%! Constructor for the Dynare dates class (unordered sequence of dates).
|
||||
%! @sp 2
|
||||
%! @strong{Inputs}
|
||||
%! @sp 1
|
||||
%! @table @ @var
|
||||
%! @item a
|
||||
%! String, date.
|
||||
%! @item b
|
||||
%! String, date.
|
||||
%! @item c
|
||||
%! @end table
|
||||
%! @sp 2
|
||||
%! @strong{Outputs}
|
||||
%! @sp 1
|
||||
%! @table @ @var
|
||||
%! @item dd
|
||||
%! Dynare dates object.
|
||||
%! @end table
|
||||
%! @sp 1
|
||||
%! @strong{Properties}
|
||||
%! @sp 1
|
||||
%! The constructor defines the following properties:
|
||||
%! @sp 1
|
||||
%! @table @ @var
|
||||
%! @item ndate
|
||||
%! Scalar integer, the number of dates.
|
||||
%! @item freq
|
||||
%! Scalar integer, the frequency of the time series. @var{freq} is equal to 1 if data are on a yearly basis or if
|
||||
%! frequency is unspecified. @var{freq} is equal to 4 if data are on a quaterly basis. @var{freq} is equal to
|
||||
%! 12 if data are on a monthly basis. @var{freq} is equal to 52 if data are on a weekly basis.
|
||||
%! @item time
|
||||
%! Array of integers (nobs*2). The first column defines the years associated to each date. The second column,
|
||||
%! depending on the frequency, indicates the week, month or quarter numbers. For yearly data or unspecified frequency
|
||||
%! the second column is filled by ones.
|
||||
%! @end table
|
||||
%! @sp 2
|
||||
%! @strong{This function is called by:}
|
||||
%! @sp 2
|
||||
%! @strong{This function calls:}
|
||||
%!
|
||||
%! @end deftypefn
|
||||
%@eod:
|
||||
|
||||
% Copyright (C) 2011 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/>.
|
||||
|
||||
% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr
|
||||
|
||||
dd = struct;
|
||||
|
||||
dd.ndat = [];
|
||||
dd.freq = [];
|
||||
dd.time = dynTime();
|
||||
|
||||
dd = class(ts,'dynDates');
|
||||
|
||||
switch nargin
|
||||
case 0
|
||||
% Returns an empty object
|
||||
return
|
||||
case 1
|
||||
if isa(varargin{1},'dynDates')
|
||||
% Returns a copy of the input argument
|
||||
dd = varargin{1};
|
||||
elseif ischar(varargin{1})
|
||||
tmp = dynDate(varargin{1});
|
||||
dd.ndat = 1;
|
||||
dd.freq = tmp.freq;
|
||||
dd.time = tmp.time;
|
||||
else
|
||||
error('dynDates:: Wrong calling sequence of the constructor!')
|
||||
end
|
||||
otherwise
|
||||
for i=
|
||||
|
||||
end
|
||||
if nargin==2
|
||||
c = [];
|
||||
d = [];
|
||||
end
|
||||
% Get data, number of observations and number of variables.
|
||||
ts.data = a;
|
||||
ts.nobs = size(a,1);
|
||||
ts.vobs = size(a,2);
|
||||
% Get the first date and set the frequency.
|
||||
if ~isempty(b)
|
||||
if ischar(b)% Weekly, Monthly or Quaterly data.
|
||||
quaterly = findstr('Q',b);
|
||||
monthly = findstr('M',b);
|
||||
weekly = findstr('W',b);
|
||||
if ~isempty(quaterly)
|
||||
ts.freq = 4;
|
||||
end
|
||||
if ~isempty(monthly)
|
||||
ts.freq = 12;
|
||||
end
|
||||
if ~isempty(weekly)
|
||||
ts.freq = 52;
|
||||
end
|
||||
if isempty(quaterly) && isempty(monthly) && isempty(weekly)
|
||||
error('dynSeries:: Using a string as a second input argument, I can only handle weekly (W), monthly (M) or quaterly (Q) data!');
|
||||
end
|
||||
else% If b is not a string then yearly data are assumed.
|
||||
ts.freq = 1;
|
||||
end
|
||||
ts.Time = ts.Time.setFreq(ts.freq);
|
||||
ts.Time = ts.Time.setTime(dynDate(b):dynDate(b)+ts.nobs);
|
||||
else% If b is empty.
|
||||
ts.freq = 1;
|
||||
ts.Time = ts.Time.setFreq(1);
|
||||
ts.Time = ts.Time.setTime([transpose(1:ts.nobs) ones(ts.nobs,1)]);
|
||||
end
|
||||
% Get the names of the variables.
|
||||
if ~isempty(c)
|
||||
if ts.vobs==size(c,1)
|
||||
ts.name = c;
|
||||
else
|
||||
error('dynSeries:: The number of declared names does not match the number of variables!')
|
||||
end
|
||||
else
|
||||
for i=1:ts.vobs
|
||||
ts.name = char(ts.name,'--NA--');
|
||||
end
|
||||
end
|
||||
if ~isempty(d)
|
||||
if ts.vobs==size(d,1)
|
||||
ts.tex = d;
|
||||
else
|
||||
error('dynSeries:: The number of declared tex names does not match the number of variables!')
|
||||
end
|
||||
else
|
||||
for i=1:ts.vobs
|
||||
ts.tex = char(ts.tex,'--NA--');
|
||||
end
|
||||
end
|
||||
otherwise
|
||||
error('dynSeries:: Can''t instantiate the class, wrong calling sequence!')
|
||||
end
|
||||
|
||||
|
||||
%@test:1
|
||||
%$ addpath ../matlab
|
||||
%$ % Define a data set.
|
||||
%$ A = transpose(1:10);
|
||||
%$
|
||||
%$ % Define initial date
|
||||
%$ B1 = 1950;
|
||||
%$ B2 = '1950Q2';
|
||||
%$ B3 = '1950M10';
|
||||
%$ B4 = '1950W50';
|
||||
%$
|
||||
%$ % Define expected results.
|
||||
%$ e1.Time = transpose([1950 1951 1952 1953 1954 1955 1956 1957 1958 1959]);
|
||||
%$ e1.freq = 1;
|
||||
%$ e2.Time = char('1950Q2','1950Q3','1950Q4','1951Q1','1951Q2','1951Q3','1951Q4','1952Q1','1952Q2','1952Q3');
|
||||
%$ e2.freq = 4;
|
||||
%$ e3.Time = char('1950M10','1950M11','1950M12','1951M1','1951M2','1951M3','1951M4','1951M5','1951M6','1951M7');
|
||||
%$ e3.freq = 12;
|
||||
%$ e4.Time = char('1950W50','1950W51','1950W52','1951W1','1951W2','1951W3','1951W4','1951W5','1951W6','1951W7');
|
||||
%$ e4.freq = 52;
|
||||
%$
|
||||
%$ % Call the tested routine.
|
||||
%$ ts1 = dynSeries(A,B1);
|
||||
%$ ts2 = dynSeries(A,B2);
|
||||
%$ ts3 = dynSeries(A,B3);
|
||||
%$ ts4 = dynSeries(A,B4);
|
||||
%$
|
||||
%$ % Check the results.
|
||||
%$ t(1) = dyn_assert(getTime(ts1),e1.Time);
|
||||
%$ t(2) = dyn_assert(getTime(ts2),e2.Time);
|
||||
%$ t(3) = dyn_assert(getTime(ts3),e3.Time);
|
||||
%$ t(4) = dyn_assert(getTime(ts4),e4.Time);
|
||||
%$ t(5) = dyn_assert(ts1.freq,e1.freq);
|
||||
%$ t(6) = dyn_assert(ts2.freq,e2.freq);
|
||||
%$ t(7) = dyn_assert(ts3.freq,e3.freq);
|
||||
%$ t(8) = dyn_assert(ts4.freq,e4.freq);
|
||||
%$ T = all(t);
|
||||
%@eof:1
|
Loading…
Reference in New Issue