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

Added dynDates class.

parent b923ccd4
function sp = colon(a,b)
%@info:
%! @deftypefn {Function File} {@var{sp} =} colon (@var{a},@var{b})
%! @anchor{@dynDates/colon}
%! @sp 1
%! Overloads the colon operator for the Dynare Dates class (@ref{dynDates}). Creates a @ref{dynTime} object.
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
%! @item a
%! Dynare date object instantiated by @ref{dynDates}, initial date.
%! @item b
%! Dynare date object instantiated by @ref{dynDates}, last date.
%! @end table
%! @sp 1
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item c
%! Dynare Time object instantiated by @ref{dynTime}.
%! @end table
%! @sp 2
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
%! @ref{dynTime}, @ref{@@dynTime/setFreq}, @ref{@@dynTime/setTime}, @ref{@@dynTime/setSize}
%!
%! @end deftypefn
%@eod:
% Copyright (C) 2011 Dynare Team
% stephane DOT adjemian AT univ DASH lemans DOT fr
%
% 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
error('dynTime::colon: I need exactly two input arguments!')
end
if ~( isa(a,'dynDates') && isa(b,'dynDates'))
error(['dynTime::colon: Input arguments ' inputname(1) 'and ' inputname(2) ' have to be a dynDates objects!'])
end
if a.freq~=b.freq
error(['dynTime::colon: Input arguments ' inputname(1) 'and ' inputname(2) ' must have common frequency!'])
end
if a>b
error(['dynTime::colon: ' inputname(1) ' must precede ' inputname(2) '!' ])
end
if a==b% Time range with only one date.
sp = dynTime();
sp = sp.setFreq(a.freq);
sp = sp.setSize(n+1);
sp = sp.setTime(1,a.time);
else
n = b-a;
sp = dynTime();
sp = sp.setFreq(a.freq);
sp = sp.setSize(n+1);
sp = sp.setTime(1,a.time);
for t=2:n+1
a = +a;
sp = sp.setTime(t,a.time);
end
end
%@test:1
%$ addpath ../matlab
%$
%$ % Define two dates
%$ date_1 = '1950Q2';
%$ date_2 = '1951Q4';
%$
%$ % Define expected results.
%$ e.freq = 4;
%$ e.time = [1950 2; 1950 3; 1950 4; 1951 1; 1951 2; 1951 3; 1951 4];
%$
%$ % Call the tested routine.
%$ d1 = dynDates(date_1);
%$ d2 = dynDates(date_2);
%$ d3 = d1:d2;
%$
%$ % Check the results.
%$ t(1) = dyn_assert(d3.time,e.time);
%$ t(2) = dyn_assert(d3.freq,e.freq);
%$ T = all(t);
%@eof:1
function date = dynDates(a)
%@info:
%! @deftypefn {Function File} {@var{date} =} dynDate (@var{a})
%! @anchor{dynDates}
%! @sp 1
%! Constructor for the Dynare dates class.
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
%! @item a
%! Date. For Quaterly, Monthly or Weekly data, a must be a string. For yearly data or if the frequence is not
%! defined must be an integer. If @var{a} is a dynDates object, then date will be a copy of this object. If
%! the constructor is called without input argument, it will return an empty dynDates object.
%! @end table
%! @sp 1
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item date
%! Dynare date object.
%! @end table
%! @sp 1
%! @strong{Properties}
%! @sp 1
%! The constructor defines the following properties:
%! @sp 1
%! @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
%! Row vector of integers (1*2) indicating the year and the week, month or quarter of the first observation.
%! @end table
%! @sp 1
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
%! @ref{set_time}
%!
%! @end deftypefn
%@eod:
% Copyright (C) 2011 Dynare Team
% stephane DOT adjemian AT univ DASH lemans DOT fr
%
% 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/>.
date = struct;
date.freq = NaN;
date.time = NaN(1,2);
date = class(date,'dynDates');
switch nargin
case 0
return
case 1
if ischar(a)% Weekly, Monthly or Quaterly data.
quaterly = findstr('Q',a);
monthly = findstr('M',a);
weekly = findstr('W',a);
if ~isempty(quaterly)
date.freq = 4;
date.time(1) = str2num(a(1:quaterly-1));
date.time(2) = str2num(a(quaterly+1:end));
end
if ~isempty(monthly)
date.freq = 12;
date.time(1) = str2num(a(1:monthly-1));
date.time(2) = str2num(a(monthly+1:end));
end
if ~isempty(weekly)
date.freq = 52;
date.time(1) = str2num(a(1:weekly-1));
date.time(2) = str2num(a(weekly+1:end));
end
if isempty(quaterly) && isempty(monthly) && isempty(weekly)
error('dynDates:: Using a string as an input argument, I can only handle weekly (W), monthly (M) or quaterly (Q) data!');
end
elseif isa(a,'dynDates') % If input argument is a dynDates object then do a copy.
date = a;
else% If b is not a string then yearly data are assumed.
date.freq = 1;
date.time(1) = a;
date.time(2) = 1;
end
otherwise
error('dynDates:: Can''t instantiate the class, wrong calling sequence!')
end
%@test:1
%$ addpath ../matlab
%$
%$ % Define some dates
%$ date_1 = 1950;
%$ date_2 = '1950Q2';
%$ date_3 = '1950M10';
%$ date_4 = '1950W50';
%$
%$ % Define expected results.
%$ e_date_1 = [1950 1];
%$ e_freq_1 = 1;
%$ e_date_2 = [1950 2];
%$ e_freq_2 = 4;
%$ e_date_3 = [1950 10];
%$ e_freq_3 = 12;
%$ e_date_4 = [1950 50];
%$ e_freq_4 = 52;
%$
%$ % Call the tested routine.
%$ d1 = dynDates(date_1);
%$ d2 = dynDates(date_2);
%$ d3 = dynDates(date_3);
%$ d4 = dynDates(date_4);
%$
%$ % Check the results.
%$ t(1) = dyn_assert(d1.time,e_date_1);
%$ t(2) = dyn_assert(d2.time,e_date_2);
%$ t(3) = dyn_assert(d3.time,e_date_3);
%$ t(4) = dyn_assert(d4.time,e_date_4);
%$ t(5) = dyn_assert(d1.freq,e_freq_1);
%$ t(6) = dyn_assert(d2.freq,e_freq_2);
%$ t(7) = dyn_assert(d3.freq,e_freq_3);
%$ t(8) = dyn_assert(d4.freq,e_freq_4);
%$ T = all(t);
%@eof:1
function c = eq(a,b)
%@info:
%! @deftypefn {Function File} {@var{c} =} eq (@var{a},@var{b})
%! @anchor{@dynDates/eq}
%! @sp 1
%! Overloads the eq (equal) operator for the Dynare dates class (@ref{dynDates}).
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
%! @item a
%! Dynare date object instantiated by @ref{dynDates}.
%! @item b
%! Dynare date object instantiated by @ref{dynDates}.
%! @end table
%! @sp 1
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item c
%! scalar integer equal to one if a==b, 0 otherwise.
%! @end table
%! @sp 2
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
%!
%! @end deftypefn
%@eod:
% Copyright (C) 2011 Dynare Team
% stephane DOT adjemian AT univ DASH lemans DOT fr
%
% 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/>.
verbose = 0;
if nargin~=2
error('dynDates::eq: I need exactly two input arguments!')
end
if ~( isa(a,'dynDates') && isa(b,'dynDates'))
error(['dynDates::eq: Input arguments ' inputname(1) 'and ' inputname(2) ' have to be a dynDates objects!'])
end
if verbose && a.freq~=b.freq
disp(['dynDates::eq: Input arguments ' inputname(1) 'and ' inputname(2) ' have no common frequencies!'])
c = 0;
end
c = isequal(a.time,b.time);
%@test:1
%$ addpath ../matlab
%$
%$ % Define some dates
%$ date_1 = 1950;
%$ date_2 = '1950Q2';
%$ date_3 = '1950M10';
%$ date_4 = '1950W50';
%$ date_5 = '1950W32';
%$
%$ % Call the tested routine.
%$ d1 = dynDates(date_1);
%$ d2 = dynDates(date_2);
%$ d3 = dynDates(date_3);
%$ d4 = dynDates(date_4);
%$ d5 = dynDates(date_5);
%$ i1 = (d1==d2);
%$ i2 = (d2==d2);
%$ i3 = (d4==d5);
%$
%$ % Check the results.
%$ t(1) = dyn_assert(i1,0);
%$ t(2) = dyn_assert(i2,1);
%$ t(3) = dyn_assert(i3,0);
%$ T = all(t);
%@eof:1
function p = format(date)
%@info:
%! @deftypefn {Function File} {@var{p} =} format (@var{date})
%! @anchor{@dynDates/format}
%! @sp 1
%! Produces a formatted date from a Dynare date object instantiated by @ref{dynDates}.
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
%! @item date
%! Dynare date object, instantiated by @ref{dynDates}.
%! @end table
%! @sp 1
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item p
%! A string containing the formatted date (for instance, '2000', '2000Q3', '2000M9' or '2000W43').
%! @end table
%! @sp 2
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
%!
%! @end deftypefn
%@eod:
% Copyright (C) 2011 Dynare Team
% stephane DOT adjemian AT univ DASH lemans DOT fr
%
% 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~=1
error('dynDates::format: I need exactly one input argument!')
end
if ~isa(date,'dynDates')
error(['dynDates::format: Input argument ' inputname(1) ' has to be a dynDates object!'])
end
switch date.freq
case 1
p = num2str(date.time(1));
case 4
p = [num2str(date.time(1)) 'Q' num2str(date.time(2))];
case 12
p = [num2str(date.time(1)) 'M' num2str(date.time(2))];
case 52
p = [num2str(date.time(1)) 'W' num2str(date.time(2))];
otherwise
error('dynDates::format: Unkonwn frequency!')
end
%@test:1
%$ addpath ../matlab
%$
%$ % Define some dates
%$ date_1 = 1950;
%$ date_2 = '1950Q2';
%$ date_3 = '1950M10';
%$ date_4 = '1950W50';
%$
%$ % Call the tested routine.
%$ d1 = dynDates(date_1); DATE_1 = format(d1);
%$ d2 = dynDates(date_2); DATE_2 = format(d2);
%$ d3 = dynDates(date_3); DATE_3 = format(d3);
%$ d4 = dynDates(date_4); DATE_4 = format(d4);
%$
%$ % Check the results.
%$ t(1) = dyn_assert(num2str(date_1),DATE_1);
%$ t(2) = dyn_assert(date_2,DATE_2);
%$ t(3) = dyn_assert(date_3,DATE_3);
%$ t(4) = dyn_assert(date_4,DATE_4);
%$ T = all(t);
%@eof:1
function c = ge(a,b)
%@info:
%! @deftypefn {Function File} {@var{c} =} ge (@var{a},@var{b})
%! @anchor{@dynDates/ge}
%! @sp 1
%! Overloads the ge (greater or equal) operator for the Dynare dates class (@ref{dynDates}).
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
%! @item a
%! Dynare date object instantiated by @ref{dynDates}.
%! @item b
%! Dynare date object instantiated by @ref{dynDates}.
%! @end table
%! @sp 1
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item c
%! scalar integer equal to one if a>=b, 0 otherwise.
%! @end table
%! @sp 2
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
%! @ref{@@dynDates/gt}, @ref{@@dynDates/eq}
%!
%! @end deftypefn
%@eod:
% Copyright (C) 2011 Dynare Team
% stephane DOT adjemian AT univ DASH lemans DOT fr
%
% 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 a>b
c=1;
else
if a==b
c=1;
else
c=0;
end
end
%@test:1
%$ addpath ../matlab
%$
%$ % Define some dates
%$ date_1 = '1950Q3';
%$ date_2 = '1950Q3';
%$ date_3 = '1950Q1';
%$ date_4 = '1949Q2';
%$
%$ % Call the tested routine.
%$ d1 = dynDates(date_1);
%$ d2 = dynDates(date_2);
%$ d3 = dynDates(date_3);
%$ d4 = dynDates(date_4);
%$ i1 = (d1>=d2);
%$ i2 = (d3>=d4);
%$ i3 = (d4>=d2);
%$ i4 = (d1>=d4);
%$
%$ % Check the results.
%$ t(1) = dyn_assert(i1,1);
%$ t(2) = dyn_assert(i2,1);
%$ t(3) = dyn_assert(i3,0);
%$ t(4) = dyn_assert(i4,1);
%$ T = all(t);
%@eof:1
function c = gt(a,b)
%@info:
%! @deftypefn {Function File} {@var{c} =} gt (@var{a},@var{b})
%! @anchor{@dynDates/gt}
%! @sp 1
%! Overloads the gt (greater than) operator for the Dynare dates class (@ref{dynDates}).
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
%! @item a
%! Dynare date object instantiated by @ref{dynDates}.
%! @item b
%! Dynare date object instantiated by @ref{dynDates}.
%! @end table
%! @sp 1
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item c
%! scalar integer equal to one if a>b, 0 otherwise.
%! @end table
%! @sp 2
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
%!
%! @end deftypefn
%@eod:
% Copyright (C) 2011 Dynare Team
% stephane DOT adjemian AT univ DASH lemans DOT fr
%
% 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/>.
verbose = 0;
if nargin~=2
error('dynDates::eq: I need exactly two input arguments!')
end
if ~( isa(a,'dynDates') && isa(b,'dynDates'))
error(['dynDates::eq: Input arguments ' inputname(1) 'and ' inputname(2) ' have to be a dynDates objects!'])
end
if verbose && a.freq~=b.freq
error(['dynDates::eq: Input arguments ' inputname(1) 'and ' inputname(2) ' have no common frequencies!'])
end