Added dynDates class.

time-shift
Stéphane Adjemian (Scylla) 2011-09-05 18:56:47 +02:00
parent b923ccd4e8
commit 1e04330fe8
14 changed files with 1418 additions and 0 deletions

104
matlab/@dynDates/colon.m Normal file
View File

@ -0,0 +1,104 @@
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

142
matlab/@dynDates/dynDates.m Normal file
View File

@ -0,0 +1,142 @@
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

93
matlab/@dynDates/eq.m Normal file
View File

@ -0,0 +1,93 @@
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

91
matlab/@dynDates/format.m Normal file
View File

@ -0,0 +1,91 @@
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

86
matlab/@dynDates/ge.m Normal file
View File

@ -0,0 +1,86 @@
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

103
matlab/@dynDates/gt.m Normal file
View File

@ -0,0 +1,103 @@
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
if a.time(1)>b.time(1)
c = 1;
elseif isequal(a.time(1),b.time(1))
if a.time(2)>b.time(2)
c = 1;
else
c = 0;
end
else
c = 0;
end
%@test:1
%$ addpath ../matlab
%$
%$ % Define some dates
%$ date_1 = 1950;
%$ date_2 = '1950Q2';
%$ date_3 = '1950Q3';
%$ date_4 = '1950Q1';
%$ date_5 = '1949Q2';
%$
%$ % 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 = (d2>d3);
%$ i2 = (d3>d4);
%$ i3 = (d4>d2);
%$ i4 = (d5>d4);
%$
%$ % Check the results.
%$ t(1) = dyn_assert(i1,0);
%$ t(2) = dyn_assert(i2,1);
%$ t(3) = dyn_assert(i3,0);
%$ t(4) = dyn_assert(i4,0);
%$ T = all(t);
%@eof:1

86
matlab/@dynDates/le.m Normal file
View File

@ -0,0 +1,86 @@
function c = le(a,b)
%@info:
%! @deftypefn {Function File} {@var{c} =} le (@var{a},@var{b})
%! @anchor{@dynDates/le}
%! @sp 1
%! Overloads the le (less 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/lt}, @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,0);
%$ t(3) = dyn_assert(i3,1);
%$ t(4) = dyn_assert(i4,0);
%$ T = all(t);
%@eof:1

103
matlab/@dynDates/lt.m Normal file
View File

@ -0,0 +1,103 @@
function c = lt(a,b)
%@info:
%! @deftypefn {Function File} {@var{c} =} lt (@var{a},@var{b})
%! @anchor{@dynDates/lt}
%! @sp 1
%! Overloads the lt (less 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
if a.time(1)<b.time(1)
c = 1;
elseif isequal(a.time(1),b.time(1))
if a.time(2)<b.time(2)
c = 1;
else
c = 0;
end
else
c = 0;
end
%@test:1
%$ addpath ../matlab
%$
%$ % Define some dates
%$ date_1 = 1950;
%$ date_2 = '1950Q2';
%$ date_3 = '1950Q3';
%$ date_4 = '1950Q1';
%$ date_5 = '1949Q2';
%$
%$ % 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 = (d2<d3);
%$ i2 = (d3<d4);
%$ i3 = (d4<d2);
%$ i4 = (d5<d4);
%$
%$ % Check the results.
%$ t(1) = dyn_assert(i1,1);
%$ t(2) = dyn_assert(i2,0);
%$ t(3) = dyn_assert(i3,1);
%$ t(4) = dyn_assert(i4,1);
%$ T = all(t);
%@eof:1

113
matlab/@dynDates/minus.m Normal file
View File

@ -0,0 +1,113 @@
function c = minus(a,b)
%@info:
%! @deftypefn {Function File} {@var{c} =} minus (@var{a},@var{b})
%! @anchor{@dynDates/minus}
%! @sp 1
%! Overloads the minus (soustraction) operator for the Dynare dates class (@ref{dynDates}). Depending on the frequency, computes the number
%! of years, quarters, months, weeks between two dates @var{a} and @var{b} (it is assumed that @var{a}>@var{B}).
%! @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
%! Integer scalar, the number of years, quarters, months or weeks between @var{a} and @var{B}.
%! @end table
%! @sp 2
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
%! @ref{@@dynDates/eq},@ref{@@dynDates/lt}
%!
%! @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 ~( isa(a,'dynDates') && isa(b,'dynDates') )
error(['dynDates::minus: Input arguments ' inputname(1) ' and ' inputname(2) ' must be dynDates objects!'])
end
if a.freq~=b.freq
error(['dynDates::minus: ' inputname(1) ' and ' inputname(2) ' must have common frequency!'])
end
if a<b
error(['dynDates::minus: ' inputname(1) ' must be posterior to ' inputname(2) '!'])
end
if a==b
c = 0;
return
end
switch a.freq
case 1
c = a.time(1)-b.time(1);
case {4,12,52}
c = a.time(2)-b.time(2) + (a.time(1)-b.time(1))*a.freq;
otherwise
error('dynDates::minus: Unknown frequency!')
end
%@test:1
%$ addpath ../matlab
%$
%$ % Define some dates
%$ date_0_1 = 1950;
%$ date_0_2 = 1950;
%$ date_0_3 = 1940;
%$ date_1_1 = '1950Q4';
%$ date_1_2 = '1950Q1';
%$ date_1_3 = '1940Q3';
%$ date_2_1 = '2000M3';
%$ date_2_2 = '1998M8';
%$
%$ % Call the tested routine.
%$ d_0_1 = dynDates(date_0_1);
%$ d_0_2 = dynDates(date_0_2);
%$ d_0_3 = dynDates(date_0_3);
%$ d_1_1 = dynDates(date_1_1);
%$ d_1_2 = dynDates(date_1_2);
%$ d_1_3 = dynDates(date_1_3);
%$ d_2_1 = dynDates(date_2_1);
%$ d_2_2 = dynDates(date_2_2);
%$ e1 = d_0_1-d_0_2;
%$ e2 = d_0_1-d_0_3;
%$ e3 = d_1_1-d_1_2;
%$ e4 = d_1_1-d_1_3;
%$ e5 = d_2_1-d_2_2;
%$
%$ % Check the results.
%$ t(1) = dyn_assert(e1,0);
%$ t(2) = dyn_assert(e2,10);
%$ t(3) = dyn_assert(e3,3);
%$ t(4) = dyn_assert(e4,41);
%$ t(4) = dyn_assert(e5,19);
%$ T = all(t);
%@eof:1

92
matlab/@dynDates/ne.m Normal file
View File

@ -0,0 +1,92 @@
function c = ne(a,b)
%@info:
%! @deftypefn {Function File} {@var{c} =} ne (@var{a},@var{b})
%! @anchor{@dynDates/ne}
%! @sp 1
%! Overloads the ne (not 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::ne: I need exactly two input arguments!')
end
if ~( isa(a,'dynDates') && isa(b,'dynDates'))
error(['dynDates::ne: Input arguments ' inputname(1) 'and ' inputname(2) ' have to be a dynDates objects!'])
end
if verbose && a.freq~=b.freq
disp(['dynDates::ne: Input arguments ' inputname(1) 'and ' inputname(2) ' have no common frequencies!'])
c = 1;
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,1);
%$ t(2) = dyn_assert(i2,0);
%$ t(3) = dyn_assert(i3,1);
%$ T = all(t);
%@eof:1

110
matlab/@dynDates/plus.m Normal file
View File

@ -0,0 +1,110 @@
function c = plus(a,b)
%@info:
%! @deftypefn {Function File} {@var{c} =} plus (@var{a},@var{b})
%! @anchor{@dynDates/plus}
%! @sp 1
%! Overloads the plus (addition) operator for the Dynare dates class (@ref{dynDates}). Given an initial date @var{a},
%! computes a new date @var{c} by adding the number of periods @var{b}.
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
%! @item a
%! Dynare date object instantiated by @ref{dynDates}.
%! @item b
%! Positive scalar integer, the number of periods
%! @end table
%! @sp 2
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item c
%! Dynare date object instantiated by @ref{dynDates}.
%! @end table
%! @sp 2
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
%! @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 ~isa(a,'dynDates')
error(['dynDates::plus: Input argument ' inputname(1) ' must be a dynDates object!'])
end
if b<0
error(['dynDates::plus: Input argument ' inputname(2) ' must be a positive integer'])
end
if b==0
c = a;
return
end
switch a.freq
case 1
c = a.time(1)-b.time(1);
case {4,12,52}
c = a.time(2)-b.time(2) + (a.time(1)-b.time(1))*a.freq;
otherwise
error('dynDates::minus: Unknown frequency!')
end
%@test:1
%$ addpath ../matlab
%$
%$ % Define some dates
%$ date_0_1 = 1950;
%$ date_0_2 = 1950;
%$ date_0_3 = 1940;
%$ date_1_1 = '1950Q4';
%$ date_1_2 = '1950Q1';
%$ date_1_3 = '1940Q3';
%$ date_2_1 = '2000M3';
%$ date_2_2 = '1998M8';
%$
%$ % Call the tested routine.
%$ d_0_1 = dynDates(date_0_1);
%$ d_0_2 = dynDates(date_0_2);
%$ d_0_3 = dynDates(date_0_3);
%$ d_1_1 = dynDates(date_1_1);
%$ d_1_2 = dynDates(date_1_2);
%$ d_1_3 = dynDates(date_1_3);
%$ d_2_1 = dynDates(date_2_1);
%$ d_2_2 = dynDates(date_2_2);
%$ e1 = d_0_1-d_0_2;
%$ e2 = d_0_1-d_0_3;
%$ e3 = d_1_1-d_1_2;
%$ e4 = d_1_1-d_1_3;
%$ e5 = d_2_1-d_2_2;
%$
%$ % Check the results.
%$ t(1) = dyn_assert(e1,0);
%$ t(2) = dyn_assert(e2,10);
%$ t(3) = dyn_assert(e3,3);
%$ t(4) = dyn_assert(e4,41);
%$ t(4) = dyn_assert(e5,19);
%$ T = all(t);
%@eof:1

View File

@ -0,0 +1,53 @@
function B = subsref(A,S)
%@info:
%! @deftypefn {Function File} {@var{us} =} subsref (@var{ts},S)
%! @anchor{@dynDates/subsref}
%! @sp 1
%! Overloads the subsref method 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 S
%! Matlab's structure array S with two fields, type and subs. The type field is string containing '()', '@{@}', or '.', where '()' specifies
%! integer subscripts, '@{@}' specifies cell array subscripts, and '.' specifies subscripted structure fields. The subs field is a cell array
%! or a string containing the actual subscripts (see matlab's documentation).
%! @end table
%! @sp 1
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item B
%! A matlab object (public member of the @ref{dynDates} object).
%! @end table
%! @sp 2
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
%! @sp2
%!
%! @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/>.
B = builtin('subsref', A, S);

121
matlab/@dynDates/uminus.m Normal file
View File

@ -0,0 +1,121 @@
function b = uminus(a)
%@info:
%! @deftypefn {Function File} {@var{b} =} uminus (@var{a})
%! @anchor{@dynDates/uplus}
%! @sp 1
%! Overloads the uminus (unary soustraction) operator for the Dynare dates class (@ref{dynDates}). Decrement the date by one year, quarter,
%! month or week depending on the frequency.
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
%! @item a
%! Dynare date object instantiated by @ref{dynDates}.
%! @end table
%! @sp 1
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item b
%! Dynare date object instantiated by @ref{dynDates}.
%! @end table
%! @sp 2
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
%! @ref{dynDates}
%!
%! @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 ~isa(a,'dynDates')
error(['dynDates::uminus: Input argument ' inputname(1) ' must be a dynDates object.'])
end
b = dynDates(a);
switch b.freq
case 1
b.time(1) = b.time(1)-1;
case 4
if b.time(2)==1
b.time(1) = b.time(1)-1;
b.time(2) = 4;
else
b.time(2) = b.time(2)-1;
end
case 12
if b.time(2)==1
b.time(1) = b.time(1)-1;
b.time(2) = 12;
else
b.time(2) = b.time(2)-1;
end
case 52
if b.time(2)==1
b.time(1) = b.time(1)-1;
b.time(2) = 52;
else
b.time(2) = b.time(2)-1;
end
otherwise
error('dynDates::minus: Unknown frequency!')
end
%@test:1
%$ addpath ../matlab
%$
%$ % Define some dates
%$ date_1 = '1950Q1';
%$ date_2 = '1950Q4';
%$ date_3 = '1950M1';
%$ date_4 = '1950M12';
%$ date_5 = '1950W1';
%$ date_6 = '1950W52';
%$ date_7 = 2000;
%$
%$ % Call the tested routine.
%$ d1 = dynDates(date_1);
%$ d2 = dynDates(date_2);
%$ d3 = dynDates(date_3);
%$ d4 = dynDates(date_4);
%$ d5 = dynDates(date_5);
%$ d6 = dynDates(date_6);
%$ d7 = dynDates(date_7);
%$ e1 = -d1;
%$ e2 = -d2;
%$ e3 = -d3;
%$ e4 = -d4;
%$ e5 = -d5;
%$ e6 = -d6;
%$ e7 = -d7;
%$
%$ % Check the results.
%$ t(1) = dyn_assert(e1.time,[1949 4]);
%$ t(2) = dyn_assert(e2.time,[1950 3]);
%$ t(3) = dyn_assert(e3.time,[1949 12]);
%$ t(4) = dyn_assert(e4.time,[1950 11]);
%$ t(5) = dyn_assert(e5.time,[1949 52]);
%$ t(6) = dyn_assert(e6.time,[1950 51]);
%$ t(7) = dyn_assert(e7.time,[1999 1]);
%$ T = all(t);
%@eof:1

121
matlab/@dynDates/uplus.m Normal file
View File

@ -0,0 +1,121 @@
function b = uplus(a)
%@info:
%! @deftypefn {Function File} {@var{b} =} uplus (@var{a})
%! @anchor{@dynDates/uplus}
%! @sp 1
%! Overloads the uplus (unary addition) operator for the Dynare dates class (@ref{dynDates}). Increment the date by one year, quarter,
%! month or week depending on the frequency.
%! @sp 2
%! @strong{Inputs}
%! @sp 1
%! @table @ @var
%! @item a
%! Dynare date object instantiated by @ref{dynDates}.
%! @end table
%! @sp 1
%! @strong{Outputs}
%! @sp 1
%! @table @ @var
%! @item b
%! Dynare date object instantiated by @ref{dynDates}.
%! @end table
%! @sp 2
%! @strong{This function is called by:}
%! @sp 2
%! @strong{This function calls:}
%! @ref{dynDates}
%!
%! @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 ~isa(a,'dynDates')
error(['dynDates::uplus: Input argument ' inputname(1) ' must be a dynDates object.'])
end
b = dynDates(a);
switch b.freq
case 1
b.time(1) = b.time(1)+1;
case 4
if b.time(2)==4% Happy new year!
b.time(1) = b.time(1)+1;
b.time(2) = 1;
else
b.time(2) = b.time(2)+1;
end
case 12
if b.time(2)==12% Happy new year!
b.time(1) = b.time(1)+1;
b.time(2) = 1;
else
b.time(2) = b.time(2)+1;
end
case 52
if b.time(2)==52% Happy new year!
b.time(1) = b.time(1)+1;
b.time(2) = 1;
else
b.time(2) = b.time(2)+1;
end
otherwise
error('dynDates::uplus: Unknown frequency!')
end
%@test:1
%$ addpath ../matlab
%$
%$ % Define some dates
%$ date_1 = '1950Q3';
%$ date_2 = '1950Q4';
%$ date_3 = '1950M3';
%$ date_4 = '1950M12';
%$ date_5 = '1950W3';
%$ date_6 = '1950W52';
%$ date_7 = 2000;
%$
%$ % Call the tested routine.
%$ d1 = dynDates(date_1);
%$ d2 = dynDates(date_2);
%$ d3 = dynDates(date_3);
%$ d4 = dynDates(date_4);
%$ d5 = dynDates(date_5);
%$ d6 = dynDates(date_6);
%$ d7 = dynDates(date_7);
%$ e1 = +d1;
%$ e2 = +d2;
%$ e3 = +d3;
%$ e4 = +d4;
%$ e5 = +d5;
%$ e6 = +d6;
%$ e7 = +d7;
%$
%$ % Check the results.
%$ t(1) = dyn_assert(e1.time,[1950 4]);
%$ t(2) = dyn_assert(e2.time,[1951 1]);
%$ t(3) = dyn_assert(e3.time,[1950 4]);
%$ t(4) = dyn_assert(e4.time,[1951 1]);
%$ t(5) = dyn_assert(e5.time,[1950 4]);
%$ t(6) = dyn_assert(e6.time,[1951 1]);
%$ t(7) = dyn_assert(e7.time,[2001 1]);
%$ T = all(t);
%@eof:1