diff --git a/src/@dseries/align.m b/src/@dseries/align.m index 37eece439fc21b9e1e45bc22f9a89eebd1da5814..e42891bcdeffa72e73e0beb4a363a91db6b31cb2 100644 --- a/src/@dseries/align.m +++ b/src/@dseries/align.m @@ -1,33 +1,17 @@ -function [a,b] = align(a, b) % --*-- Unitary tests --*-- - -%@info: -%! @deftypefn {Function File} {[@var{a}, @var{b}] =} align (@var{a}, @var{b}) -%! @anchor{dseries/align} -%! @sp 1 -%! If dseries objects @var{a} and @var{b} are defined on different time ranges, extend @var{a} and/or -%! @var{b} with NaNs so that they are defined on the same time range. -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @ @var -%! @item a -%! Object instantiated by @ref{dseries}. -%! @item b -%! Object instantiated by @ref{dseries}. -%! @end table -%! @sp 2 -%! @strong{Outputs} -%! @sp 1 -%! @table @ @var -%! @item a -%! Object instantiated by @ref{dseries}. -%! @item b -%! Object instantiated by @ref{dseries}. -%! @end table -%! @end deftypefn -%@eod: +function [o, p] = align(o, p) % --*-- Unitary tests --*-- -% Copyright (C) 2013 Dynare Team +% If necessay completes dseries object o and p so that they are defined on the same time range +% (in place modification). +% +% INPUTS +% - o [dseries] +% - p [dseries] +% +% OUTPUTS +% - o [dseries] +% - p [dseries] + +% Copyright (C) 2013-2015 Dynare Team % % This file is part of Dynare. % @@ -44,46 +28,9 @@ function [a,b] = align(a, b) % --*-- 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 ~isequal(frequency(a),frequency(b)) - error(['dseries::align: ''' inputname(1) ''' and ''' inputname(2) ''' dseries objects must have common frequencies!']) -end - -init = min(firstdate(a),firstdate(b)); -last = max(lastdate(a),lastdate(b)); - -if isempty(intersect(a.dates,b.dates)) - error(['dseries::align: ''' inputname(1) ''' and ''' inputname(2) ''' dseries object must have at least one common date!']) -end - -a_init = init; -b_init = init; -a_last = last; -b_last = last; - -if firstdate(b)>init - n = firstdate(b)-init; - b.data = [NaN(n, vobs(b)); b.data]; - b_init = init; -end - -if firstdate(a)>init - n = firstdate(a)-init; - a.data = [NaN(n, vobs(a)); a.data]; - a_init = init; -end - -if lastdate(b)<last - n = last-lastdate(b); - b.data = [b.data; NaN(n, vobs(b))]; -end - -if lastdate(a)<last - n = last-lastdate(a); - a.data = [a.data; NaN(n, vobs(a))]; -end - -a.dates = a_init:a_init+(nobs(a)-1); -b.dates = b_init:b_init+(nobs(b)-1); +o = copy(o); +p = copy(p); +align_(o, p); %@test:1 %$ % Define a datasets. diff --git a/src/@dseries/align_.m b/src/@dseries/align_.m new file mode 100644 index 0000000000000000000000000000000000000000..ba718c0ec678a7c35f245f13dd1c7f09aa586e56 --- /dev/null +++ b/src/@dseries/align_.m @@ -0,0 +1,166 @@ +function [o, p] = align_(o, p) % --*-- Unitary tests --*-- + +% If necessay completes dseries object o and p so that they are defined on the same time range +% (in place modification). +% +% INPUTS +% - o [dseries] +% - p [dseries] +% +% OUTPUTS +% - o [dseries] +% - p [dseries] + +% Copyright (C) 2013-2015 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 ~isequal(frequency(o),frequency(p)) + error(['dseries::align: ''' inputname(1) ''' and ''' inputname(2) ''' dseries objects must have common frequencies!']) +end + +init = min(firstdate(o),firstdate(p)); +last = max(lastdate(o),lastdate(p)); + +if isempty(intersect(o.dates,p.dates)) + error(['dseries::align: ''' inputname(1) ''' and ''' inputname(2) ''' dseries object must have at least one common date!']) +end + +o_init = init; +p_init = init; +o_last = last; +p_last = last; + +if firstdate(p)>init + n = firstdate(p)-init; + p.data = [NaN(n, vobs(p)); p.data]; + p_init = init; +end + +if firstdate(o)>init + n = firstdate(o)-init; + o.data = [NaN(n, vobs(o)); o.data]; + o_init = init; +end + +if lastdate(p)<last + n = last-lastdate(p); + p.data = [p.data; NaN(n, vobs(p))]; +end + +if lastdate(o)<last + n = last-lastdate(o); + o.data = [o.data; NaN(n, vobs(o))]; +end + +o.dates = o_init:o_init+(nobs(o)-1); +o.dates = o_init:o_init+(nobs(o)-1); + +%@test:1 +%$ % Define a datasets. +%$ A = rand(8,3); B = rand(7,2); +%$ +%$ % Define names +%$ A_name = {'A1';'A2';'A3'}; +%$ B_name = {'B1';'B2'}; +%$ +%$ % Define initial dates +%$ A_init = '1990Q1'; +%$ B_init = '1989Q2'; +%$ +%$ % Instantiate two dseries objects +%$ ts1 = dseries(A,A_init,A_name); +%$ ts2 = dseries(B,B_init,B_name); +%$ +%$ try +%$ [ts1, ts2] = align(ts1, ts2); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(ts1.nobs,ts2.nobs); +%$ t(3) = dassert(ts1.init,ts2.init); +%$ t(4) = dassert(ts1.data,[NaN(3,3); A], 1e-15); +%$ t(5) = dassert(ts2.data,[B; NaN(4,2)], 1e-15); +%$ end +%$ T = all(t); +%@eof:1 + +%@test:2 +%$ % Define a datasets. +%$ A = rand(8,3); B = rand(7,2); +%$ +%$ % Define names +%$ A_name = {'A1';'A2';'A3'}; +%$ B_name = {'B1';'B2'}; +%$ +%$ % Define initial dates +%$ A_init = '1990Q1'; +%$ B_init = '1990Q1'; +%$ +%$ % Instantiate two dseries objects +%$ ts1 = dseries(A,A_init,A_name); +%$ ts2 = dseries(B,B_init,B_name); +%$ +%$ try +%$ [ts1, ts2] = align(ts1, ts2); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(ts1.nobs,ts2.nobs); +%$ t(3) = dassert(ts1.init,ts2.init); +%$ t(4) = dassert(ts1.data,A, 1e-15); +%$ t(5) = dassert(ts2.data,[B; NaN(1,2)], 1e-15); +%$ end +%$ T = all(t); +%@eof:2 + +%@test:3 +%$ % Define a datasets. +%$ A = rand(8,3); B = rand(7,2); +%$ +%$ % Define names +%$ A_name = {'A1';'A2';'A3'}; +%$ B_name = {'B1';'B2'}; +%$ +%$ % Define initial dates +%$ A_init = '1990Q1'; +%$ B_init = '1990Q1'; +%$ +%$ % Instantiate two dseries objects +%$ ts1 = dseries(A,A_init,A_name); +%$ ts2 = dseries(B,B_init,B_name); +%$ +%$ try +%$ [ts2, ts1] = align(ts2, ts1); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(ts1.nobs,ts2.nobs); +%$ t(3) = dassert(ts1.init,ts2.init); +%$ t(4) = dassert(ts1.data,A, 1e-15); +%$ t(5) = dassert(ts2.data,[B; NaN(1,2)], 1e-15); +%$ end +%$ T = all(t); +%@eof:3 \ No newline at end of file diff --git a/src/@dseries/copy.m b/src/@dseries/copy.m new file mode 100644 index 0000000000000000000000000000000000000000..e668936a4bc0d4a328575c25fda121a0856386cd --- /dev/null +++ b/src/@dseries/copy.m @@ -0,0 +1,52 @@ +function p = copy(o) % --*-- Unitary tests --*-- + +% Do a copy of a dseries object. +% +% INPUTS +% - o [dates] +% +% OUTPUTS +% - p [dates] + +% Copyright (C) 2015 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 +% the Free Software Foundation, either version 3 of the License, or +% (at your option) any later version. +% +% Dynare dates submodule 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/>. + +p = dseries(); +p.data = o.data; +p.name = o.name; +p.tex = o.tex; +p.dates = o.dates; + +%@test:1 +%$ % Define a dates object +%$ data = rand(10,2); +%$ o = dseries(data); +%$ q = dseries(data); +%$ +%$ % Call the tested routine. +%$ try +%$ p = copy(o); +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ if t(1) +%$ o.log_(); +%$ t(2) = dassert(p, q); +%$ end +%$ +%$ T = all(t); +%@eof:1 \ No newline at end of file diff --git a/src/@dseries/exp.m b/src/@dseries/exp.m index 06ed6a1f03291cb11d51ea5fcd35855c87987cab..d293877be02a1ff8c93025874a210315c11040e4 100644 --- a/src/@dseries/exp.m +++ b/src/@dseries/exp.m @@ -1,33 +1,14 @@ -function ts = exp(ts) -% Apply the exponential function to a Dynare time series object. +function o = exp(o) % --*-- Unitary tests --*-- -%@info: -%! @deftypefn {Function File} {@var{ts} =} log(@var{ts}) -%! @anchor{exp} -%! Apply the exponential function to a Dynare time series object. -%! -%! @strong{Inputs} -%! @table @var -%! @item ts -%! Dynare time series object, instantiated by @ref{dseries} -%! @end table -%! -%! @strong{Outputs} -%! @table @var -%! @item ts -%! Dynare time series object with transformed data field. -%! @end table -%! -%! @strong{This function is called by:} -%! None. -%! -%! @strong{This function calls:} -%! None. -%! -%! @end deftypefn -%@eod: +% Apply the exponential to all the variables in a dseries object (without in place modification). +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - o [dseries] -% Copyright (C) 2011-2013 Dynare Team +% Copyright (C) 2011-2015 Dynare Team % % This file is part of Dynare. % @@ -44,9 +25,52 @@ function ts = exp(ts) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -ts.data = exp(ts.data); +o = copy(o); +o.exp_(); + +%@test:1 +%$ % Define a dates object +%$ data = zeros(10,2); +%$ o = dseries(data); +%$ q = dseries(data); +%$ +%$ % Call the tested routine. +%$ try +%$ p = o.exp(); +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(o, q); +%$ t(3) = dassert(p.data, ones(10, 2)); +%$ end +%$ +%$ T = all(t); +%@eof:1 -for i=1:vobs(ts) - ts.name(i) = {['exp(' ts.name{i} ')']}; - ts.tex(i) = {['\exp(' ts.tex{i} ')']}; -end \ No newline at end of file +%@test:2 +%$ % Define a dates object +%$ data = zeros(10,2); +%$ o = dseries(data); +%$ q = dseries(data); +%$ +%$ % Call the tested routine. +%$ try +%$ p = o.exp(); +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(length(p.name), 2); +%$ t(3) = dassert(p.name{1},'exp(Variable_1)'); +%$ t(4) = dassert(p.name{2},'exp(Variable_2)'); +%$ t(5) = dassert(o.name{1},'Variable_1'); +%$ t(6) = dassert(o.name{2},'Variable_2'); +%$ end +%$ +%$ T = all(t); +%@eof:2 \ No newline at end of file diff --git a/src/@dseries/exp_.m b/src/@dseries/exp_.m new file mode 100644 index 0000000000000000000000000000000000000000..6a6fdd35540ae443279c34ef3ef947a1280a304b --- /dev/null +++ b/src/@dseries/exp_.m @@ -0,0 +1,85 @@ +function o = exp_(o) % --*-- Unitary tests --*-- + +% Apply the exponential to all the variables in a dseries object (in place modification). +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - o [dseries] + +% Copyright (C) 2015 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.data = exp(o.data); + +for i=1:vobs(o) + o.name(i) = {['exp(' o.name{i} ')']}; + o.tex(i) = {['\exp(' o.tex{i} ')']}; +end + +%@test:1 +%$ % Define a dates object +%$ data = zeros(10,2); +%$ o = dseries(data); +%$ q = o; +%$ r = copy(o); +%$ +%$ % Call the tested routine. +%$ try +%$ o.exp_(); +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(o.data, ones(10,2)); +%$ t(3) = dassert(q.data, ones(10,2)); +%$ t(4) = dassert(r.data, zeros(10, 2)); +%$ end +%$ +%$ T = all(t); +%@eof:1 + +%@test:2 +%$ % Define a dates object +%$ data = zeros(10,2); +%$ o = dseries(data); +%$ q = o; +%$ r = copy(o); +%$ +%$ % Call the tested routine. +%$ try +%$ o.exp_(); +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(length(o.name), 2); +%$ t(3) = dassert(o.name{1},'exp(Variable_1)'); +%$ t(4) = dassert(o.name{2},'exp(Variable_2)'); +%$ t(5) = dassert(q.name{1},'exp(Variable_1)'); +%$ t(6) = dassert(q.name{2},'exp(Variable_2)'); +%$ t(7) = dassert(r.name{1},'Variable_1'); +%$ t(8) = dassert(r.name{2},'Variable_2'); +%$ end +%$ +%$ T = all(t); +%@eof:2 \ No newline at end of file diff --git a/src/@dseries/log.m b/src/@dseries/log.m index 2ff8e6488572bc1bf7497ca771f996beb34a4962..da357b3a7c200b78b805917c0b4f6656c55e365b 100644 --- a/src/@dseries/log.m +++ b/src/@dseries/log.m @@ -1,32 +1,14 @@ -function ts = log(ts) +function o = log(o) % --*-- Unitary tests --*-- -%@info: -%! @deftypefn {Function File} {@var{ts} =} log(@var{ts}) -%! @anchor{log} -%! Apply the logarithm function to a Dynare time series object. -%! -%! @strong{Inputs} -%! @table @var -%! @item ts -%! Dynare time series object, instantiated by @ref{dseries} -%! @end table -%! -%! @strong{Outputs} -%! @table @var -%! @item ts -%! Dynare time series object with transformed data field. -%! @end table -%! -%! @strong{This function is called by:} -%! None. -%! -%! @strong{This function calls:} -%! None. -%! -%! @end deftypefn -%@eod: +% Apply the logarithm to all the variables in a dseries object (without in place modification). +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - o [dseries] -% Copyright (C) 2011-2013 Dynare Team +% Copyright (C) 2011-2015 Dynare Team % % This file is part of Dynare. % @@ -43,13 +25,56 @@ function ts = log(ts) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -if any(ts.data<eps) - error('dseries::log: Input argument has to be strictly positive!') +if any(o.data<eps) + error('dseries:WrongInputArguments', 'Variables in %s have be strictly positive!', inputname(1)) end -for i=1:vobs(ts) - ts.name(i) = {['log(' ts.name{i} ')']}; - ts.tex(i) = {['\log(' ts.tex{i} ')']}; -end +o = copy(o); +o.log_; + +%@test:1 +%$ % Define a dates object +%$ data = ones(10,2); +%$ o = dseries(data); +%$ q = dseries(data); +%$ +%$ % Call the tested routine. +%$ try +%$ p = o.log(); +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(o, q); +%$ t(3) = dassert(p.data, zeros(10, 2)); +%$ end +%$ +%$ T = all(t); +%@eof:1 -ts.data = log(ts.data); \ No newline at end of file +%@test:2 +%$ % Define a dates object +%$ data = ones(10,2); +%$ o = dseries(data); +%$ q = dseries(data); +%$ +%$ % Call the tested routine. +%$ try +%$ p = o.log(); +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(length(p.name), 2); +%$ t(3) = dassert(p.name{1},'log(Variable_1)'); +%$ t(4) = dassert(p.name{2},'log(Variable_2)'); +%$ t(5) = dassert(o.name{1},'Variable_1'); +%$ t(6) = dassert(o.name{2},'Variable_2'); +%$ end +%$ +%$ T = all(t); +%@eof:2 \ No newline at end of file diff --git a/src/@dseries/log_.m b/src/@dseries/log_.m new file mode 100644 index 0000000000000000000000000000000000000000..f6e8d9528c5f507394edc40573911a6637d5122f --- /dev/null +++ b/src/@dseries/log_.m @@ -0,0 +1,89 @@ +function o = log_(o) % --*-- Unitary tests --*-- + +% Apply the logarithm to all the variables in a dseries object (in place modification). +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - o [dseries] + +% Copyright (C) 2011-2015 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 any(o.data<eps) + error('dseries:WrongInputArguments', 'Variables in %s have be strictly positive!', inputname(1)) +end + +for i=1:vobs(o) + o.name(i) = {['log(' o.name{i} ')']}; + o.tex(i) = {['\log(' o.tex{i} ')']}; +end + +o.data = log(o.data); + +%@test:1 +%$ % Define a dates object +%$ data = ones(10,2); +%$ o = dseries(data); +%$ q = o; +%$ r = copy(o); +%$ +%$ % Call the tested routine. +%$ try +%$ o.log_(); +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(o.data, zeros(10,2)); +%$ t(3) = dassert(q.data, zeros(10,2)); +%$ t(4) = dassert(r.data, ones(10, 2)); +%$ end +%$ +%$ T = all(t); +%@eof:1 + +%@test:2 +%$ % Define a dates object +%$ data = ones(10,2); +%$ o = dseries(data); +%$ q = o; +%$ r = copy(o); +%$ +%$ % Call the tested routine. +%$ try +%$ o.log_(); +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(length(o.name), 2); +%$ t(3) = dassert(o.name{1},'log(Variable_1)'); +%$ t(4) = dassert(o.name{2},'log(Variable_2)'); +%$ t(5) = dassert(q.name{1},'log(Variable_1)'); +%$ t(6) = dassert(q.name{2},'log(Variable_2)'); +%$ t(7) = dassert(r.name{1},'Variable_1'); +%$ t(8) = dassert(r.name{2},'Variable_2'); +%$ end +%$ +%$ T = all(t); +%@eof:2 \ No newline at end of file diff --git a/src/@dseries/subsref.m b/src/@dseries/subsref.m index ec731ef2910e59d5163777bcd5d66d37cf2e0cb2..df7ba22311a3469b61e31671ecee8fbe0978812a 100644 --- a/src/@dseries/subsref.m +++ b/src/@dseries/subsref.m @@ -70,7 +70,7 @@ switch S(1).type error(['dseries::subsref: ' S(1).subs ' is not a method but a member!']) end B = builtin('subsref', A, S(1)); - case {'log','exp','ygrowth','qgrowth','ydiff','qdiff','abs'} % Give "dot access" to public methods without args. + case {'log','log_','exp','exp_','ygrowth','qgrowth','ydiff','qdiff','abs'} % Give "dot access" to public methods without args. B = feval(S(1).subs,A); if length(S)>1 && isequal(S(2).type,'()') && isempty(S(2).subs) S = shiftS(S,1);