Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • Dynare/dseries
  • sebastien/dseries
  • houtanb/dseries
  • wmutschl/dseries
  • DoraK/dseries
  • JohannesPfeifer/dseries
6 results
Show changes
Commits on Source (340)
variables:
GIT_SUBMODULE_STRATEGY: normal
TERM: linux
MATLAB_VERSION: R2024b
OLD_MATLAB_VERSION: R2018b
# To ensure that "false && true" fails, see https://gitlab.com/gitlab-org/gitlab-runner/-/issues/25394#note_412609647
FF_ENABLE_BASH_EXIT_CODE_CHECK: 'true'
before_script:
- git clone https://git.dynare.org/Dynare/m-unit-tests
testsuite_matlab:
test_matlab:
stage: test
script:
- make check-matlab
- make check-matlab MATLAB=/opt/MATLAB/$MATLAB_VERSION/bin/matlab
testsuite_octave:
test_old_matlab:
stage: test
script:
- make check-matlab MATLAB=/opt/MATLAB/$OLD_MATLAB_VERSION/bin/matlab
test_octave:
stage: test
variables:
OPENBLAS_NUM_THREADS: 1
script:
- make check-octave
allow_failure: true
[submodule "src/modules/matlab-fame-io"]
path = src/modules/matlab-fame-io
url = ../../Dynare/matlab-fame-io.git
> Copyright (C) 2014-2018 Dynare Team
> Copyright © 2014-2018 Dynare Team
>
> The dseries and dates Matlab/Octave classes are licensed under GNU GPL as published by
> The dseries and dates MATLAB/Octave classes are licensed under GNU GPL as published by
> the Free Software Foundation, either version 3 of the License, or (at
> your option) any later version.
>
......@@ -10,4 +10,4 @@
> General Public License for more details.
> A copy of the GNU General Public License is available here
> <http://www.gnu.org/licenses/>.
> <https://www.gnu.org/licenses/>.
OCTAVE=octave-cli
MATLAB=`which matlab`
OCTAVE ?= octave-cli
MATLAB ?= matlab
all: check-octave check-matlab
......@@ -11,7 +11,7 @@ check-octave: m-unit-tests/src/mtest.m
$(OCTAVE) --no-init-file --silent --no-history --eval "addpath([pwd() '/../m-unit-tests/src']); runalltests" && [ ! -f ./failed ] && [ -f ./pass ]
check-matlab: m-unit-tests/src/mtest.m
@$(MATLAB) -nosplash -nodisplay -r "addpath m-unit-tests/src; cd tests; runalltests; quit" && [ ! -f ./tests/failed ] && [ -f ./tests/pass ]
@$(MATLAB) -batch "addpath([pwd '/m-unit-tests/src']); cd tests; runalltests; quit" && [ ! -f ./tests/failed ] && [ -f ./tests/pass ]
check-clean:
rm -f tests/*_test_*.m tests/*.csv tests/*.xls tests/*.xlsx tests/*.mat tests/failed tests/datafile_for_test
......
[![pipeline status](https://git.dynare.org/Dynare/dseries/badges/master/pipeline.svg)](https://git.dynare.org/Dynare/dseries/commits/master)
This Matlab/Octave toolbox comes with two classes:
This MATLAB/Octave toolbox comes with three classes:
- `@dates` which is used to handle dates.
- `@dseries` which is used to handle time series data.
- `@x13` which provides an interface to X13-ARIMEA-SEATS.
The package is a dependence of
[Dynare](https=//git.dynare.org/Dynare/dynare), but can also be used
as a standalone package without Dynare. The package is
compatible with Matlab 2008a and following versions, and (almost
compatible with MATLAB 2008a and following versions, and (almost
compatible with) the latest Octave version.
## Installation
......@@ -25,30 +26,23 @@ or downloading a zip archive:
## Usage
Add the `dseries/src` folder to the Matlab/Octave path, and run the following command (on Matlab/Octave) prompt:
Add the `dseries/src` folder to the MATLAB/Octave path, and run the following command (on MATLAB/Octave) prompt:
>> dseries().initialize()
>> initialize_dseries_class
which, depending on your system, will add the necessary subfolders to
the Matlab/Octave path. Also, if
[X13-ARIMA-SEATS](https://www.census.gov/srd/www/x13as/) is not
installed in your system (on debian it is possible to install it with
the `apt-get`) you will need (only the first time) to install the
binary. Scripts are available to install (or update) this
dependency. From the Matlab/Octave prompt:
the MATLAB/Octave path.
>> cd dseries/externals/x13
>> installx13()
and run the configuration again:
>> dseries.initialize()
You should not see the warning related to the missing `x13as`
binary. You are then ready to go. A full documentation will come soon,
but you can already obtain a general idea by looking into the Dynare
You are then ready to go. A full documentation is available in the Dynare
reference manual.
Note that [X13-ARIMA-SEATS](https://www.census.gov/srd/www/x13as/) is required
for accessing all the features of the toolbox. On Windows and macOS, an
X13-ARIMA-SEATS binary is included in standalone dseries packages and in Dynare
packages. On Debian and Ubuntu it is possible to install X13-ARIMA-SEATS with
`apt install x13as` (on Debian, you must have the non-free archive area listed
in package sources).
## Examples
### Instantiate a dseries object from an array
......
x13.zip
linux/*
windows/*
*~
classdef arima<handle % --*-- Unitary tests --*--
classdef arima<handle
% Class for ARIMA models (interface to X13).
% Copyright (C) 2017 Dynare Team
% Copyright © 2017-2023 Dynare Team
%
% 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
......@@ -15,7 +15,7 @@ classdef arima<handle % --*-- Unitary tests --*--
% 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/>.
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
properties
y = []; % dseries object with a single variable.
......@@ -48,7 +48,6 @@ methods
%
% OUPUTS
% - o [arima] ARIMA model object.
nargin
switch nargin
case 0
% Return empty object.
......@@ -152,7 +151,7 @@ methods
end
end % arima
end % methods
end % classdef
end % classdef --*-- Unit tests --*--
%@test:1
%$ try
......
......@@ -2,7 +2,7 @@ function estimate(o, varargin)
% Estimate method for arima class.
% Copyright (C) 2017 Dynare Team
% Copyright © 2017-2021 Dynare Team
%
% 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
......@@ -15,7 +15,7 @@ function estimate(o, varargin)
% 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/>.
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
% Set default values for options.
options.tol = 1e-5;
......@@ -73,7 +73,7 @@ fprintf(fid, '}\n');
fclose(fid);
% Run estimation of the ARIMA model
[status, result] = system(sprintf('%s %s', select_x13_binary(), basefilename));
system(sprintf('%s %s', select_x13_binary(), basefilename));
% Get the content of the generated *.est file
fid = fopen(sprintf('%s.est', basefilename), 'r');
......@@ -205,17 +205,18 @@ if isequal(linea{1}{1}, 'bic')
o.estimation.information_criteria.Schwarz = linea{2};
end
return
%@test:1
%$ try
%$ y = dseries([0; 0], '1938Q3', 'y')
%$ e = dseries(randn(201,1),'1938Q4','e');
%$ from from 1939Q1 to 1988Q4 do y(t) = .8*y(t-1) - .2*y(t-2) + e(t) - .15*e(t-1)
%$ model = arima(y, '(2,0,1)');
%$ estimate(model);
%$ catch
%$ t(1) = false;
%$ end
%$
%$ T = all(t);
try
y = dseries([0; 0], '1938Q3', 'y')
e = dseries(randn(201,1),'1938Q4','e');
from from 1939Q1 to 1988Q4 do y(t) = .8*y(t-1) - .2*y(t-2) + e(t) - .15*e(t-1)
model = arima(y, '(2,0,1)');
estimate(model);
catch
t(1) = false;
end
T = all(t);
%@eof:1
function o = append(o, d) % --*-- Unitary tests --*--
function o = append(o, d)
% append method for dates class.
%
% INPUTS
% - o [dates]
% - a [dates or string] dates object with one element or string that can be interpreted as a date.
% - d [dates,char] date to be appended (if not a dates object, it must be a row char array that can be interpreted as a date).
%
% OUTPUTS
% - o [dates] dates object containing dates defined in o and d.
% - o [dates] containing dates defined in o and d.
% Copyright (C) 2012-2017 Dynare Team
% Copyright © 2012-2022 Dynare Team
%
% 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
......@@ -22,7 +22,7 @@ function o = append(o, d) % --*-- Unitary tests --*--
% 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/>.
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
if isa(d, 'dates')
if ~isequal(length(d), 1)
......@@ -42,95 +42,97 @@ end
o = copy(o);
o.append_(d);
return % --*-- Unit tests --*--
%@test:1
%$ % Define some dates
%$ B1 = '1953Q4';
%$ B2 = '1950Q2';
%$ B3 = '1950Q1';
%$ B4 = '1945Q3';
%$ B5 = '2009Q2';
%$
%$ % Define expected results.
%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4];
%$ e.freq = 4;
%$
%$ % Call the tested routine.
%$ d = dates(B4,B3,B2,B1);
%$ try
%$ d.append(dates(B5));
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ % Check the results.
%$ if t(1)
%$ t(2) = dassert(d.time,e.time);
%$ t(3) = dassert(d.freq,e.freq);
%$ end
%$ T = all(t);
% Define some dates
B1 = '1953Q4';
B2 = '1950Q2';
B3 = '1950Q1';
B4 = '1945Q3';
B5 = '2009Q2';
% Define expected results.
e.time = [1945*4+3; 1950*4+1; 1950*4+2; 1953*4+4];
e.freq = 4;
% Call the tested routine.
d = dates(B4, B3, B2, B1);
try
d.append(dates(B5));
t(1) = true;
catch
t(1) = false;
end
% Check the results.
if t(1)
t(2) = isequal(d.time,e.time);
t(3) = isequal(d.freq,e.freq);
end
T = all(t);
%@eof:1
%@test:2
%$ % Define some dates
%$ B1 = '1953Q4';
%$ B2 = '1950Q2';
%$ B3 = '1950Q1';
%$ B4 = '1945Q3';
%$ B5 = '2009q2';
%$
%$ % Define expected results.
%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4; 2009 2];
%$ f.time = [1945 3; 1950 1; 1950 2; 1953 4];
%$ e.freq = 4;
%$
%$ % Call the tested routine.
%$ d = dates(B4,B3,B2,B1);
%$ try
%$ c = d.append(B5);
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ % Check the results.
%$ if t(1)
%$ t(2) = dassert(d.time,f.time);
%$ t(3) = dassert(c.time,e.time);
%$ t(4) = dassert(c.freq,e.freq);
%$ t(5) = dassert(d.freq,e.freq);
%$ end
%$ T = all(t);
% Define some dates
B1 = '1953Q4';
B2 = '1950Q2';
B3 = '1950Q1';
B4 = '1945Q3';
B5 = '2009q2';
% Define expected results.
e.time = [1945*4+3; 1950*4+1; 1950*4+2; 1953*4+4; 2009*4+2];
f.time = [1945*4+3; 1950*4+1; 1950*4+2; 1953*4+4];
e.freq = 4;
% Call the tested routine.
d = dates(B4,B3,B2,B1);
try
c = d.append(B5);
t(1) = true;
catch
t(1) = false;
end
% Check the results.
if t(1)
t(2) = isequal(d.time,f.time);
t(3) = isequal(c.time,e.time);
t(4) = isequal(c.freq,e.freq);
t(5) = isequal(d.freq,e.freq);
end
T = all(t);
%@eof:2
%@test:3
%$ % Define some dates
%$ B1 = '1953Q4';
%$ B2 = '1950Q2';
%$ B3 = '1950Q1';
%$ B4 = '1945Q3';
%$ B5 = '2009q2';
%$
%$ % Define expected results.
%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4; 2009 2];
%$ f.time = [1945 3; 1950 1; 1950 2; 1953 4];
%$ e.freq = 4;
%$
%$ % Call the tested routine.
%$ d = dates(B4,B3,B2,B1);
%$ try
%$ c = append(d, B5);
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ % Check the results.
%$ if t(1)
%$ t(2) = dassert(d.time,f.time);
%$ t(3) = dassert(c.time,e.time);
%$ t(4) = dassert(c.freq,e.freq);
%$ t(5) = dassert(d.freq,e.freq);
%$ end
%$ T = all(t);
% Define some dates
B1 = '1953Q4';
B2 = '1950Q2';
B3 = '1950Q1';
B4 = '1945Q3';
B5 = '2009q2';
% Define expected results.
e.time = [1945*4+3; 1950*4+1; 1950*4+2; 1953*4+4; 2009*4+2];
f.time = [1945*4+3; 1950*4+1; 1950*4+2; 1953*4+4];
e.freq = 4;
% Call the tested routine.
d = dates(B4,B3,B2,B1);
try
c = append(d, B5);
t(1) = true;
catch
t(1) = false;
end
% Check the results.
if t(1)
t(2) = isequal(d.time,f.time);
t(3) = isequal(c.time,e.time);
t(4) = isequal(c.freq,e.freq);
t(5) = isequal(d.freq,e.freq);
end
T = all(t);
%@eof:3
function o = append_(o, d) % --*-- Unitary tests --*--
function o = append_(o, d)
% append method for dates class (in place modification).
%
% INPUTS
% - o [dates]
% - a [dates or string] dates object with one element or string that can be interpreted as a date.
% - d [dates,char] date to be appended (if not a dates object, it must be a row char array that can be interpreted as a date).
%
% OUTPUTS
% - o [dates] dates object containing dates defined in o and d.
% Copyright (C) 2012-2017 Dynare Team
% Copyright © 2012-2022 Dynare Team
%
% 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
......@@ -22,7 +22,7 @@ function o = append_(o, d) % --*-- Unitary tests --*--
% 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/>.
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
if isa(d, 'dates')
if ~isequal(length(d), 1)
......@@ -41,149 +41,215 @@ end
o.time = [o.time; d.time];
return % --*-- Unit tests --*--
%@test:1
%$ % Define some dates
%$ B1 = '1953Q4';
%$ B2 = '1950Q2';
%$ B3 = '1950Q1';
%$ B4 = '1945Q3';
%$ B5 = '2009Q2';
%$
%$ % Define expected results.
%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4; 2009 2];
%$ e.freq = 4;
%$
%$ % Call the tested routine.
%$ d = dates(B4,B3,B2,B1);
%$ try
%$ d.append_(dates(B5));
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ % Check the results.
%$ if t(1)
%$ t(2) = dassert(d.time,e.time);
%$ t(3) = dassert(d.freq,e.freq);
%$ end
%$ T = all(t);
% Define some dates
B1 = '1953Q4';
B2 = '1950Q2';
B3 = '1950Q1';
B4 = '1945Q3';
B5 = '2009Q2';
% Define expected results.
e.time = [1945*4+3; 1950*4+1; 1950*4+2; 1953*4+4; 2009*4+2];
e.freq = 4;
% Call the tested routine.
d = dates(B4,B3,B2,B1);
try
d.append_(dates(B5));
t(1) = true;
catch
t(1) = false;
end
% Check the results.
if t(1)
t(2) = isequal(d.time,e.time);
t(3) = isequal(d.freq,e.freq);
end
T = all(t);
%@eof:1
%@test:2
%$ % Define some dates
%$ B1 = '1953Q4';
%$ B2 = '1950Q2';
%$ B3 = '1950Q1';
%$ B4 = '1945Q3';
%$ B5 = '2009Q2';
%$
%$ % Define expected results.
%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4; 2009 2];
%$ e.freq = 4;
%$
%$ % Call the tested routine.
%$ d = dates(B4,B3,B2,B1);
%$ try
%$ d.append_(B5);
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ % Check the results.
%$ if t(1)
%$ t(2) = dassert(d.time,e.time);
%$ t(3) = dassert(d.freq,e.freq);
%$ end
%$ T = all(t);
% Define some dates
B1 = '1953Q4';
B2 = '1950Q2';
B3 = '1950Q1';
B4 = '1945Q3';
B5 = '2009Q2';
% Define expected results.
e.time = [1945*4+3; 1950*4+1; 1950*4+2; 1953*4+4; 2009*4+2];
e.freq = 4;
% Call the tested routine.
d = dates(B4,B3,B2,B1);
try
d.append_(B5);
t(1) = true;
catch
t(1) = false;
end
% Check the results.
if t(1)
t(2) = isequal(d.time,e.time);
t(3) = isequal(d.freq,e.freq);
end
T = all(t);
%@eof:2
%@test:3
%$ % Define some dates
%$ B1 = '1953Q4';
%$ B2 = '1950Q2';
%$ B3 = '1950Q1';
%$ B4 = '1945Q3';
%$ B5 = '2009Q2';
%$
%$ % Define expected results.
%$ e.time = [1945 3; 1950 1; 1950 2; 1953 4; 2009 2];
%$ e.freq = 4;
%$
%$ % Call the tested routine.
%$ d = dates(B4,B3,B2,B1);
%$ try
%$ c = d.append_(B5);
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ % Check the results.
%$ if t(1)
%$ t(2) = dassert(d.time,e.time);
%$ t(3) = dassert(c.time,e.time);
%$ t(4) = dassert(d.freq,e.freq);
%$ t(5) = dassert(c.freq,e.freq);
%$ end
%$ T = all(t);
% Define some dates
B1 = '1953Q4';
B2 = '1950Q2';
B3 = '1950Q1';
B4 = '1945Q3';
B5 = '2009Q2';
% Define expected results.
e.time = [1945*4+3; 1950*4+1; 1950*4+2; 1953*4+4; 2009*4+2];
e.freq = 4;
% Call the tested routine.
d = dates(B4,B3,B2,B1);
try
c = d.append_(B5);
t(1) = true;
catch
t(1) = false;
end
% Check the results.
if t(1)
t(2) = isequal(d.time,e.time);
t(3) = isequal(c.time,e.time);
t(4) = isequal(d.freq,e.freq);
t(5) = isequal(c.freq,e.freq);
end
T = all(t);
%@eof:3
%@test:4
%$ % Define some dates
%$ B1 = '1953Q4';
%$ B2 = '1950Q2';
%$ B3 = '1950Q1';
%$ B4 = '1945Q3';
%$ B5 = '2009Q2';
%$
%$ % Call the tested routine.
%$ d = dates(B4,B3);
%$ e = dates(B1,B2,B5);
%$ try
%$ d.append_(e);
%$ t(1) = false;
%$ catch
%$ t(1) = true;
%$ end
%$
%$ T = all(t);
% Define some dates
B1 = '1953Q4';
B2 = '1950Q2';
B3 = '1950Q1';
B4 = '1945Q3';
B5 = '2009Q2';
% Call the tested routine.
d = dates(B4,B3);
e = dates(B1,B2,B5);
try
d.append_(e);
t(1) = false;
catch
t(1) = true;
end
T = all(t);
%@eof:4
%@test:5
%$ % Define some dates
%$ B = '1950Q2';
%$
%$ % Call the tested routine.
%$ d = dates(B);
%$ try
%$ d.append_('1950Q3');
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(d.time, [1950 2; 1950 3]);
%$ end
%$
%$ T = all(t);
% Define some dates
B = '1950Q2';
% Call the tested routine.
d = dates(B);
try
d.append_('1950Q3');
t(1) = true;
catch
t(1) = false;
end
if t(1)
t(2) = isequal(d.time, [1950*4+2; 1950*4+3]);
end
T = all(t);
%@eof:5
%@test:6
%$ % Define some dates
%$ B = '1950Q2';
%$
%$ % Call the tested routine.
%$ d = dates(B);
%$ try
%$ d.append_('1950Z3');
%$ t(1) = false;
%$ catch
%$ t(1) = true;
%$ end
%$
%$ T = all(t);
% Define some dates
B = '1950Q2';
% Call the tested routine.
d = dates(B);
try
d.append_('1950Z3');
t(1) = false;
catch
t(1) = true;
end
T = all(t);
%@eof:6
%@test:7
% Define some dates
B1 = '2020-01-01';
B2 = '2020-01-02';
B3 = '2020-01-03';
B4 = '2020-01-04';
B5 = '2020-01-05';
% Call the tested routine.
d = dates(B4,B3);
e = dates(B1,B2,B5);
try
d.append_(e);
t(1) = false;
catch
t(1) = true;
end
T = all(t);
%@eof:7
%@test:8
% Define some dates
B1 = '2020-01-01';
B2 = '2020-01-02';
B3 = '2020Q1';
% Call the tested routine.
d = dates(B1,B2);
e = dates(B3);
try
d.append_(e);
t(1) = false;
catch
t(1) = true;
end
T = all(t);
%@eof:8
%@test:9
% Define some dates
B1 = '2020W1';
B2 = '2020W2';
B3 = '2020W3';
B4 = '2020W4';
% Call the tested routine.
try
d1 = dates(B1,B2,B3);
d2 = dates(B4);
d1.append_(d2);
t(1) = true;
catch
t(1) = false;
end
if t(1)
t(2) = isequal(d1, dates(B1, B2, B3, B4));
end
T = all(t);
%@eof:9
\ No newline at end of file
function s = char(o) % --*-- Unitary tests --*--
function s = char(o)
% Given a one element dates object, returns a string with the formatted date.
%
......@@ -8,7 +8,7 @@ function s = char(o) % --*-- Unitary tests --*--
% OUTPUTS
% - s [string]
% Copyright (C) 2014-2017 Dynare Team
% Copyright © 2014-2023 Dynare Team
%
% 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
......@@ -21,7 +21,7 @@ function s = char(o) % --*-- Unitary tests --*--
% 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/>.
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
if length(o)>1
error('dates:char:ArgCheck', 'The input argument must be a dates object with one element!')
......@@ -29,92 +29,112 @@ end
s = date2string(o.time, o.freq);
return % --*-- Unit tests --*--
%@test:1
%$ % Define a dates object
%$ o = dates('1950Q1');
%$
%$ % Call the tested routine.
%$ try
%$ str = char(o);
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(str,'1950Q1');
%$ end
%$ T = all(t);
% Define a dates object
o = dates('1950Q1');
% Call the tested routine.
try
str = char(o);
t(1) = true;
catch
t(1) = false;
end
if t(1)
t(2) = isequal(str,'1950Q1');
end
T = all(t);
%@eof:1
%@test:2
%$ % Define a dates object
%$ o = dates('1950M1');
%$
%$ % Call the tested routine.
%$ try
%$ str = char(o);
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(str,'1950M1');
%$ end
%$ T = all(t);
% Define a dates object
o = dates('1950M1');
% Call the tested routine.
try
str = char(o);
t(1) = true;
catch
t(1) = false;
end
if t(1)
t(2) = isequal(str,'1950M1');
end
T = all(t);
%@eof:2
%@test:3
%$ % Define a dates object
%$ o = dates('1950W50');
%$
%$ % Call the tested routine.
%$ try
%$ str = char(o);
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(str,'1950W50');
%$ end
%$ T = all(t);
% Define a dates object
o = dates('2020-10-01');
% Call the tested routine.
try
str = char(o);
t(1) = true;
catch
t(1) = false;
end
if t(1)
t(2) = isequal(str,'2020-10-01');
end
T = all(t);
%@eof:3
%@test:4
%$ % Define a dates object
%$ o = dates('1950Y');
%$
%$ % Call the tested routine.
%$ try
%$ str = char(o);
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(str,'1950Y');
%$ end
%$ T = all(t);
% Define a dates object
o = dates('1950Y');
% Call the tested routine.
try
str = char(o);
t(1) = true;
catch
t(1) = false;
end
if t(1)
t(2) = isequal(str,'1950Y');
end
T = all(t);
%@eof:4
%@test:5
%$ % Define a dates object
%$ o = dates('1950A');
%$
%$ % Call the tested routine.
%$ try
%$ str = char(o);
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(str,'1950Y');
%$ end
%$ T = all(t);
% Define a dates object
o = dates('1950A');
% Call the tested routine.
try
str = char(o);
t(1) = true;
catch
t(1) = false;
end
if t(1)
t(2) = isequal(str,'1950Y');
end
T = all(t);
%@eof:5
%@test:6
% Define a dates object
o = dates('1950W1');
% Call the tested routine.
try
str = char(o);
t(1) = true;
catch
t(1) = false;
end
if t(1)
t(2) = isequal(str,'1950W1');
end
T = all(t);
%@eof:6
function q = colon(varargin) % --*-- Unitary tests --*--
function q = colon(varargin)
% Overloads the colon operator (:). This method can be used to create ranges of dates.
%
......@@ -14,7 +14,7 @@ function q = colon(varargin) % --*-- Unitary tests --*--
% 1. p must be greater than o if d>0.
% 2. p and q are dates objects with one element.
% Copyright (C) 2013-2017 Dynare Team
% Copyright © 2013-2023 Dynare Team
%
% 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
......@@ -27,166 +27,149 @@ function q = colon(varargin) % --*-- Unitary tests --*--
% 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/>.
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
% Check the input arguments.
if isequal(nargin,2)
if isequal(nargin, 2)
o = varargin{1};
p = varargin{2};
d = 1;
if ~(isa(o,'dates') && isa(p,'dates') && isequal(o.length(),1) && isequal(p.length(),1))
error('dates:colon:ArgCheck','In an expression like A:B, A and B must be one element dates objects!')
if ~(isa(o, 'dates') && isa(p, 'dates') && isequal(o.length(), 1) && isequal(p.length(), 1))
error('dates:colon:ArgCheck', 'In an expression like A:B, A and B must be one element dates objects.')
end
elseif isequal(nargin,3)
o = varargin{1};
p = varargin{3};
d = varargin{2};
if ~(isa(o,'dates') && isa(p,'dates') && isequal(o.length(),1) && isequal(o.length(),1) && isscalar(d) && isint(d))
error('dates:colon:ArgCheck','In an expression like A:d:B, A and B must be one element dates objects and d a scalar integer!')
if ~(isa(o, 'dates') && isa(p, 'dates') && isequal(o.length(), 1) && isequal(o.length(), 1) && isscalar(d) && isint(d))
error('dates:colon:ArgCheck', 'In an expression like A:d:B, A and B must be one element dates objects and d a scalar integer.')
end
if isequal(d,0)
error('dates:colon:ArgCheck','In an expression like A:d:B, d (the incremental number of periods) must nonzero!')
if isequal(d, 0)
error('dates:colon:ArgCheck', 'In an expression like A:d:B, d (the incremental number of periods) must nonzero.')
end
else
error('dates:colon:ArgCheck','See the manual for the colon (:) operator and dates objects.')
error('dates:colon:ArgCheck', 'See the manual for the colon (:) operator and dates objects.')
end
if ~isequal(o.freq, p.freq)
error('dates:colon:ArgCheck','dates::colon: Input arguments %s and %s must have common frequency!', inputname(1), inputname(2))
error('dates:colon:ArgCheck', 'Input arguments %s and %s must have common frequency.', inputname(1), inputname(2))
end
if o>p && d>0
error('dates:colon:ArgCheck','First date must preceed the second one!')
error('dates:colon:ArgCheck', 'First date must preceed the second one.')
end
if p>o && d<0
error('dates:colon:ArgCheck','Second date must preceed the first one!')
error('dates:colon:ArgCheck', 'Second date must preceed the first one.')
end
% Initialize the output argument.
q = dates();
% Compute the number of elements in the returned dates object.
n = (p-o)+1; % The number of elements in q dates object if d==1.
m = n;
if d>1 % Correction of the number of elements (if d is not equal to one).
m = length(1:d:n);
end
% Set the frequency in q
q.freq = o.freq;
if isequal(q.freq, 1)
% Yearly
q.time = NaN(m,2);
q.time(:,1) = o.time(1)+transpose(0:d:n-1);
q.time(:,2) = 1;
else
% Weekly, Monthly, Quaterly
q.time = NaN(n,2);
initperiods = min(q.freq-o.time(2)+1,n);
q.time(1:initperiods,1) = o.time(1);
q.time(1:initperiods,2) = transpose(o.time(2)-1+(1:initperiods));
if n>initperiods
l = n-initperiods;
if l<=q.freq
q.time(initperiods+(1:l),1) = o.time(1)+1;
q.time(initperiods+(1:l),2) = transpose(1:l);
else
k = fix(l/o.freq);
r = rem(l,o.freq);
q.time(initperiods+(1:q.freq*k),2) = repmat(transpose(1:q.freq),k,1);
q.time(initperiods+(1:q.freq*k),1) = kron(o.time(1)+transpose(1:k),ones(q.freq,1));
if r>0
q.time(initperiods+q.freq*k+(1:r),1) = q.time(initperiods+q.freq*k,1)+1;
q.time(initperiods+q.freq*k+(1:r),2) = transpose(1:r);
end
end
end
if d>1
q.time = q.time(1:d:n,:);
end
end
% Set time
q.time = transpose(o.time:d:p.time);
return % --*-- Unit tests --*--
%@test:1
%$ % 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 = dates(date_1);
%$ d2 = dates(date_2);
%$ d3 = d1:d2;
%$
%$ % Check the results.
%$ t(1) = dassert(d3.time,e.time);
%$ t(2) = dassert(d3.freq,e.freq);
%$ T = all(t);
% Define two dates
date_1 = '1950Q2';
date_2 = '1951Q4';
% Define expected results.
e.freq = 4;
e.time = [1950*4+2; 1950*4+3; 1950*4+4; 1951*4+1; 1951*4+2; 1951*4+3; 1951*4+4];
% Call the tested routine.
d1 = dates(date_1);
d2 = dates(date_2);
d3 = d1:d2;
% Check the results.
t(1) = isequal(d3.time, e.time);
t(2) = isequal(d3.freq, e.freq);
T = all(t);
%@eof:1
%@test:2
%$ % 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.
%$ d = dates('1950Q2'):dates('1951Q4');
%$
%$ % Check the results.
%$ t(1) = dassert(d.time,e.time);
%$ t(2) = dassert(d.freq,e.freq);
%$ T = all(t);
% Define expected results.
e.freq = 4;
e.time = [1950*4+2; 1950*4+3; 1950*4+4; 1951*4+1; 1951*4+2; 1951*4+3; 1951*4+4];
% Call the tested routine.
d = dates('1950Q2'):dates('1951Q4');
% Check the results.
t(1) = isequal(d.time, e.time);
t(2) = isequal(d.freq, e.freq);
T = all(t);
%@eof:2
%@test:3
%$ % Define expected results.
%$ e.freq = 4;
%$ e.time = [1950 2; 1950 4; 1951 2; 1951 4];
%$
%$ % Call the tested routine.
%$ d = dates('1950Q2'):2:dates('1951Q4');
%$
%$ % Check the results.
%$ t(1) = dassert(d.time,e.time);
%$ t(2) = dassert(d.freq,e.freq);
%$ T = all(t);
% Define expected results.
e.freq = 4;
e.time = [1950*4+2; 1950*4+4; 1951*4+2; 1951*4+4];
% Call the tested routine.
d = dates('1950Q2'):2:dates('1951Q4');
% Check the results.
t(1) = isequal(d.time, e.time);
t(2) = isequal(d.freq, e.freq);
T = all(t);
%@eof:3
%$ @test:3
%$ % Create an empty dates object for quaterly data
%$ qq = dates('Q');
%$
%$ % 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.
%$ d = qq(1950,2):qq(1951,4);
%$
%$ % Check the results.
%$ t(1) = dassert(d.time,e.time);
%$ t(2) = dassert(d.freq,e.freq);
%$ T = all(t);
%$ @eof:3
%$ @test:4
%$ % Create an empty dates object for quaterly data
%$ qq = dates('Q');
%$
%$ % Define expected results.
%$ e.freq = 4;
%$ e.time = [1950 1; 1950 2; 1950 3];
%$
%$ % Call the tested routine.
%$ d = qq(1950,1):qq(1950,3);
%$
%$ % Check the results.
%$ t(1) = dassert(d.time,e.time);
%$ t(2) = dassert(d.freq,e.freq);
%$ T = all(t);
%$ @eof:4
%@test:4
% Create an empty dates object for quaterly data
qq = dates('Q');
% Define expected results.
e.freq = 4;
e.time = [1950*4+2; 1950*4+3; 1950*4+4; 1951*4+1; 1951*4+2; 1951*4+3; 1951*4+4];
% Call the tested routine.
d = qq(1950,2):qq(1951,4);
% Check the results.
t(1) = isequal(d.time, e.time);
t(2) = isequal(d.freq, e.freq);
T = all(t);
%@eof:4
%@test:5
% Create an empty dates object for quaterly data
qq = dates('Q');
% Define expected results.
e.freq = 4;
e.time = [1950*4+1; 1950*4+2; 1950*4+3];
% Call the tested routine.
d = qq(1950,1):qq(1950,3);
% Check the results.
t(1) = isequal(d.time, e.time);
t(2) = isequal(d.freq, e.freq);
T = all(t);
%@eof:5
%@test:6
% Create an empty dates object for daily data
dd = dates('D');
% Define expected results.
e.freq = 365;
e.time = [712251; 712252; 712253];
% Call the tested routine.
d = dd(1950, 01, 28):dd(1950, 01, 30);
% Check the results.
t(1) = isequal(d.time(:,1), e.time(:,1));
t(2) = isequal(d.freq, e.freq);
T = all(t);
%@eof:6
function p = copy(o) % --*-- Unitary tests --*--
function p = copy(o)
% Do a copy of a dates object.
%
......@@ -8,7 +8,7 @@ function p = copy(o) % --*-- Unitary tests --*--
% OUTPUTS
% - p [dates]
% Copyright (C) 2014-2017 Dynare Team
% Copyright © 2014-2023 Dynare Team
%
% 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
......@@ -21,29 +21,31 @@ function p = copy(o) % --*-- Unitary tests --*--
% 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/>.
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
p = dates();
p.freq = o.freq;
p.time = o.time;
return % --*-- Unit tests --*--
%@test:1
%$ % Define a dates object
%$ o = dates('1945Q3','1944Q4');
%$ q = dates('1945Q3','1944Q4');
%$
%$ % Call the tested routine.
%$ try
%$ p = copy(o);
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ o.sort_();
%$ t(2) = dassert(p, q);
%$ end
%$
%$ T = all(t);
%@eof:1
\ No newline at end of file
% Define a dates object
o = dates('1945Q3','1944Q4');
q = dates('1945Q3','1944Q4');
% Call the tested routine.
try
p = copy(o);
t(1) = true;
catch
t(1) = false;
end
if t(1)
o.sort_();
t(2) = dassert(p, q);
end
T = all(t);
%@eof:1
classdef dates<handle % --*-- Unitary tests --*--
classdef dates<handle
% Copyright (C) 2014-2017 Dynare Team
% Copyright © 2014-2023 Dynare Team
%
% 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
......@@ -13,11 +13,11 @@ classdef dates<handle % --*-- Unitary tests --*--
% 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/>.
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
properties
freq = []; % Frequency (integer scalar)
time = []; % Array (one row for every date. first column is the year, second is the period)
time = []; % One dimensional array of integers (counts subperiods since year 0)
end
methods
......@@ -25,7 +25,7 @@ methods
if ~nargin
% Returns empty dates object.
o.freq = NaN(0);
o.time = NaN(0,2);
o.time = NaN(0,1);
return
end
if all(cellfun(@isdates, varargin))
......@@ -33,20 +33,20 @@ methods
o = horzcat(varargin{:});
return
end
if all(cellfun(@isstringdate,varargin))
if all(cellfun(@isstringdate, varargin))
% Concatenates dates in a dates object.
tmp = cellfun(@string2date,varargin);
tmp = cellfun(@string2date, varargin);
if all([tmp.freq]-tmp(1).freq==0)
o.freq = tmp(1).freq;
else
error('dates:ArgCheck', 'All dates passed as inputs must have the same frequency!')
end
o.time = transpose(reshape([tmp.time], 2, length(tmp)));
o.time = transpose([tmp.time]);
return
end
if isequal(nargin,1) && isfreq(varargin{1})
% Instantiate an empty dates object (only set frequency)
o.time = NaN(0,2);
o.time = NaN(0,1);
if ischar(varargin{1})
o.freq = string2freq(varargin{1});
else
......@@ -54,30 +54,46 @@ methods
end
return
end
if isequal(nargin,3) && isfreq(varargin{1})
o.time = NaN(0,2);
if isequal(nargin, 3) && isfreq(varargin{1})
o.time = NaN(0, 1);
if ischar(varargin{1})
o.freq = string2freq(varargin{1});
else
o.freq = varargin{1};
end
if o.freq==365
error('dates:ArgCheck', 'Requires four inputs for daily dates.')
end
if (isnumeric(varargin{2}) && isvector(varargin{2}) && all(isint(varargin{2})))
if isnumeric(varargin{3}) && isvector(varargin{3}) && all(isint(varargin{3}))
if all(varargin{3}>=1) && all(varargin{3}<=o.freq)
o.time = [varargin{2}(:), varargin{3}(:)];
if o.freq==52
if all(varargin{3}>=1)
if any(islongyear(varargin{2}(:))) && any(varargin{3}(islongyear(varargin{2}(:)))>53)
error('dates:ArgCheck', 'Third input specifies non existing ISO weeks (argument cannot be greater than 53).')
end
if any(~islongyear(varargin{2}(:))) && any(varargin{3}(~islongyear(varargin{2}(:)))>52)
error('dates:ArgCheck', 'Third input specifies non existing ISO weeks (argument cannot be greater than 52 except in long years).')
end
else
error('dates:ArgCheck', 'Third input must contain strictly positive integers.')
end
else
error('dates:ArgCheck','Third input must contain integers between 1 and %i.', o.freq)
if all(varargin{3}>=1) && all(varargin{3}<=o.freq)
o.time = varargin{2}(:)*o.freq+varargin{3}(:);
else
error('dates:ArgCheck', 'Third input must contain integers between 1 and %i.', o.freq)
end
end
else
error('dates:ArgCheck','Third input must be a vector of integers.')
error('dates:ArgCheck', 'Third input must be a vector of integers.')
end
else
error('dates:ArgCheck','Second input must be a vector of integers.')
error('dates:ArgCheck', 'Second input must be a vector of integers.')
end
return
end
if isequal(nargin,2) && isfreq(varargin{1})
o.time = NaN(0,2);
o.time = NaN(0, 1);
if ischar(varargin{1})
o.freq = string2freq(varargin{1});
else
......@@ -85,25 +101,77 @@ methods
end
if isequal(o.freq, 1)
if (isnumeric(varargin{2}) && isvector(varargin{2}) && all(isint(varargin{2})))
o.time = [varargin{2}, ones(length(varargin{2}),1)];
o.time = varargin{2}(:);
return
else
error('dates:ArgCheck','Second input must be a vector of integers.')
end
else
if isequal(size(varargin{2},2), 2)
if all(isint(varargin{2}(:,1))) && all(isint(varargin{2}(:,2)))
elseif ismember(o.freq, [2,4,12])
if isequal(size(varargin{2}, 2), 2)
if all(all(isint(varargin{2})))
if all(varargin{2}(:,2)>=1) && all(varargin{2}(:,2)<=o.freq)
o.time = [varargin{2}(:,1), varargin{2}(:,2)];
o.time = varargin{2}(:,1)*o.freq+varargin{2}(:,2);
else
error('dates:ArgCheck', 'Second column of the last input must contain integers between 1 and %i.', o.freq)
end
else
error('dates:ArgCheck', 'Second input argument must be an array of integers.')
end
else
error('dates:ArgCheck', 'The second input must be a n*2 array of integers.')
end
elseif isequal(o.freq, 365)
if isequal(size(varargin{2}, 2), 3)
if all(all(isint(varargin{2})))
if all(varargin{2}(:,2)>=1) && all(varargin{2}(:,2)<=12)
if all(varargin{2}(:,3)>=1) && all(varargin{2}(:,2)<=31)
o.time = datenum(varargin{2}(:,1), varargin{2}(:,2), varargin{2}(:,3));
else
error('dates:ArgCheck', 'Third column of the last input must contain integers between 1 and 31 (days).')
end
else
error('dates:ArgCheck', 'Second column of the last input must contain integers between 1 and 12 (months).')
end
else
error('dates:ArgCheck', 'Second input argument must be an array of integers.')
end
else
error('dates:ArgCheck', 'For daily dates the second input must be a n*3 array of integers.')
end
end
return
end
if isequal(nargin,4) && isfreq(varargin{1}) && ( isequal(varargin{1}, 365) || strcmpi(varargin{1}, 'D'))
o.time = NaN(0,1);
if ischar(varargin{1})
o.freq = string2freq(varargin{1});
else
o.freq = varargin{1};
end
if (isnumeric(varargin{2}) && isvector(varargin{2}) && all(isint(varargin{2})))
if isnumeric(varargin{3}) && isvector(varargin{3}) && all(isint(varargin{3}))
if isnumeric(varargin{4}) && isvector(varargin{4}) && all(isint(varargin{4}))
if all(varargin{3}>=1) && all(varargin{3}<=12)
if all(varargin{4}>=1) && all(varargin{3}<=31)
if length(varargin{2})==length(varargin{3}) && length(varargin{2})==length(varargin{4})
o.time = datenum(varargin{2}(:), varargin{3}(:), varargin{4}(:));
else
error('dates:ArgCheck', 'Vectors passed as second, third, and fourth arguments must have the same number of elements.')
end
else
error('dates:ArgCheck', 'Fourth input must contain integers between 1 and 31 (days).')
end
else
error('dates:ArgCheck','Second column of the last input must contain integers between 1 and %i.',o.freq)
error('dates:ArgCheck', 'Third input must contain integers between 1 and 12 (days).')
end
else
error('dates:ArgCheck','Second input argument must be an array of integers.')
error('dates:ArgCheck','Fourth input must be a vector of integers.')
end
else
error('dates:ArgCheck','Wrong calling sequence!')
error('dates:ArgCheck','Third input must be a vector of integers.')
end
else
error('dates:ArgCheck','Second input must be a vector of integers.')
end
return
end
......@@ -125,8 +193,7 @@ methods
n = ndat(o);
n = length(o);
end % methods
end % classdef
end % classdef --*-- Unit tests --*--
%@test:1
%$ % Define some dates
......@@ -136,16 +203,16 @@ end % classdef
%$ B4 = '1953Q4';
%$
%$ % Define expected results.
%$ e.time = [1945 3; 1950 2; 1950 1; 1953 4];
%$ e.time = [1945*4+3; 1950*4+2; 1950*4+1; 1953*4+4];
%$ e.freq = 4;
%$
%$ % Call the tested routine.
%$ d = dates(B1,B2,B3,B4);
%$
%$ % Check the results.
%$ t(1) = dassert(d.time, e.time);
%$ t(2) = dassert(d.freq, e.freq);
%$ t(3) = dassert(d.ndat(), size(e.time, 1));
%$ t(1) = isequal(d.time, e.time);
%$ t(2) = isequal(d.freq, e.freq);
%$ t(3) = isequal(d.ndat(), size(e.time, 1));
%$ T = all(t);
%@eof:1
......@@ -157,41 +224,82 @@ end % classdef
%$ B4 = '1953M12';
%$
%$ % Define expected results.
%$ e.time = [1945 3; 1950 2; 1950 10; 1953 12];
%$ e.time = [1945*12+3; 1950*12+2; 1950*12+10; 1953*12+12];
%$ e.freq = 12;
%$
%$ % Call the tested routine.
%$ d = dates(B1,B2,B3,B4);
%$
%$ % Check the results.
%$ t(1) = dassert(d.time,e.time);
%$ t(2) = dassert(d.freq,e.freq);
%$ t(3) = dassert(d.ndat(), size(e.time, 1));
%$ t(1) = isequal(d.time,e.time);
%$ t(2) = isequal(d.freq,e.freq);
%$ t(3) = isequal(d.ndat(), size(e.time, 1));
%$ T = all(t);
%@eof:2
%@test:3
%$ % Define some dates
%$ B1 = '1945H1';
%$ B2 = '1950S2';
%$ B3 = '1950h1';
%$ B4 = '1953s2';
%$
%$ % Define expected results.
%$ e.time = [1945*2+1; 1950*2+2; 1950*2+1; 1953*2+2];
%$ e.freq = 2;
%$
%$ % Call the tested routine.
%$ d = dates(B1,B2,B3,B4);
%$
%$ % Check the results.
%$ t(1) = isequal(d.time,e.time);
%$ t(2) = isequal(d.freq,e.freq);
%$ t(3) = isequal(d.ndat(), size(e.time, 1));
%$ T = all(t);
%@eof:3
%@test:4
%$ % Define some dates
%$ B1 = '1945y';
%$ B2 = '1950Y';
%$ B3 = '1950a';
%$ B4 = '1953A';
%$
%$ % Define expected results.
%$ e.time = [1945 1; 1950 1; 1950 1; 1953 1];
%$ e.time = [1945; 1950; 1950; 1953];
%$ e.freq = 1;
%$
%$ % Call the tested routine.
%$ d = dates(B1,B2,B3,B4);
%$
%$ % Check the results.
%$ t(1) = dassert(d.time, e.time);
%$ t(2) = dassert(d.freq, e.freq);
%$ t(3) = dassert(d.ndat(), size(e.time, 1));
%$ t(1) = isequal(d.time, e.time);
%$ t(2) = isequal(d.freq, e.freq);
%$ t(3) = isequal(d.ndat(), size(e.time, 1));
%$ T = all(t);
%@eof:3
%@eof:4
%@test:4
%@test:5
%$ % Define a dates object
%$ B = dates('1950H1'):dates('1960H2');
%$
%$
%$ % Call the tested routine.
%$ d = B(2);
%$ if isa(d,'dates')
%$ t(1) = true;
%$ else
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = isequal(d.freq,B.freq);
%$ t(3) = isequal(d.time,1950*2+2);
%$ end
%$ T = all(t);
%@eof:5
%@test:6
%$ % Define a dates object
%$ B = dates('1950Q1'):dates('1960Q3');
%$
......@@ -199,74 +307,74 @@ end % classdef
%$ % Call the tested routine.
%$ d = B(2);
%$ if isa(d,'dates')
%$ t(1) = 1;
%$ t(1) = true;
%$ else
%$ t(1) = 0;
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(d.freq,B.freq);
%$ t(3) = dassert(d.time,[1950 2]);
%$ t(2) = isequal(d.freq,B.freq);
%$ t(3) = isequal(d.time,1950*4+2);
%$ end
%$ T = all(t);
%@eof:4
%@eof:6
%@test:5
%@test:7
%$ % Define a dates object
%$ B = dates(4,1950,1):dates(4,1960,3);
%$
%$ % Call the tested routine.
%$ d = B(2);
%$ if isa(d,'dates')
%$ t(1) = 1;
%$ t(1) = true;
%$ else
%$ t(1) = 0;
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(d.freq,B.freq);
%$ t(3) = dassert(d.time,[1950 2]);
%$ t(2) = isequal(d.freq,B.freq);
%$ t(3) = isequal(d.time,1950*4+2);
%$ end
%$ T = all(t);
%@eof:5
%@eof:7
%@test:6
%@test:8
%$ % Define a dates object
%$ B = dates(4,[1950 1]):dates(4,[1960 3]);
%$
%$ % Call the tested routine.
%$ d = B(2);
%$ if isa(d,'dates')
%$ t(1) = 1;
%$ t(1) = true;
%$ else
%$ t(1) = 0;
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(d.freq,B.freq);
%$ t(3) = dassert(d.time,[1950 2]);
%$ t(2) = isequal(d.freq,B.freq);
%$ t(3) = isequal(d.time,1950*4+2);
%$ end
%$ T = all(t);
%@eof:6
%@eof:8
%@test:7
%@test:9
%$ try
%$ B = dates(4,[1950; 1950], [1; 2]);
%$ t = 1;
%$ t = true;
%$ catch
%$ t = 0;
%$ t = false;
%$ end
%$
%$ T = all(t);
%@eof:7
%@eof:9
%@test:8
%@test:10
%$ try
%$ B = dates(4,[1950, 1950], [1, 2]);
%$ t = 1;
%$ B = dates(365,[1956; 1956], [1; 1], [12; 13]);
%$ t = true;
%$ catch
%$ t = 0;
%$ t = false;
%$ end
%$
%$ T = all(t);
%@eof:8
%@eof:10
......@@ -8,7 +8,7 @@ function disp(o)
% OUTPTS
% None
% Copyright (C) 2013-2017 Dynare Team
% Copyright © 2013-2017 Dynare Team
%
% 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
......@@ -21,6 +21,6 @@ function disp(o)
% 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/>.
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
fprintf(['\n' dates4display(o, inputname(1), Inf) '\n\n']);
\ No newline at end of file
......@@ -8,7 +8,7 @@ function display(o)
% OUTPTS
% None
% Copyright (C) 2013-2017 Dynare Team
% Copyright © 2013-2017 Dynare Team
%
% 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
......@@ -21,6 +21,6 @@ function display(o)
% 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/>.
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
fprintf(['\n' dates4display(o, inputname(1), 5) '\n\n']);
\ No newline at end of file
function [m, f] = double(o) % --*-- Unitary tests --*--
function [m, f] = double(o)
% Returns a vector of doubles with the fractional part corresponding
% to the subperiod. Used for plots and to store dates in a matrix.
......@@ -8,12 +8,12 @@ function [m, f] = double(o) % --*-- Unitary tests --*--
%
% OUTPUTS
% - m [double] o.ndat*1 vector of doubles.
% - f [integer] scalar, the frequency (1, 4, 12 or 52).
% - f [integer] scalar, the frequency (1, 2, 4, or 12).
%
% REMARKS
% Obviously the frequency is lost during the conversion.
% Copyright (C) 2013-2017 Dynare Team
% Copyright © 2013-2022 Dynare Team
%
% 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
......@@ -26,50 +26,84 @@ function [m, f] = double(o) % --*-- Unitary tests --*--
% 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/>.
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
if o.freq==365 || o.freq==52
error('This method is not implemented for daily frequency.')
% Would need to find a way to deal with leap years and long years.
end
if o.freq == 1
m = o.time;
else
years = floor((o.time-1)/o.freq);
subperiods = o.time-years*o.freq;
m = years+(subperiods-1)/o.freq;
end
m = o.time(:,1)+(o.time(:,2)-1)/o.freq;
if nargout>1
f = o.freq;
end
return % --*-- Unit tests --*--
%@test:1
%$ % Define a dates object
%$ B = dates('1950Q1'):dates('1951Q1');
%$
%$ % Call the tested routine.
%$ try
%$ C = double(B);
%$ t(1) = 1;
%$ catch
%$ t(1) = 0;
%$ end
%$
%$ % Define expected results.
%$ E = [ones(4,1)*1950; 1951];
%$ E = E + [(transpose(1:4)-1)/4; 0];
%$ if t(1)
%$ t(2) = dassert(C,E);
%$ end
%$ T = all(t);
% Define a dates object
B = dates('1950Q1'):dates('1951Q1');
% Call the tested routine.
try
C = double(B);
t(1) = 1;
catch
t(1) = 0;
end
% Define expected results.
E = [ones(4,1)*1950; 1951];
E = E + [(transpose(1:4)-1)/4; 0];
if t(1)
t(2) = isequal(C,E);
end
T = all(t);
%@eof:1
%@test:2
%$ % Call the tested routine.
%$ try
%$ C = NaN(2,1);
%$ C(1) = double(dates('1950Q1'));
%$ C(2) = double(dates('1950Q2'));
%$ t(1) = 1;
%$ catch
%$ t(1) = 0;
%$ end
%$
%$ % Define expected results.
%$ E = ones(2,1)*1950;
%$ E = E + [0; .25];
%$ if t(1)
%$ t(2) = dassert(C,E);
%$ end
%$ T = all(t);
%@eof:2
\ No newline at end of file
% Call the tested routine.
try
C = NaN(2,1);
C(1) = double(dates('1950Q1'));
C(2) = double(dates('1950Q2'));
t(1) = 1;
catch
t(1) = 0;
end
% Define expected results.
E = ones(2,1)*1950;
E = E + [0; .25];
if t(1)
t(2) = isequal(C,E);
end
T = all(t);
%@eof:2
%@test:3
% Regression test for dseries#47
B = dates('1950Y'):dates('1952Y');
% Call the tested routine.
try
C = double(B);
t(1) = 1;
catch
t(1) = 0;
end
% Define expected results.
E = [1950; 1951; 1952];
if t(1)
t(2) = isequal(C,E);
end
T = all(t);
%@eof:3
function lastIndex = end(o, k, n) % --*-- Unitary tests --*--
function lastIndex = end(o, k, n)
% Overloads end keyword.
%
......@@ -10,7 +10,7 @@ function lastIndex = end(o, k, n) % --*-- Unitary tests --*--
% OUTPUTS
% lastIndex [integer] last dates index
% Copyright (C) 2013-2017 Dynare Team
% Copyright © 2013-2022 Dynare Team
%
% 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
......@@ -23,27 +23,29 @@ function lastIndex = end(o, k, n) % --*-- Unitary tests --*--
% 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/>.
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
assert(k==1 && n==1, 'dates:end:ArgCheck', 'dates only has one dimension');
lastIndex = o.ndat();
return % --*-- Unit tests --*--
%@test:1
%$ % Define a dates object
%$ o = dates('1938Q4'):dates('2015Q4');
%$ q = dates('2015Q4');
%$
%$ % Call the tested routine.
%$ try
%$ p = o(end);
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(p, q);
%$ end
%$
%$ T = all(t);
% Define a dates object
o = dates('1938Q4'):dates('2015Q4');
q = dates('2015Q4');
% Call the tested routine.
try
p = o(end);
t(1) = true;
catch
t(1) = false;
end
if t(1)
t(2) = dassert(p, q);
end
T = all(t);
%@eof:1
\ No newline at end of file
function l = eq(varargin) % --*-- Unitary tests --*--
function l = eq(varargin)
% Overloads == operator for dates objects.
%
......@@ -9,7 +9,7 @@ function l = eq(varargin) % --*-- Unitary tests --*--
% OUTPUTS
% - l [logical] column vector of max(n,1) elements (zeros or ones).
% Copyright (C) 2013-2015 Dynare Team
% Copyright © 2013-2022 Dynare Team
%
% 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
......@@ -22,7 +22,7 @@ function l = eq(varargin) % --*-- Unitary tests --*--
% 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/>.
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
if varargin{1}.ndat>1 && varargin{2}.ndat>1 && ~isequal(varargin{1}.ndat, varargin{2}.ndat)
l = false;
......@@ -32,59 +32,106 @@ end
[o, p] = comparison_arg_checks(varargin{:});
if isequal(o.ndat(), p.ndat())
l = logical(transpose(all(transpose(eq(o.time,p.time)))));
l = eq(o.time(:),p.time(:));
else
l = logical(transpose(all(transpose(bsxfun(@eq,o.time,p.time)))));
l = bsxfun(@eq,o.time(:,1),p.time(:,1));
end
return % --*-- Unit tests --*--
%@test:1
%$ % Define some dates objects
%$ d1 = dates('1950Q1','1950Q2','1950Q3','1950Q4') ;
%$ d2 = dates('1960Q1','1960Q2','1960Q3','1960Q4') ;
%$ d3 = dates('1950Q1','1960Q2','1950Q3','1960Q4') ;
%$
%$ % Call the tested routine.
%$ t1 = d1==d1;
%$ t2 = d1==d2;
%$ t3 = d1==d3;
%$
%$ % Check the results.
%$ t(1) = dassert(t1,true(4,1));
%$ t(2) = dassert(t2,false(4,1));
%$ t(2) = dassert(t3,[true; false; true; false]);
%$ T = all(t);
% Define some dates objects
d1 = dates('1950Q1','1950Q2','1950Q3','1950Q4') ;
d2 = dates('1960Q1','1960Q2','1960Q3','1960Q4') ;
d3 = dates('1950Q1','1960Q2','1950Q3','1960Q4') ;
% Call the tested routine.
try
t1 = d1==d1;
t2 = d1==d2;
t3 = d1==d3;
t(1) = true;
catch
t(1) = false;
end
% Check the results.
if t(1)
t(2) = isequal(t1, true(4,1));
t(3) = isequal(t2, false(4,1));
t(4) = isequal(t3, [true; false; true; false]);
end
T = all(t);
%@eof:1
%@test:2
%$ % Define some dates objects
%$ d1 = dates('1950Q1') ;
%$ d2 = dates('1960Q1') ;
%$ d3 = dates('1960Q1') ;
%$
%$ % Call the tested routine.
%$ t1 = d1==d1;
%$ t2 = d1==d2;
%$ t3 = d1==d3;
%$
%$ % Check the results.
%$ t(1) = dassert(t1,true);
%$ t(2) = dassert(t2,false);
%$ t(2) = dassert(t3,false);
%$ T = all(t);
% Define some dates objects
d1 = dates('1950Q1') ;
d2 = dates('1960Q1') ;
d3 = dates('1960Q1') ;
% Call the tested routine.
try
t1 = d1==d1;
t2 = d1==d2;
t3 = d1==d3;
t(1) = true;
catch
t(1) = false;
end
% Check the results.
if t(1)
t(2) = t1;
t(3) = ~t2;
t(4) = ~t3;
end
T = all(t);
%@eof:2
%@test:3
%$ % Define some dates objects
%$ d1 = dates('1950Q1','1950Q2','1950Q3','1950Q4') ;
%$ d2 = dates('1950Q2') ;
%$ d3 = dates('1970Q1') ;
%$
%$ % Call the tested routine.
%$ t1 = d1==d2;
%$ t2 = d1==d3;
%$
%$ % Check the results.
%$ t(1) = dassert(t1,[false; true; false; false]);
%$ t(2) = dassert(t2,false(4,1));
%$ T = all(t);
% Define some dates objects
d1 = dates('1950Q1','1950Q2','1950Q3','1950Q4') ;
d2 = dates('1950Q2') ;
d3 = dates('1970Q1') ;
% Call the tested routine.
try
t1 = d1==d2;
t2 = d1==d3;
t(1) = true;
catch
t(1) = false;
end
% Check the results.
if t(1)
t(2) = isequal(t1, [false; true; false; false]);
t(3) = isequal(t2, false(4,1));
end
T = all(t);
%@eof:3
%@test:4
% Define some dates objects
d1 = dates('2000-01-01','2000-01-02','2000-01-03');
d2 = dates('2000-01-02');
% Call the tested routine.
try
t1 = d1==d2;
t(1) = true;
catch
t(1) = false;
end
% Check the results.
if t(1)
t(2) = ~t1(1) && t1(2) && ~t1(3);
end
T = all(t);
%@eof:4
function l = ge(varargin) % --*-- Unitary tests --*--
function l = ge(varargin)
% Overloads the >= operator for dates objects.
%
% INPUTS
% - o [dates] dates object with n or 1 elements.
% - p [dates] dates object with n or 1 elements.
% - o [dates] object with n or 1 elements.
% - p [dates] object with n or 1 elements.
%
% OUTPUTS
% - l [logical] column vector of max(n,1) elements (zeros or ones).
% Copyright (C) 2013-2017 Dynare Team
% Copyright © 2013-2022 Dynare Team
%
% 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
......@@ -22,74 +22,58 @@ function l = ge(varargin) % --*-- Unitary tests --*--
% 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/>.
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
[o, p] = comparison_arg_checks(varargin{:});
if isequal(o.ndat(), p.ndat())
l = (o==p);
idx = find(l==false);
for i=1:length(idx)
l(idx(i)) = greaterorequal(o.time(idx(i),:), p.time(idx(i),:));
end
else
if isequal(o.ndat(),1)
l = false(p.ndat(),1);
for i=1:p.ndat()
l(i) = greaterorequal(o.time, p.time(i,:));
end
else
l = false(o.ndat(),1);
for i=1:o.ndat()
l(i) = greaterorequal(o.time(i,:), p.time);
end
end
end
l = o.time>=p.time;
return % --*-- Unit tests --*--
%@test:1
%$ % Define some dates
%$ date_2 = '1950Q2';
%$ date_3 = '1950Q3';
%$ date_4 = '1950Q1';
%$ date_5 = '1949Q2';
%$
%$ % Call the tested routine.
%$ d2 = dates(date_2);
%$ d3 = dates(date_3);
%$ d4 = dates(date_4);
%$ d5 = dates(date_5);
%$ i1 = (d2>=d3);
%$ i2 = (d3>=d4);
%$ i3 = (d4>=d2);
%$ i4 = (d5>=d4);
%$ i5 = (d5>=d5);
%$
%$ % Check the results.
%$ t(1) = dassert(i1,false);
%$ t(2) = dassert(i2,true);
%$ t(3) = dassert(i3,false);
%$ t(4) = dassert(i4,false);
%$ t(5) = dassert(i5,true);
%$ T = all(t);
% Define some dates
date_2 = '1950Q2';
date_3 = '1950Q3';
date_4 = '1950Q1';
date_5 = '1949Q2';
% Call the tested routine.
d2 = dates(date_2);
d3 = dates(date_3);
d4 = dates(date_4);
d5 = dates(date_5);
i1 = (d2>=d3);
i2 = (d3>=d4);
i3 = (d4>=d2);
i4 = (d5>=d4);
i5 = (d5>=d5);
% Check the results.
t(1) = isequal(i1,false);
t(2) = isequal(i2,true);
t(3) = isequal(i3,false);
t(4) = isequal(i4,false);
t(5) = isequal(i5,true);
T = all(t);
%@eof:1
%@test:2
%$ % Define some dates
%$ B1 = '1945Q1';
%$ B2 = '1945Q2';
%$ B3 = '1945Q3';
%$ B4 = '1945Q4';
%$ B5 = '1950Q1';
%$
%$ % Create dates objects.
%$ dd = dates(B1,B2,B3,B4);
%$
%$ % Check the results.
%$ t(1) = dassert(dates(B1)>=dates(B2),false);
%$ t(2) = dassert(dates(B2)>=dates(B1),true);
%$ t(3) = dassert(dates(B2)>=dates(B2),true);
%$ t(4) = dassert(dd>=dates(B5),false(4,1));
%$ t(5) = dassert(dates(B5)>=dd,true(4,1));
%$ t(6) = dassert(dates(B1)>=dd,[true; false(3,1)]);
%$ T = all(t);
% Define some dates
B1 = '1945Q1';
B2 = '1945Q2';
B3 = '1945Q3';
B4 = '1945Q4';
B5 = '1950Q1';
% Create dates objects.
dd = dates(B1,B2,B3,B4);
% Check the results.
t(1) = isequal(dates(B1)>=dates(B2),false);
t(2) = isequal(dates(B2)>=dates(B1),true);
t(3) = isequal(dates(B2)>=dates(B2),true);
t(4) = isequal(dd>=dates(B5),false(4,1));
t(5) = isequal(dates(B5)>=dd,true(4,1));
t(6) = isequal(dates(B1)>=dd,[true; false(3,1)]);
T = all(t);
%@eof:2