...
 
Commits (6)
......@@ -13,7 +13,7 @@ function q = backcast(o, p, diff) % --*-- Unitary tests --*--
% The two dseries objects must have common frequency and the same number of variables. Also the
% two samples must overlap.
% Copyright (C) 2019 Dynare Team
% Copyright © 2019-2020 Dynare Team
%
% This file is part of Dynare.
%
......@@ -31,7 +31,12 @@ function q = backcast(o, p, diff) % --*-- Unitary tests --*--
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
q = copy(o);
q.backcast_(p);
if nargin>2
q.backcast_(p, diff);
else
q.backcast_(p);
end
return
......
function o = backcast_(o, p, diff) % --*-- Unitary tests --*--
% Copyright (C) 2019 Dynare Team
% Copyright © 2019-2020 Dynare Team
%
% This file is part of Dynare.
%
......@@ -43,11 +43,11 @@ idp = find(o.dates(1)==p.dates);
if diff
FirstDifference = p.data(2:idp,:)-p.data(1:idp-1,:);
CumulatedDifferences = rcumsum(FirstDifference);
o.data = [bsxfun(@minus, o.data(1:end), CumulatedDifferences); o.data];
o.data = [bsxfun(@minus, o.data(1), CumulatedDifferences); o.data];
else
GrowthFactor = p.data(2:idp,:)./p.data(1:idp-1,:);
CumulatedGrowthFactors = rcumprod(GrowthFactor);
o.data = [bsxfun(@rdivide, o.data(1:end), CumulatedGrowthFactors); o.data];
o.data = [bsxfun(@rdivide, o.data(1), CumulatedGrowthFactors); o.data];
end
o.dates = firstdate(p):lastdate(o);
......
function o = flip(o) % --*-- Unitary tests --*--
% Flips the rows in the data member (without changing the
% periods order)
%
% INPUTS
% - o [dseries]
%
% OUTPUTS
% - o [dseries]
% Copyright © 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.flip_;
return
%@test:1
% Define a dates object
data = transpose(1:3);
o = dseries(data);
q = dseries(data);
% Call the tested routine.
try
p = o.flip();
t(1) = true;
catch
t(1) = false;
end
if t(1)
t(2) = dassert(o, q);
t(3) = dassert(p.data, [3; 2; 1]);
end
T = all(t);
%@eof:1
\ No newline at end of file
function o = flip_(o) % --*-- Unitary tests --*--
% Flips the rows in the data member (without changing the
% periods order)
%
% INPUTS
% - o [dseries]
%
% OUTPUTS
% - o [dseries]
% Copyright © 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/>.
for i=1:vobs(o)
if isempty(o.ops{i})
o.ops(i) = {['flip(' o.name{i} ')']};
else
o.ops(i) = {['flip(' o.ops{i} ')']};
end
end
o.data = flipud(o.data);
return
%@test:1
% Define a dates object
data = [transpose(1:3) transpose(4:6)];
o = dseries(data);
q = o;
r = copy(o);
% Call the tested routine.
try
o.flip_();
t(1) = true;
catch
t(1) = false;
end
if t(1)
t(2) = dassert(o.data, [3 6; 2 5; 1 4]);
t(3) = dassert(q.data, [3 6; 2 5; 1 4]);
t(4) = dassert(r.data, data);
t(5) = dassert(o.name{1}, 'Variable_1') && dassert(o.name{2}, 'Variable_2');
t(6) = dassert(q.name{1}, 'Variable_1') && dassert(q.name{2}, 'Variable_2');
t(7) = dassert(r.name{1}, 'Variable_1') && dassert(r.name{2}, 'Variable_2');
t(8) = dassert(o.ops{1}, 'flip(Variable_1)') && dassert(o.ops{2}, 'flip(Variable_2)');
t(9) = dassert(q.ops{1}, 'flip(Variable_1)') && dassert(q.ops{2}, 'flip(Variable_2)');
end
T = all(t);
%@eof:1
%@test:2
%$ % Define a dates object
%$ data = [ones(10,1), -ones(10,1)];
%$ o = dseries(data);
%$ q = o;
%$ r = copy(o);
%$
%$ % Call the tested routine.
%$ try
%$ o.abs_();
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(length(o.name), 2);
%$ t(3) = dassert(o.name{1},'Variable_1');
%$ t(4) = dassert(o.name{2},'Variable_2');
%$ t(5) = dassert(q.name{1},'Variable_1');
%$ t(6) = dassert(q.name{2},'Variable_2');
%$ t(7) = dassert(r.name{1},'Variable_1');
%$ t(8) = dassert(r.name{2},'Variable_2');
%$ t(9) = dassert(o.ops{1},'abs(Variable_1)');
%$ t(10) = dassert(o.ops{2},'abs(Variable_2)');
%$ t(11) = dassert(q.ops{1},'abs(Variable_1)');
%$ t(12) = dassert(q.ops{2},'abs(Variable_2)');
%$ t(13) = isempty(r.ops{1});
%$ t(14) = isempty(r.ops{2});
%$ end
%$
%$ T = all(t);
%@eof:2
\ No newline at end of file
function m = nanmean(o) % --*-- Unitary tests --*--
function m = nanmean(o, geometric) % --*-- Unitary tests --*--
% Returns the mean of the variables in a @dseries object o.
% Returns the mean of the variables in a @dseries object o (robust
% to the presence of NaNs).
%
% INPUTS
% o o dseries object [mandatory].
% o geometric logical [default is false], if true returns the geometric mean.
% - o dseries object [mandatory].
% - geometric logical [default is false], if true returns the geometric mean.
%
% OUTPUTS
% o m 1*vobs(o) vector of doubles.
% - m 1*vobs(o) vector of doubles.
% Copyright (C) 2019 Dynare Team
% Copyright © 2019-2020 Dynare Team
%
% This file is part of Dynare.
%
......@@ -26,26 +27,59 @@ function m = nanmean(o) % --*-- Unitary tests --*--
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
m = nanmean(o.data);
if nargin<2
geometric = false;
end
if geometric
m = NaN(1,o.vobs());
for i = 1:o.vobs()
tmp = o.data(~isnan(o.data(:,i)),i);
m(i) = prod(tmp)^(1.0/length(tmp));
end
else
m = nanmean(o.data);
end
return
%@test:1
%$ % Define a dataset.
%$ A = repmat([1.005, 1.05], 10, 1);
%$ A(3,1) = NaN;
%$ A(5,2) = NaN;
%$
%$ % Instantiate a time series object and compute the mean.
%$ try
%$ ts = dseries(A);
%$ m = nanmean(ts);
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(isequal(size(m),[1, 2]), true);
%$ t(3) = dassert(m, [1.005, 1.05]);
%$ end
%$ T = all(t);
%@eof:1
\ No newline at end of file
% Define a dataset.
A = repmat([1.005, 1.05], 10, 1);
A(3,1) = NaN;
A(5,2) = NaN;
% Instantiate a time series object and compute the mean.
try
ts = dseries(A);
m = nanmean(ts);
t(1) = 1;
catch
t = 0;
end
if t(1)
t(2) = dassert(isequal(size(m),[1, 2]), true);
t(3) = dassert(m, [1.005, 1.05]);
end
T = all(t);
%@eof:1
%@test:2
% Define a dataset.
a = [1 0; NaN 2; 3 4];
% Instantiate a time series object and compute the geometric mean.
try
ts = dseries(a);
m = nanmean(ts, true);
t(1) = 1;
catch
t = 0;
end
if t(1)
t(2) = dassert(isequal(size(m),[1, 2]), true);
t(3) = dassert(m, [sqrt(3), 0]);
end
T = all(t);
%@eof:2
\ No newline at end of file
......@@ -116,6 +116,7 @@ switch S(1).type
'diff', 'diff_', ...
'mdiff','mdiff_', ...
'abs','abs_', ...
'flip','flip_', ...
'isnan', ...
'isinf', ...
'isreal', ...
......
......@@ -423,72 +423,73 @@ if ~isempty(idv)
end
i = 0;
return
%@test:1
%$ try
%$ y = dseries(zeros(400,1),dates('1950Q1')) ;
%$ v = dseries(randn(400,1),dates('1950Q1')) ;
%$ u = dseries(randn(400,1),dates('1950Q1')) ;
%$ from 1950Q2 to 2049Q4 do y(t) = (1+.01*u(t))*y(t-1) + v(t)
%$ t(1) = 1;
%$ catch
%$ t(1) = 0;
%$ end
%$
%$ T = all(t);
try
y = dseries(zeros(400,1),dates('1950Q1')) ;
v = dseries(randn(400,1),dates('1950Q1')) ;
u = dseries(randn(400,1),dates('1950Q1')) ;
from 1950Q2 to 2049Q4 do y(t) = (1+.01*u(t))*y(t-1) + v(t)
t(1) = 1;
catch
t(1) = 0;
end
T = all(t);
%@eof:1
%@test:2
%$ try
%$ y = dseries(zeros(400,1),dates('1950Q1')) ;
%$ v = dseries(randn(400,1),dates('1950Q1')) ;
%$ u = dseries(randn(400,1),dates('1950Q1')) ;
%$ from 1950Q2 to 2049Q3 do y(t) = (1+.01*u(t))*y(t+1) + v(t)
%$ t(1) = 0;
%$ catch
%$ t(1) = 1;
%$ end
%$
%$ T = all(t);
try
y = dseries(zeros(400,1),dates('1950Q1')) ;
v = dseries(randn(400,1),dates('1950Q1')) ;
u = dseries(randn(400,1),dates('1950Q1')) ;
from 1950Q2 to 2049Q3 do y(t) = (1+.01*u(t))*y(t+1) + v(t)
t(1) = 0;
catch
t(1) = 1;
end
T = all(t);
%@eof:2
%@test:3
%$ try
%$ y = dseries(zeros(400,1),dates('1950Q1')) ;
%$ v = dseries(randn(400,1),dates('1950Q1')) ;
%$ u = dseries(randn(400,1),dates('1950Q1')) ;
%$ from 1950Q2 to 2049Q4 do y(t) = v(t) -.5*v(t-1);
%$ t(1) = 1;
%$ catch
%$ t(1) = 0;
%$ end
%$
%$ T = all(t);
try
y = dseries(zeros(400,1),dates('1950Q1')) ;
v = dseries(randn(400,1),dates('1950Q1')) ;
u = dseries(randn(400,1),dates('1950Q1')) ;
from 1950Q2 to 2049Q4 do y(t) = v(t) -.5*v(t-1);
t(1) = 1;
catch
t(1) = 0;
end
T = all(t);
%@eof:3
%@test:4
%$ try
%$ y = dseries(zeros(400,1),dates('1950Q1')) ;
%$ v = dseries(randn(400,1),dates('1950Q1')) ;
%$ u = dseries(randn(400,1),dates('1950Q1')) ;
%$ from 1950Q2 to 2049Q4 do y(t) = 2*((v(t) -.5*v(t-1))>0)-1;
%$ t(1) = 1;
%$ catch
%$ t(1) = 0;
%$ end
%$
%$ T = all(t);
try
y = dseries(zeros(400,1),dates('1950Q1')) ;
v = dseries(randn(400,1),dates('1950Q1')) ;
u = dseries(randn(400,1),dates('1950Q1')) ;
from 1950Q2 to 2049Q4 do y(t) = 2*((v(t) -.5*v(t-1))>0)-1;
t(1) = 1;
catch
t(1) = 0;
end
T = all(t);
%@eof:4
%@test:5
%$ try
%$ y = dseries(zeros(4000,1),dates('1950Q1')) ;
%$ v = dseries(randn(4000,1),dates('1950Q1')) ;
%$ u = dseries(randn(4000,1),dates('1950Q1')) ;
%$ from 1950Q2 to 2949Q4 do y(t) = y(t-1)*(2*((v(t) -.5*v(t-1))>u(t))-1)+u(t); %plot(y)
%$ t(1) = 1;
%$ catch
%$ t(1) = 0;
%$ end
%$
%$ T = all(t);
%@eof:5
try
y = dseries(zeros(4000,1),dates('1950Q1')) ;
v = dseries(randn(4000,1),dates('1950Q1')) ;
u = dseries(randn(4000,1),dates('1950Q1')) ;
from 1950Q2 to 2949Q4 do y(t) = y(t-1)*(2*((v(t) -.5*v(t-1))>u(t))-1)+u(t); %plot(y)
t(1) = 1;
catch
t(1) = 0;
end
T = all(t);
%@eof:5
\ No newline at end of file