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 (53)
Showing
with 536 additions and 235 deletions
function [a,b] = align(a, b) % --*-- Unitary tests --*--
%@info:
%! @deftypefn {Function File} {[@var{a}, @var{b}] =} align (@var{a}, @var{b})
%! @anchor{dseries/align}
......@@ -27,7 +27,7 @@ function [a,b] = align(a, b) % --*-- Unitary tests --*--
%! @end deftypefn
%@eod:
% Copyright (C) 2013 Dynare Team
% Copyright (C) 2013-2017 Dynare Team
%
% This file is part of Dynare.
%
......
......@@ -3,23 +3,23 @@ function ts = baxter_king_filter(ts, high_frequency, low_frequency, K) % --*-- U
% ts = baxter_king_filter(ts, high_frequency, low_frequency, K)
%
% Implementation of Baxter and King (1999) band pass filter for dseries objects. The code is adapted from
% the one provided by Baxter and King. This filter isolates business cycle fluctuations with a period of length
% the one provided by Baxter and King. This filter isolates business cycle fluctuations with a period of length
% ranging between high_frequency to low_frequency (quarters).
%
% INPUTS
% INPUTS
% o ts dseries object.
% o high_frequency positive scalar, period length (default value is 6).
% o low_frequency positive scalar, period length (default value is 32).
% o K positive scalar integer, truncation parameter (default value is 12).
%
% OUTPUTS
% OUTPUTS
% o ts dseries object.
%
% REMARKS
% This filter use a (symmetric) moving average smoother, so that K observations at the beginning and at the end of the
% REMARKS
% This filter use a (symmetric) moving average smoother, so that K observations at the beginning and at the end of the
% sample are lost in the computation of the filter.
% Copyright (C) 2013 Dynare Team
% Copyright (C) 2013-2017 Dynare Team
%
% This file is part of Dynare.
%
......@@ -57,7 +57,7 @@ if nargin<4 || isempty(K)
end
end
end
% translate periods into frequencies.
hf=2.0*pi/high_frequency;
lf=2.0*pi/low_frequency;
......@@ -88,7 +88,7 @@ tmp = zeros(size(ts.data));
% Filtering step.
for t = K+1:nobs(ts)-K
tmp(t,:) = weights'*ts.data(t-K:t+K,:);
tmp(t,:) = weights'*ts.data(t-K:t+K,:);
end
% Update dseries object.
......@@ -102,7 +102,7 @@ ts.dates = init:init+(nobs(ts)-1);
%$ % Create a dataset.
%$ e = .2*randn(200,1);
%$ u = randn(200,1);
%$ stochastic_trend = cumsum(e);
%$ stochastic_trend = cumsum(e);
%$ deterministic_trend = .1*transpose(1:200);
%$ x = zeros(200,1);
%$ for i=2:200
......
function o = center(o, geometric) % --*-- Unitary tests --*--
% Centers @dseries object o around its mean (arithmetic or geometric).
%
% INPUTS
% o o dseries object [mandatory].
% o geometric logical [default is false], if true returns the geometric mean.
%
% OUTPUTS
% o o o
% Copyright (C) 2016-2017 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/>.
if nargin<2
geometric = false;
end
if geometric
o = o/mean(o, true);
else
o = o-mean(o, false);
end
%@test:1
%$ % Define a dataset.
%$ A = repmat([1.005, 1.05], 10, 1);
%$
%$ % Instantiate a time series object and compute the mean.
%$ try
%$ ts = dseries(A);
%$ ts = center(ts, true);
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if t(1)
%$ t(2) = all(all(abs(ts.data-ones(10,2))<1e-12));
%$ end
%$ T = all(t);
%@eof:1
......@@ -10,7 +10,7 @@ function B = cumprod(varargin) % --*-- Unitary tests --*--
% OUTPUTS
% o B dseries object.
% Copyright (C) 2014 Dynare Team
% Copyright (C) 2014-2017 Dynare Team
%
% This file is part of Dynare.
%
......@@ -27,28 +27,45 @@ function B = cumprod(varargin) % --*-- 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/>.
% Get indices of the columns without NaNs
idx = find(~any(isnan(varargin{1}.data)));
% Get the firt observation number where all the variables are observed (ie without NaNs)
idx = find(all(~isnan(varargin{1}.data), 2),1);
if isempty(idx)
error('dseries::cumprod: All the variables have NaNs. The cumulated product cannot be computed!')
idx = 0;
end
if ~isequal(idx(:),transpose(1:vobs(varargin{1})))
warning('dseries::cumprod: The cumulated product is not computed for some variables because they have NaNs!')
% Is the first period where variables are observed common?
common_first_period_witout_nan = true;
if ~idx
if any(~isnan(varargin{1}.data(:)))
common_first_period_witout_nan = false;
end
else
if idx>1
if any(any(~isnan(varargin{1}.data(1:idx-1,:))))
common_first_period_witout_nan = false;
end
end
end
switch nargin
case 1
% Initialize the output.
B = varargin{1};
% Perform the cumulated sum
B.data(:,idx) = cumprod(B.data(:,idx));
% Change the name of the variables
for i=1:vobs(B)
B.name(i) = {['cumprod(' B.name{i} ')']};
B.tex(i) = {['\prod_t ' B.tex{i}]};
end
case 1
% Initialize the output.
B = varargin{1};
% Perform the cumulated sum
if isequal(idx, 1)
B.data = cumprod(B.data);
else
if common_first_period_witout_nan
B.data(idx:end,:) = cumprod(B.data(idx:end,:));
else
B.data = cumprodnan(B.data);
end
end
% Change the name of the variables
for i=1:vobs(B)
B.name(i) = {['cumprod(' B.name{i} ')']};
B.tex(i) = {['\prod_t ' B.tex{i}]};
end
case 2
if isdseries(varargin{2})
if ~isequal(vobs(varargin{1}), vobs(varargin{2}))
......@@ -225,4 +242,19 @@ end
%$ % Check the results.
%$ t(1) = dassert(ts3.data,ts4.data);
%$ T = all(t);
%@eof:5
\ No newline at end of file
%@eof:5
%@test:6
%$ % Define a data set.
%$ A = [NaN, NaN; 1 NaN; 1 1; 1 1; 1 NaN];
%$
%$ % Instantiate a time series object.
%$ ts0 = dseries(A);
%$
%$ % Call the tested method.
%$ ts0 = ts0.cumprod();
%$
%$ % Check the results.
%$ t(1) = dassert(ts0.data, A);
%$ T = all(t);
%@eof:6
\ No newline at end of file
......@@ -2,15 +2,15 @@ function B = cumsum(varargin) % --*-- Unitary tests --*--
% Overloads matlab's cumsum function for dseries objects.
%
% INPUTS
% INPUTS
% o A dseries object [mandatory].
% o d dates object [optional]
% o v dseries object with one observation [optional]
%
% OUTPUTS
% OUTPUTS
% o B dseries object.
% Copyright (C) 2013 Dynare Team
% Copyright (C) 2013-2017 Dynare Team
%
% This file is part of Dynare.
%
......@@ -27,28 +27,45 @@ function B = cumsum(varargin) % --*-- 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/>.
% Get indices of the columns without NaNs
idx = find(~any(isnan(varargin{1}.data)));
% Get the firt observation number where all the variables are observed (ie without NaNs)
idx = find(all(~isnan(varargin{1}.data), 2),1);
if isempty(idx)
error('dseries::cumsum: All the variables have NaNs. The cumulated sum cannot be computed!')
idx = 0;
end
if ~isequal(idx(:),transpose(1:vobs(varargin{1})))
warning('dseries::cumsum: The cumulated sum is not computed for some variables because they have NaNs!')
% Is the first period where variables are observed common?
common_first_period_witout_nan = true;
if ~idx
if any(~isnan(varargin{1}.data(:)))
common_first_period_witout_nan = false;
end
else
if idx>1
if any(any(~isnan(varargin{1}.data(1:idx-1,:))))
common_first_period_witout_nan = false;
end
end
end
switch nargin
case 1
% Initialize the output.
B = varargin{1};
% Perform the cumulated sum
B.data(:,idx) = cumsum(B.data(:,idx));
% Change the name of the variables
for i=1:vobs(B)
B.name(i) = {['cumsum(' B.name{i} ')']};
B.tex(i) = {['\sum_t ' B.tex{i}]};
end
case 1
% Initialize the output.
B = varargin{1};
% Perform the cumulated sum
if isequal(idx, 1)
B.data = cumsum(B.data);
else
if common_first_period_witout_nan
B.data(idx:end,:) = cumsum(B.data(idx:end,:));
else
B.data = cumsumnan(B.data);
end
end
% Change the name of the variables
for i=1:vobs(B)
B.name(i) = {['cumsum(' B.name{i} ')']};
B.tex(i) = {['\sum_t ' B.tex{i}]};
end
case 2
if isdseries(varargin{2})
if ~isequal(vobs(varargin{1}), vobs(varargin{2}))
......@@ -212,4 +229,22 @@ end
%$ % Check the results.
%$ t(1) = dassert(ts3.data,ts4.data);
%$ T = all(t);
%@eof:5
\ No newline at end of file
%@eof:5
%@test:6
%$ % Define a data set.
%$ A = [NaN, NaN; 1 NaN; 1 1; 1 1; 1 NaN];
%$
%$ % Instantiate a time series object.
%$ ts0 = dseries(A);
%$
%$ % Call the tested method.
%$ ts0 = ts0.cumsum();
%$
%$ % Expected results.
%$ A = [NaN NaN; 1 NaN; 2 1; 3 2; 4 NaN];
%$
%$ % Check the results.
%$ t(1) = dassert(ts0.data, A);
%$ T = all(t);
%@eof:6
\ No newline at end of file
......@@ -59,7 +59,7 @@ function ts = dseries(varargin) % --*-- Unitary tests --*--
%! @end deftypefn
%@eod:
% Copyright (C) 2011-2013 Dynare Team
% Copyright (C) 2011-2019 Dynare Team
%
% This file is part of Dynare.
%
......@@ -276,7 +276,7 @@ end
%$ t = zeros(6,1);
%$
%$ try
%$ [strfile, status] = urlwrite('http://www.dynare.org/Datasets/dseries/dynseries_test_data.m','dynseries_test_data.m');
%$ [strfile, status] = urlwrite('https://www.dynare.org/Datasets/dseries/dynseries_test_data.m','dynseries_test_data.m');
%$ if ~status
%$ error()
%$ end
......@@ -302,7 +302,7 @@ end
%$ t = zeros(6,1);
%$
%$ try
%$ [strfile, status] = urlwrite('http://www.dynare.org/Datasets/dseries/dynseries_test_data.mat','dynseries_test_data.mat');
%$ [strfile, status] = urlwrite('https://www.dynare.org/Datasets/dseries/dynseries_test_data.mat','dynseries_test_data.mat');
%$ if ~status
%$ error()
%$ end
......@@ -328,7 +328,7 @@ end
%$ t = zeros(8,1);
%$
%$ try
%$ [strfile, status] = urlwrite('http://www.dynare.org/Datasets/dseries/dynseries_test_data.csv','dynseries_test_data.csv');
%$ [strfile, status] = urlwrite('https://www.dynare.org/Datasets/dseries/dynseries_test_data.csv','dynseries_test_data.csv');
%$ if ~status
%$ error()
%$ end
......@@ -423,12 +423,21 @@ end
%@test:9
%$ try
%$ [strfile, status] = urlwrite('http://www.dynare.org/Datasets/dseries/dynseries_test_data-1.xls','dynseries_test_data-1.xls');
%$ if isoctave()
%$ [strfile, status] = urlwrite('https://www.dynare.org/Datasets/dseries/dynseries_test_data-1.xlsx','dynseries_test_data-1.xlsx');
%$ else
%$ [strfile, status] = urlwrite('https://www.dynare.org/Datasets/dseries/dynseries_test_data-1.xls','dynseries_test_data-1.xls');
%$ end
%$ if ~status
%$ error()
%$ end
%$ ts = dseries('dynseries_test_data-1.xls');
%$ delete('dynseries_test_data-1.xls');
%$ if isoctave()
%$ ts = dseries('dynseries_test_data-1.xlsx');
%$ delete('dynseries_test_data-1.xlsx');
%$ else
%$ ts = dseries('dynseries_test_data-1.xls');
%$ delete('dynseries_test_data-1.xls');
%$ end
%$ t(1) = 1;
%$ catch
%$ t(1) = 0;
......@@ -449,12 +458,21 @@ end
%@test:10
%$ try
%$ [strfile, status] = urlwrite('http://www.dynare.org/Datasets/dseries/dynseries_test_data-2.xls','dynseries_test_data-2.xls');
%$ if isoctave()
%$ [strfile, status] = urlwrite('https://www.dynare.org/Datasets/dseries/dynseries_test_data-2.xlsx','dynseries_test_data-2.xlsx');
%$ else
%$ [strfile, status] = urlwrite('https://www.dynare.org/Datasets/dseries/dynseries_test_data-2.xls','dynseries_test_data-2.xls');
%$ end
%$ if ~status
%$ error()
%$ end
%$ ts = dseries('dynseries_test_data-2.xls');
%$ delete('dynseries_test_data-2.xls');
%$ if isoctave()
%$ ts = dseries('dynseries_test_data-2.xlsx');
%$ delete('dynseries_test_data-2.xlsx');
%$ else
%$ ts = dseries('dynseries_test_data-2.xls');
%$ delete('dynseries_test_data-2.xls');
%$ end
%$ t(1) = 1;
%$ catch
%$ t(1) = 0;
......@@ -475,12 +493,21 @@ end
%@test:11
%$ try
%$ [strfile, status] = urlwrite('http://www.dynare.org/Datasets/dseries/dynseries_test_data-3.xls','dynseries_test_data-3.xls');
%$ if isoctave()
%$ [strfile, status] = urlwrite('https://www.dynare.org/Datasets/dseries/dynseries_test_data-3.xlsx','dynseries_test_data-3.xlsx');
%$ else
%$ [strfile, status] = urlwrite('https://www.dynare.org/Datasets/dseries/dynseries_test_data-3.xls','dynseries_test_data-3.xls');
%$ end
%$ if ~status
%$ error()
%$ end
%$ ts = dseries('dynseries_test_data-3.xls');
%$ delete('dynseries_test_data-3.xls');
%$ if isoctave()
%$ ts = dseries('dynseries_test_data-3.xlsx');
%$ delete('dynseries_test_data-3.xlsx');
%$ else
%$ ts = dseries('dynseries_test_data-3.xls');
%$ delete('dynseries_test_data-3.xls');
%$ end
%$ t(1) = 1;
%$ catch
%$ t(1) = 0;
......@@ -501,12 +528,21 @@ end
%@test:12
%$ try
%$ [strfile, status] = urlwrite('http://www.dynare.org/Datasets/dseries/dynseries_test_data-4.xls','dynseries_test_data-4.xls');
%$ if isoctave()
%$ [strfile, status] = urlwrite('https://www.dynare.org/Datasets/dseries/dynseries_test_data-4.xlsx','dynseries_test_data-4.xlsx');
%$ else
%$ [strfile, status] = urlwrite('https://www.dynare.org/Datasets/dseries/dynseries_test_data-4.xls','dynseries_test_data-4.xls');
%$ end
%$ if ~status
%$ error()
%$ end
%$ ts = dseries('dynseries_test_data-4.xls');
%$ delete('dynseries_test_data-4.xls');
%$ if isoctave()
%$ ts = dseries('dynseries_test_data-4.xlsx');
%$ delete('dynseries_test_data-4.xlsx');
%$ else
%$ ts = dseries('dynseries_test_data-4.xls');
%$ delete('dynseries_test_data-4.xls');
%$ end
%$ t(1) = 1;
%$ catch
%$ t(1) = 0;
......@@ -570,7 +606,7 @@ end
%@test:15
%$ try
%$ ts = dseries([1; 2],dates('1990Q1'):dates('1990Q4'));
%$ evalc('dseries([1; 2],dates(''1990Q1''):dates(''1990Q4''));');
%$ t = 0;
%$ catch
%$ t = 1;
......
......@@ -2,17 +2,17 @@ function C = eq(A,B) % --*-- Unitary tests --*--
% Overloads eq (==) operator.
%
% INPUTS
% INPUTS
% o A dseries object (T periods, N variables).
% o B dseries object (T periods, N variables).
%
% OUTPUTS
% o C T*N matrix of zeros and ones. Element C(t,n) is nonzero iff observation t of variable n in A and B are equal.
% OUTPUTS
% o C T*N matrix of zeros and ones. Element C(t,n) is nonzero iff observation t of variable n in A and B are equal.
%
% REMARKS
% If the number of variables, the number of observations or the frequencies are different in A and B, the function returns a zero scalar.
% REMARKS
% If the number of variables, the number of observations or the frequencies are different in A and B, the function returns a zero scalar.
% Copyright (C) 2013 Dynare Team
% Copyright (C) 2013-2017 Dynare Team
%
% This file is part of Dynare.
%
......
......@@ -2,14 +2,14 @@ function l = exist(o, varname) % --*-- Unitary tests --*--
% Tests if a variable exists in dseries object o.
%
% INPUTS
% INPUTS
% - o [dseries], dseries object.
% - varname [string], name of a variable.
%
% OUTPUTS
% - l [logical], equal to 1 (true) iff varname is a variable in dseries object o.
% OUTPUTS
% - l [logical], equal to 1 (true) iff varname is a variable in dseries object o.
% Copyright (C) 2014 Dynare Team
% Copyright (C) 2014-2017 Dynare Team
%
% This file is part of Dynare.
%
......
function A = extract(B,varargin) % --*-- Unitary tests --*--
% Extract some variables from a database.
% Copyright (C) 2012-2013 Dynare Team
% Copyright (C) 2012-2017 Dynare Team
%
% This file is part of Dynare.
%
......@@ -25,87 +25,26 @@ A = dseries();
VariableName_ = {};
for i=1:nargin-1
VariableName = varargin{i};
% Implicit loop.
idArobase = strfind(VariableName,'@');
if mod(length(idArobase),2)
error('dseries::extract: (Implicit loops) The number of @ symbols must be even!')
end
% Regular expression
idBracket.open = strfind(VariableName,'[');
idBracket.close = strfind(VariableName,']');
if ~isequal(length(idBracket.open),length(idBracket.open))
error('dseries::extract: (Matlab/Octave''s regular expressions) Check opening and closing square brackets!')
end
% Loops and regular expressions are not compatible
if length(idArobase) && length(idBracket.open)
error(['dseries::extract: You cannot use implicit loops and regular expressions in the same rule!'])
end
% Update the list of variables.
if length(idArobase)
NumberOfImplicitLoops = .5*length(idArobase);
idComma = cell(NumberOfImplicitLoops,1);
expressions = cell(NumberOfImplicitLoops,1);
for i=0:NumberOfImplicitLoops-1
idComma(i+1) = { strfind(VariableName(idArobase(2*i+1)+1:idArobase(2*i+2)-1),',') };
expressions(i+1) = { VariableName(idArobase(2*i+1)+1:idArobase(2*i+2)-1) };
end
if any(cellfun(@isempty,idComma))
error('dseries::extract: (Implicit loops) Wrong syntax!')
end
switch NumberOfImplicitLoops
case 1
expression = expressions{1};
idVariables_ = [];
while ~isempty(expression)
[token, expression] = strtok(expression,',');
candidate = [VariableName(1:idArobase(1)-1), token, VariableName(idArobase(2)+1:end)];
id = find(strcmp(candidate,B.name));
if isempty(id)
error(['dseries::extract: (Implicit loops) Variable ''' candidate ''' does not exist in dseries object ''' inputname(1) '''!'])
else
idVariables_ = [idVariables_; id];
end
end
VariableName = B.name(idVariables_);
case 2
idVariables_ = [];
expression_1 = expressions{1};
while ~isempty(expression_1)
[token_1, expression_1] = strtok(expression_1,',');
expression_2 = expressions{2};
while ~isempty(expression_2)
[token_2, expression_2] = strtok(expression_2,',');
candidate = [VariableName(1:idArobase(1)-1), token_1, VariableName(idArobase(2)+1:idArobase(3)-1), token_2, VariableName(idArobase(4)+1:end)];
id = find(strcmp(candidate,B.name));
if isempty(id)
error(['dseries::extract: (Implicit loops) Variable ''' candidate ''' does not exist in dseries object ''' inputname(1) '''!'])
else
idVariables_ = [idVariables_; id];
end
end
end
VariableName = B.name(idVariables_);
otherwise
error('dseries::extract: (Implicit loops) Cannot unroll more than two implicit loops!')
end
VariableName_ = vertcat(VariableName_,VariableName);
VariableName_ = build_list_of_variables_with_loops(B.name, idArobase, VariableName, VariableName_);
elseif length(idBracket.open)
% Matlab/Octave's regular expressions.
first_block_id = 0;
last_block_id = 0;
idVariables = find(isnotempty_cell(regexp(B.name,VariableName,'match')));
if isempty(idVariables)
error(['dseries::extract: Can''t find any variable matching ' VariableName ' pattern!'])
end
idVariables_ = [];
for j = 1:length(idVariables)
first_block_flag = 0;
if (first_block_id && strcmp(B.name{idVariables(j)}(1:first_block_id),VariableName(1:first_block_id))) || ~first_block_id
first_block_flag = 1;
end
last_block_flag = 0;
if (last_block_id && strcmp(B.name{idVariables(j)}(end-last_block_id:end),VariableName(end-last_block_id:end))) || ~last_block_id
last_block_flag = 1;
end
if first_block_flag && last_block_flag
idVariables_ = [idVariables_; idVariables(j)];
end
end
VariableName = B.name(idVariables_);
VariableName_ = vertcat(VariableName_,VariableName);
VariableName_ = build_list_of_variables_with_regexp(B.name, idBracket, VariableName, VariableName_);
else
VariableName_ = varargin(:);
end
......@@ -129,16 +68,7 @@ A.dates = B.dates;
A.name = B.name(idVariableName);
A.tex = B.tex(idVariableName);
function b = isnotempty_cell(CellArray)
CellArrayDimension = size(CellArray);
b = NaN(CellArrayDimension);
for i=1:CellArrayDimension(1)
for j = 1:CellArrayDimension(2)
b(i,j) = ~isempty(CellArray{i,j});
end
end
%@test:1
%$ % Define a data set.
%$ A = rand(10,24);
......@@ -227,4 +157,4 @@ function b = isnotempty_cell(CellArray)
%$ end
%$
%$ T = all(t);
%@eof:3
%@eof:3
\ No newline at end of file
function f = firstdate(o)
function f = firstdate(o) % --*-- Unitary tests --*--
% Copyright (C) 2014 Dynare Team
% Copyright (C) 2014-2016 Dynare Team
%
% This file is part of Dynare.
%
......@@ -17,4 +17,20 @@ function f = firstdate(o)
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <http://www.gnu.org/licenses/>.
f = o.dates(1);
\ No newline at end of file
f = o.dates(1);
%@test:1
%$ try
%$ ts = dseries(randn(10, 3),'1938Q3');
%$ dd = ts.firstdate();
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = isequal(dd, dates('1938Q3'));
%$ end
%$
%$ T = all(t);
%@eof:1
function d = firstobservedperiod(o) % --*-- Unitary tests --*--
% Returns the first period where all the variables are observed (first period without NaNs).
%
% INPUTS
% - o [dseries] with N variables and T periods.
%
% OUTPUTS
% - b [dates] First period where the N variables are observed (without NaNs).
% Copyright (C) 2016-2017 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/>.
b = ~isnan(o);
c = find(prod(b, 2));
if isempty(c)
error('No overlapping non-NaN data points found in dseries.');
end
d = firstdate(o)+(c(1)-1);
%@test:1
%$ try
%$ ts = dseries([NaN, NaN; NaN, 1; 2, 2; 3, 3; 4, NaN; 5, 5]);
%$ dd = ts.firstobservedperiod();
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = isequal(dd, dates('3Y'));
%$ end
%$
%$ T = all(t);
%@eof:1
%@test:2
%$ try
%$ ts = dseries([0, 0; NaN, 1; 2, 2; 3, 3; 4, NaN; 5, 5]);
%$ dd = ts.firstobservedperiod();
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = isequal(dd, dates('1Y'));
%$ end
%$
%$ T = all(t);
%@eof:2
......@@ -2,25 +2,25 @@ function B = horzcat(varargin) % --*-- Unitary tests --*--
% Overloads horzcat method for dseries objects.
%
% INPUTS
% INPUTS
% o A1 dseries object.
% o A2 dseries object.
% o ...
%
% OUTPUTS
% OUTPUTS
% o B dseries object.
%
% EXAMPLE 1
% EXAMPLE 1
% If A, B and C are dseries objects the following syntax:
%
%
% D = [A, B, C] ;
%
% Defines a dseries object D containing the variables appearing in A, B and C.
%
% REMARKS
% REMARKS
% o A1, A2, ... must not have common variables.
% Copyright (C) 2011-2013 Dynare Team
% Copyright (C) 2011-2017 Dynare Team
%
% This file is part of Dynare.
%
......@@ -50,57 +50,72 @@ switch nargin
end
function a = concatenate(b,c)
[n,message] = common_strings_in_cell_arrays(b.name,c.name);
if isempty(b)
a = c;
return
end
if isempty(c)
a = b;
return
end
if n
error(['dseries::horzcat: I cannot concatenate dseries objects with common variable names (' message ')!'])
end
if ~isequal(frequency(b),frequency(c))
error('dseries::horzcat: All time series objects must have common frequency!')
[n,message] = common_strings_in_cell_arrays(b.name,c.name);
if isempty(b)
a = c;
return
end
if isempty(c)
a = b;
return
end
if n
error(['dseries::horzcat: I cannot concatenate dseries objects with common variable names (' message ')!'])
end
if ~isequal(frequency(b),frequency(c))
error('dseries::horzcat: All time series objects must have common frequency!')
else
a = dseries();
end
d_nobs_flag = 0;
if ~isequal(nobs(b),nobs(c))
d_nobs_flag = 1;
end
d_init_flag = 0;
if ~isequal(firstdate(b),firstdate(c))
d_init_flag = 1;
end
a.name = vertcat(b.name,c.name);
a.tex = vertcat(b.tex,c.tex);
if ~( d_nobs_flag(1) || d_init_flag(1) )
a.data = [b.data,c.data];
a.dates = b.dates;
else
nobs_b = nobs(b);
nobs_c = nobs(c);
if firstdate(b)<=firstdate(c)
if firstdate(b)<firstdate(c)
c.data = [NaN(firstdate(c)-firstdate(b), vobs(c)); c.data];
end
else
a = dseries();
b.data = [NaN(firstdate(b)-firstdate(c), vobs(b)); b.data];
end
d_nobs_flag = 0;
if ~isequal(nobs(b),nobs(c))
d_nobs_flag = 1;
b_last_date = firstdate(b)+nobs_b;
c_last_date = firstdate(c)+nobs_c;
if b_last_date<c_last_date
b.data = [b.data; NaN(c_last_date-b_last_date, vobs(b))];
elseif b_last_date>c_last_date
c.data = [c.data; NaN(b_last_date-c_last_date, vobs(c))];
end
d_init_flag = 0;
if ~isequal(firstdate(b),firstdate(c))
d_init_flag = 1;
fillerdates = dates();
if max(c.dates) < min(b.dates)
fillerdates = max(c.dates):min(b.dates);
end
a.name = vertcat(b.name,c.name);
a.tex = vertcat(b.tex,c.tex);
if ~( d_nobs_flag(1) || d_init_flag(1) )
a.data = [b.data,c.data];
a.dates = b.dates;
if max(b.dates) < min(c.dates)
fillerdates = max(b.dates):min(c.dates);
end
if isempty(fillerdates)
hd = [b.dates, c.dates];
else
nobs_b = nobs(b);
nobs_c = nobs(c);
if firstdate(b)<=firstdate(c)
if firstdate(b)<firstdate(c)
c.data = [NaN(firstdate(c)-firstdate(b), vobs(c)); c.data];
end
else
b.data = [NaN(firstdate(b)-firstdate(c), vobs(b)); b.data];
end
b_last_date = firstdate(b)+nobs_b;
c_last_date = firstdate(c)+nobs_c;
if b_last_date<c_last_date
b.data = [b.data; NaN(c_last_date-b_last_date, vobs(b))];
elseif b_last_date>c_last_date
c.data = [c.data; NaN(b_last_date-c_last_date, vobs(c))];
end
a.data = [b.data, c.data];
a.dates = sort(unique([b.dates, c.dates]));
hd = [b.dates, fillerdates, c.dates];
end
a.data = [b.data, c.data];
a.dates = sort(unique(hd));
end
%@test:1
%$ % Define a data set.
%$ A = [transpose(1:10),2*transpose(1:10)];
......
......@@ -4,14 +4,14 @@ function ts = hpcycle(ts, lambda) % --*-- Unitary tests --*--
%
% Extracts the cycle component from a dseries object using Hodrick Prescott filter.
%
% INPUTS
% INPUTS
% o ts dseries object.
% o lambda positive scalar, trend smoothness parameter.
%
% OUTPUTS
% OUTPUTS
% o ts dseries object, with time series replaced by the cyclical component of the original time series.
% Copyright (C) 2013 Dynare Team
% Copyright (C) 2013-2017 Dynare Team
%
% This file is part of Dynare.
%
......@@ -28,13 +28,13 @@ function ts = hpcycle(ts, lambda) % --*-- 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/>.
if nargin>1
if nargin>1
if lambda<=0
error(['dseries::hpcycle: Lambda must be a positive integer!'])
end
else
lambda = [];
end
end
for i=1:vobs(ts)
ts.name(i) = {['hpcycle(' ts.name{i} ')']};
......@@ -50,7 +50,7 @@ ts.data = data;
%$ % Create a dataset.
%$ e = .2*randn(200,1);
%$ u = randn(200,1);
%$ stochastic_trend = cumsum(e);
%$ stochastic_trend = cumsum(e);
%$ deterministic_trend = .1*transpose(1:200);
%$ x = zeros(200,1);
%$ for i=2:200
......
......@@ -4,14 +4,14 @@ function ts = hptrend(ts, lambda) % --*-- Unitary tests --*--
%
% Extracts the trend component from a dseries object using Hodrick Prescott filter.
%
% INPUTS
% INPUTS
% o ts dseries object.
% o lambda positive scalar, trend smoothness parameter.
%
% OUTPUTS
% OUTPUTS
% o ts dseries object, with time series replaced by the trend component of the original time series.
% Copyright (C) 2013 Dynare Team
% Copyright (C) 2013-2017 Dynare Team
%
% This file is part of Dynare.
%
......@@ -28,7 +28,7 @@ function ts = hptrend(ts, lambda) % --*-- 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/>.
if nargin>1
if nargin>1
if lambda<=0
error(['dseries::hptrend: Lambda must be a positive integer!'])
end
......@@ -44,12 +44,12 @@ end
ts.data = sample_hp_filter(ts.data,lambda);
%@test:1
%$ plot_flag = 0;
%$ plot_flag = 0;
%$
%$ % Create a dataset.
%$ e = .2*randn(200,1);
%$ u = randn(200,1);
%$ stochastic_trend = cumsum(e);
%$ stochastic_trend = cumsum(e);
%$ deterministic_trend = .1*transpose(1:200);
%$ x = zeros(200,1);
%$ for i=2:200
......
......@@ -29,7 +29,7 @@ function ts = insert(ts,us,id) % --*-- Unitary tests --*--
%! @end deftypefn
%@eod:
% Copyright (C) 2013 Dynare Team
% Copyright (C) 2013-2017 Dynare Team
%
% This file is part of Dynare.
%
......@@ -61,10 +61,10 @@ end
n = length(id);
if n>1
[id, jd] = sort(id);
us.data = us.data(:,jd);
us.name = us.name(jd);
us.tex = us.tex(jd);
[id, jd] = sort(id);
us.data = us.data(:,jd);
us.name = us.name(jd);
us.tex = us.tex(jd);
end
for i=1:n
......
......@@ -2,15 +2,15 @@ function C = isequal(A, B, tol)
% Overloads the isequal Matlab/Octave's function.
%
% INPUTS
% INPUTS
% o A dseries object (T periods, N variables).
% o B dseries object (T periods, N variables).
% o tol tolerance parameter.
%
% OUTPUTS
% OUTPUTS
% o C Integer scalar equal to zero or one.
% Copyright (C) 2013 Dynare Team
% Copyright (C) 2013-2017 Dynare Team
%
% This file is part of Dynare.
%
......
function b = isnan(o) % --*-- Unitary tests --*--
% Returns an array of logicals (true/false). Element (t,i) is true iff the i-th variable at
% period number t is not a NaN.
%
% INPUTS
% - o [dseries] with N variables and T periods.
%
% OUTPUTS
% - b [logical] T*N array of logicals.
% Copyright (C) 2016-2017 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/>.
b = isnan(o.data);
%@test:1
%$ try
%$ data = randn(100, 10);
%$ cnan = randi(10, 50, 1);
%$ rnan = randi(100, 50, 1);
%$ for i=1:50, data(rnan(i),cnan(i)) = NaN; end
%$ inan = isnan(data);
%$ ts = dseries(data);
%$ dd = ts.isnan();
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = isequal(dd, inan);
%$ end
%$
%$ T = all(t);
%@eof:1
......@@ -44,7 +44,7 @@ if nargin<2
p = 1;
end
if p<=0
if p<0
error('dseries::lag: Second input argument must be strictly positive! Use lead method instead.')
end
......
function d = lastobservedperiod(o) % --*-- Unitary tests --*--
% Returns the last period where all the variables are observed (last period without NaNs).
%
% INPUTS
% - o [dseries] with N variables and T periods.
%
% OUTPUTS
% - b [dates] Last period where the N variables are observed (without NaNs).
% Copyright (C) 2017 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/>.
b = ~isnan(o);
c = find(prod(b, 2));
if isempty(c)
error('No overlapping non-NaN data points found in dseries.');
end
d = firstdate(o)+(c(end)-1);
%@test:1
%$ try
%$ ts = dseries([NaN, NaN; NaN, 1; 2, 2; 3, 3; 4, NaN; 5, 5]);
%$ dd = ts.lastobservedperiod();
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = isequal(dd, dates('6Y'));
%$ end
%$
%$ T = all(t);
%@eof:1
%@test:2
%$ try
%$ ts = dseries([0, 0; NaN, 1; 2, 2; 3, 3; 4, NaN; 5, NaN]);
%$ dd = ts.lastobservedperiod();
%$ t(1) = true;
%$ catch
%$ t(1) = false;
%$ end
%$
%$ if t(1)
%$ t(2) = isequal(dd, dates('4Y'));
%$ end
%$
%$ T = all(t);
%@eof:2
......@@ -44,7 +44,7 @@ if nargin<2
p = 1;
end
if p<=0
if p<0
error('dseries::lead: Second input argument must be strictly positive! Use lag method instead.')
end
......