Commit 5ae52153 authored by Sébastien Villemot's avatar Sébastien Villemot

Merge branch 'master' into 'master'

Add bi-annual frequency.

See merge request !37
parents 7fedc20a fefe79e7
Pipeline #3717 passed with stage
in 1 minute and 15 seconds
......@@ -172,6 +172,27 @@ end % classdef
%@test:3
%$ % Define some dates
%$ B1 = '1945H1';
%$ B2 = '1950H2';
%$ B3 = '1950h1';
%$ B4 = '1953h2';
%$
%$ % Define expected results.
%$ e.time = [1945 1; 1950 2; 1950 1; 1953 2];
%$ e.freq = 2;
%$
%$ % 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 = all(t);
%@eof:3
%@test:4
%$ % Define some dates
%$ B1 = '1945y';
%$ B2 = '1950Y';
%$ B3 = '1950a';
......@@ -189,9 +210,29 @@ end % classdef
%$ t(2) = dassert(d.freq, e.freq);
%$ t(3) = dassert(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) = 1;
%$ else
%$ t(1) = 0;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(d.freq,B.freq);
%$ t(3) = dassert(d.time,[1950 2]);
%$ end
%$ T = all(t);
%@eof:5
%@test:6
%$ % Define a dates object
%$ B = dates('1950Q1'):dates('1960Q3');
%$
......@@ -209,9 +250,9 @@ end % classdef
%$ t(3) = dassert(d.time,[1950 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);
%$
......@@ -228,9 +269,9 @@ end % classdef
%$ t(3) = dassert(d.time,[1950 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]);
%$
......@@ -247,9 +288,9 @@ end % classdef
%$ t(3) = dassert(d.time,[1950 2]);
%$ end
%$ T = all(t);
%@eof:6
%@eof:8
%@test:7
%@test:9
%$ try
%$ B = dates(4,[1950; 1950], [1; 2]);
%$ t = 1;
......@@ -258,9 +299,9 @@ end % classdef
%$ end
%$
%$ T = all(t);
%@eof:7
%@eof:9
%@test:8
%@test:10
%$ try
%$ B = dates(4,[1950, 1950], [1, 2]);
%$ t = 1;
......@@ -269,4 +310,4 @@ end % classdef
%$ end
%$
%$ T = all(t);
%@eof:8
%@eof:10
function s = subperiod(d)
% Returns the subperiod (quarter, month or week depending on the frequency).
% Returns the subperiod (half-year, quarter, month or week depending on the frequency).
% Copyright (C) 2016-2017 Dynare Team
% Copyright (C) 2016-2020 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
......
......@@ -124,7 +124,7 @@ methods
init = dates(1,1);
elseif (isdates(b) && isequal(length(b),1))
init = b;
elseif ischar(b) && isdate(b)% Weekly, Monthly, Quaterly or Annual data (string).
elseif ischar(b) && isdate(b)% Weekly, Monthly, Quaterly, Bi-annual or Annual data (string).
init = dates(b);
elseif (isnumeric(b) && isscalar(b) && isint(b)) % Yearly data.
init = dates([num2str(b) 'Y']);
......
function o = hdiff(o) % --*-- Unitary tests --*--
% Computes bi-annual differences.
%
% INPUTS
% - o [dseries]
%
% OUTPUTS
% - o [dseries]
% Copyright (C) 2020 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
o = copy(o);
o.hdiff_();
%@test:1
%$ try
%$ data = transpose(0:1:50);
%$ ts = dseries(data,'1950H1');
%$ ds = ts.hdiff();
%$ t(1) = 1;
%$ catch
%$ t(1) = 0;
%$ end
%$
%$ if t(1)
%$ DATA = NaN(1,ds.vobs);
%$ DATA = [DATA; ones(ds.nobs-1,ds.vobs)];
%$ t(2) = dassert(ds.data, DATA, 1e-15);
%$ t(3) = dassert(ts.data, data, 1e-15);
%$ end
%$
%$ T = all(t);
%@eof:1
\ No newline at end of file
function o = hdiff_(o) % --*-- Unitary tests --*--
% Computes bi-annual differences.
%
% INPUTS
% - o [dseries]
%
% OUTPUTS
% - o [dseries]
% Copyright (C) 2020 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
switch frequency(o)
case 1
error('dseries::hdiff: I cannot compute bi-annual differences from yearly data!')
case 2
o.data(2:end,:) = o.data(2:end,:)-o.data(1:end-1,:);
o.data(1,:) = NaN;
case 4
o.data(3:end,:) = o.data(3:end,:)-o.data(1:end-2,:);
o.data(1:2,:) = NaN;
case 12
o.data(7:end,:) = o.data(7:end,:)-o.data(1:end-6,:);
o.data(1:6,:) = NaN;
case 52
error('dseries::hdiff: I do not know yet how to compute bi-annual differences from weekly data!')
otherwise
error(['dseries::hdiff: object ' inputname(1) ' has unknown frequency']);
end
for i = 1:vobs(o)
if isempty(o.ops{i})
o.ops(i) = {['hdiff(' o.name{i} ')']};
else
o.ops(i) = {['hdiff(' o.ops{i} ')']};
end
end
%@test:1
%$ try
%$ data = transpose(1:100);
%$ ts = dseries(data,'1950Q1',{'H1'},{'H_1'});
%$ ts.hdiff_;
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$
%$ if t(1)
%$ DATA = NaN(2,ts.vobs);
%$ DATA = [DATA; 2*ones(ts.nobs-2,ts.vobs)];
%$ t(2) = dassert(ts.data,DATA);
%$ t(3) = dassert(ts.ops{1},['hdiff(H1)']);
%$ end
%$
%$ T = all(t);
%@eof:1
%@test:2
%$ try
%$ data = transpose(1:100);
%$ ts = dseries(data,'1950M1',{'H1'},{'H_1'});
%$ ts.hdiff_;
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$
%$ if t(1)
%$ DATA = NaN(6,ts.vobs);
%$ DATA = [DATA; 6*ones(ts.nobs-6,ts.vobs)];
%$ t(2) = dassert(ts.data,DATA);
%$ t(3) = dassert(ts.ops{1},['hdiff(H1)']);
%$ end
%$
%$ T = all(t);
%@eof:2
%@test:3
%$ try
%$ data = transpose(1:100);
%$ ts = dseries(data,'1950W1',{'H1'},{'H_1'});
%$ ts.hdiff_;
%$ t(1) = false;
%$ catch
%$ t(1) = true;
%$ end
%$
%$ T = all(t);
%@eof:3
%@test:4
%$ try
%$ data = transpose(1:100);
%$ ts = dseries(data,'1950Y',{'H1'},{'H_1'});
%$ ts.hdiff_;
%$ t(1) = false;
%$ catch
%$ t(1) = true;
%$ end
%$
%$ T = all(t);
%@eof:4
%@test:5
%$ try
%$ data = transpose(1:100);
%$ ts = dseries(data,'1950H1',{'H1'},{'H_1'});
%$ ts.hdiff_;
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ DATA = NaN(1,ts.vobs);
%$ DATA = [DATA; ones(ts.nobs-1,ts.vobs)];
%$ t(2) = dassert(ts.data,DATA);
%$ t(3) = dassert(ts.ops{1},['hdiff(H1)']);
%$ end
%$
%$ T = all(t);
%@eof:5
function o = hgrowth(o) % --*-- Unitary tests --*--
% Computes bi-annual growth rates.
%
% INPUTS
% - o [dseries]
%
% OUTPUTS
% - o [dseries]
% Copyright (C) 2020 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
o = copy(o);
o.hgrowth_();
%@test:1
%$ t = zeros(2,1);
%$
%$ try
%$ data = repmat(transpose(1:2),100,1);
%$ ts = dseries(data,'1950Q1');
%$ ds = ts.hgrowth;
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$
%$ if length(t)>1
%$ DATA = NaN(2,ts.vobs);
%$ DATA = [DATA; zeros(ts.nobs-2,ts.vobs)];
%$ t(2) = dassert(ds.data,DATA);
%$ t(3) = dassert(ts.data,data);
%$ end
%$
%$ T = all(t);
%@eof:1
\ No newline at end of file
function o = hgrowth_(o) % --*-- Unitary tests --*--
% Computes bi-annual growth rates.
%
% INPUTS
% - o [dseries]
%
% OUTPUTS
% - o [dseries]
% Copyright (C) 2020 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
switch frequency(o)
case 1
error('dseries::hgrowth: I cannot compute bi-annual growth rates from yearly data!')
case 2
o.data(2:end,:) = o.data(2:end,:)./o.data(1:end-1,:) - 1;
o.data(1,:) = NaN;
case 4
o.data(3:end,:) = o.data(3:end,:)./o.data(1:end-2,:) - 1;
o.data(1:2,:) = NaN;
case 12
o.data(7:end,:) = o.data(7:end,:)./o.data(1:end-6,:) - 1;
o.data(1:6,:) = NaN;
case 52
error('dseries::hgrowth: I do not know yet how to compute bi-annual growth rates from weekly data!')
otherwise
error(['dseries::hgrowth: object ' inputname(1) ' has unknown frequency']);
end
for i = 1:vobs(o)
if isempty(o.ops{i})
o.ops(i) = {['hgrowth(' o.name{i} ')']};
else
o.ops(i) = {['hgrowth(' o.ops{i} ')']};
end
end
%@test:1
%$ try
%$ data = (1+.01).^transpose(0:1:50);
%$ ts = dseries(data,'1950H1');
%$ ts.hgrowth_;
%$ t(1) = 1;
%$ catch
%$ t(1) = 0;
%$ end
%$
%$ if t(1)
%$ DATA = NaN(1,ts.vobs);
%$ DATA = [DATA; .01*ones(ts.nobs-1,ts.vobs)];
%$ t(2) = dassert(ts.data,DATA,1e-15);
%$ end
%$
%$ T = all(t);
%@eof:1
%@test:2
%$ try
%$ data = repmat(transpose(1:2),100,1);
%$ ts = dseries(data,'1950Q1');
%$ ts.hgrowth_;
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$
%$ if t(1)
%$ DATA = NaN(2,ts.vobs);
%$ DATA = [DATA; zeros(ts.nobs-2,ts.vobs)];
%$ t(2) = dassert(ts.data,DATA);
%$ end
%$
%$ T = all(t);
%@eof:2
%@test:3
%$ try
%$ data = repmat(transpose(1:6),100,1);
%$ ts = dseries(data,'1950M1');
%$ ts.hgrowth_();
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ DATA = NaN(6,ts.vobs);
%$ DATA = [DATA; zeros(ts.nobs-6,ts.vobs)];
%$ t(2) = dassert(ts.data,DATA);
%$ end
%$
%$ T = all(t);
%@eof:3
......@@ -8,7 +8,7 @@ function o = mdiff_(o) % --*-- Unitary tests --*--
% OUTPUTS
% - o [dseries]
% Copyright (C) 2017 Dynare Team
% Copyright (C) 2017-2020 Dynare Team
%
% This file is part of Dynare.
%
......@@ -28,6 +28,8 @@ function o = mdiff_(o) % --*-- Unitary tests --*--
switch frequency(o)
case 1
error('dseries::mdiff: I cannot compute monthly differences from yearly data!')
case 2
error('dseries::mdiff: I cannot compute monthly differences from bi-annual data!')
case 4
error('dseries::mdiff: I cannot compute monthly differences from quarterly data!')
case 12
......
......@@ -8,7 +8,7 @@ function o = mgrowth_(o) % --*-- Unitary tests --*--
% OUTPUTS
% - o [dseries]
% Copyright (C) 2017 Dynare Team
% Copyright (C) 2017-2020 Dynare Team
%
% This file is part of Dynare.
%
......@@ -28,6 +28,8 @@ function o = mgrowth_(o) % --*-- Unitary tests --*--
switch frequency(o)
case 1
error('dseries::mgrowth: I cannot compute monthly growth rates from yearly data!')
case 2
error('dseries::mgrowth: I cannot compute monthly growth rates from bi-annual data!')
case 4
error('dseries::mgrowth: I cannot compute monthly growth rates from quaterly data!')
case 12
......
......@@ -8,7 +8,7 @@ function o = qdiff_(o) % --*-- Unitary tests --*--
% OUTPUTS
% - o [dseries]
% Copyright (C) 2012-2017 Dynare Team
% Copyright (C) 2012-2020 Dynare Team
%
% This file is part of Dynare.
%
......@@ -28,6 +28,8 @@ function o = qdiff_(o) % --*-- Unitary tests --*--
switch frequency(o)
case 1
error('dseries::qdiff: I cannot compute quaterly differences from yearly data!')
case 2
error('dseries::qdiff: I cannot compute quaterly differences from bi-annual data!');
case 4
o.data(2:end,:) = o.data(2:end,:)-o.data(1:end-1,:);
o.data(1,:) = NaN;
......
......@@ -8,7 +8,7 @@ function o = qgrowth_(o) % --*-- Unitary tests --*--
% OUTPUTS
% - o [dseries]
% Copyright (C) 2012-2017 Dynare Team
% Copyright (C) 2012-2020 Dynare Team
%
% This file is part of Dynare.
%
......@@ -28,6 +28,8 @@ function o = qgrowth_(o) % --*-- Unitary tests --*--
switch frequency(o)
case 1
error('dseries::qgrowth: I cannot compute quaterly growth rates from yearly data!')
case 2
error('dseries::qgrowth: I cannot compute quaterly growth rates from bi-annual data!')
case 4
o.data(2:end,:) = o.data(2:end,:)./o.data(1:end-1,:) - 1;
o.data(1,:) = NaN;
......
......@@ -15,7 +15,7 @@ function r = subsref(o, S) % --*-- Unitary tests --*--
% by applying a public method on `o`, or a dseries object built by extracting
% a variable from `o`, or a dseries object containing a subsample.
% Copyright (C) 2011-2019 Dynare Team
% Copyright (C) 2011-2020 Dynare Team
%
% This file is part of Dynare.
%
......@@ -109,9 +109,11 @@ switch S(1).type
'log','log_', ...
'exp','exp_', ...
'ygrowth','ygrowth_', ...
'hgrowth','hgrowth_', ...
'qgrowth','qgrowth_', ...
'mgrowth','mgrowth_', ...
'ydiff','ydiff_', ...
'hdiff','hdiff_', ...
'qdiff','qdiff_', ...
'diff', 'diff_', ...
'mdiff','mdiff_', ...
......@@ -725,3 +727,20 @@ return
T = all(t);
%@eof:19
%@test:20
try
tseries = dseries(dates('2000H1'));
ts = tseries(ones(6,1));
t(1) = true;
catch
t(1) = false;
end
if t(1)
t(2) = ts.dates(1)==dates('2000H1');
t(3) = ts.dates(6)==dates('2002H2');
end
T = all(t);
%@eof:20
\ No newline at end of file
......@@ -8,7 +8,7 @@ function o = ydiff_(o) % --*-- Unitary tests --*--
% OUTPUTS
% - o [dseries]
% Copyright (C) 2012-2017 Dynare Team
% Copyright (C) 2012-2020 Dynare Team
%
% This file is part of Dynare.
%
......@@ -29,6 +29,9 @@ switch frequency(o)
case 1
o.data(2:end,:) = o.data(2:end,:)-o.data(1:end-1,:);
o.data(1,:) = NaN;
case 2
o.data(3:end,:) = o.data(3:end,:)-o.data(1:end-2,:);
o.data(1:2,:) = NaN;
case 4
o.data(5:end,:) = o.data(5:end,:)-o.data(1:end-4,:);
o.data(1:4,:) = NaN;
......@@ -132,3 +135,23 @@ end
%$
%$ T = all(t);
%@eof:4
%@test:5
%$ try
%$ data = transpose(1:100);
%$ ts = dseries(data,'1950H1',{'A1'},{'A_1'});
%$ ts.ydiff_;
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ DATA = NaN(2,ts.vobs);
%$ DATA = [DATA; 2*ones(ts.nobs-2,ts.vobs)];
%$ t(2) = dassert(ts.data,DATA);
%$ t(3) = dassert(ts.ops{1},['ydiff(A1)']);
%$ end
%$
%$ T = all(t);
%@eof:5
......@@ -8,7 +8,7 @@ function o = ygrowth_(o) % --*-- Unitary tests --*--
% OUTPUTS
% - o [dseries]
% Copyright (C) 2012-2017 Dynare Team
% Copyright (C) 2012-2020 Dynare Team
%
% This file is part of Dynare.
%
......@@ -29,6 +29,9 @@ switch frequency(o)
case 1
o.data(2:end,:) = o.data(2:end,:)./o.data(1:end-1,:) - 1;
o.data(1,:) = NaN;
case 2
o.data(3:end,:) = o.data(3:end,:)./o.data(1:end-2,:) - 1;
o.data(1:2,:) = NaN;
case 4