diff --git a/src/@dseries/abs.m b/src/@dseries/abs.m index 0b7be9af4931b6a80204abe58d0d294a08463488..b4def71a9e8a420a8b4ae6da21ea5a682a7aff2d 100644 --- a/src/@dseries/abs.m +++ b/src/@dseries/abs.m @@ -1,27 +1,14 @@ -function A = abs(B) % --*-- Unitary tests --*-- +function o = abs(o) % --*-- Unitary tests --*-- -%@info: -%! @deftypefn {Function File} {@var{A} =} abs (@var{B}) -%! @anchor{@dseries/uminus} -%! @sp 1 -%! Overloads the abs method for the Dynare time series class (@ref{dseries}). -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @ @var -%! @item B -%! Dynare time series object instantiated by @ref{dseries}. -%! @end table -%! @sp 1 -%! @strong{Outputs} -%! @sp 1 -%! @table @ @var -%! @item A -%! Dynare time series object. -%! @end deftypefn -%@eod: +% Apply the absolute value to all the variables in a dseries object (without in place modification). +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - o [dseries] -% Copyright (C) 2013 Dynare Team +% Copyright (C) 2011-2017 Dynare Team % % This file is part of Dynare. % @@ -38,26 +25,63 @@ function A = abs(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/>. -A = dseries(); +o = copy(o); +o.abs_; -A.data = abs(B.data); -A.dates = B.dates; +%@test:1 +%$ % Define a dates object +%$ data = -ones(10,2); +%$ o = dseries(data); +%$ q = dseries(data); +%$ +%$ % Call the tested routine. +%$ try +%$ p = o.abs(); +%$ 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 -A.name = cell(vobs(A), 1); -A.tex = cell(vobs(A), 1); -for i = 1:vobs(A) - A.name(i) = {[ 'abs(' B.name{i} ')']}; - A.tex(i) = {[ '|' B.tex{i} '|']}; -end +%@test:2 +%$ % Define a dates object +%$ data = -ones(10,2); +%$ o = dseries(data); +%$ q = dseries(data); +%$ +%$ % Call the tested routine. +%$ try +%$ p = o.abs(); +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(length(p.name), 2); +%$ t(3) = dassert(p.name{1},'abs(Variable_1)'); +%$ t(4) = dassert(p.name{2},'abs(Variable_2)'); +%$ t(5) = dassert(o.name{1},'Variable_1'); +%$ t(6) = dassert(o.name{2},'Variable_2'); +%$ end +%$ +%$ T = all(t); +%@eof:2 -%@test:1 +%@test:3 %$ % Define a datasets. %$ A = randn(10,2); %$ %$ % Define names %$ A_name = {'A1';'A2'}; %$ A_tex = {'A_1';'A_2'}; -%$ t = zeros(6,1); %$ %$ % Instantiate a time series object and compute the absolute value. %$ try @@ -74,18 +98,22 @@ end %$ t(4) = dassert(ts2.data,abs(A),1e-15); %$ t(5) = dassert(ts2.name,{'abs(A1)';'abs(A2)'}); %$ t(6) = dassert(ts2.tex,{'|A_1|';'|A_2|'}); +%$ t(7) = dassert(ts1.vobs, 2); +%$ t(8) = dassert(ts1.nobs, 10); +%$ t(9) = dassert(ts1.data, A, 1e-15); +%$ t(10) = dassert(ts1.name, {'A1';'A2'}); +%$ t(11) = dassert(ts1.tex, {'A_1';'A_2'}); %$ end %$ T = all(t); -%@eof:1 +%@eof:3 -%@test:2 +%@test:4 %$ % Define a datasets. %$ A = randn(10,2); %$ %$ % Define names %$ A_name = {'A1';'A2'}; %$ A_tex = {'A_1';'A_2'}; -%$ t = zeros(6,1); %$ %$ % Instantiate a time series object and compute the absolute value. %$ try @@ -102,6 +130,11 @@ end %$ t(4) = dassert(ts2.data,abs(A),1e-15); %$ t(5) = dassert(ts2.name,{'abs(A1)';'abs(A2)'}); %$ t(6) = dassert(ts2.tex,{'|A_1|';'|A_2|'}); +%$ t(7) = dassert(ts1.vobs, 2); +%$ t(8) = dassert(ts1.nobs, 10); +%$ t(9) = dassert(ts1.data, A, 1e-15); +%$ t(10) = dassert(ts1.name, {'A1';'A2'}); +%$ t(11) = dassert(ts1.tex, {'A_1';'A_2'}); %$ end %$ T = all(t); -%@eof:2 \ No newline at end of file +%@eof:4 diff --git a/src/@dseries/abs_.m b/src/@dseries/abs_.m new file mode 100644 index 0000000000000000000000000000000000000000..a4d0378aecf6a75223ead9bf0eb80bf88937e901 --- /dev/null +++ b/src/@dseries/abs_.m @@ -0,0 +1,85 @@ +function o = abs_(o) % --*-- Unitary tests --*-- + +% Apply the absolute value to all the variables in a dseries object (in place modification). +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - o [dseries] + +% 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/>. + +for i=1:vobs(o) + o.name(i) = {['abs(' o.name{i} ')']}; + o.tex(i) = {['|' o.tex{i} '|']}; +end + +o.data = abs(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.abs_(); +%$ 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, -ones(10, 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},'abs(Variable_1)'); +%$ t(4) = dassert(o.name{2},'abs(Variable_2)'); +%$ t(5) = dassert(q.name{1},'abs(Variable_1)'); +%$ t(6) = dassert(q.name{2},'abs(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/baxter_king_filter.m b/src/@dseries/baxter_king_filter.m index ea9abef08fac6bc3218e3d9bfddb90ce6ba1479c..be6b2585b9f972a6aa239c79fa9d4236a4a52308 100644 --- a/src/@dseries/baxter_king_filter.m +++ b/src/@dseries/baxter_king_filter.m @@ -1,25 +1,23 @@ -function ts = baxter_king_filter(ts, high_frequency, low_frequency, K) % --*-- Unitary tests --*-- +function o = baxter_king_filter(o, high_frequency, low_frequency, K) % --*-- Unitary tests --*-- -% 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 % ranging between high_frequency to low_frequency (quarters). % % 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). +% - o dseries object. +% - high_frequency positive scalar, period length (default value is 6). +% - low_frequency positive scalar, period length (default value is 32). +% - K positive scalar integer, truncation parameter (default value is 12). % % OUTPUTS -% o ts dseries object. +% - o dseries object. % % 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,47 +55,12 @@ if nargin<4 || isempty(K) end end end - -% translate periods into frequencies. -hf=2.0*pi/high_frequency; -lf=2.0*pi/low_frequency; - -% Set weights for the band-pass filter's lag polynomial. -weights = zeros(K+1,1); lpowers = transpose(1:K); -weights(2:K+1) = (sin(lpowers*hf)-sin(lpowers*lf))./(lpowers*pi); -weights(1) = (hf-lf)/pi; - -% Set the constraint on the sum of weights. -if low_frequency>1000 - % => low pass filter. - sum_of_weights_constraint = 1.0; -else - sum_of_weights_constraint = 0.0; -end - -% Compute the sum of weights. -sum_of_weights = weights(1) + 2*sum(weights(2:K+1)); - -% Correct the weights. -weights = weights + (sum_of_weights_constraint - sum_of_weights)/(2*K+1); - -% Weights are symmetric! -weights = [flipud(weights(2:K+1)); weights]; - -tmp = zeros(size(ts.data)); - -% Filtering step. -for t = K+1:nobs(ts)-K - tmp(t,:) = weights'*ts.data(t-K:t+K,:); -end -% Update dseries object. -ts.data = tmp(K+1:end-K,:); -init = firstdate(ts)+K; -ts.dates = init:init+(nobs(ts)-1); +o = copy(o); +o.baxter_king_filter_(high_frequency, low_frequency, K); %@test:1 -%$ plot_flag = 0; +%$ plot_flag = false; %$ %$ % Create a dataset. %$ e = .2*randn(200,1); @@ -113,7 +76,7 @@ ts.dates = init:init+(nobs(ts)-1); %$ % Test the routine. %$ try %$ ts = dseries(y,'1950Q1'); -%$ ts = ts.baxter_king_filter(); +%$ ds = ts.baxter_king_filter(); %$ xx = dseries(x,'1950Q1'); %$ t(1) = 1; %$ catch @@ -121,11 +84,17 @@ ts.dates = init:init+(nobs(ts)-1); %$ end %$ %$ if t(1) -%$ t(2) = dassert(ts.freq,4); -%$ t(3) = dassert(ts.init.freq,4); -%$ t(4) = dassert(ts.init.time,[1953, 1]); -%$ t(5) = dassert(ts.vobs,1); -%$ t(6) = dassert(ts.nobs,176); +%$ t(2) = dassert(ds.freq, 4); +%$ t(3) = dassert(ds.init.freq, 4); +%$ t(4) = dassert(ds.init.time, [1953, 1]); +%$ t(5) = dassert(ds.vobs, 1); +%$ t(6) = dassert(ds.nobs, 176); +%$ t(7) = dassert(ts.freq, 4); +%$ t(8) = dassert(ts.init.freq, 4); +%$ t(9) = dassert(ts.init.time, [1950, 1]); +%$ t(10) = dassert(ts.vobs, 1); +%$ t(11) = dassert(ts.nobs, length(y)); +%$ t(12) = dassert(ts.data, y); %$ end %$ %$ % Show results diff --git a/src/@dseries/baxter_king_filter_.m b/src/@dseries/baxter_king_filter_.m new file mode 100644 index 0000000000000000000000000000000000000000..c77018e3325260aa16582eb63806f68cf89c6d12 --- /dev/null +++ b/src/@dseries/baxter_king_filter_.m @@ -0,0 +1,146 @@ +function o = baxter_king_filter_(o, high_frequency, low_frequency, K) % --*-- Unitary tests --*-- + +% 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 +% ranging between high_frequency to low_frequency (quarters). +% +% INPUTS +% - o dseries object. +% - high_frequency positive scalar, period length (default value is 6). +% - low_frequency positive scalar, period length (default value is 32). +% - K positive scalar integer, truncation parameter (default value is 12). +% +% OUTPUTS +% - o dseries object. +% +% 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-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<4 || isempty(K) + K = 12; + if nargin<3 || isempty(low_frequency) + % Set default number of periods corresponding to the lowest frequency. + low_frequency = 32; + if nargin<2 || isempty(high_frequency) + % Set default number of periods corresponding to the highest frequency. + high_frequency = 6; + if nargin<1 + error('dseries::baxter_king_filter: I need at least one argument') + end + else + if high_frequency<2 + error('dseries::baxter_king_filter: Second argument must be greater than 2!') + end + if high_frequency>low_frequency + error('dseries::baxter_king_filter: Second argument must be smaller than the third argument!') + end + end + end +end + +% translate periods into frequencies. +hf=2.0*pi/high_frequency; +lf=2.0*pi/low_frequency; + +% Set weights for the band-pass filter's lag polynomial. +weights = zeros(K+1,1); lpowers = transpose(1:K); +weights(2:K+1) = (sin(lpowers*hf)-sin(lpowers*lf))./(lpowers*pi); +weights(1) = (hf-lf)/pi; + +% Set the constraint on the sum of weights. +if low_frequency>1000 + % => low pass filter. + sum_of_weights_constraint = 1.0; +else + sum_of_weights_constraint = 0.0; +end + +% Compute the sum of weights. +sum_of_weights = weights(1) + 2*sum(weights(2:K+1)); + +% Correct the weights. +weights = weights + (sum_of_weights_constraint - sum_of_weights)/(2*K+1); + +% Weights are symmetric! +weights = [flipud(weights(2:K+1)); weights]; + +tmp = zeros(size(o.data)); + +% Filtering step. +for t = K+1:nobs(o)-K + tmp(t,:) = weights'*o.data(t-K:t+K,:); +end + +% Update dseries object. +o.data = tmp(K+1:end-K,:); +init = firstdate(o)+K; +o.dates = init:init+(nobs(o)-1); + +%@test:1 +%$ plot_flag = false; +%$ +%$ % Create a dataset. +%$ e = .2*randn(200,1); +%$ u = randn(200,1); +%$ stochastic_trend = cumsum(e); +%$ deterministic_trend = .1*transpose(1:200); +%$ x = zeros(200,1); +%$ for i=2:200 +%$ x(i) = .75*x(i-1) + e(i); +%$ end +%$ y = x + stochastic_trend + deterministic_trend; +%$ +%$ % Test the routine. +%$ try +%$ ts = dseries(y,'1950Q1'); +%$ ts = ts.baxter_king_filter_(); +%$ xx = dseries(x,'1950Q1'); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(ts.freq,4); +%$ t(3) = dassert(ts.init.freq,4); +%$ t(4) = dassert(ts.init.time,[1953, 1]); +%$ t(5) = dassert(ts.vobs,1); +%$ t(6) = dassert(ts.nobs,176); +%$ end +%$ +%$ % Show results +%$ if plot_flag +%$ plot(xx(ts.dates).data,'-k'); +%$ hold on +%$ plot(ts.data,'--r'); +%$ hold off +%$ axis tight +%$ id = get(gca,'XTick'); +%$ set(gca,'XTickLabel',strings(ts.dates(id))); +%$ legend({'Stationary component of y', 'Filtered y'}) +%$ print('-depsc2','../doc/dynare.plots/BaxterKingFilter.eps') +%$ system('convert -density 300 ../doc/dynare.plots/BaxterKingFilter.eps ../doc/dynare.plots/BaxterKingFilter.png'); +%$ system('convert -density 300 ../doc/dynare.plots/BaxterKingFilter.eps ../doc/dynare.plots/BaxterKingFilter.pdf'); +%$ system('convert -density 300 ../doc/dynare.plots/BaxterKingFilter.eps ../doc/dynare.plots/BaxterKingFilter.jpg'); +%$ end +%$ +%$ T = all(t); +%@eof:1 diff --git a/src/@dseries/center.m b/src/@dseries/center.m index ba1dc7dc9e7d5955a3f10e55105a6666d57c69c9..d351745b9d22052d2e41311b326a09e74374bd80 100644 --- a/src/@dseries/center.m +++ b/src/@dseries/center.m @@ -1,15 +1,15 @@ function o = center(o, geometric) % --*-- Unitary tests --*-- -% Centers @dseries object o around its mean (arithmetic or geometric). +% 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. +% INPUTS +% - o dseries object [mandatory]. +% - geometric logical [default is false], if true returns the geometric mean. % -% OUTPUTS -% o o o +% OUTPUTS +% - o dseries object. -% Copyright (C) 2016 Dynare Team +% Copyright (C) 2016-2017 Dynare Team % % This file is part of Dynare. % @@ -30,11 +30,8 @@ if nargin<2 geometric = false; end -if geometric - o = o/mean(o, true); -else - o = o-mean(o, false); -end +o = copy(o); +o.center_(geometric); %@test:1 %$ % Define a dataset. @@ -43,14 +40,15 @@ end %$ % Instantiate a time series object and compute the mean. %$ try %$ ts = dseries(A); -%$ ts.center(true); +%$ ds = ts.center(true); %$ t(1) = 1; %$ catch %$ t = 0; %$ end %$ %$ if t(1) -%$ t(2) = all(all(abs(ts.data-ones(10,2))<1e-12)); +%$ t(2) = all(all(abs(ds.data-ones(10,2))<1e-12)); +%$ t(3) = all(all(abs(ts.data-A)<1e-12)); %$ end %$ T = all(t); %@eof:1 diff --git a/src/@dseries/center_.m b/src/@dseries/center_.m new file mode 100644 index 0000000000000000000000000000000000000000..0ea1b97d15ec3e2a097f5e39bf6a7958d212db72 --- /dev/null +++ b/src/@dseries/center_.m @@ -0,0 +1,56 @@ +function o = center_(o, geometric) % --*-- Unitary tests --*-- + +% Centers dseries object o around its mean (arithmetic or geometric). +% +% INPUTS +% - o dseries object [mandatory]. +% - geometric logical [default is false], if true returns the geometric mean. +% +% OUTPUTS +% - o dseries object. + +% 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_(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 diff --git a/src/@dseries/chain.m b/src/@dseries/chain.m index da0bcb813c50de6ce7b1694e6daad8e135d89b47..a0bc54a707970da403bbf168bb26a90e0963678d 100644 --- a/src/@dseries/chain.m +++ b/src/@dseries/chain.m @@ -1,4 +1,17 @@ -function vs = chain(ts,us) % --*-- Unitary tests --*-- +function q = chain(o, p) % --*-- Unitary tests --*-- + +% Chains two dseries objects. +% +% INPUTS +% - o [dseries] +% - p [dseries] +% +% OUTPUTS +% - q [dseries] +% +% REMARKS +% The two dseries objects must have common frequency and the same number of variables. Also the +% two samples must overlap. % Copyright (C) 2014 Dynare Team % @@ -17,26 +30,8 @@ function vs = chain(ts,us) % --*-- 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 vobs(ts)-vobs(us) - error(['dseries::chain: dseries objects ' inputname(1) ' and ' inputname(2) ' must have the same number of variables!']) -end - -if frequency(ts)-frequency(us) - error(['dseries::chain: dseries objects ' inputname(1) ' and ' inputname(2) ' must have common frequencies!']) -end - -if lastdate(ts)<firstdate(us) - error(['dseries::chain: The last date in ' inputname(1) ' (' date2string(ts.dates(end)) ') must not preceed the first date in ' inputname(2) ' (' date2string(us.dates(1)) ')!']) -end - -tdx = find(sum(bsxfun(@eq,us.dates.time,ts.dates.time(end,:)),2)==2); -GrowthFactor = us.data(tdx+1:end,:)./us.data(tdx:end-1,:); -CumulatedGrowthFactors = cumprod(GrowthFactor); - -vs = ts; -vs.data = [vs.data; bsxfun(@times,CumulatedGrowthFactors,vs.data(end,:))]; - -vs.dates = firstdate(vs):firstdate(vs)+nobs(vs); +q = copy(o); +q.chain_(p); %@test:1 %$ try @@ -49,12 +44,14 @@ vs.dates = firstdate(vs):firstdate(vs)+nobs(vs); %$ end %$ %$ if t(1) -%$ t(2) = dassert(vs.freq,4); -%$ t(3) = dassert(vs.init.freq,4); -%$ t(4) = dassert(vs.init.time,[1950, 1]); -%$ t(5) = dassert(ts.vobs,1); -%$ t(6) = dassert(vs.nobs,6); -%$ t(7) = isequal(vs.data,transpose(1:6)); +%$ t(2) = dassert(vs.freq, 4); +%$ t(3) = dassert(vs.init.freq, 4); +%$ t(4) = dassert(vs.init.time, [1950, 1]); +%$ t(5) = dassert(vs.vobs, 1); +%$ t(6) = dassert(vs.nobs, 6); +%$ t(7) = isequal(vs.data, transpose(1:6)); +%$ t(8) = isequal(ts.data, transpose(1:4)); +%$ t(9) = isequal(ts.init.time, [1950, 1]); %$ end %$ %$ T = all(t); diff --git a/src/@dseries/chain_.m b/src/@dseries/chain_.m new file mode 100644 index 0000000000000000000000000000000000000000..6e73e14f549c6ccebbeaa61da938fa75f0a44695 --- /dev/null +++ b/src/@dseries/chain_.m @@ -0,0 +1,60 @@ +function o = chain_(o, p) % --*-- Unitary tests --*-- + +% Copyright (C) 2014-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 vobs(o)-vobs(p) + error(['dseries::chain: dseries objects ' inputname(1) ' and ' inputname(2) ' must have the same number of variables!']) +end + +if frequency(o)-frequency(p) + error(['dseries::chain: dseries objects ' inputname(1) ' and ' inputname(2) ' must have common frequencies!']) +end + +if lastdate(o)<firstdate(p) + error(['dseries::chain: The last date in ' inputname(1) ' (' date2string(o.dates(end)) ') must not preceed the first date in ' inputname(2) ' (' date2string(p.dates(1)) ')!']) +end + +tdx = find(sum(bsxfun(@eq, p.dates.time, o.dates.time(end,:)),2)==2); +GrowthFactor = p.data(tdx+1:end,:)./p.data(tdx:end-1,:); +CumulatedGrowthFactors = cumprod(GrowthFactor); + +o.data = [o.data; bsxfun(@times,CumulatedGrowthFactors, o.data(end,:))]; + +o.dates = firstdate(o):firstdate(o)+nobs(o); + +%@test:1 +%$ try +%$ ts = dseries([1; 2; 3; 4],dates('1950Q1')) ; +%$ us = dseries([3; 4; 5; 6],dates('1950Q3')) ; +%$ ts.chain_(us); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(ts.freq,4); +%$ t(3) = dassert(ts.init.freq,4); +%$ t(4) = dassert(ts.init.time,[1950, 1]); +%$ t(5) = dassert(ts.vobs,1); +%$ t(6) = dassert(ts.nobs,6); +%$ t(7) = isequal(ts.data,transpose(1:6)); +%$ end +%$ +%$ T = all(t); +%@eof:1 \ No newline at end of file diff --git a/src/@dseries/check.m b/src/@dseries/check.m index c4bfce37a40008176eaed90794a31e953cfa01f6..8c113348f3e1ad0b472318ecf0344dd6a470660c 100644 --- a/src/@dseries/check.m +++ b/src/@dseries/check.m @@ -1,6 +1,6 @@ -function [error_flag,message] = check(A) +function [error_flag, message] = check(o) -% Copyright (C) 2013 Dynare Team +% Copyright (C) 2013-2017 Dynare Team % % This file is part of Dynare. % @@ -19,9 +19,9 @@ function [error_flag,message] = check(A) error_flag = 0; -[n,m] = size(A.data); +[n,m] = size(o.data); -if ~isequal(m, vobs(A)); +if ~isequal(m, vobs(o)); error_flag = 1; if nargout>1 message = ['dseries: Wrong number of variables in dseries object ''' inputname(1) '''!']; @@ -29,7 +29,7 @@ if ~isequal(m, vobs(A)); return end -if ~isequal(n,nobs(A)); +if ~isequal(n,nobs(o)); error_flag = 1; if nargout>1 message = ['dseries: Wrong number of observations in dseries object ''' inputname(1) '''!']; @@ -37,7 +37,7 @@ if ~isequal(n,nobs(A)); return end -if ~isequal(m,numel(A.name)); +if ~isequal(m,numel(o.name)); error_flag = 1; if nargout>1 message = ['dseries: Wrong number of variable names in dseries object ''' inputname(1) '''!']; @@ -45,7 +45,7 @@ if ~isequal(m,numel(A.name)); return end -if ~isequal(m,numel(A.tex)); +if ~isequal(m,numel(o.tex)); error_flag = 1; if nargout>1 message = ['dseries: Wrong number of variable tex names in dseries object ''' inputname(1) '''!']; @@ -53,7 +53,7 @@ if ~isequal(m,numel(A.tex)); return end -if ~isequal(numel(A.name),numel(A.tex)); +if ~isequal(numel(o.name), numel(o.tex)); error_flag = 1; if nargout>1 message = ['dseries: The number of variable tex names has to be equal to the number of variable names in dseries object ''' inputname(1) '''!']; @@ -61,7 +61,7 @@ if ~isequal(numel(A.name),numel(A.tex)); return end -if ~isequal(numel(unique(A.name)),numel(A.name)); +if ~isequal(numel(unique(o.name)), numel(o.name)); error_flag = 1; if nargout>1 message = ['dseries: The variable names in dseries object ''' inputname(1) ''' are not unique!']; @@ -69,7 +69,7 @@ if ~isequal(numel(unique(A.name)),numel(A.name)); return end -if ~isequal(numel(unique(A.tex)),numel(A.tex)); +if ~isequal(numel(unique(o.tex)), numel(o.tex)); error_flag = 1; if nargout>1 message = ['dseries: The variable tex names in dseries object ''' inputname(1) ''' are not unique!']; @@ -77,7 +77,7 @@ if ~isequal(numel(unique(A.tex)),numel(A.tex)); return end -if ~isequal(A.dates,firstdate(A):firstdate(A)+nobs(A)) +if ~isequal(o.dates, firstdate(o):firstdate(o)+nobs(o)) error_flag = 1; if nargout>1 message = ['dseries: Wrong definition of the dates member in dseries object ''' inputname(1) '''!']; diff --git a/src/@dseries/cumprod.m b/src/@dseries/cumprod.m index 4e79bae6c11999b2587befc42f6f15b2276d84ed..8d89ff452e4e47f0057f193c940b7f007417992c 100644 --- a/src/@dseries/cumprod.m +++ b/src/@dseries/cumprod.m @@ -1,16 +1,16 @@ -function B = cumprod(varargin) % --*-- Unitary tests --*-- +function o = cumprod(varargin) % --*-- Unitary tests --*-- % Overloads matlab's cumprod function for dseries objects. % % INPUTS -% o A dseries object [mandatory]. -% o d dates object [optional] -% o v dseries object with one observation [optional] +% - A dseries object [mandatory]. +% - d dates object [optional] +% - v dseries object with one observation [optional] % % OUTPUTS -% o B dseries object. +% - B dseries object. -% Copyright (C) 2014 Dynare Team +% Copyright (C) 2014-2017 Dynare Team % % This file is part of Dynare. % @@ -27,103 +27,12 @@ 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 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) - idx = 0; -end +o = copy(varargin{1}); -% 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 +if nargin<2 + o.cumprod_(); 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 - 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})) - error('dseries::cumprod: First and second input arguments must be dseries objects with the same number of variables!') - end - if ~isequal(varargin{1}.name, varargin{2}.name) - warning('dseries::cumprod: First and second input arguments must be dseries objects do not have the same variables!') - end - if ~isequal(nobs(varargin{2}),1) - error('dseries::cumprod: Second input argument must be a dseries object with only one observation!') - end - B = cumprod(varargin{1}); - B.data = bsxfun(@rdivide,B.data,B.data(1,:)); - B.data = bsxfun(@times,B.data,varargin{2}.data); - elseif isdates(varargin{2}) - B = cumprod(varargin{1}); - t = find(B.dates==varargin{2}); - if isempty(t) - if varargin{2}==(firstdate(B)-1) - return - else - error(['dseries::cumprod: date ' date2string(varargin{2}) ' is not in the sample!']) - end - end - B.data = bsxfun(@rdivide,B.data,B.data(t,:)); - else - error('dseries::cumprod: Second input argument must be a dseries object or a dates object!') - end - case 3 - if ~isdates(varargin{2}) - error('dseries::cumprod: Second input argument must be a dates object!') - end - if ~isdseries(varargin{3}) - error('dseries::cumprod: Third input argument must be a dseries object!') - end - if ~isequal(vobs(varargin{1}), vobs(varargin{3})) - error('dseries::cumprod: First and third input arguments must be dseries objects with the same number of variables!') - end - if ~isequal(varargin{1}.name, varargin{3}.name) - warning('dseries::cumprod: First and third input arguments must be dseries objects do not have the same variables!') - end - if ~isequal(nobs(varargin{3}),1) - error('dseries::cumprod: Third input argument must be a dseries object with only one observation!') - end - B = cumprod(varargin{1}); - t = find(B.dates==varargin{2}); - if isempty(t) - if varargin{2}==(firstdate(B)-1) - B.data = bsxfun(@times,B.data,varargin{3}.data); - return - else - error(['dseries::cumprod: date ' date2string(varargin{2}) ' is not in the sample!']) - end - end - B.data = bsxfun(@rdivide, B.data, B.data(t,:)); - B.data = bsxfun(@times, B.data, varargin{3}.data); - otherwise - error('dseries::cumprod: Wrong number of input arguments!') + o.cumprod_(varargin{2:end}); end %@test:1 @@ -138,15 +47,17 @@ end %$ %$ % Call the tested method. %$ try -%$ ts = cumprod(ts); +%$ ds = cumprod(ts); %$ t(1) = 1; %$ catch %$ t(1) = 0; %$ end %$ %$ if t(1) -%$ t(2) = isequal(ts.data,cumprod(A)); -%$ t(3) = isequal(ts.name{1},'cumprod(A1)'); +%$ t(2) = isequal(ds.data, cumprod(A)); +%$ t(3) = isequal(ds.name{1}, 'cumprod(A1)'); +%$ t(4) = isequal(ts.data, A); +%$ t(5) = isequal(ts.name{1}, 'A1'); %$ end %$ %$ T = all(t); @@ -154,107 +65,30 @@ end %@test:2 %$ % Define a data set. -%$ A = 2*ones(4,1); +%$ A = 2*ones(7,1); %$ %$ % Define names %$ A_name = {'A1'}; %$ %$ % Instantiate a time series object. -%$ ts = dseries(A,[],A_name,[]); +%$ ts1 = dseries(A, [], A_name, []); +%$ ts2 = dseries(pi, [], A_name, []); %$ %$ % Call the tested method. %$ try -%$ ts = ts.cumprod(); -%$ t(1) = 1; +%$ ts3 = ts1.cumprod(dates('3Y'),ts2); +%$ t(1) = 1; %$ catch -%$ t(1) = 0; +%$ t(1) = 0; %$ end %$ -%$ if t(1) -%$ t(2) = isequal(ts.data,cumprod(A)); -%$ t(3) = isequal(ts.name{1},'cumprod(A1)'); -%$ end -%$ -%$ T = all(t); -%@eof:2 - - -%@test:3 -%$ % Define a data set. -%$ A = 2*ones(7,1); -%$ -%$ % Define names -%$ A_name = {'A1'}; -%$ -%$ % Instantiate a time series object. -%$ ts = dseries(A,[],A_name,[]); -%$ -%$ % Call the tested method. -%$ ts = cumprod(ts,dates('3Y')); -%$ -%$ % Expected results. -%$ ds = dseries([.25; .5; 1; 2; 4; 8; 16], [], A_name, []); -%$ -%$ % Check the results. -%$ t(1) = dassert(ts.data,ds.data); -%$ T = all(t); -%@eof:3 - -%@test:4 -%$ % Define a data set. -%$ A = 2*ones(7,1); -%$ -%$ % Define names -%$ A_name = {'A1'}; -%$ -%$ % Instantiate a time series object. -%$ ts1 = dseries(A,[],A_name,[]); -%$ ts2 = dseries(pi, [], A_name, []); -%$ -%$ % Call the tested method. -%$ ts3 = cumprod(ts1,dates('3Y'),ts2); -%$ -%$ % Expected results. -%$ ts4 = dseries([.25; .5; 1; 2; 4; 8; 16]*pi, [], A_name, []); -%$ -%$ % Check the results. -%$ t(1) = dassert(ts3.data,ts4.data); -%$ T = all(t); -%@eof:4 - -%@test:5 -%$ % Define a data set. -%$ A = 2*ones(7,1); -%$ -%$ % Define names -%$ A_name = {'A1'}; -%$ -%$ % Instantiate a time series object. -%$ ts1 = dseries(A,[],A_name,[]); -%$ ts2 = dseries(pi, [], A_name, []); -%$ -%$ % Call the tested method. -%$ ts3 = ts1.cumprod(dates('3Y'),ts2); -%$ %$ % Expected results. %$ ts4 = dseries([.25; .5; 1; 2; 4; 8; 16]*pi, [], A_name, []); %$ %$ % Check the results. -%$ t(1) = dassert(ts3.data,ts4.data); -%$ T = all(t); -%@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.cumprod(); -%$ -%$ % Check the results. -%$ t(1) = dassert(ts0.data, A); +%$ if t(1) +%$ t(2) = dassert(ts3.data, ts4.data); +%$ t(3) = dassert(ts1.data, A); +%$ end %$ T = all(t); -%@eof:6 \ No newline at end of file +%@eof:2 diff --git a/src/@dseries/cumprod_.m b/src/@dseries/cumprod_.m new file mode 100644 index 0000000000000000000000000000000000000000..836090fa24dd8fcaa5ac67cc50f0ce56cca2e9d0 --- /dev/null +++ b/src/@dseries/cumprod_.m @@ -0,0 +1,288 @@ +function o = cumprod_(varargin) % --*-- Unitary tests --*-- + +% Overloads matlab's cumprod function for dseries objects. +% +% INPUTS +% - o dseries object [mandatory]. +% - d dates object [optional] +% - v dseries object with one observation [optional] +% +% OUTPUTS +% - o dseries object. + +% Copyright (C) 2014-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/>. + +% 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) + idx = 0; +end + +% 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. + o = varargin{1}; + % Perform the cumulated sum + if isequal(idx, 1) + o.data = cumprod(o.data); + else + if common_first_period_witout_nan + o.data(idx:end,:) = cumprod(o.data(idx:end,:)); + else + o.data = cumprodnan(o.data); + end + end + % Change the name of the variables + for i=1:vobs(o) + o.name(i) = {['cumprod(' o.name{i} ')']}; + o.tex(i) = {['\prod_t ' o.tex{i}]}; + end + case 2 + if isdseries(varargin{2}) + if ~isequal(vobs(varargin{1}), vobs(varargin{2})) + error('dseries::cumprod: First and second input arguments must be dseries objects with the same number of variables!') + end + if ~isequal(varargin{1}.name, varargin{2}.name) + warning('dseries::cumprod: First and second input arguments must be dseries objects do not have the same variables!') + end + if ~isequal(nobs(varargin{2}),1) + error('dseries::cumprod: Second input argument must be a dseries object with only one observation!') + end + o = cumprod_(varargin{1}); + o.data = bsxfun(@rdivide, o.data, o.data(1,:)); + o.data = bsxfun(@times, o.data, varargin{2}.data); + elseif isdates(varargin{2}) + o = cumprod_(varargin{1}); + t = find(o.dates==varargin{2}); + if isempty(t) + if varargin{2}==(firstdate(o)-1) + return + else + error(['dseries::cumprod: date ' date2string(varargin{2}) ' is not in the sample!']) + end + end + o.data = bsxfun(@rdivide, o.data, o.data(t,:)); + else + error('dseries::cumprod: Second input argument must be a dseries object or a dates object!') + end + case 3 + if ~isdates(varargin{2}) + error('dseries::cumprod: Second input argument must be a dates object!') + end + if ~isdseries(varargin{3}) + error('dseries::cumprod: Third input argument must be a dseries object!') + end + if ~isequal(vobs(varargin{1}), vobs(varargin{3})) + error('dseries::cumprod: First and third input arguments must be dseries objects with the same number of variables!') + end + if ~isequal(varargin{1}.name, varargin{3}.name) + warning('dseries::cumprod: First and third input arguments must be dseries objects do not have the same variables!') + end + if ~isequal(nobs(varargin{3}),1) + error('dseries::cumprod: Third input argument must be a dseries object with only one observation!') + end + o = cumprod_(varargin{1}); + t = find(o.dates==varargin{2}); + if isempty(t) + if varargin{2}==(firstdate(o)-1) + o.data = bsxfun(@times, o.data, varargin{3}.data); + return + else + error(['dseries::cumprod: date ' date2string(varargin{2}) ' is not in the sample!']) + end + end + o.data = bsxfun(@rdivide, o.data, o.data(t,:)); + o.data = bsxfun(@times, o.data, varargin{3}.data); + otherwise + error('dseries::cumprod: Wrong number of input arguments!') +end + +%@test:1 +%$ % Define a data set. +%$ A = 2*ones(4,1); +%$ +%$ % Define names +%$ A_name = {'A1'}; +%$ +%$ % Instantiate a time series object. +%$ ts = dseries(A,[],A_name,[]); +%$ +%$ % Call the tested method. +%$ try +%$ ts.cumprod_(); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = isequal(ts.data, cumprod(A)); +%$ t(3) = isequal(ts.name{1}, 'cumprod(A1)'); +%$ end +%$ +%$ T = all(t); +%@eof:1 + +%@test:2 +%$ % Define a data set. +%$ A = 2*ones(4,1); +%$ +%$ % Define names +%$ A_name = {'A1'}; +%$ +%$ % Instantiate a time series object. +%$ ts = dseries(A,[],A_name,[]); +%$ +%$ % Call the tested method. +%$ try +%$ cumprod_(ts); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = isequal(ts.data,cumprod(A)); +%$ t(3) = isequal(ts.name{1},'cumprod(A1)'); +%$ end +%$ +%$ T = all(t); +%@eof:2 + + +%@test:3 +%$ % Define a data set. +%$ A = 2*ones(7,1); +%$ +%$ % Define names +%$ A_name = {'A1'}; +%$ +%$ % Instantiate a time series object. +%$ ts = dseries(A,[],A_name,[]); +%$ +%$ % Call the tested method. +%$ try +%$ ts.cumprod_(dates('3Y')); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ % Expected results. +%$ ds = dseries([.25; .5; 1; 2; 4; 8; 16], [], A_name, []); +%$ +%$ % Check the results. +%$ t(1) = dassert(ts.data, ds.data); +%$ T = all(t); +%@eof:3 + +%@test:4 +%$ % Define a data set. +%$ A = 2*ones(7,1); +%$ +%$ % Define names +%$ A_name = {'A1'}; +%$ +%$ % Instantiate a time series object. +%$ ts1 = dseries(A, [], A_name, []); +%$ ts2 = dseries(pi, [], A_name, []); +%$ +%$ % Call the tested method. +%$ try +%$ ts3 = ts1.cumprod_(dates('3Y'), ts2); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ % Expected results. +%$ ts4 = dseries([.25; .5; 1; 2; 4; 8; 16]*pi, [], A_name, []); +%$ +%$ % Check the results. +%$ if t(1) +%$ t(2) = dassert(ts3.data,ts4.data); +%$ t(3) = dassert(ts1.data,ts4.data); +%$ end +%$ T = all(t); +%@eof:4 + +%@test:5 +%$ % Define a data set. +%$ A = 2*ones(7,1); +%$ +%$ % Define names +%$ A_name = {'A1'}; +%$ +%$ % Instantiate a time series object. +%$ ts1 = dseries(A, [], A_name, []); +%$ ts2 = dseries(pi, [], A_name, []); +%$ +%$ % Call the tested method. +%$ try +%$ ts3 = ts1.cumprod_(dates('3Y'),ts2); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ % Expected results. +%$ ts4 = dseries([.25; .5; 1; 2; 4; 8; 16]*pi, [], A_name, []); +%$ +%$ % Check the results. +%$ if t(1) +%$ t(2) = dassert(ts3.data, ts4.data); +%$ t(3) = dassert(ts1.data, ts4.data); +%$ end +%$ T = all(t); +%@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. +%$ try +%$ ts0.cumprod_(); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ % Check the results. +%$ if t(1) +%$ t(2) = dassert(ts0.data, A); +%$ end +%$ T = all(t); +%@eof:6 \ No newline at end of file diff --git a/src/@dseries/cumsum.m b/src/@dseries/cumsum.m index 6b6ed11d3c474da3695a7ff7e943ddf699813dcf..cb70b316e32e4e900300c5804a4d4b4accfce91e 100644 --- a/src/@dseries/cumsum.m +++ b/src/@dseries/cumsum.m @@ -1,16 +1,16 @@ -function B = cumsum(varargin) % --*-- Unitary tests --*-- +function o = cumsum(varargin) % --*-- Unitary tests --*-- % Overloads matlab's cumsum function for dseries objects. % % INPUTS -% o A dseries object [mandatory]. -% o d dates object [optional] -% o v dseries object with one observation [optional] +% - o dseries object [mandatory]. +% - d dates object [optional] +% - v dseries object with one observation [optional] % % OUTPUTS -% o B dseries object. +% - o dseries object. -% Copyright (C) 2013 Dynare Team +% Copyright (C) 2013-2017 Dynare Team % % This file is part of Dynare. % @@ -27,101 +27,12 @@ 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 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) - idx = 0; -end +o = copy(varargin{1}); -% 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 +if nargin<2 + o.cumsum_(); 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 - 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})) - error('dseries::cumsum: First and second input arguments must be dseries objects with the same number of variables!') - end - if ~isequal(varargin{1}.name, varargin{2}.name) - warning('dseries::cumsum: First and second input arguments must be dseries objects do not have the same variables!') - end - if ~isequal(nobs(varargin{2}),1) - error('dseries::cumsum: Second input argument must be a dseries object with only one observation!') - end - B = cumsum(varargin{1}); - B.data = bsxfun(@plus,B.data,varargin{2}.data); - elseif isdates(varargin{2}) - B = cumsum(varargin{1}); - t = find(B.dates==varargin{2}); - if isempty(t) - if varargin{2}==(firstdate(B)-1) - return - else - error(['dseries::cumsum: date ' date2string(varargin{2}) ' is not in the sample!']) - end - end - B.data = bsxfun(@minus,B.data,B.data(t,:)); - else - error('dseries::cumsum: Second input argument must be a dseries object or a dates object!') - end - case 3 - if ~isdates(varargin{2}) - error('dseries::cumsum: Second input argument must be a dates object!') - end - if ~isdseries(varargin{3}) - error('dseries::cumsum: Third input argument must be a dseries object!') - end - if ~isequal(vobs(varargin{1}), vobs(varargin{3})) - error('dseries::cumsum: First and third input arguments must be dseries objects with the same number of variables!') - end - if ~isequal(varargin{1}.name, varargin{3}.name) - warning('dseries::cumsum: First and third input arguments must be dseries objects do not have the same variables!') - end - if ~isequal(nobs(varargin{3}),1) - error('dseries::cumsum: Third input argument must be a dseries object with only one observation!') - end - B = cumsum(varargin{1}); - t = find(B.dates==varargin{2}); - if isempty(t) - if varargin{2}==(firstdate(B)-1) - B.data = bsxfun(@plus,B.data,varargin{3}.data); - return - else - error(['dseries::cumsum: date ' date2string(varargin{2}) ' is not in the sample!']) - end - end - B.data = bsxfun(@plus,B.data,varargin{3}.data-B.data(t,:)); - otherwise - error('dseries::cumsum: Wrong number of input arguments!') + o.cumsum_(varargin{2:end}); end %@test:1 @@ -135,13 +46,21 @@ end %$ ts1 = dseries(A,[],A_name,[]); %$ %$ % Call the tested method. -%$ ts1 = cumsum(ts1); +%$ try +%$ ts3 = cumsum(ts1); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end %$ %$ % Expected results. %$ ts2 = dseries(transpose(1:10), [], A_name, []); %$ %$ % Check the results. -%$ t(1) = dassert(ts1.data,ts2.data); +%$ if t(1) +%$ t(2) = dassert(ts3.data, ts2.data); +%$ t(3) = dassert(ts1.data, A); +%$ end %$ T = all(t); %@eof:1 @@ -154,97 +73,23 @@ end %$ %$ % Instantiate a time series object. %$ ts1 = dseries(A,[],A_name,[]); -%$ -%$ % Call the tested method. -%$ ts1 = ts1.cumsum(); -%$ -%$ % Expected results. -%$ ts2 = dseries(transpose(1:10), [], A_name, []); -%$ -%$ % Check the results. -%$ t(1) = dassert(ts1.data,ts2.data); -%$ T = all(t); -%@eof:2 - -%@test:3 -%$ % Define a data set. -%$ A = ones(10,1); -%$ -%$ % Define names -%$ A_name = {'A1'}; -%$ -%$ % Instantiate a time series object. -%$ ts1 = dseries(A,[],A_name,[]); -%$ -%$ % Call the tested method. -%$ ts1 = cumsum(ts1,dates('3Y')); -%$ -%$ % Expected results. -%$ ts2 = dseries([-2; -1; 0; 1; 2; 3; 4; 5; 6; 7], [], A_name, []); -%$ -%$ % Check the results. -%$ t(1) = dassert(ts1.data,ts2.data); -%$ T = all(t); -%@eof:3 - -%@test:4 -%$ % Define a data set. -%$ A = ones(10,1); -%$ -%$ % Define names -%$ A_name = {'A1'}; -%$ -%$ % Instantiate a time series object. -%$ ts1 = dseries(A,[],A_name,[]); -%$ ts2 = dseries(pi, [], A_name, []); -%$ -%$ % Call the tested method. -%$ ts3 = cumsum(ts1,dates('3Y'),ts2); -%$ -%$ % Expected results. -%$ ts4 = dseries([-2; -1; 0; 1; 2; 3; 4; 5; 6; 7]+pi, [], A_name, []); -%$ -%$ % Check the results. -%$ t(1) = dassert(ts3.data,ts4.data); -%$ T = all(t); -%@eof:4 - -%@test:5 -%$ % Define a data set. -%$ A = ones(10,1); -%$ -%$ % Define names -%$ A_name = {'A1'}; -%$ -%$ % Instantiate a time series object. -%$ ts1 = dseries(A,[],A_name,[]); %$ ts2 = dseries(pi, [], A_name, []); %$ %$ % Call the tested method. -%$ ts3 = ts1.cumsum(dates('3Y'),ts2); +%$ try +%$ ts3 = ts1.cumsum(dates('3Y'),ts2); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end %$ %$ % Expected results. %$ ts4 = dseries([-2; -1; 0; 1; 2; 3; 4; 5; 6; 7]+pi, [], A_name, []); %$ %$ % Check the results. -%$ t(1) = dassert(ts3.data,ts4.data); -%$ T = all(t); -%@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.cumsum(); -%$ -%$ % Expected results. -%$ A = [NaN NaN; 1 NaN; 2 1; 3 2; 4 NaN]; -%$ -%$ % Check the results. -%$ t(1) = dassert(ts0.data, A); +%$ if t(1) +%$ t(2) = dassert(ts3.data, ts4.data); +%$ t(3) = dassert(ts1.data, A); +%$ end %$ T = all(t); -%@eof:6 \ No newline at end of file +%@eof:2 \ No newline at end of file diff --git a/src/@dseries/cumsum_.m b/src/@dseries/cumsum_.m new file mode 100644 index 0000000000000000000000000000000000000000..a5e371a5682c2f860e44ed9c9380f94fb1caa2ee --- /dev/null +++ b/src/@dseries/cumsum_.m @@ -0,0 +1,292 @@ +function o = cumsum_(varargin) % --*-- Unitary tests --*-- + +% Overloads matlab's cumsum function for dseries objects. +% +% INPUTS +% - o dseries object [mandatory]. +% - d dates object [optional] +% - v dseries object with one observation [optional] +% +% OUTPUTS +% - o dseries object. + +% Copyright (C) 2013-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/>. + +% 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) + idx = 0; +end + +% 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. + o = varargin{1}; + % Perform the cumulated sum + if isequal(idx, 1) + o.data = cumsum(o.data); + else + if common_first_period_witout_nan + o.data(idx:end,:) = cumsum(o.data(idx:end,:)); + else + o.data = cumsumnan(o.data); + end + end + % Change the name of the variables + for i=1:vobs(o) + o.name(i) = {['cumsum(' o.name{i} ')']}; + o.tex(i) = {['\sum_t ' o.tex{i}]}; + end + case 2 + if isdseries(varargin{2}) + if ~isequal(vobs(varargin{1}), vobs(varargin{2})) + error('dseries::cumsum: First and second input arguments must be dseries objects with the same number of variables!') + end + if ~isequal(varargin{1}.name, varargin{2}.name) + warning('dseries::cumsum: First and second input arguments must be dseries objects do not have the same variables!') + end + if ~isequal(nobs(varargin{2}),1) + error('dseries::cumsum: Second input argument must be a dseries object with only one observation!') + end + o = cumsum_(varargin{1}); + o.data = bsxfun(@plus,o.data,varargin{2}.data); + elseif isdates(varargin{2}) + o = cumsum_(varargin{1}); + t = find(o.dates==varargin{2}); + if isempty(t) + if varargin{2}==(firstdate(o)-1) + return + else + error(['dseries::cumsum: date ' date2string(varargin{2}) ' is not in the sample!']) + end + end + o.data = bsxfun(@minus,o.data,o.data(t,:)); + else + error('dseries::cumsum: Second input argument must be a dseries object or a dates object!') + end + case 3 + if ~isdates(varargin{2}) + error('dseries::cumsum: Second input argument must be a dates object!') + end + if ~isdseries(varargin{3}) + error('dseries::cumsum: Third input argument must be a dseries object!') + end + if ~isequal(vobs(varargin{1}), vobs(varargin{3})) + error('dseries::cumsum: First and third input arguments must be dseries objects with the same number of variables!') + end + if ~isequal(varargin{1}.name, varargin{3}.name) + warning('dseries::cumsum: First and third input arguments must be dseries objects do not have the same variables!') + end + if ~isequal(nobs(varargin{3}),1) + error('dseries::cumsum: Third input argument must be a dseries object with only one observation!') + end + o = cumsum_(varargin{1}); + t = find(o.dates==varargin{2}); + if isempty(t) + if varargin{2}==(firstdate(o)-1) + o.data = bsxfun(@plus, o.data, varargin{3}.data); + return + else + error(['dseries::cumsum: date ' date2string(varargin{2}) ' is not in the sample!']) + end + end + o.data = bsxfun(@plus, o.data,varargin{3}.data-o.data(t,:)); + otherwise + error('dseries::cumsum: Wrong number of input arguments!') +end + +%@test:1 +%$ % Define a data set. +%$ A = ones(10,1); +%$ +%$ % Define names +%$ A_name = {'A1'}; +%$ +%$ % Instantiate a time series object. +%$ ts1 = dseries(A,[],A_name,[]); +%$ +%$ % Call the tested method. +%$ try +%$ ts1.cumsum_(); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ % Expected results. +%$ ts2 = dseries(transpose(1:10), [], A_name, []); +%$ +%$ % Check the results. +%$ if t(1) +%$ t(2) = dassert(ts1.data, ts2.data); +%$ end +%$ T = all(t); +%@eof:1 + +%@test:2 +%$ % Define a data set. +%$ A = ones(10,1); +%$ +%$ % Define names +%$ A_name = {'A1'}; +%$ +%$ % Instantiate a time series object. +%$ ts1 = dseries(A,[],A_name,[]); +%$ +%$ % Call the tested method. +%$ try +%$ cumsum_(ts1); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ % Expected results. +%$ ts2 = dseries(transpose(1:10), [], A_name, []); +%$ +%$ % Check the results. +%$ if t(1) +%$ t(2) = dassert(ts1.data, ts2.data); +%$ end +%$ T = all(t); +%@eof:2 + +%@test:3 +%$ % Define a data set. +%$ A = ones(10,1); +%$ +%$ % Define names +%$ A_name = {'A1'}; +%$ +%$ % Instantiate a time series object. +%$ ts1 = dseries(A,[],A_name,[]); +%$ +%$ % Call the tested method. +%$ try +%$ ts1.cumsum_(dates('3Y')); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ % Expected results. +%$ ts2 = dseries([-2; -1; 0; 1; 2; 3; 4; 5; 6; 7], [], A_name, []); +%$ +%$ % Check the results. +%$ if t(1) +%$ t(2) = dassert(ts1.data, ts2.data); +%$ end +%$ T = all(t); +%@eof:3 + +%@test:4 +%$ % Define a data set. +%$ A = ones(10,1); +%$ +%$ % Define names +%$ A_name = {'A1'}; +%$ +%$ % Instantiate a time series object. +%$ ts1 = dseries(A,[],A_name,[]); +%$ ts2 = dseries(pi, [], A_name, []); +%$ +%$ % Call the tested method. +%$ try +%$ ts1.cumsum_(dates('3Y'), ts2); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ % Expected results. +%$ ts3 = dseries([-2; -1; 0; 1; 2; 3; 4; 5; 6; 7]+pi, [], A_name, []); +%$ +%$ % Check the results. +%$ if t(1) +%$ t(2) = dassert(ts1.data, ts3.data); +%$ end +%$ T = all(t); +%@eof:4 + +%@test:5 +%$ % Define a data set. +%$ A = ones(10,1); +%$ +%$ % Define names +%$ A_name = {'A1'}; +%$ +%$ % Instantiate a time series object. +%$ ts1 = dseries(A,[],A_name,[]); +%$ ts2 = dseries(pi, [], A_name, []); +%$ +%$ % Call the tested method. +%$ try +%$ ts1.cumsum_(dates('3Y'), ts2); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ % Expected results. +%$ ts3 = dseries([-2; -1; 0; 1; 2; 3; 4; 5; 6; 7]+pi, [], A_name, []); +%$ +%$ % Check the results. +%$ if t(1) +%$ t(2) = dassert(ts1.data, ts3.data); +%$ end +%$ T = all(t); +%@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. +%$ try +%$ ts0.cumsum_(); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ % Expected results. +%$ A = [NaN NaN; 1 NaN; 2 1; 3 2; 4 NaN]; +%$ +%$ % Check the results. +%$ if t(1) +%$ t(2) = dassert(ts0.data, A); +%$ end +%$ T = all(t); +%@eof:6 \ No newline at end of file diff --git a/src/@dseries/detrend.m b/src/@dseries/detrend.m index 1a0d3ff99515ccf8af2443a8b8fffaef6d713e09..34b04f80e5e79bf766443fac498927a0ab40fb1e 100644 --- a/src/@dseries/detrend.m +++ b/src/@dseries/detrend.m @@ -1,6 +1,15 @@ function o = detrend(o, model) % --*-- Unitary tests --*-- -% Copyright (C) 2014 Dynare Team +% Detrends a dseries object with a polynomial of order model. +% +% INPUTS +% - o [dseries] time series to be detrended. +% - model [integer] scalar, order of the fitted polynomial. +% +% OUTPUTS +% - o [dseries] detrended time series. + +% Copyright (C) 2014-2017 Dynare Team % % This file is part of Dynare. % @@ -22,89 +31,24 @@ if nargin<2 model = 0; end -data = o.data; - -if isnumeric(model) - if isscalar(model) && isint(model) - switch model - case 0 - data = demean(data); - otherwise - x = NaN(nobs(o), model+1); - x(:,1) = ones(nobs(o), 1); - x(:,2) = transpose(1:nobs(o)); - for c=3:model+1 - x(:,c) = x(:,c-1).*x(:,2); - end - data = data - x*(x\data); - end - else - error('dseries::detrend: Second argument must be a positive integer scalar!') - end -else - error('dseries::detrend: Second argument must be a positive integer scalar!') -end - -o = dseries(data, o.dates, o.name, o.tex); +o = copy(o); +o.detrend_(model); %@test:1 %$ % Define a dataset. -%$ a = dseries(randn(1000,3)); +%$ A = ones(3,1); +%$ a = dseries(A); %$ -%$ % detrend (default). %$ try -%$ b1 = a.detrend(); -%$ t(1) = 1; +%$ b = a.detrend(); +%$ t(1) = 1; %$ catch -%$ t = 0; +%$ t(1) = 0; %$ end %$ -%$ % detrend (constant). %$ if t(1) -%$ try -%$ b2 = a.detrend(0); -%$ t(2) = 1; -%$ catch -%$ t(2) = 0; -%$ end +%$ t(2) = max(max(abs(a.data-A)))<1e-12; %$ end %$ -%$ % detrend (linear). -%$ if t(2) -%$ try -%$ b3 = a.detrend(1); -%$ t(3) = 1; -%$ catch -%$ t(3) = 0; -%$ end -%$ end -%$ -%$ % detrend (quadratic). -%$ if t(3) -%$ try -%$ b4 = a.detrend(2); -%$ t(4) = 1; -%$ catch -%$ t(4) = 0; -%$ end -%$ end -%$ -%$ % detrend (cubic). -%$ if t(4) -%$ try -%$ b5 = a.detrend(3); -%$ t(5) = 1; -%$ catch -%$ t(5) = 0; -%$ end -%$ end -%$ -%$ if t(5) -%$ t(6) = dassert(max(mean(b1.data)),0,1e-12); -%$ t(7) = dassert(max(mean(b2.data)),0,1e-12); -%$ t(8) = dassert(max(mean(b3.data)),0,1e-12); -%$ t(9) = dassert(max(mean(b4.data)),0,1e-12); -%$ t(10) = dassert(max(mean(b5.data)),0,1e-9); -%$ end %$ T = all(t); -%@eof:1 +%@eof:1 \ No newline at end of file diff --git a/src/@dseries/detrend_.m b/src/@dseries/detrend_.m new file mode 100644 index 0000000000000000000000000000000000000000..f6bc755eeec6cec946ea2e931400e9296db82fc9 --- /dev/null +++ b/src/@dseries/detrend_.m @@ -0,0 +1,139 @@ +function o = detrend_(o, model) % --*-- Unitary tests --*-- + +% Detrends a dseries object with a polynomial of order model. +% +% INPUTS +% - o [dseries] time series to be detrended. +% - model [integer] scalar, order of the fitted polynomial. +% +% OUTPUTS +% - o [dseries] detrended time series. + +% Copyright (C) 2014-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/>. + +% Set default for the order of the polynomial trend (constant). +if nargin<2 + model = 0; +end + +if isnumeric(model) + if isscalar(model) && isint(model) + switch model + case 0 + o.data = demean(o.data); + otherwise + x = NaN(nobs(o), model+1); + x(:,1) = ones(nobs(o), 1); + x(:,2) = transpose(1:nobs(o)); + for c=3:model+1 + x(:,c) = x(:,c-1).*x(:,2); + end + o.data = o.data - x*(x\o.data); + end + else + error('dseries::detrend: Second argument must be a positive integer scalar!') + end +else + error('dseries::detrend: Second argument must be a positive integer scalar!') +end + +%@test:1 +%$ % Define a dataset. +%$ a = dseries(randn(1000,3)); +%$ +%$ % detrend (default). +%$ try +%$ b = copy(a); +%$ b1 = b.detrend_(); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ % detrend (constant). +%$ if t(1) +%$ try +%$ b = copy(a); +%$ b2 = b.detrend_(0); +%$ t(2) = 1; +%$ catch +%$ t(2) = 0; +%$ end +%$ end +%$ +%$ % detrend (linear). +%$ if t(1) && t(2) +%$ try +%$ b = copy(a); +%$ b3 = b.detrend_(1); +%$ t(3) = 1; +%$ catch +%$ t(3) = 0; +%$ end +%$ end +%$ +%$ % detrend (quadratic). +%$ if t(1) && t(2) && t(3) +%$ try +%$ b = copy(a); +%$ b4 = b.detrend_(2); +%$ t(4) = 1; +%$ catch +%$ t(4) = 0; +%$ end +%$ end +%$ +%$ % detrend (cubic). +%$ if t(1) && t(2) && t(3) && t(4) +%$ try +%$ b = copy(a); +%$ b5 = b.detrend_(3); +%$ t(5) = 1; +%$ catch +%$ t(5) = 0; +%$ end +%$ end +%$ +%$ if t(1) && t(2) && t(3) && t(4) && t(5) +%$ t(6) = dassert(max(mean(b1.data)), 0,1e-12); +%$ t(7) = dassert(max(mean(b2.data)), 0,1e-12); +%$ t(8) = dassert(max(mean(b3.data)), 0,1e-12); +%$ t(9) = dassert(max(mean(b4.data)), 0,1e-12); +%$ t(10) = dassert(max(mean(b5.data)), 0,1e-9); +%$ end +%$ T = all(t); +%@eof:1 + +%@test:2 +%$ % Define a dataset. +%$ A = randn(1000,3); +%$ a = dseries(randn(1000,3)); +%$ +%$ try +%$ b = a.detrend_(); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = max(max(abs(a.data-A)))>1e-8; +%$ end +%$ +%$ T = all(t); +%@eof:2 \ No newline at end of file diff --git a/src/@dseries/disp.m b/src/@dseries/disp.m index 5bd38f464e997b25ec3fdc661bff2a2900096b19..f4141f82308093afdf674612e1bbbd8f41d1eb51 100644 --- a/src/@dseries/disp.m +++ b/src/@dseries/disp.m @@ -1,35 +1,41 @@ -function disp(A) +function disp(o) -%@info: -%! @deftypefn {Function File} disp (@var{A}) -%! @anchor{@dseries/disp} -%! @sp 1 -%! Overloads the disp method for the Dynare time series class (@ref{dseries}). -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @ @var -%! @item A -%! Dynare time series object instantiated by @ref{dseries}. -%! @end table -%! @sp 1 -%! @strong{Outputs} -%! @sp 1 -%! None -%! @end deftypefn -%@eod: +% Overloads disp method. +% +% INPUTS +% - o [dseries] Object to be displayed. +% +% OUTPUTS +% None -separator = repmat(' | ', nobs(A)+1,1); +% Copyright (C) 2011-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/>. + +separator = repmat(' | ', nobs(o)+1,1); vspace = ' '; TABLE = ' '; -for t=1:nobs(A) - TABLE = char(TABLE, date2string(A.dates(t))); +for t=1:nobs(o) + TABLE = char(TABLE, date2string(o.dates(t))); end -for i = 1:vobs(A) +for i = 1:vobs(o) TABLE = horzcat(TABLE,separator); - tmp = A.name{i}; - for t=1:nobs(A) - tmp = char(tmp,num2str(A.data(t,i))); + tmp = o.name{i}; + for t=1:nobs(o) + tmp = char(tmp,num2str(o.data(t,i))); end TABLE = horzcat(TABLE, tmp); end diff --git a/src/@dseries/display.m b/src/@dseries/display.m index d6826a801354b943a64438e15e15982629873f37..bcb4093fdd08d85a3c80f2dfe5486883b8dcf051 100644 --- a/src/@dseries/display.m +++ b/src/@dseries/display.m @@ -1,37 +1,48 @@ -function display(A) -%@info: -%! @deftypefn {Function File} display (@var{A}) -%! @anchor{@dseries/display} -%! @sp 1 -%! Overloads the disp method for the Dynare time series class (@ref{dseries}). -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @ @var -%! @item A -%! Dynare time series object instantiated by @ref{dseries}. -%! @end table -%! @sp 1 -%! @strong{Outputs} -%! @sp 1 -%! None -%! @end deftypefn -%@eod: +function display(o) + +% Overloads display method. +% +% INPUTS +% - o [dseries] Object to be displayed. +% +% OUTPUTS +% None +% +% REMARKS +% Contray to the disp method, the whole dseries object is not displayed if the number of +% observations is greater than 40 and if the number of variables is greater than 10. + +% Copyright (C) 2011-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/>. vspace = ' '; TABLE = ' '; -if vobs(A)<=10 - if nobs(A)<=40 - separator = repmat(' | ', nobs(A)+1,1); - for t=1:nobs(A) - TABLE = char(TABLE, date2string(A.dates(t))); +if vobs(o)<=10 + if nobs(o)<=40 + separator = repmat(' | ', nobs(o)+1,1); + for t=1:nobs(o) + TABLE = char(TABLE, date2string(o.dates(t))); end - for i = 1:vobs(A) + for i = 1:vobs(o) TABLE = horzcat(TABLE,separator); - tmp = A.name{i}; - for t=1:nobs(A) - tmp = char(tmp,num2str(A.data(t,i))); + tmp = o.name{i}; + for t=1:nobs(o) + tmp = char(tmp,num2str(o.data(t,i))); end TABLE = horzcat(TABLE, tmp); end @@ -39,46 +50,46 @@ if vobs(A)<=10 n = 10; separator = repmat(' | ',2*n+3,1); for t=1:n - TABLE = char(TABLE, date2string(A.dates(t))); + TABLE = char(TABLE, date2string(o.dates(t))); end TABLE = char(TABLE,vspace); - for t = nobs(A)-n:nobs(A) - TABLE = char(TABLE, date2string(A.dates(t))); + for t = nobs(o)-n:nobs(o) + TABLE = char(TABLE, date2string(o.dates(t))); end - for i=1:vobs(A) + for i=1:vobs(o) TABLE = horzcat(TABLE,separator); - tmp = A.name{i}; + tmp = o.name{i}; for t=1:10 - tmp = char(tmp,num2str(A.data(t,i))); + tmp = char(tmp,num2str(o.data(t,i))); end tmp = char(tmp,vspace); - for t=nobs(A)-10:nobs(A) - tmp = char(tmp,num2str(A.data(t,i))); + for t=nobs(o)-10:nobs(o) + tmp = char(tmp,num2str(o.data(t,i))); end TABLE = horzcat(TABLE, tmp); end end else m = 4; - if nobs(A)<=40 - separator = repmat(' | ', nobs(A)+1,1); - for t=1:nobs(A) - TABLE = char(TABLE, date2string(A.dates(t))); + if nobs(o)<=40 + separator = repmat(' | ', nobs(o)+1,1); + for t=1:nobs(o) + TABLE = char(TABLE, date2string(o.dates(t))); end for i = 1:m TABLE = horzcat(TABLE,separator); - tmp = A.name{i}; - for t=1:nobs(A) - tmp = char(tmp,num2str(A.data(t,i))); + tmp = o.name{i}; + for t=1:nobs(o) + tmp = char(tmp,num2str(o.data(t,i))); end TABLE = horzcat(TABLE, tmp); end - TABLE = horzcat(TABLE, separator, repmat(' ... ', nobs(A)+1,1)); - for i = vobs(A)-m+1:vobs(A) + TABLE = horzcat(TABLE, separator, repmat(' ... ', nobs(o)+1,1)); + for i = vobs(o)-m+1:vobs(o) TABLE = horzcat(TABLE,separator); - tmp = A.name{i}; - for t=1:nobs(A) - tmp = char(tmp,num2str(A.data(t,i))); + tmp = o.name{i}; + for t=1:nobs(o) + tmp = char(tmp,num2str(o.data(t,i))); end TABLE = horzcat(TABLE, tmp); end @@ -86,34 +97,34 @@ else n = 10; separator = repmat(' | ',2*n+3,1); for t=1:n - TABLE = char(TABLE, date2string(A.dates(t))); + TABLE = char(TABLE, date2string(o.dates(t))); end TABLE = char(TABLE,vspace); - for t = nobs(A)-n:nobs(A) - TABLE = char(TABLE, date2string(A.dates(t))); + for t = nobs(o)-n:nobs(o) + TABLE = char(TABLE, date2string(o.dates(t))); end for i=1:m TABLE = horzcat(TABLE,separator); - tmp = A.name{i}; + tmp = o.name{i}; for t=1:10 - tmp = char(tmp,num2str(A.data(t,i))); + tmp = char(tmp,num2str(o.data(t,i))); end tmp = char(tmp,vspace); - for t=nobs(A)-10:nobs(A) - tmp = char(tmp,num2str(A.data(t,i))); + for t=nobs(o)-10:nobs(o) + tmp = char(tmp,num2str(o.data(t,i))); end TABLE = horzcat(TABLE, tmp); end TABLE = horzcat(TABLE, separator, repmat(' ... ', 2*n+3,1)); - for i=vobs(A)-m+1:vobs(A) + for i=vobs(o)-m+1:vobs(o) TABLE = horzcat(TABLE,separator); - tmp = A.name{i}; + tmp = o.name{i}; for t=1:10 - tmp = char(tmp,num2str(A.data(t,i))); + tmp = char(tmp,num2str(o.data(t,i))); end tmp = char(tmp,vspace); - for t=nobs(A)-10:nobs(A) - tmp = char(tmp,num2str(A.data(t,i))); + for t=nobs(o)-10:nobs(o) + tmp = char(tmp,num2str(o.data(t,i))); end TABLE = horzcat(TABLE, tmp); end diff --git a/src/@dseries/end.m b/src/@dseries/end.m index 8f60971367a15c179134630fbabde65724df11c6..1dc343c61d5818f654b357330f94cae12776fd09 100644 --- a/src/@dseries/end.m +++ b/src/@dseries/end.m @@ -1,6 +1,16 @@ function lastIndex = end(o, k, n) -% Copyright (C) 2014 Dynare Team +% Overloads the end method. +% +% INPUTS +% - o [dseries] +% - k [integer] +% - n [integer] +% +% OUTPUTS +% - lastIndex [integer] + +% Copyright (C) 2014-2017 Dynare Team % % This file is part of Dynare. % diff --git a/src/@dseries/eq.m b/src/@dseries/eq.m index e60d45e642d7e083d59e6599010348e6b981461a..837bfb2ded86c4eff218b85d31800e6572d9050f 100644 --- a/src/@dseries/eq.m +++ b/src/@dseries/eq.m @@ -1,4 +1,4 @@ -function C = eq(A,B) % --*-- Unitary tests --*-- +function b = eq(o, p) % --*-- Unitary tests --*-- % Overloads eq (==) operator. % @@ -12,7 +12,7 @@ function C = eq(A,B) % --*-- Unitary tests --*-- % 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. % @@ -33,35 +33,35 @@ if nargin~=2 error('dseries::eq: I need exactly two input arguments!') end -if ~(isdseries(A) && isdseries(B)) +if ~(isdseries(o) && isdseries(p)) error('dseries::eq: Both input arguments must be dseries objects!') end -if ~isequal(nobs(A), nobs(B)) +if ~isequal(nobs(o), nobs(p)) warning('dseries::eq: Both input arguments should have the same number of observations!') - C = 0; + b = 0; return end -if ~isequal(vobs(A), vobs(B)) +if ~isequal(vobs(o), vobs(p)) warning('dseries::eq: Both input arguments should have the same number of observations!') - C = 0; + b = 0; return end -if ~isequal(frequency(A),frequency(B)) +if ~isequal(frequency(o),frequency(p)) warning('dseries::eq: Both input arguments should have the same frequencies!') - C = 0; + b = 0; return end -if ~isequal(firstdate(A),firstdate(B)) +if ~isequal(firstdate(o),firstdate(p)) warning('dseries::eq: Both input arguments should have the same initial period!') - C = 0; + b = 0; return end -C = eq(A.data, B.data); +b = eq(o.data, p.data); %@test:1 %$ % Define a datasets. diff --git a/src/@dseries/extract.m b/src/@dseries/extract.m index f50899a1ff3f7a701c30e26f67b8710552192722..640eeee29672022bccaae7b11eb2be4467480536 100644 --- a/src/@dseries/extract.m +++ b/src/@dseries/extract.m @@ -1,8 +1,8 @@ -function A = extract(B,varargin) % --*-- Unitary tests --*-- +function p = extract(o, varargin) % --*-- Unitary tests --*-- % Extract some variables from a database. -% Copyright (C) 2012-2016 Dynare Team +% Copyright (C) 2012-2017 Dynare Team % % This file is part of Dynare. % @@ -19,7 +19,7 @@ function A = extract(B,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/>. -A = dseries(); +p = dseries(); % Get the names of the variables to be extracted from dseries object B. VariableName_ = {}; @@ -42,9 +42,9 @@ for i=1:nargin-1 end % Update the list of variables. if length(idArobase) - VariableName_ = build_list_of_variables_with_loops(B.name, idArobase, VariableName, VariableName_); + VariableName_ = build_list_of_variables_with_loops(o.name, idArobase, VariableName, VariableName_); elseif length(idBracket.open) - VariableName_ = build_list_of_variables_with_regexp(B.name, idBracket, VariableName, VariableName_); + VariableName_ = build_list_of_variables_with_regexp(o.name, idBracket, VariableName, VariableName_); else VariableName_ = varargin(:); end @@ -56,17 +56,17 @@ VariableName_ = strtrim(VariableName_); % Get indices of the selected variables idVariableName = NaN(length(VariableName_),1); for i = 1:length(idVariableName) - idx = find(strcmp(VariableName_{i},B.name)); + idx = find(strcmp(VariableName_{i},o.name)); if isempty(idx) error(['dseries::extract: Variable ' VariableName_{i} ' is not a member of ' inputname(1) '!']) end idVariableName(i) = idx; end -A.data = B.data(:,idVariableName); -A.dates = B.dates; -A.name = B.name(idVariableName); -A.tex = B.tex(idVariableName); +p.data = o.data(:,idVariableName); +p.dates = o.dates; +p.name = o.name(idVariableName); +p.tex = o.tex(idVariableName); %@test:1 diff --git a/src/@dseries/firstobservedperiod.m b/src/@dseries/firstobservedperiod.m index 1f5b287af51a6b60dda6b2670772b2d1d562b1da..fb980f6bfa47b2583a5ac4832276702337668541 100644 --- a/src/@dseries/firstobservedperiod.m +++ b/src/@dseries/firstobservedperiod.m @@ -6,9 +6,9 @@ function d = firstobservedperiod(o) % --*-- Unitary tests --*-- % - o [dseries] with N variables and T periods. % % OUTPUTS -% - b [dates] First period where the N variables are observed (without NaNs). +% - d [dates] First period where the N variables are observed (without NaNs). -% Copyright (C) 2016 Dynare Team +% Copyright (C) 2016-2017 Dynare Team % % This file is part of Dynare. % diff --git a/src/@dseries/frequency.m b/src/@dseries/frequency.m index c2befb5477059cc992cc9f2fb4d0efd95f9fb892..ba4950f21905da5b4f35e482fdb699f2945b4b24 100644 --- a/src/@dseries/frequency.m +++ b/src/@dseries/frequency.m @@ -1,6 +1,14 @@ function f = frequency(o) -% Copyright (C) 2014 Dynare Team +% Returns the frequency of a dseries object. +% +% INPUTS +% - o [dseries] +% +% OUPUTS +% - f [integer] 1 (annual), 4 (quarterly), 12 (monthly) + +% Copyright (C) 2014-2017 Dynare Team % % This file is part of Dynare. % diff --git a/src/@dseries/horzcat.m b/src/@dseries/horzcat.m index 6191e16bd46251d495d8acf4888eec947b32d475..5722922b32879feac758b930b8b336ccb72f266f 100644 --- a/src/@dseries/horzcat.m +++ b/src/@dseries/horzcat.m @@ -1,26 +1,26 @@ -function B = horzcat(varargin) % --*-- Unitary tests --*-- +function o = horzcat(varargin) % --*-- Unitary tests --*-- % Overloads horzcat method for dseries objects. % % INPUTS -% o A1 dseries object. -% o A2 dseries object. +% o o1 dseries object. +% o o2 dseries object. % o ... % % OUTPUTS -% o B dseries object. +% o o dseries object. % % EXAMPLE 1 -% If A, B and C are dseries objects the following syntax: +% If o1, o2 and o3 are dseries objects the following syntax: % -% D = [A, B, C] ; +% o = [o1, o2, o3] ; % -% Defines a dseries object D containing the variables appearing in A, B and C. +% defines a dseries object o containing the variables appearing in o1, o2 and o3. % % REMARKS -% o A1, A2, ... must not have common variables. +% o o1, o2, ... must not have common variables. -% Copyright (C) 2011-2013 Dynare Team +% Copyright (C) 2011-2017 Dynare Team % % This file is part of Dynare. % @@ -39,18 +39,18 @@ function B = horzcat(varargin) % --*-- Unitary tests --*-- switch nargin case 0 - B = dseries(); + o = dseries(); case 1 - B = varargin{1}; + o = varargin{1}; otherwise - B = concatenate(varargin{1}, varargin{2}); + o = concatenate(varargin{1}, varargin{2}); if nargin>2 - B = horzcat(B, varargin{3:end}); + o = horzcat(o, varargin{3:end}); end end function a = concatenate(b,c) - [n,message] = common_strings_in_cell_arrays(b.name,c.name); + [n,message] = common_strings_in_cell_arrays(b.name, c.name); if isempty(b) a = c; return diff --git a/src/@dseries/hpcycle.m b/src/@dseries/hpcycle.m index 90dd65303f5bee08361c36fecea3c43d3f614d75..5461b0b1912d3329e8816cc00d781cb413bcc8b4 100644 --- a/src/@dseries/hpcycle.m +++ b/src/@dseries/hpcycle.m @@ -1,17 +1,15 @@ -function ts = hpcycle(ts, lambda) % --*-- Unitary tests --*-- +function o = hpcycle(o, lambda) % --*-- Unitary tests --*-- -% ts = hpcycle(ts, lambda) -% % Extracts the cycle component from a dseries object using Hodrick Prescott filter. % % INPUTS -% o ts dseries object. -% o lambda positive scalar, trend smoothness parameter. +% - o [dseries] Original time series. +% - lambda [double] scalar, trend smoothness parameter. % % OUTPUTS -% o ts dseries object, with time series replaced by the cyclical component of the original time series. +% - o [dseries] Cyclical component of the original time series. -% Copyright (C) 2013 Dynare Team +% Copyright (C) 2013-2017 Dynare Team % % This file is part of Dynare. % @@ -34,62 +32,24 @@ if nargin>1 end else lambda = []; -end - -for i=1:vobs(ts) - ts.name(i) = {['hpcycle(' ts.name{i} ')']}; - ts.tex(i) = {['\text{hpcycle}(' ts.tex{i} ')']}; end -[junk, data] = sample_hp_filter(ts.data,lambda); -ts.data = data; +o = copy(o); +o.hpcycle_(lambda); %@test:1 -%$ plot_flag = 0; -%$ -%$ % Create a dataset. -%$ e = .2*randn(200,1); -%$ u = randn(200,1); -%$ stochastic_trend = cumsum(e); -%$ deterministic_trend = .1*transpose(1:200); -%$ x = zeros(200,1); -%$ for i=2:200 -%$ x(i) = .9*x(i-1) + e(i); -%$ end -%$ y = x + stochastic_trend + deterministic_trend; +%$ d = randn(100,1); +%$ o = dseries(d); %$ -%$ % Test the routine. %$ try -%$ ts0 = dseries(y,'1950Q1'); -%$ ts1 = dseries(x,'1950Q1'); -%$ ts2 = ts0.hpcycle(); +%$ p = o.hpcycle(); %$ t(1) = 1; %$ catch %$ t(1) = 0; %$ end %$ %$ if t(1) -%$ t(2) = dassert(ts2.freq,4); -%$ t(3) = dassert(ts2.init.freq,4); -%$ t(4) = dassert(ts2.init.time,[1950, 1]); -%$ t(5) = dassert(ts2.vobs,1); -%$ t(6) = dassert(ts2.nobs,200); -%$ end -%$ -%$ % Show results -%$ if plot_flag -%$ plot(ts1.data,'-k'); % Plot of the stationary component. -%$ hold on -%$ plot(ts2.data,'--r'); % Plot of the filtered y. -%$ hold off -%$ axis tight -%$ id = get(gca,'XTick'); -%$ set(gca,'XTickLabel',strings(ts1.dates(id))); -%$ legend({'Stationary component of y', 'Filtered y'}) -%$ print('-depsc2','../doc/dynare.plots/HPCycle.eps') -%$ system('convert -density 300 ../doc/dynare.plots/HPCycle.eps ../doc/dynare.plots/HPCycle.png'); -%$ system('convert -density 300 ../doc/dynare.plots/HPCycle.eps ../doc/dynare.plots/HPCycle.pdf'); -%$ system('convert -density 300 ../doc/dynare.plots/HPCycle.eps ../doc/dynare.plots/HPCycle.jpg'); +%$ t(2) = dassert(o.data,d); %$ end %$ %$ T = all(t); diff --git a/src/@dseries/hpcycle_.m b/src/@dseries/hpcycle_.m new file mode 100644 index 0000000000000000000000000000000000000000..5437b0aea3ba150e906a18b2b1d80df89b62df35 --- /dev/null +++ b/src/@dseries/hpcycle_.m @@ -0,0 +1,94 @@ +function o = hpcycle_(o, lambda) % --*-- Unitary tests --*-- + +% Extracts the cycle component from a dseries object using Hodrick Prescott filter. +% +% INPUTS +% - o [dseries] Original time series. +% - lambda [double] scalar, trend smoothness parameter. +% +% OUTPUTS +% - o [dseries] Cyclical component of the original time series. + +% Copyright (C) 2013-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>1 + if lambda<=0 + error(['dseries::hpcycle: Lambda must be a positive integer!']) + end +else + lambda = []; +end + +for i=1:vobs(o) + o.name(i) = {['hpcycle(' o.name{i} ')']}; + o.tex(i) = {['\text{hpcycle}(' o.tex{i} ')']}; +end + +[junk, data] = sample_hp_filter(o.data,lambda); +o.data = data; + +%@test:1 +%$ plot_flag = false; +%$ +%$ % Create a dataset. +%$ e = .2*randn(200,1); +%$ u = randn(200,1); +%$ stochastic_trend = cumsum(e); +%$ deterministic_trend = .1*transpose(1:200); +%$ x = zeros(200,1); +%$ for i=2:200 +%$ x(i) = .9*x(i-1) + e(i); +%$ end +%$ y = x + stochastic_trend + deterministic_trend; +%$ +%$ % Test the routine. +%$ try +%$ ts0 = dseries(y,'1950Q1'); +%$ ts1 = dseries(x,'1950Q1'); +%$ ts0.hpcycle_(); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(ts0.freq,4); +%$ t(3) = dassert(ts0.init.freq,4); +%$ t(4) = dassert(ts0.init.time,[1950, 1]); +%$ t(5) = dassert(ts0.vobs,1); +%$ t(6) = dassert(ts0.nobs,200); +%$ end +%$ +%$ % Show results +%$ if plot_flag +%$ plot(ts1.data,'-k'); % Plot of the stationary component. +%$ hold on +%$ plot(ts0.data,'--r'); % Plot of the filtered y. +%$ hold off +%$ axis tight +%$ id = get(gca,'XTick'); +%$ set(gca,'XTickLabel',strings(ts1.dates(id))); +%$ legend({'Stationary component of y', 'Filtered y'}) +%$ print('-depsc2','../doc/dynare.plots/HPCycle.eps') +%$ system('convert -density 300 ../doc/dynare.plots/HPCycle.eps ../doc/dynare.plots/HPCycle.png'); +%$ system('convert -density 300 ../doc/dynare.plots/HPCycle.eps ../doc/dynare.plots/HPCycle.pdf'); +%$ system('convert -density 300 ../doc/dynare.plots/HPCycle.eps ../doc/dynare.plots/HPCycle.jpg'); +%$ end +%$ +%$ T = all(t); +%@eof:1 \ No newline at end of file diff --git a/src/@dseries/hptrend.m b/src/@dseries/hptrend.m index 6c699f9222672c286c379e977648781f3935f181..bb07d5a33c8791b72b60e9871aacc7d987eeb5b8 100644 --- a/src/@dseries/hptrend.m +++ b/src/@dseries/hptrend.m @@ -1,17 +1,15 @@ -function ts = hptrend(ts, lambda) % --*-- Unitary tests --*-- +function o = hptrend_(o, lambda) % --*-- Unitary tests --*-- -% ts = hptrend(ts, lambda) -% % Extracts the trend component from a dseries object using Hodrick Prescott filter. % % INPUTS -% o ts dseries object. -% o lambda positive scalar, trend smoothness parameter. +% - o [dseries] Original time series. +% - lambda [double] scalar, trend smoothness parameter. % % OUTPUTS -% o ts dseries object, with time series replaced by the trend component of the original time series. +% - o [dseries] Trend component of the original time series. -% Copyright (C) 2013 Dynare Team +% Copyright (C) 2013-2017 Dynare Team % % This file is part of Dynare. % @@ -36,59 +34,23 @@ else lambda = []; end -for i=1:vobs(ts) - ts.name(i) = {['hptrend(' ts.name{i} ')']}; - ts.tex(i) = {['\text{hptrend}(' ts.tex{i} ')']}; -end - -ts.data = sample_hp_filter(ts.data,lambda); +o = copy(o); +o.hptrend_(lambda); %@test:1 -%$ plot_flag = 0; +%$ d = randn(100,1); +%$ o = dseries(d); %$ -%$ % Create a dataset. -%$ e = .2*randn(200,1); -%$ u = randn(200,1); -%$ stochastic_trend = cumsum(e); -%$ deterministic_trend = .1*transpose(1:200); -%$ x = zeros(200,1); -%$ for i=2:200 -%$ x(i) = .75*x(i-1) + e(i); -%$ end -%$ y = x + stochastic_trend + deterministic_trend; -%$ -%$ % Test the routine. %$ try -%$ ts0 = dseries(y,'1950Q1'); -%$ ts1 = dseries(stochastic_trend+deterministic_trend,'1950Q1'); -%$ ts2 = ts0.hptrend(1600); +%$ p = o.hptrend(); %$ t(1) = 1; %$ catch %$ t(1) = 0; %$ end %$ %$ if t(1) -%$ t(2) = dassert(ts2.freq,4); -%$ t(3) = dassert(ts2.init.freq,4); -%$ t(4) = dassert(ts2.init.time,[1950, 1]); -%$ t(5) = dassert(ts2.vobs,1); -%$ t(6) = dassert(ts2.nobs,200); +%$ t(2) = dassert(o.data,d); %$ end %$ -%$ % Show results -%$ if plot_flag -%$ plot(ts1.data,'-k'); % Plot of the stationary component. -%$ hold on -%$ plot(ts2.data,'--r'); % Plot of the filtered y. -%$ hold off -%$ axis tight -%$ id = get(gca,'XTick'); -%$ set(gca,'XTickLabel',strings(ts1.dates(id))); -%$ legend({'Nonstationary component of y', 'Estimated trend of y'}) -%$ print('-depsc2','../doc/dynare.plots/HPTrend.eps') -%$ system('convert -density 300 ../doc/dynare.plots/HPTrend.eps ../doc/dynare.plots/HPTrend.png'); -%$ system('convert -density 300 ../doc/dynare.plots/HPTrend.eps ../doc/dynare.plots/HPTrend.pdf'); -%$ system('convert -density 300 ../doc/dynare.plots/HPTrend.eps ../doc/dynare.plots/HPTrend.jpg'); -%$ end %$ T = all(t); %@eof:1 \ No newline at end of file diff --git a/src/@dseries/hptrend_.m b/src/@dseries/hptrend_.m new file mode 100644 index 0000000000000000000000000000000000000000..80742f18de3cd64517f8e76772adb9ab1231a17a --- /dev/null +++ b/src/@dseries/hptrend_.m @@ -0,0 +1,92 @@ +function o = hptrend_(o, lambda) % --*-- Unitary tests --*-- + +% Extracts the trend component from a dseries object using Hodrick Prescott filter. +% +% INPUTS +% - o [dseries] Original time series. +% - lambda [double] scalar, trend smoothness parameter. +% +% OUTPUTS +% - o [dseries] Trend component of the original time series. + +% Copyright (C) 2013-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>1 + if lambda<=0 + error(['dseries::hptrend: Lambda must be a positive integer!']) + end +else + lambda = []; +end + +for i=1:vobs(o) + o.name(i) = {['hptrend(' o.name{i} ')']}; + o.tex(i) = {['\text{hptrend}(' o.tex{i} ')']}; +end + +o.data = sample_hp_filter(o.data, lambda); + +%@test:1 +%$ plot_flag = 0; +%$ +%$ % Create a dataset. +%$ e = .2*randn(200,1); +%$ u = randn(200,1); +%$ stochastic_trend = cumsum(e); +%$ deterministic_trend = .1*transpose(1:200); +%$ x = zeros(200,1); +%$ for i=2:200 +%$ x(i) = .75*x(i-1) + e(i); +%$ end +%$ y = x + stochastic_trend + deterministic_trend; +%$ +%$ % Test the routine. +%$ try +%$ ts0 = dseries(y,'1950Q1'); +%$ ts1 = dseries(stochastic_trend+deterministic_trend,'1950Q1'); +%$ ts0.hptrend(1600); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(ts0.freq,4); +%$ t(3) = dassert(ts0.init.freq,4); +%$ t(4) = dassert(ts0.init.time,[1950, 1]); +%$ t(5) = dassert(ts0.vobs,1); +%$ t(6) = dassert(ts0.nobs,200); +%$ end +%$ +%$ % Show results +%$ if plot_flag +%$ plot(ts1.data,'-k'); % Plot of the stationary component. +%$ hold on +%$ plot(ts2.data,'--r'); % Plot of the filtered y. +%$ hold off +%$ axis tight +%$ id = get(gca,'XTick'); +%$ set(gca,'XTickLabel',strings(ts1.dates(id))); +%$ legend({'Nonstationary component of y', 'Estimated trend of y'}) +%$ print('-depsc2','../doc/dynare.plots/HPTrend.eps') +%$ system('convert -density 300 ../doc/dynare.plots/HPTrend.eps ../doc/dynare.plots/HPTrend.png'); +%$ system('convert -density 300 ../doc/dynare.plots/HPTrend.eps ../doc/dynare.plots/HPTrend.pdf'); +%$ system('convert -density 300 ../doc/dynare.plots/HPTrend.eps ../doc/dynare.plots/HPTrend.jpg'); +%$ end +%$ T = all(t); +%@eof:1 \ No newline at end of file diff --git a/src/@dseries/insert.m b/src/@dseries/insert.m index a6f17eafdcbafab4fcdb18691f731f5dd27c602d..b83b4a6ef1ef53c7aa2c4fd90eecd00452ee22fd 100644 --- a/src/@dseries/insert.m +++ b/src/@dseries/insert.m @@ -1,35 +1,16 @@ -function ts = insert(ts,us,id) % --*-- Unitary tests --*-- +function o = insert(o, p, id) % --*-- Unitary tests --*-- -% Add a variable in a dseries object. - -%@info: -%! @deftypefn {Function File} {@var{ts} =} insert (@var{ts}, @var{us}, @var{id}) -%! @anchor{dseries/insert} -%! @sp 1 -%! Insert method for the dseries class. Insert new variables (@var{us}) in a dseries object (@var{ts}) at -%! positions @var{id}. -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @ @var -%! @item ts -%! Object instantiated by @ref{dseries}. -%! @item us -%! Object instantiated by @ref{dseries}. -%! @item id -%! vector of integers. -%! @end table -%! @sp 2 -%! @strong{Outputs} -%! @sp 1 -%! @table @ @var -%! @item ts -%! Object instantiated by @ref{dseries}, without variable (@var{a}). -%! @end table -%! @end deftypefn -%@eod: +% Adds a variable in a dseries object. +% +% INPUTS +% - o [dseries] +% - p [dseries] +% - id [integer] vector of indices. +% +% OUTPUTS +% - o [dseries] -% Copyright (C) 2013 Dynare Team +% Copyright (C) 2013-2017 Dynare Team % % This file is part of Dynare. % @@ -46,31 +27,34 @@ function ts = insert(ts,us,id) % --*-- 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/>. -[n,message] = common_strings_in_cell_arrays(ts.name,us.name); +[n, message] = common_strings_in_cell_arrays(o.name, p.name); if n error(['dseries::insert: Variable(s) ' message ' already exist in ''' inputname(1) '''!']) end -if ~isequal(frequency(ts),frequency(us)) +if ~isequal(frequency(o),frequency(p)) error(['dseries::insert: ''' inputname(1) ''' and ''' inputname(2) ''' dseries objects must have common frequencies!']) end -[ts,us] = align(ts, us); +% Keep the second input argument constant. +p = copy(p); + +[o, p] = align(o, p); 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); + p.data = p.data(:,jd); + p.name = p.name(jd); + p.tex = p.tex(jd); end for i=1:n - ts.data = insert_column_vector_in_a_matrix(ts.data,us.data(:,i),id(i)); - ts.name = insert_object_in_a_one_dimensional_cell_array(ts.name,us.name{i},id(i)); - ts.tex = insert_object_in_a_one_dimensional_cell_array(ts.tex,us.tex{i},id(i)); + o.data = insert_column_vector_in_a_matrix(o.data, p.data(:,i),id(i)); + o.name = insert_object_in_a_one_dimensional_cell_array(o.name, p.name{i}, id(i)); + o.tex = insert_object_in_a_one_dimensional_cell_array(o.tex, p.tex{i}, id(i)); id = id+1; end @@ -104,4 +88,37 @@ end %$ t(4) = dassert(ts1.data,[eB(:,1), A(:,1), eB(:,2), A(:,2:3)], 1e-15); %$ end %$ T = all(t); -%@eof:1 \ No newline at end of file +%@eof:1 + +%@test:2 +%$ % Define a datasets. +%$ A = rand(10,3); B = rand(5,2); +%$ +%$ % Define names. +%$ A_name = {'A1'; 'A2';'A3'}; +%$ B_name = {'B1'; 'B2'}; +%$ +%$ % Define initial dates. +%$ A_init = '1950Q1'; +%$ B_init = '1950Q3'; +%$ +%$ % Instantiate two dseries objects. +%$ ts1 = dseries(A, A_init, A_name,[]); +%$ ts2 = dseries(B, B_init, B_name,[]); +%$ +%$ try +%$ ts1.insert(ts2,[1,2]); +%$ t(1) = 1; +%$ catch +%$ t = 0; +%$ end +%$ +%$ if length(t)>1 +%$ t(2) = dassert(ts1.vobs,{'B1';'A1';'B2';'A3'}); +%$ t(3) = dassert(ts1.nobs,10); +%$ eB = [NaN(2,2); B; NaN(3,2)]; +%$ t(4) = dassert(ts1.data,[eB(:,1), A(:,1), eB(:,2), A(:,2:3)], 1e-15); +%$ t(5) = dassert(ts2.data, B); +%$ end +%$ T = all(t); +%@eof:2 \ No newline at end of file diff --git a/src/@dseries/isempty.m b/src/@dseries/isempty.m index 5df634e46ee8ab8a70499adf1b3d79950c281f98..f54f2c436d510e6a43d9b01c2c7660f431caab5d 100644 --- a/src/@dseries/isempty.m +++ b/src/@dseries/isempty.m @@ -1,26 +1,14 @@ -function b = isempty(A) -%@info: -%! @deftypefn {Function File} {@var{b} =} isempty (@var{A}) -%! @anchor{@dseries/isempty} -%! @sp 1 -%! Overloads the isempty function for the Dynare time series class (@ref{dseries}). -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @ @var -%! @item 1 -%! Dynare time series object instantiated by @ref{dseries}. -%! @end table -%! @sp 1 -%! @strong{Outputs} -%! @sp 1 -%! @table @ @var -%! @item b -%! Scalar integer (0 or 1). -%! @end deftypefn -%@eod: +function b = isempty(o) -% Copyright (C) 2011-2012 Dynare Team +% Overloads isempty method. +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - b [logical] + +% Copyright (C) 2011-2017 Dynare Team % % This file is part of Dynare. % @@ -37,4 +25,4 @@ function b = isempty(A) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -b = isempty(A.data); \ No newline at end of file +b = isempty(o.data); \ No newline at end of file diff --git a/src/@dseries/isequal.m b/src/@dseries/isequal.m index 8a3c99a37afdae16e20d3287ab2e784f8f2d41f7..e6bfc61b3a37fec32d51f1196034db8085b5df3f 100644 --- a/src/@dseries/isequal.m +++ b/src/@dseries/isequal.m @@ -1,16 +1,16 @@ -function C = isequal(A, B, tol) +function b = isequal(o, p, tol) % Overloads the isequal Matlab/Octave's function. % % INPUTS -% o A dseries object (T periods, N variables). -% o B dseries object (T periods, N variables). -% o tol tolerance parameter. +% - o [dseries] T periods, N variables. +% - p [dseries] T periods, N variables. +% - tol [double] tolerance parameter. % % OUTPUTS -% o C Integer scalar equal to zero or one. +% o b [logical] -% Copyright (C) 2013 Dynare Team +% Copyright (C) 2013-2017 Dynare Team % % This file is part of Dynare. % @@ -31,40 +31,40 @@ if nargin~=2 error('dseries::isequal: I need exactly two input arguments!') end -if ~isdseries(B) +if ~isdseries(p) error('dseries::isequal: Both input arguments must be dseries objects!') end -if ~isequal(nobs(A), nobs(B)) - C = 0; +if ~isequal(nobs(o), nobs(p)) + b = 0; return end -if ~isequal(vobs(A), vobs(B)) - C = 0; +if ~isequal(vobs(o), vobs(p)) + b = 0; return end -if ~isequal(frequency(A),frequency(B)) - C = 0; +if ~isequal(frequency(o), frequency(p)) + b = 0; return end -if ~isequal(A.dates,B.dates) - C = 0; +if ~isequal(o.dates, p.dates) + b = 0; return end -if ~isequal(A.name,B.name) +if ~isequal(o.name, p.name) warning('dseries::isequal: Both input arguments do not have the same variables!') end -if ~isequal(A.tex,B.tex) +if ~isequal(o.tex, p.tex) warning('dseries::isequal: Both input arguments do not have the same tex names!') end if nargin<3 - C = isequal(A.data, B.data); + b = isequal(o.data, p.data); else - C = ~(max(abs(A.data(:)-B.data(:)))>tol); + b = ~(max(abs(o.data(:)-p.data(:)))>tol); end \ No newline at end of file diff --git a/src/@dseries/lag.m b/src/@dseries/lag.m index 97489c7c8724a66e77cebddf62841d9a1def48a3..8a02c9da532b5734b060d2e02de16d081972bea6 100644 --- a/src/@dseries/lag.m +++ b/src/@dseries/lag.m @@ -1,4 +1,4 @@ -function q = lag(o, p) % --*-- Unitary tests --*-- +function o = lag(o, p) % --*-- Unitary tests --*-- % Returns a lagged time series % @@ -45,29 +45,10 @@ if nargin<2 p = 1; end -% Check second input argument -if p<=0 - error('dseries:WrongInputArguments','Second input argument must be strictly positive! Use lead method instead.') -end - -if ~isint(p) - error('dseries:WrongInputArguments','Second input argument must be an integer!') -end - -% Copy of ts dseries object -q = copy(o); - -% Update data member -q.data = [NaN(p, vobs(o)); q.data(1:end-p,:)]; - -for i=1:vobs(o) - q.name(i) = {[ 'lag(' o.name{i} ',' int2str(p) ')']}; - q.tex(i) = {[ o.tex{i} '_{-' int2str(p) '}']}; -end +o = copy(o); +o.lag_(p); %@test:1 -%$ t = zeros(4,1); -%$ %$ try %$ data = transpose(0:1:50); %$ ts = dseries(data,'1950Q1'); diff --git a/src/@dseries/lag_.m b/src/@dseries/lag_.m new file mode 100644 index 0000000000000000000000000000000000000000..d8925d865f9b5c1527969b725372e2c0b7697663 --- /dev/null +++ b/src/@dseries/lag_.m @@ -0,0 +1,81 @@ +function o = lag(o, p) % --*-- Unitary tests --*-- + +% Returns a lagged time series +% +% INPUTS +% - o [dseries] +% - p [integer] Number of lags +% +% OUTPUTS +% - o [dseries] +% +% EXAMPLE +% Define a dseries object as follows: +% +% >> o = dseries(transpose(1:5)) +% +% then o.lag(1) returns +% +% | lag(Variable_1,1) +% 1Y | NaN +% 2Y | 1 +% 3Y | 2 +% 4Y | 3 +% 5Y | 4 + +% Copyright (C) 2013-2016 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/>. + +% Set default number of lags +if nargin<2 + p = 1; +end + +% Check second input argument +if p<=0 + error('dseries:WrongInputArguments','Second input argument must be strictly positive! Use lead method instead.') +end + +if ~isint(p) + error('dseries:WrongInputArguments','Second input argument must be an integer!') +end + +% Update data member +o.data = [NaN(p, vobs(o)); o.data(1:end-p,:)]; + +for i=1:vobs(o) + o.name(i) = {[ 'lag(' o.name{i} ',' int2str(p) ')']}; + o.tex(i) = {[ o.tex{i} '_{-' int2str(p) '}']}; +end + +%@test:1 +%$ try +%$ data = transpose(0:1:50); +%$ ts = dseries(data,'1950Q1'); +%$ ts.lag_(); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ DATA = [NaN(1,ts.vobs); transpose(0:1:49)]; +%$ t(2) = dassert(ts.data,DATA,1e-15); +%$ end +%$ +%$ T = all(t); +%@eof:1 \ No newline at end of file diff --git a/src/@dseries/lastdate.m b/src/@dseries/lastdate.m index 972a70e1899f3ea255d10db1522466c0da7e21d3..30f9bedc8ea12ae92697a0e112a1593aa49a9bc9 100644 --- a/src/@dseries/lastdate.m +++ b/src/@dseries/lastdate.m @@ -1,6 +1,14 @@ function l = lastdate(o) -% Copyright (C) 2014 Dynare Team +% Returns last period of teh sample. +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - l [dates] + +% Copyright (C) 2014-2017 Dynare Team % % This file is part of Dynare. % diff --git a/src/@dseries/lead.m b/src/@dseries/lead.m index baa7f2bf285c252874ed4c1cd59ca07e3e78088c..3ac0b0fe508fdb8da4d82a23b93aa7329cc34530 100644 --- a/src/@dseries/lead.m +++ b/src/@dseries/lead.m @@ -1,4 +1,4 @@ -function q = lead(o, p) % --*-- Unitary tests --*-- +function o = lead(o, p) % --*-- Unitary tests --*-- % Returns a lagged time series % @@ -23,7 +23,7 @@ function q = lead(o, p) % --*-- Unitary tests --*-- % 4Y | 5 % 5Y | NaN -% Copyright (C) 2013-2016 Dynare Team +% Copyright (C) 2013-2017 Dynare Team % % This file is part of Dynare. % @@ -45,25 +45,8 @@ if nargin<2 p = 1; end -% Check second input argument -if p<=0 - error('dseries:WrongInputArguments','Second input argument must be strictly positive! Use lag method instead.') -end - -if ~isint(p) - error('dseries:WrongInputArguments','Second input argument must be an integer!') -end - -% Copy of ts dseries object -q = copy(o); - -% Update data member -q.data = [ o.data(p+1:end,:); NaN(p, vobs(o));]; - -for i=1:vobs(o) - q.name(i) = {[ 'lead(' q.name{i} ',' int2str(p) ')']}; - q.tex(i) = {[ q.tex{i} '_{+' int2str(p) '}']}; -end +o = copy(o); +o.lead_(p); %@test:1 %$ try diff --git a/src/@dseries/lead_.m b/src/@dseries/lead_.m new file mode 100644 index 0000000000000000000000000000000000000000..b9192e43371729d3dcb181c3cf0fc5c66d7f062f --- /dev/null +++ b/src/@dseries/lead_.m @@ -0,0 +1,98 @@ +function o = lead_(o, p) % --*-- Unitary tests --*-- + +% Returns a leaded time series +% +% INPUTS +% - o [dseries] +% - p [integer] Number of leads +% +% OUTPUTS +% - o [dseries] +% +% EXAMPLE +% Define a dseries object as follows: +% +% >> o = dseries(transpose(1:5)) +% +% then o.lag(1) returns +% +% | lead(Variable_1,1) +% 1Y | 2 +% 2Y | 3 +% 3Y | 4 +% 4Y | 5 +% 5Y | NaN + +% Copyright (C) 2013-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/>. + +% Set default number of leads +if nargin<2 + p = 1; +end + +% Check second input argument +if p<=0 + error('dseries:WrongInputArguments','Second input argument must be strictly positive! Use lag method instead.') +end + +if ~isint(p) + error('dseries:WrongInputArguments','Second input argument must be an integer!') +end + +% Update data member +o.data = [ o.data(p+1:end,:); NaN(p, vobs(o));]; + +for i=1:vobs(o) + o.name(i) = {[ 'lead(' o.name{i} ',' int2str(p) ')']}; + o.tex(i) = {[ o.tex{i} '_{+' int2str(p) '}']}; +end + +%@test:1 +%$ try +%$ data = transpose(1:50); +%$ ts = dseries(data,'1950Q1'); +%$ ts.lead_; +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ if t(1) +%$ DATA = [data(2:end); NaN(1)]; +%$ t(2) = dassert(ts.data, DATA, 1e-15); +%$ end +%$ +%$ T = all(t); +%@eof:1 + +%@test:2 +%$ try +%$ data = transpose(1:50); +%$ ts = dseries(data,'1950Q1'); +%$ ts.lead_.lead_; +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ if t(1) +%$ t(2) = all(isnan(ts.data(end-1:end))) && isequal(ts.data(1:end-2), data(3:end)); +%$ end +%$ +%$ T = all(t); +%@eof:2 \ No newline at end of file diff --git a/src/@dseries/merge.m b/src/@dseries/merge.m index 9b3d2caba9f717948294b24b50fb40b1b5498a41..1e82168a4a857932871cb8d011a7b41bf8c9d4c6 100644 --- a/src/@dseries/merge.m +++ b/src/@dseries/merge.m @@ -1,32 +1,18 @@ -function A = merge(B,C) % --*-- Unitary tests --*-- +function q = merge(o, p) % --*-- Unitary tests --*-- -%@info: -%! @deftypefn {Function File} {@var{A} =} merge (@var{B},@var{C}) -%! @anchor{@dseries/plus} -%! @sp 1 -%! Overloads the merge method for the Dynare time series class (@ref{dseries}). -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @ @var -%! @item B -%! Dynare time series object instantiated by @ref{dseries}. -%! @item C -%! Dynare time series object instantiated by @ref{dseries}. -%! @end table -%! @sp 1 -%! @strong{Outputs} -%! @sp 1 -%! @table @ @var -%! @item A -%! Dynare time series object. -%! @sp 1 -%! @strong{Remarks} -%! If @var{B} and @var{C} have common variables, the variables in @var{C} take precedence. -%! @end deftypefn -%@eod: +% Merge method for dseries objects. +% +% INPUTS +% - o [dseries] +% - p [dseries] +% +% OUTPUTS +% - q [dseries] +% +% REMARKS +% If dseries objects o and p have common variables, the variables in p take precedence. -% Copyright (C) 2013-2015 Dynare Team +% Copyright (C) 2013-2017 Dynare Team % % This file is part of Dynare. % @@ -43,56 +29,56 @@ function A = merge(B,C) % --*-- 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 ~isdseries(C) +if ~isdseries(p) error('dseries::merge: Both inputs must be dseries objects!') end -if ~isequal(frequency(B),frequency(C)) +if ~isequal(frequency(o), frequency(p)) error(['dseries::merge: Cannot merge ' inputname(1) ' and ' inputname(2) ' (frequencies are different)!']) end -A = dseries(); -[A.name, IBC, junk] = unique([B.name; C.name], 'last'); -tex = [B.tex; C.tex]; -A.tex = tex(IBC); +q = dseries(); +[q.name, IBC, junk] = unique([o.name; p.name], 'last'); +tex = [o.tex; p.tex]; +q.tex = tex(IBC); -if nobs(B) == 0 - A = C; -elseif nobs(C) == 0 - A = B; -elseif firstdate(B) >= firstdate(C) - diff = firstdate(B) - firstdate(C); - A_nobs = max(nobs(B) + diff, nobs(C)); - A.data = NaN(A_nobs, vobs(A)); - Z1 = [NaN(diff, vobs(B));B.data]; - if nobs(A) > nobs(B) + diff - Z1 = [Z1; NaN(nobs(A)-(nobs(B) + diff), vobs(B))]; +if nobs(o) == 0 + q = copy(p); +elseif nobs(p) == 0 + q = copy(o); +elseif firstdate(o) >= firstdate(p) + diff = firstdate(o) - firstdate(p); + q_nobs = max(nobs(o) + diff, nobs(p)); + q.data = NaN(q_nobs, vobs(q)); + Z1 = [NaN(diff, vobs(o));o.data]; + if nobs(q) > nobs(o) + diff + Z1 = [Z1; NaN(nobs(q)-(nobs(o) + diff), vobs(o))]; end; - Z2 = C.data; - if nobs(A) > nobs(C) - Z2 = [Z2; NaN(nobs(A) - nobs(C), vobs(C))]; + Z2 = p.data; + if nobs(q) > nobs(p) + Z2 = [Z2; NaN(nobs(q) - nobs(p), vobs(p))]; end; Z = [Z1 Z2]; - A.data = Z(:,IBC); - A_init = firstdate(C); + q.data = Z(:,IBC); + q_init = firstdate(p); else - diff = firstdate(C) - firstdate(B); - A_nobs = max(nobs(C) + diff, nobs(B)); - A.data = NaN(A_nobs, vobs(A)); - Z1 = [NaN(diff, vobs(C)); C.data]; - if nobs(A) > nobs(C) + diff - Z1 = [Z1; NaN(nobs(A)-(nobs(C) + diff), vobs(C))]; + diff = firstdate(p) - firstdate(o); + q_nobs = max(nobs(p) + diff, nobs(o)); + q.data = NaN(q_nobs, vobs(q)); + Z1 = [NaN(diff, vobs(p)); p.data]; + if nobs(q) > nobs(p) + diff + Z1 = [Z1; NaN(nobs(q)-(nobs(p) + diff), vobs(p))]; end - Z2 = B.data; - if nobs(A) > nobs(B) - Z2 = [Z2; NaN(nobs(A) - nobs(B), vobs(B))]; + Z2 = o.data; + if nobs(q) > nobs(o) + Z2 = [Z2; NaN(nobs(q) - nobs(o), vobs(o))]; end; Z = [Z2 Z1]; - A.data = Z(:,IBC); - A_init = firstdate(B); + q.data = Z(:,IBC); + q_init = firstdate(o); end -A.dates = A_init:A_init+(nobs(A)-1); +q.dates = q_init:q_init+(nobs(q)-1); %@test:1 %$ % Define a datasets. diff --git a/src/@dseries/minus.m b/src/@dseries/minus.m index 49fe3e91d246311b2e8974ca8e0b6be760b98c30..5fe4af70240749a620b0568299c6fc62925eeda2 100644 --- a/src/@dseries/minus.m +++ b/src/@dseries/minus.m @@ -131,6 +131,7 @@ q.data = bsxfun(@minus, o.data, p.data); %$ t(3) = dassert(ts3.nobs,10); %$ t(4) = dassert(ts3.data,[A(:,1)-B, A(:,2)-B],1e-15); %$ t(5) = dassert(ts3.name,{'minus(A1;B1)';'minus(A2;B1)'}); +%$ t(6) = dassert(ts1.data, A, 1e-15); %$ end %$ T = all(t); %@eof:1 diff --git a/src/@dseries/mpower.m b/src/@dseries/mpower.m index 970fda7d169d84ab390aef61c73e4ee72e54ce25..2beb41c3a00b402602573d57371879681008bed4 100644 --- a/src/@dseries/mpower.m +++ b/src/@dseries/mpower.m @@ -3,13 +3,13 @@ function q = mpower(o, p) % --*-- Unitary tests --*-- % Overloads the power (^) operator for dseries objects. % % INPUTS -% - o [dseries] A dseries object with T observations and N variables. -% - p [real] A real scalar. +% - o [dseries] T observations and N variables. +% - p [dseries,double] scalar, vector or dseries object. % % OUTPUTS -% - q [dseries] A dseries object with T observations and N variables. +% - q [dseries] T observations and N variables. -% Copyright (C) 2013-2016 Dynare Team +% Copyright (C) 2013-2017 Dynare Team % % This file is part of Dynare. % @@ -100,6 +100,7 @@ error('dseries:WrongInputArguments', 'Wrong calling sequence! Please check the m %$ t(4) = dassert(ts3.data,A.^B,1e-15); %$ t(5) = dassert(ts3.name,{'power(A1;B1)';'power(A2;B2)'}); %$ t(6) = dassert(ts3.tex,{'A1^{B1}';'A2^{B2}'}); +%$ t(7) = dassert(ts1.data, A, 1e-15); %$ end %$ T = all(t); %@eof:1 diff --git a/src/@dseries/mrdivide.m b/src/@dseries/mrdivide.m index 8dfacbc5bebc0f9507c5fc5bb421a4e8714c331b..531fcfd46fd34f460920e1d496beff8d8833b1e8 100644 --- a/src/@dseries/mrdivide.m +++ b/src/@dseries/mrdivide.m @@ -1,29 +1,15 @@ -function A = mrdivide(B,C) % --*-- Unitary tests --*-- +function q = mrdivide(o, p) % --*-- Unitary tests --*-- -%@info: -%! @deftypefn {Function File} {@var{A} =} mrdivide (@var{B},@var{C}) -%! @anchor{@dseries/mrdivide} -%! @sp 1 -%! Overloads the mrdivide method for the Dynare time series class (@ref{dseries}). -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @ @var -%! @item B -%! Dynare time series object instantiated by @ref{dseries}. -%! @item C -%! Dynare time series object instantiated by @ref{dseries}. -%! @end table -%! @sp 1 -%! @strong{Outputs} -%! @sp 1 -%! @table @ @var -%! @item A -%! Dynare time series object. -%! @end deftypefn -%@eod: +% Overloads the mrdivde (/) operator for dseries objects. +% +% INPUTS +% - o [dseries] T observations and N variables. +% - p [dseries,double] scalar, vector or dseries object. +% +% OUTPUTS +% - q [dseries] T observations and N variables. -% Copyright (C) 2012-2014 Dynare Team +% Copyright (C) 2013-2017 Dynare Team % % This file is part of Dynare. % @@ -40,56 +26,56 @@ function A = mrdivide(B,C) % --*-- 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 isnumeric(B) && (isscalar(B) || isvector(B)) - if ~isdseries(C) +if isnumeric(o) && (isscalar(o) || isvector(o)) + if ~isdseries(p) error('dseries::mrdivide: Second input argument must be a dseries object!') end - A = C; - A.data = bsxfun(@rdivide,B,C.data); + q = copy(p); + q.data = bsxfun(@rdivide, o, p.data); return; end -if isnumeric(C) && (isscalar(C) || isvector(C)) - if ~isdseries(B) +if isnumeric(p) && (isscalar(p) || isvector(p)) + if ~isdseries(o) error('dseries::mrdivide: First input argument must be a dseries object!') end - A = B; - A.data = bsxfun(@rdivide,B.data,C); + q = copy(o); + q.data = bsxfun(@rdivide, o.data, p); return end -if isdseries(B) && isdseries(C) +if isdseries(o) && isdseries(p) % Element by element divisions of two dseries object - if ~isequal(vobs(B), vobs(C)) && ~(isequal(vobs(B),1) || isequal(vobs(C),1)) + if ~isequal(vobs(o), vobs(p)) && ~(isequal(vobs(o),1) || isequal(vobs(p),1)) error(['dseries::times: Cannot divide ' inputname(1) ' and ' inputname(2) ' (wrong number of variables)!']) else - if vobs(B)>vobs(C) - idB = 1:vobs(B); - idC = ones(1:vobs(B)); - elseif vobs(B)<vobs(C) - idB = ones(1,vobs(C)); - idC = 1:vobs(C); + if vobs(o)>vobs(p) + idB = 1:vobs(o); + idC = ones(1:vobs(o)); + elseif vobs(o)<vobs(p) + idB = ones(1,vobs(p)); + idC = 1:vobs(p); else - idB = 1:vobs(B); - idC = 1:vobs(C); + idB = 1:vobs(o); + idC = 1:vobs(p); end end - if ~isequal(frequency(B),frequency(C)) + if ~isequal(frequency(o),frequency(p)) error(['dseries::times: Cannot divide ' inputname(1) ' and ' inputname(2) ' (frequencies are different)!']) end - if ~isequal(nobs(B), nobs(C)) || ~isequal(firstdate(B),firstdate(C)) - [B, C] = align(B, C); + if ~isequal(nobs(o), nobs(p)) || ~isequal(firstdate(o),firstdate(p)) + [o, p] = align(o, p); end - A = dseries(); - A.dates = B.dates; - A_vobs = max(vobs(B),vobs(C)); - A.name = cell(A_vobs,1); - A.tex = cell(A_vobs,1); - for i=1:A_vobs - A.name(i) = {['divide(' B.name{idB(i)} ';' C.name{idC(i)} ')']}; - A.tex(i) = {['(' B.tex{idB(i)} '/' C.tex{idC(i)} ')']}; + q = dseries(); + q.dates = o.dates; + q_vobs = max(vobs(o),vobs(p)); + q.name = cell(q_vobs,1); + q.tex = cell(q_vobs,1); + for i=1:q_vobs + q.name(i) = {['divide(' o.name{idB(i)} ';' p.name{idC(i)} ')']}; + q.tex(i) = {['(' o.tex{idB(i)} '/' p.tex{idC(i)} ')']}; end - A.data = bsxfun(@rdivide,B.data,C.data); + q.data = bsxfun(@rdivide, o.data, p.data); else error() end diff --git a/src/@dseries/mtimes.m b/src/@dseries/mtimes.m index 33280102622f6b79830cc187c514381804cc21b4..6d1df4d088da1a0fb6983b6f0d6fcce58c3906db 100644 --- a/src/@dseries/mtimes.m +++ b/src/@dseries/mtimes.m @@ -1,29 +1,15 @@ -function A = mtimes(B,C) % --*-- Unitary tests --*-- +function q = mtimes(o, p) % --*-- Unitary tests --*-- -%@info: -%! @deftypefn {Function File} {@var{A} =} mtimes (@var{B},@var{C}) -%! @anchor{@dseries/mtimes} -%! @sp 1 -%! Overloads the mtimes method for the Dynare time series class (@ref{dseries}). -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @ @var -%! @item B -%! Dynare time series object instantiated by @ref{dseries}. -%! @item C -%! Dynare time series object instantiated by @ref{dseries}. -%! @end table -%! @sp 1 -%! @strong{Outputs} -%! @sp 1 -%! @table @ @var -%! @item A -%! Dynare time series object. -%! @end deftypefn -%@eod: +% Overloads the mtimes (*) operator for dseries objects. +% +% INPUTS +% - o [dseries] T observations and N variables. +% - p [dseries,double] scalar, vector or dseries object. +% +% OUTPUTS +% - q [dseries] T observations and N variables. -% Copyright (C) 2012-2014 Dynare Team +% Copyright (C) 2013-2017 Dynare Team % % This file is part of Dynare. % @@ -40,56 +26,56 @@ function A = mtimes(B,C) % --*-- 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 isnumeric(B) && (isscalar(B) || isvector(B)) - if ~isdseries(C) +if isnumeric(o) && (isscalar(o) || isvector(o)) + if ~isdseries(p) error('dseries::mtimes: Second input argument must be a dseries object!') end - A = C; - A.data = bsxfun(@times,C.data,B); + q = copy(p); + q.data = bsxfun(@times, p.data, o); return; end -if isnumeric(C) && (isscalar(C) || isvector(C)) - if ~isdseries(B) +if isnumeric(p) && (isscalar(p) || isvector(p)) + if ~isdseries(o) error('dseries::mtimes: First input argument must be a dseries object!') end - A = B; - A.data = bsxfun(@times,B.data,C); + q = copy(o); + q.data = bsxfun(@times, o.data, p); return end -if isdseries(B) && isdseries(C) +if isdseries(o) && isdseries(p) % Element by element multiplication of two dseries object - if ~isequal(vobs(B), vobs(C)) && ~(isequal(vobs(B),1) || isequal(vobs(C),1)) + if ~isequal(vobs(o), vobs(p)) && ~(isequal(vobs(o),1) || isequal(vobs(p),1)) error(['dseries::times: Cannot multiply ' inputname(1) ' and ' inputname(2) ' (wrong number of variables)!']) else - if vobs(B)>vobs(C) - idB = 1:vobs(B); - idC = ones(1:vobs(B)); - elseif vobs(B)<vobs(C) - idB = ones(1,vobs(C)); - idC = 1:vobs(C); + if vobs(o)>vobs(p) + idB = 1:vobs(o); + idC = ones(1:vobs(o)); + elseif vobs(o)<vobs(p) + idB = ones(1,vobs(p)); + idC = 1:vobs(p); else - idB = 1:vobs(B); - idC = 1:vobs(C); + idB = 1:vobs(o); + idC = 1:vobs(p); end end - if ~isequal(frequency(B),frequency(C)) + if ~isequal(frequency(o),frequency(p)) error(['dseries::times: Cannot multiply ' inputname(1) ' and ' inputname(2) ' (frequencies are different)!']) end - if ~isequal(nobs(B), nobs(C)) || ~isequal(firstdate(B),firstdate(C)) - [B, C] = align(B, C); + if ~isequal(nobs(o), nobs(p)) || ~isequal(firstdate(o),firstdate(p)) + [o, p] = align(o, p); end - A = dseries(); - A.dates = B.dates; - A_vobs = max(vobs(B),vobs(C)); - A.name = cell(A_vobs,1); - A.tex = cell(A_vobs,1); - for i=1:A_vobs - A.name(i) = {['multiply(' B.name{idB(i)} ';' C.name{idC(i)} ')']}; - A.tex(i) = {['(' B.tex{idB(i)} '*' C.tex{idC(i)} ')']}; + q = dseries(); + q.dates = o.dates; + q_vobs = max(vobs(o),vobs(p)); + q.name = cell(q_vobs,1); + q.tex = cell(q_vobs,1); + for i=1:q_vobs + q.name(i) = {['multiply(' o.name{idB(i)} ';' p.name{idC(i)} ')']}; + q.tex(i) = {['(' o.tex{idB(i)} '*' p.tex{idC(i)} ')']}; end - A.data = bsxfun(@times,B.data,C.data); + q.data = bsxfun(@times, o.data, p.data); else error() end diff --git a/src/@dseries/ne.m b/src/@dseries/ne.m index d87f15f008efbf26363b84e35f57dea3c8d36a68..77f4493fad932c2d3aa7f4d66683791992e38065 100644 --- a/src/@dseries/ne.m +++ b/src/@dseries/ne.m @@ -1,4 +1,4 @@ -function C = ne(A,B) % --*-- Unitary tests --*-- +function b = ne(o, p) % --*-- Unitary tests --*-- % Overloads ne (~=) operator. % @@ -33,43 +33,43 @@ if nargin~=2 error('dseries::ne: I need exactly two input arguments!') end -if ~(isdseries(A) && isdseries(B)) +if ~(isdseries(o) && isdseries(p)) error('dseries::ne: Both input arguments must be dseries objects!') end -if ~isequal(nobs(A), nobs(B)) +if ~isequal(nobs(o), nobs(p)) warning('dseries::ne: Both input arguments should have the same number of observations!') - C = 1; + b = 1; return end -if ~isequal(vobs(A), vobs(B)) +if ~isequal(vobs(o), vobs(p)) warning('dseries::ne: Both input arguments should have the same number of observations!') - C = 1; + b = 1; return end -if ~isequal(frequency(A),frequency(B)) +if ~isequal(frequency(o), frequency(p)) warning('dseries::ne: Both input arguments should have the same frequencies!') - C = 1; + b = 1; return end -if ~isequal(firstdate(A),firstdate(B)) +if ~isequal(firstdate(o), firstdate(p)) warning('dseries::ne: Both input arguments should have the same initial period!') - C = 1; + b = 1; return end -if ~isequal(A.name,B.name) +if ~isequal(o.name, p.name) warning('dseries::ne: Both input arguments do not have the same variables!') end -if ~isequal(A.tex,B.tex) +if ~isequal(o.tex, p.tex) warning('dseries::ne: Both input arguments do not have the same tex names!') end -C = ne(A.data, B.data); +b = ne(o.data, p.data); %@test:1 %$ % Define a datasets. diff --git a/src/@dseries/nobs.m b/src/@dseries/nobs.m index dc6fc76536d5d8a5d86fc3c731c80a70694fbc28..e69f6c9e1edf265e2a13d2ca739a3a6955b1ba12 100644 --- a/src/@dseries/nobs.m +++ b/src/@dseries/nobs.m @@ -1,8 +1,8 @@ -function s = nobs(ts) +function s = nobs(o) % Returns the number of observations in a @dseries object. -% Copyright (C) 2014 Dynare Team +% Copyright (C) 2014-2017 Dynare Team % % This file is part of Dynare. % @@ -19,4 +19,4 @@ function s = nobs(ts) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -s = rows(ts.data); \ No newline at end of file +s = rows(o.data); \ No newline at end of file diff --git a/src/@dseries/numel.m b/src/@dseries/numel.m index 85cd84962a05b56f411ca3f60de28d691a09dd2f..15b9cb05f6b0d6374205fa5fb42566affc2120bd 100644 --- a/src/@dseries/numel.m +++ b/src/@dseries/numel.m @@ -1,6 +1,6 @@ -function n = numel(obj, varargin) +function n = numel(o, varargin) -% Copyright (C) 2012-2013 Dynare Team +% Copyright (C) 2012-2017 Dynare Team % % This file is part of Dynare. % diff --git a/src/@dseries/plot.m b/src/@dseries/plot.m index 058a44dd0cf1a08af81def389fb0ac868f5dd42b..f515c4187d7d563f3d8cc97e1c652e8d257785e3 100644 --- a/src/@dseries/plot.m +++ b/src/@dseries/plot.m @@ -1,8 +1,8 @@ -function h = plot(ts, varargin) +function h = plot(o, varargin) % Overloads Matlab/Octave's plot function for dseries objects. -% Copyright (C) 2013 Dynare Team +% Copyright (C) 2013-2017 Dynare Team % % This file is part of Dynare. % @@ -22,13 +22,13 @@ function h = plot(ts, varargin) % Get the number of dseries objects if isequal(nargin,1) ndseries = 1; - nvariables = vobs(ts); - nobservations = nobs(ts); + nvariables = vobs(o); + nobservations = nobs(o); else if isdseries(varargin{1}) ndseries = 2; - nvariables = vobs(ts); - nobservations = nobs(ts); + nvariables = vobs(o); + nobservations = nobs(o); if nargin>2 && any(cellfun(@isdseries,varargin(2:end))) error('dseries::plot: You cannot pass more two dseries objects!') end @@ -40,40 +40,40 @@ else end else ndseries = 1; - nvariables = vobs(ts); - nobservations = nobs(ts); + nvariables = vobs(o); + nobservations = nobs(o); end end switch ndseries case 1 if isequal(nvariables,1) - hh = plot(ts.data,varargin{:}); + hh = plot(o.data,varargin{:}); else if length(varargin) message = sprintf('dseries::plot: dseries object %s has %d>1 variables but you passed additional arguments to the plot function.\n These additional arguments won''t ne interpreted. Use the Matlab/Octave set command and the plot\n handle instead if you wish to modify the properties of the plotted time series.',inputname(1),nvariables); warning(message) end - hh = plot(ts.data); + hh = plot(o.data); end axis tight; id = get(gca,'XTick'); if isequal(id(1),0) - dates = strings([ts.dates(1)-1,ts.dates(id(2:end))]); + dates = strings([o.dates(1)-1,o.dates(id(2:end))]); else - dates = strings(ts.dates(id)); + dates = strings(o.dates(id)); end set(gca,'XTickLabel',dates); case 2 - [ts0, ts1] = align(ts, varargin{1}); + [o0, o1] = align(o, varargin{1}); if isequal(nvariables,1) - hh = plot(ts0.data, ts1.data, varargin{2:end}); + hh = plot(o0.data, o1.data, varargin{2:end}); else if length(varargin)>1 message = sprintf('dseries::plot: dseries objects %s and %s have %d>1 variables but you passed additional arguments to the plot function.\n These additional arguments won''t ne interpreted. Use the Matlab/Octave set command and the plot\n handle instead if you wish to modify the properties of the plotted time series.',inputname(1),inputname(2),nvariables); warning(message) end - hh = plot(ts0.data, ts1.data); + hh = plot(o0.data, o1.data); end otherwise error('dseries::plot: This is a bug! Please report the bug to the authors of Dynare.') diff --git a/src/@dseries/pop.m b/src/@dseries/pop.m index 015267429af135f71ef51ea81ebbf9bc02582ae0..a623074b3959e57bc267b2d8e2cd5ebedfcfce2c 100644 --- a/src/@dseries/pop.m +++ b/src/@dseries/pop.m @@ -1,34 +1,15 @@ -function [ts,id] = pop(ts,a) % --*-- Unitary tests --*-- +function [o, id] = pop(o, a) % --*-- Unitary tests --*-- % Removes a variable from a dseries object. +% +% INPUTS +% - o [dseries] T observations and N variables. +% - a [string] Name of the variable to be removed. +% +% OUTPUTS +% - o [dseries] T observations and N-1 variables. -%@info: -%! @deftypefn {Function File} {@var{ts} =} pop (@var{ts}, @var{a}) -%! @anchor{dseries/pop} -%! @sp 1 -%! Pop method for the dseries class. Removes a variable from a dseries object. -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @ @var -%! @item ts -%! Object instantiated by @ref{dseries}. -%! @item a -%! String, name of the variable to be removed. -%! @end table -%! @sp 2 -%! @strong{Outputs} -%! @sp 1 -%! @table @ @var -%! @item ts -%! Object instantiated by @ref{dseries}, without variable (@var{a}). -%! @item id -%! Scalar integer, position of variable (@var{a}) in the original dseries object @var{ts}. -%! @end table -%! @end deftypefn -%@eod: - -% Copyright (C) 2013 Dynare Team +% Copyright (C) 2013-2017 Dynare Team % % This file is part of Dynare. % @@ -45,22 +26,14 @@ function [ts,id] = pop(ts,a) % --*-- 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/>. +o = copy(o); + if nargin<2 - % Removes the last variable - id = vobs(ts); + [o, id] = pop_(o); else - id = find(strcmp(a,ts.name)); + [o, id] = pop_(o, a); end -if isempty(id) - id = 0; - return -end - -ts.data(:,id) = []; -ts.name(id) = []; -ts.tex(id) = []; - %@test:1 %$ % Define a datasets. %$ A = rand(10,3); @@ -73,7 +46,7 @@ ts.tex(id) = []; %$ % Instantiate a time series object. %$ try %$ ts1 = dseries(A,[],A_name,[]); -%$ ts2 = pop(ts1,'A2'); +%$ ts2 = ts1.pop('A2'); %$ t(1) = 1; %$ catch %$ t = 0; @@ -83,6 +56,10 @@ ts.tex(id) = []; %$ t(2) = dassert(ts2.vobs,2); %$ t(3) = dassert(ts2.nobs,10); %$ t(4) = dassert(ts2.data,[A(:,1), A(:,3)],1e-15); +%$ t(5) = dassert(ts1.vobs,3); +%$ if t(5) +%$ t(6) = dassert(ts1.data,A,1e-15); +%$ end %$ end %$ T = all(t); %@eof:1 diff --git a/src/@dseries/pop_.m b/src/@dseries/pop_.m new file mode 100644 index 0000000000000000000000000000000000000000..abd3bcb764f080a3b36fc08ecbd62e80cd7d6437 --- /dev/null +++ b/src/@dseries/pop_.m @@ -0,0 +1,67 @@ +function [o, id] = pop_(o, a) % --*-- Unitary tests --*-- + +% Removes a variable from a dseries object. +% +% INPUTS +% - o [dseries] T observations and N variables. +% - a [string] Name of the variable to be removed. +% +% OUTPUTS +% - o [dseries] T observations and N-1 variables. + +% Copyright (C) 2013-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 + % Removes the last variable + id = vobs(o); +else + id = find(strcmp(a, o.name)); +end + +if isempty(id) + id = 0; + return +end + +o.data(:,id) = []; +o.name(id) = []; +o.tex(id) = []; + +%@test:1 +%$ % Define a datasets. +%$ A = rand(10,3); +%$ +%$ % Define names +%$ A_name = {'A1';'A2';'A3'}; +%$ +%$ % Instantiate a time series object. +%$ try +%$ ts1 = dseries(A,[],A_name,[]); +%$ ts1.pop_('A2'); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if length(t)>1 +%$ t(2) = dassert(ts1.vobs,2); +%$ t(3) = dassert(ts1.nobs,10); ts1 +%$ t(4) = dassert(ts1.data,[A(:,1), A(:,3)],1e-15); +%$ end +%$ T = all(t); +%@eof:1 \ No newline at end of file diff --git a/src/@dseries/qdiff.m b/src/@dseries/qdiff.m index 083490d7d9f1c5de23947b860372dc478bfc3ebb..c3ae5454ce2283e29e0add62e298f217c3e5999f 100644 --- a/src/@dseries/qdiff.m +++ b/src/@dseries/qdiff.m @@ -1,28 +1,14 @@ -function us = qdiff(ts) % --*-- Unitary tests --*-- +function o = qdiff(o) % --*-- Unitary tests --*-- -%@info: -%! @deftypefn {Function File} {@var{us} =} qdiff (@var{ts}) -%! @anchor{qdiff} -%! @sp 1 -%! Computes quaterly differences. -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @var -%! @item ts -%! Dynare time series object, instantiated by @ref{dseries} -%! @end table -%! @sp 2 -%! @strong{Outputs} -%! @sp 1 -%! @table @var -%! @item us -%! Dynare time series object with transformed data field. -%! @end table -%! @end deftypefn -%@eod: +% Computes quaterly differences. +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - o [dseries] -% Copyright (C) 2012-2015 Dynare Team +% Copyright (C) 2012-2017 Dynare Team % % This file is part of Dynare. % @@ -39,69 +25,25 @@ function us = qdiff(ts) % --*-- 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/>. -us = ts; - -switch frequency(ts) - case 1 - error('dseries::qdiff: I cannot compute quaterly differences from yearly data!') - case 4 - us.data(2:end,:) = ts.data(2:end,:)-ts.data(1:end-1,:); - us.data(1,:) = NaN; - for i = 1:vobs(ts) - us.name(i) = {['qdiff(' us.name{i} ')']}; - us.tex(i) = {['\Delta ' us.tex{i}]}; - end - case 12 - us.data(4:end,:) = ts.data(4:end,:)-ts.data(1:end-3,:); - us.data(1:3,:) = NaN; - for i = 1:vobs(ts) - us.name(i) = {['qdiff(' us.name{i} ')']}; - us.tex(i) = {['\Delta_3 ' us.tex{i}]}; - end - case 52 - error('dseries::qdiff: I do not know yet how to compute quaterly differences from weekly data!') - otherwise - error(['dseries::qdiff: object ' inputname(1) ' has unknown frequency']); -end +o = copy(o); +o.qdiff_(); %@test:1 -%$ t = zeros(2,1); -%$ %$ try %$ data = transpose(0:1:50); %$ ts = dseries(data,'1950Q1'); -%$ ts = ts.qdiff; -%$ t(1) = 1; -%$ catch -%$ t = 0; -%$ end -%$ -%$ if length(t)>1 -%$ DATA = NaN(1,ts.vobs); -%$ DATA = [DATA; ones(ts.nobs-1,ts.vobs)]; -%$ t(2) = dassert(ts.data,DATA,1e-15); -%$ end -%$ -%$ T = all(t); -%@eof:1 - -%@test:2 -%$ t = zeros(2,1); -%$ -%$ try -%$ data = transpose(0:1:80); -%$ ts = dseries(data,'1950M1'); -%$ ts = ts.qdiff; +%$ ds = ts.qdiff(); %$ t(1) = 1; %$ catch -%$ t = 0; +%$ t(1) = 0; %$ end %$ -%$ if length(t)>1 -%$ DATA = NaN(3,ts.vobs); -%$ DATA = [DATA; 3*ones(ts.nobs-3,ts.vobs)]; -%$ t(2) = dassert(ts.data,DATA,1e-15); +%$ 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:2 +%@eof:1 \ No newline at end of file diff --git a/src/@dseries/qdiff_.m b/src/@dseries/qdiff_.m new file mode 100644 index 0000000000000000000000000000000000000000..881cb8de97fa942386530f386828300dc61878a8 --- /dev/null +++ b/src/@dseries/qdiff_.m @@ -0,0 +1,87 @@ +function o = qdiff_(o) % --*-- Unitary tests --*-- + +% Computes quaterly differences. +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - o [dseries] + +% Copyright (C) 2012-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/>. + +switch frequency(o) + case 1 + error('dseries::qdiff: I cannot compute quaterly differences from yearly data!') + case 4 + o.data(2:end,:) = o.data(2:end,:)-o.data(1:end-1,:); + o.data(1,:) = NaN; + for i = 1:vobs(o) + o.name(i) = {['qdiff(' o.name{i} ')']}; + o.tex(i) = {['\Delta ' o.tex{i}]}; + end + case 12 + o.data(4:end,:) = o.data(4:end,:)-o.data(1:end-3,:); + o.data(1:3,:) = NaN; + for i = 1:vobs(o) + o.name(i) = {['qdiff(' o.name{i} ')']}; + o.tex(i) = {['\Delta_3 ' o.tex{i}]}; + end + case 52 + error('dseries::qdiff: I do not know yet how to compute quaterly differences from weekly data!') + otherwise + error(['dseries::qdiff: object ' inputname(1) ' has unknown frequency']); +end + +%@test:1 +%$ try +%$ data = transpose(0:1:50); +%$ ts = dseries(data,'1950Q1'); +%$ ts.qdiff_; +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ DATA = NaN(1,ts.vobs); +%$ DATA = [DATA; ones(ts.nobs-1,ts.vobs)]; +%$ t(2) = dassert(ts.data,DATA,1e-15); +%$ end +%$ +%$ T = all(t); +%@eof:1 + +%@test:2 +%$ try +%$ data = transpose(0:1:80); +%$ ts = dseries(data,'1950M1'); +%$ ts.qdiff; +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ DATA = NaN(3,ts.vobs); +%$ DATA = [DATA; 3*ones(ts.nobs-3,ts.vobs)]; +%$ t(2) = dassert(ts.data,DATA,1e-15); +%$ end +%$ +%$ T = all(t); +%@eof:2 diff --git a/src/@dseries/qgrowth.m b/src/@dseries/qgrowth.m index a0e79fb0ca1b71451afe82cc95954f984552c6e4..65ee58cabdc37f6035d2057e429049be440c58d9 100644 --- a/src/@dseries/qgrowth.m +++ b/src/@dseries/qgrowth.m @@ -1,28 +1,14 @@ -function us = qgrowth(ts) % --*-- Unitary tests --*-- +function o = qgrowth_(o) % --*-- Unitary tests --*-- -%@info: -%! @deftypefn {Function File} {@var{us} =} qgrowth (@var{ts}) -%! @anchor{qgrowth} -%! @sp 1 -%! Computes quaterly growth rates. -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @var -%! @item ts -%! Dynare time series object, instantiated by @ref{dseries} -%! @end table -%! @sp 2 -%! @strong{Outputs} -%! @sp 1 -%! @table @var -%! @item us -%! Dynare time series object with transformed data field. -%! @end table -%! @end deftypefn -%@eod: +% Computes quaterly growth rates. +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - o [dseries] -% Copyright (C) 2012-2015 Dynare Team +% Copyright (C) 2012-2017 Dynare Team % % This file is part of Dynare. % @@ -39,69 +25,25 @@ function us = qgrowth(ts) % --*-- 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/>. -us = ts; - -switch frequency(ts) - case 1 - error('dseries::qgrowth: I cannot compute quaterly growth rates from yearly data!') - case 4 - us.data(2:end,:) = ts.data(2:end,:)./ts.data(1:end-1,:) - 1; - us.data(1,:) = NaN; - for i = 1:vobs(ts) - us.name(i) = {['qgrowth(' us.name{i} ')']}; - us.tex(i) = {['\delta ' us.tex{i}]}; - end - case 12 - us.data(4:end,:) = ts.data(4:end,:)./ts.data(1:end-3,:) - 1; - us.data(1:3,:) = NaN; - for i = 1:vobs(ts) - us.name(i) = {['qgrowth(' us.name{i} ')']}; - us.tex(i) = {['\delta_3 ' us.tex{i}]}; - end - case 52 - error('dseries::qgrowth: I do not know yet how to compute quaterly growth rates from weekly data!') - otherwise - error(['dseries::qgrowth: object ' inputname(1) ' has unknown frequency']); -end +o = copy(o); +o.qgrowth_(); %@test:1 -%$ t = zeros(2,1); -%$ %$ try %$ data = (1+.01).^transpose(0:1:50); %$ ts = dseries(data,'1950Q1'); -%$ ts = ts.qgrowth; -%$ t(1) = 1; -%$ catch -%$ t = 0; -%$ end -%$ -%$ if length(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 -%$ t = zeros(2,1); -%$ -%$ try -%$ data = (1+.01).^transpose(0:1:80); -%$ ts = dseries(data,'1950M1'); -%$ ts = ts.qgrowth; +%$ ds = ts.qgrowth(); %$ t(1) = 1; %$ catch -%$ t = 0; +%$ t(1) = 0; %$ end %$ -%$ if length(t)>1 -%$ DATA = NaN(3,ts.vobs); -%$ DATA = [DATA; (1.01^3-1)*ones(ts.nobs-3,ts.vobs)]; -%$ t(2) = dassert(ts.data,DATA,1e-15); +%$ if t(1) +%$ DATA = NaN(1,ds.vobs); +%$ DATA = [DATA; .01*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:2 +%@eof:1 \ No newline at end of file diff --git a/src/@dseries/qgrowth_.m b/src/@dseries/qgrowth_.m new file mode 100644 index 0000000000000000000000000000000000000000..98971e8df8fe07ebfb2a5289385233f29455b4a6 --- /dev/null +++ b/src/@dseries/qgrowth_.m @@ -0,0 +1,87 @@ +function o = qgrowth_(o) % --*-- Unitary tests --*-- + +% Computes quaterly growth rates. +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - o [dseries] + +% Copyright (C) 2012-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/>. + +switch frequency(o) + case 1 + error('dseries::qgrowth: I cannot compute quaterly growth rates from yearly data!') + case 4 + o.data(2:end,:) = o.data(2:end,:)./o.data(1:end-1,:) - 1; + o.data(1,:) = NaN; + for i = 1:vobs(o) + o.name(i) = {['qgrowth(' o.name{i} ')']}; + o.tex(i) = {['\delta ' o.tex{i}]}; + end + case 12 + o.data(4:end,:) = o.data(4:end,:)./o.data(1:end-3,:) - 1; + o.data(1:3,:) = NaN; + for i = 1:vobs(o) + o.name(i) = {['qgrowth(' o.name{i} ')']}; + o.tex(i) = {['\delta_3 ' o.tex{i}]}; + end + case 52 + error('dseries::qgrowth: I do not know yet how to compute quaterly growth rates from weekly data!') + otherwise + error(['dseries::qgrowth: object ' inputname(1) ' has unknown frequency']); +end + +%@test:1 +%$ try +%$ data = (1+.01).^transpose(0:1:50); +%$ ts = dseries(data,'1950Q1'); +%$ ts.qgrowth_; +%$ 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 = (1+.01).^transpose(0:1:80); +%$ ts = dseries(data,'1950M1'); +%$ ts.qgrowth_; +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ DATA = NaN(3,ts.vobs); +%$ DATA = [DATA; (1.01^3-1)*ones(ts.nobs-3,ts.vobs)]; +%$ t(2) = dassert(ts.data,DATA,1e-15); +%$ end +%$ +%$ T = all(t); +%@eof:2 diff --git a/src/@dseries/remove.m b/src/@dseries/remove.m index 9f9b19a53138b32a53c835831a2402178cb84251..52e0eea8c671a790adc07be0edd7399d060b221d 100644 --- a/src/@dseries/remove.m +++ b/src/@dseries/remove.m @@ -1,32 +1,8 @@ -function ts = remove(ts,a) % --*-- Unitary tests --*-- +function o = remove(o, a) % --*-- Unitary tests --*-- % Removes a variable from a dseries object (alias for the pop method). -%@info: -%! @deftypefn {Function File} {@var{ts} =} pop (@var{ts}, @var{a}) -%! @anchor{dseries/pop} -%! @sp 1 -%! Remove method for the dseries class. Removes a variable from a dseries object. -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @ @var -%! @item ts -%! Object instantiated by @ref{dseries}. -%! @item a -%! String, name of the variable to be removed. -%! @end table -%! @sp 2 -%! @strong{Outputs} -%! @sp 1 -%! @table @ @var -%! @item ts -%! Object instantiated by @ref{dseries}, without variable (@var{a}). -%! @end table -%! @end deftypefn -%@eod: - -% Copyright (C) 2014 Dynare Team +% Copyright (C) 2014-2017 Dynare Team % % This file is part of Dynare. % @@ -43,56 +19,4 @@ function ts = remove(ts,a) % --*-- 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/>. -ts = pop(ts, a); - -%@test:1 -%$ % Define a datasets. -%$ A = rand(10,3); -%$ -%$ % Define names -%$ A_name = {'A1';'A2';'A3'}; -%$ -%$ t = zeros(4,1); -%$ -%$ % Instantiate a time series object. -%$ try -%$ ts1 = dseries(A,[],A_name,[]); -%$ ts2 = remove(ts1,'A2'); -%$ t(1) = 1; -%$ catch -%$ t = 0; -%$ end -%$ -%$ if length(t)>1 -%$ t(2) = dassert(ts2.vobs,2); -%$ t(3) = dassert(ts2.nobs,10); -%$ t(4) = dassert(ts2.data,[A(:,1), A(:,3)],1e-15); -%$ end -%$ T = all(t); -%@eof:1 - -%@test:2 -%$ % Define a datasets. -%$ A = rand(10,3); -%$ -%$ % Define names -%$ A_name = {'A1';'A2';'A3'}; -%$ -%$ t = zeros(4,1); -%$ -%$ % Instantiate a time series object. -%$ try -%$ ts1 = dseries(A,[],A_name,[]); -%$ ts2 = ts1.remove('A2'); -%$ t(1) = 1; -%$ catch -%$ t = 0; -%$ end -%$ -%$ if length(t)>1 -%$ t(2) = dassert(ts2.vobs,2); -%$ t(3) = dassert(ts2.nobs,10); -%$ t(4) = dassert(ts2.data,[A(:,1), A(:,3)],1e-15); -%$ end -%$ T = all(t); -%@eof:2 \ No newline at end of file +o = pop(o, a); \ No newline at end of file diff --git a/src/@dseries/remove_.m b/src/@dseries/remove_.m new file mode 100644 index 0000000000000000000000000000000000000000..453bf0e04b8c36fdeb5e5392c2ad5ddc4de9420a --- /dev/null +++ b/src/@dseries/remove_.m @@ -0,0 +1,22 @@ +function o = remove_(o, a) % --*-- Unitary tests --*-- + +% Removes a variable from a dseries object (alias for the pop_ method). + +% 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/>. + +o = pop_(o, a); \ No newline at end of file diff --git a/src/@dseries/rename.m b/src/@dseries/rename.m index ada05115d6b845699c67845fb87baff0959826f4..3607321321b1311743a4d1711a1be6d3580b52a3 100644 --- a/src/@dseries/rename.m +++ b/src/@dseries/rename.m @@ -1,6 +1,16 @@ -function ts = rename(ts,old,new) % --*-- Unitary tests --*-- +function o = rename_(o, old, new) % --*-- Unitary tests --*-- -% Copyright (C) 2013-2015 Dynare Team +% Renames variables in a dseries object. +% +% INPUTS +% - o [dseries] +% - old [string, cell] +% - new [string, cell] +% +% OUTPUTS +% - o [dseries] + +% Copyright (C) 2013-2017 Dynare Team % % This file is part of Dynare. % @@ -17,114 +27,47 @@ function ts = rename(ts,old,new) % --*-- 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 isempty(ts) - error('dseries::rename: Cannot rename variable(s) because the object is empty!') -end - +o = copy(o); if nargin<3 - if isequal(vobs(ts), 1) || isequal(vobs(ts), length(old)) - new = old; - else - error('dseries::rename: Missing argument!') - end - if ~ischar(new) && ~(iscellstr(new) && isequal(vobs(ts), length(new))) - error(['dseries::rename: Input argument ''' ... - inputname(2) ... - ''' has to be either a string or a cellstring that has the same number of entries as observed variables!']) - end - idname = 1; -else - if ~ischar(old) || ~ischar(new) - error(['dseries::rename: Input arguments ''' inputname(2) ''' and ''' inputname(3) ''' have to be strings!']) - end - idname = find(strcmp(old,ts.name)); - if isempty(idname) - error(['dseries::rename: Variable ' old ' is unknown in dseries object ' inputname(1) '!']) - end -end - -if iscellstr(new) - ts.name = new; + o.rename_(old); else - ts.name(idname) = {new}; + o.rename_(old, new); end %@test:1 -%$ t = zeros(8,1); %$ ts = dseries([transpose(1:5), transpose(6:10)],'1950q1',{'Output'; 'Consumption'}, {'Y_t'; 'C_t'}); %$ try -%$ ts = rename(ts,'Output','Production'); +%$ ds = ts.rename('Output','Production'); %$ t(1) = 1; %$ catch -%$ t = 0; +%$ t(1) = 0; %$ end %$ -%$ if length(t)>1 -%$ t(2) = dassert(ts.freq,4); -%$ t(3) = dassert(ts.init.freq,4); -%$ t(4) = dassert(ts.init.time,[1950, 1]); -%$ t(5) = dassert(ts.vobs,2); -%$ t(6) = dassert(ts.nobs,5); -%$ t(7) = dassert(ts.name,{'Production'; 'Consumption'}); -%$ t(8) = dassert(ts.tex,{'Y_t'; 'C_t'}); +%$ if t(1) +%$ t(2) = dassert(ds.freq,4); +%$ t(3) = dassert(ds.init.freq,4); +%$ t(4) = dassert(ds.init.time,[1950, 1]); +%$ t(5) = dassert(ds.vobs,2); +%$ t(6) = dassert(ds.nobs,5); +%$ t(7) = dassert(ds.name,{'Production'; 'Consumption'}); +%$ t(8) = dassert(ds.tex,{'Y_t'; 'C_t'}); %$ end %$ %$ T = all(t); %@eof:1 %@test:2 -%$ t = zeros(8,1); -%$ ts = dseries([transpose(1:5), transpose(6:10)],'1950q1',{'Output'; 'Consumption'}, {'Y_t'; 'C_t'}); -%$ try -%$ ts = ts.rename('Output','Production'); -%$ t(1) = 1; -%$ catch -%$ t = 0; -%$ end -%$ -%$ if length(t)>1 -%$ t(2) = dassert(ts.freq,4); -%$ t(3) = dassert(ts.init.freq,4); -%$ t(4) = dassert(ts.init.time,[1950, 1]); -%$ t(5) = dassert(ts.vobs,2); -%$ t(6) = dassert(ts.nobs,5); -%$ t(7) = dassert(ts.name,{'Production'; 'Consumption'}); -%$ t(8) = dassert(ts.tex,{'Y_t'; 'C_t'}); -%$ end -%$ -%$ T = all(t); -%@eof:2 - -%@test:3 -%$ t = zeros(2,1); %$ ts = dseries(randn(10,1)); %$ try -%$ ts = ts.rename('Dora'); -%$ t(1) = 1; -%$ catch -%$ t = 0; -%$ end -%$ -%$ if length(t)>1 -%$ t(2) = dassert(ts.name,{'Dora'}); -%$ end -%$ -%$ T = all(t); -%@eof:3 - -%@test:4 -%$ t = zeros(2,1); -%$ ts = dseries(randn(10,3)); -%$ try -%$ ts = ts.rename({'Dora', 'The', 'Explorer'}); +%$ ds = ts.rename('Dora'); %$ t(1) = 1; %$ catch -%$ t = 0; +%$ t(1) = 0; %$ end %$ -%$ if length(t)>1 -%$ t(2) = dassert(ts.name, {'Dora', 'The', 'Explorer'}); +%$ if t(1) +%$ t(2) = dassert(ds.name,{'Dora'}); %$ end %$ %$ T = all(t); -%@eof:4 +%@eof:2 \ No newline at end of file diff --git a/src/@dseries/rename_.m b/src/@dseries/rename_.m new file mode 100644 index 0000000000000000000000000000000000000000..76cc532e5419090813beb37ca8fa032b6146260b --- /dev/null +++ b/src/@dseries/rename_.m @@ -0,0 +1,114 @@ +function o = rename_(o, old, new) % --*-- Unitary tests --*-- + +% Renames variables in a dseries object. +% +% INPUTS +% - o [dseries] +% - old [string, cell] +% - new [string, cell] +% +% OUTPUTS +% - o [dseries] + +% Copyright (C) 2013-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 isempty(o) + error('dseries::rename: Cannot rename variable(s) because the object is empty!') +end + +if nargin<3 + if isequal(vobs(o), 1) || isequal(vobs(o), length(old)) + new = old; + else + error('dseries::rename: Missing argument!') + end + if ~ischar(new) && ~(iscellstr(new) && isequal(vobs(o), length(new))) + error(['dseries::rename: Input argument ''' ... + inputname(2) ... + ''' has to be either a string or a cellstring that has the same number of entries as observed variables!']) + end + idname = 1; +else + if ~ischar(old) || ~ischar(new) + error(['dseries::rename: Input arguments ''' inputname(2) ''' and ''' inputname(3) ''' have to be strings!']) + end + idname = find(strcmp(old,o.name)); + if isempty(idname) + error(['dseries::rename: Variable ' old ' is unknown in dseries object ' inputname(1) '!']) + end +end + +if iscellstr(new) + o.name = new; +else + o.name(idname) = {new}; +end + +%@test:1 +%$ ts = dseries([transpose(1:5), transpose(6:10)],'1950q1',{'Output'; 'Consumption'}, {'Y_t'; 'C_t'}); +%$ try +%$ ts.rename_('Output','Production'); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1)>1 +%$ t(2) = dassert(ts.freq,4); +%$ t(3) = dassert(ts.init.freq,4); +%$ t(4) = dassert(ts.init.time,[1950, 1]); +%$ t(5) = dassert(ts.vobs,2); +%$ t(6) = dassert(ts.nobs,5); +%$ t(7) = dassert(ts.name,{'Production'; 'Consumption'}); +%$ t(8) = dassert(ts.tex,{'Y_t'; 'C_t'}); +%$ end +%$ +%$ T = all(t); +%@eof:1 + +%@test:2 +%$ ts = dseries(randn(10,1)); +%$ try +%$ ts.rename_('Dora'); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1)>1 +%$ t(2) = dassert(ts.name,{'Dora'}); +%$ end +%$ +%$ T = all(t); +%@eof:2 + +%@test:3 +%$ ts = dseries(randn(10,3)); +%$ try +%$ ts.rename_({'Dora', 'The', 'Explorer'}); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(ts.name, {'Dora', 'The', 'Explorer'}); +%$ end +%$ +%$ T = all(t); +%@eof:3 diff --git a/src/@dseries/set_names.m b/src/@dseries/set_names.m index d17a2e88a5d08bb113e8808260068a6c40bad315..cf54735e38b096a3b9cdcb53381eca8240263795 100644 --- a/src/@dseries/set_names.m +++ b/src/@dseries/set_names.m @@ -1,29 +1,15 @@ -function A = set_names(B,varargin) % --*-- Unitary tests --*-- +function o = set_names(o,varargin) % --*-- Unitary tests --*-- -%@info: -%! @deftypefn {Function File} {@var{A} =} times (@var{B},@code{varargin}) -%! @anchor{@nSeries/set_names} -%! @sp 1 -%! Specify names of the variables in a @ref{dseries} object. -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @ @var -%! @item B -%! Dynare time series object instantiated by @ref{dseries}. -%! @item C -%! Dynare time series object instantiated by @ref{dseries}. -%! @end table -%! @sp 1 -%! @strong{Outputs} -%! @sp 1 -%! @table @ @var -%! @item A -%! Dynare time series object. -%! @end deftypefn -%@eod: +% Specifies names of the variables in a dseries object (in place modification). +% +% INPUTS +% - o [dseries] +% - s1, s2, s3, ... [string] +% +% OUTPUTS +% - o [dseries] -% Copyright (C) 2012 Dynare Team +% Copyright (C) 2017 Dynare Team % % This file is part of Dynare. % @@ -40,23 +26,19 @@ function A = set_names(B,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/>. -% AUTHOR(S) stephane DOT adjemian AT univ DASH lemans DOT fr - n = nargin-1; -if ~isdseries(B) - error(['dseries::rename: ' inputname(1) ' must be a dseries object!']) +if ~isdseries(o) + error(['dseries::set_names: ' inputname(1) ' must be a dseries object!']) end -if ~isequal(vobs(B),n) - error(['dseries::rename: The number of variables in ' inputname(1) ' does not match the number of declared names!']) +if ~isequal(vobs(o), n) + error(['dseries::set_names: The number of variables in ' inputname(1) ' does not match the number of declared names!']) end -A = B; - -for i=1:vobs(A) +for i=1:vobs(o) if ~isempty(varargin{i}) - A.name(i) = { varargin{i} }; + o.name(i) = { varargin{i} }; end end @@ -72,42 +54,16 @@ end %$ % Instantiate a time series object. %$ try %$ ts1 = dseries(A,[],[],[]); -%$ ts2 = set_names(ts1,'A1',[],'A3'); +%$ ts1.set_names('A1',[],'A3'); %$ t(1) = 1; %$ catch %$ t = 0; %$ end %$ %$ if length(t)>1 -%$ t(2) = dassert(ts2.vobs,3); -%$ t(3) = dassert(ts2.nobs,10); -%$ t(4) = dassert(ts2.name,A_name); -%$ end -%$ T = all(t); -%@eof:1 - -%@test:2 -%$ % Define a datasets. -%$ A = rand(10,3); -%$ -%$ % Define names -%$ A_name = {'A1';'Variable_2';'A3'}; -%$ -%$ t = zeros(4,1); -%$ -%$ % Instantiate a time series object. -%$ try -%$ ts1 = dseries(A,[],[],[]); -%$ ts1 = ts1.set_names('A1',[],'A3'); -%$ t(1) = 1; -%$ catch -%$ t = 0; -%$ end -%$ -%$ if length(t)>1 %$ t(2) = dassert(ts1.vobs,3); %$ t(3) = dassert(ts1.nobs,10); %$ t(4) = dassert(ts1.name,A_name); %$ end %$ T = all(t); -%@eof:2 +%@eof:1 \ No newline at end of file diff --git a/src/@dseries/size.m b/src/@dseries/size.m index c83090b51258e2c8afd4e370848b383bc4be82be..22b8ad4c8addfd8037bdba46c2544e671ecc4c9f 100644 --- a/src/@dseries/size.m +++ b/src/@dseries/size.m @@ -1,6 +1,8 @@ function varargout = size(o, varargin) -% Copyright (C) 2013-2014 Dynare Team +% Overloads size function. + +% Copyright (C) 2013-2017 Dynare Team % % This file is part of Dynare. % @@ -31,4 +33,21 @@ switch nargout end otherwise error('dseries::size: Wrong calling sequence! Cannot return more than two arguments.') -end \ No newline at end of file +end + +%@test:2 +%$ ts = dseries(randn(10,1)); +%$ try +%$ dimension = ts.size(); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(dimension(1), 10); +%$ t(3) = dassert(dimension(2), 1); +%$ end +%$ +%$ T = all(t); +%@eof:2 \ No newline at end of file diff --git a/src/@dseries/std.m b/src/@dseries/std.m index 7cedf390d8826e56586426cb8e488f469f42b475..cdbe7c05304e8466daee017ac466e16b46a12680 100644 --- a/src/@dseries/std.m +++ b/src/@dseries/std.m @@ -4,13 +4,13 @@ function s = std(o, geometric) % --*-- Unitary tests --*-- % See https://en.wikipedia.org/wiki/Geometric_standard_deviation % % INPUTS -% o o dseries object [mandatory]. -% o geometric logical [default is false], if true returns the geometric standard deviation. +% - o [dseries] T observations and N variables. +% - geometric [logical] if true returns the geometric standard deviation (default is false). % % OUTPUTS -% o s 1*vobs(o) vector of doubles. +% - s [double] 1*N vector. -% Copyright (C) 2016 Dynare Team +% Copyright (C) 2016-2017 Dynare Team % % This file is part of Dynare. % diff --git a/src/@dseries/subsref.m b/src/@dseries/subsref.m index be86968515f338166b61ae9d9f5a1ff07d5b299e..4c28f355e68c0621eb99efd404c1b666d21de07e 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','log_','exp','exp_','ygrowth','qgrowth','ydiff','qdiff','abs','isnan','firstdate','firstobservedperiod'} % Give "dot access" to public methods without args. + case {'log','log_','exp','exp_','ygrowth','ygrowth_','qgrowth','qgrowth_','ydiff','ydiff_','qdiff','qdiff_','abs','abs_','isnan','firstdate','firstobservedperiod'} % 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); @@ -90,7 +90,7 @@ switch S(1).type case 'freq' % Returns an integer characterizing the data frequency (1, 4, 12 or 52) B = A.dates.freq; - case {'lag','lead','hptrend','hpcycle','chain','detrend','exist','mean','std','center'} % Methods with less than two arguments. + case {'lag','lag_','lead','lead_','hptrend','hptrend_','hpcycle','hpcycle_','chain','chain_','detrend','detrend_','exist','mean','std','center','center_'} % Methods with less than two arguments. if length(S)>1 && isequal(S(2).type,'()') if isempty(S(2).subs) B = feval(S(1).subs,A); @@ -105,7 +105,7 @@ switch S(1).type else B = feval(S(1).subs,A); end - case {'cumsum','insert','pop','cumprod','remove'} % Methods with less than three argument. + case {'cumsum','cumsum_','insert','pop','pop_','cumprod','cumprod_','remove','remove_'} % Methods with less than three argument. if length(S)>1 && isequal(S(2).type,'()') if isempty(S(2).subs) B = feval(S(1).subs,A); @@ -120,7 +120,7 @@ switch S(1).type else B = feval(S(1).subs,A); end - case 'baxter_king_filter' + case {'baxter_king_filter', 'baxter_king_filter_'} if length(S)>1 && isequal(S(2).type,'()') if isempty(S(2).subs) B = feval(S(1).subs,A); @@ -169,7 +169,7 @@ switch S(1).type else error('dseries::subsref: Call to size method must come in last position!') end - case {'set_names','rename','tex_rename'} + case {'set_names','rename','rename_','tex_rename','tex_rename_'} B = feval(S(1).subs,A,S(2).subs{:}); S = shiftS(S,1); case {'disp'} diff --git a/src/@dseries/tex_rename.m b/src/@dseries/tex_rename.m index 801b8df789187607821cd09a19b4e505258e5af3..c903fc86c156b06bb5a49aefec2504335ea58483 100644 --- a/src/@dseries/tex_rename.m +++ b/src/@dseries/tex_rename.m @@ -1,4 +1,4 @@ -function ts = tex_rename(ts, varargin) % --*-- Unitary tests --*-- +function o = tex_rename(o, varargin) % --*-- Unitary tests --*-- % Copyright (C) 2013-2015 Dynare Team % @@ -17,96 +17,28 @@ function ts = tex_rename(ts, 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/>. -assert(nargin <= 3, 'dseries::tex_rename: accepts at most three args'); - -if nargin == 2 - newtexname = varargin{1}; - assert(vobs(ts) == 1 || vobs(ts) == length(newtexname), ... - 'dseries::tex_rename: with one argument, the dseries contain only one variable.'); -else - newtexname = varargin{2}; - name = varargin{1}; - assert(ischar(name), 'dseries::tex_rename: second input argument (name) must be a string'); -end - -assert(ischar(newtexname) || (iscellstr(newtexname) && length(newtexname) == vobs(ts)), ... - ['dseries::tex_rename: third input argument (newtexname) name must either be a string' ... - ' or a cell array of strings with the same number of entries as varibles in the dseries']); - -if nargin == 2 - idname = 1; -else - idname = find(strcmp(name, ts.name)); - if isempty(idname) - error(['dseries::tex_rename: Variable ' name ' is unknown in dseries object ' inputname(1) '!']) - end -end - -if iscellstr(newtexname) - ts.tex = newtexname; -else - ts.tex(idname) = {newtexname}; -end +o = copy(o); +o.tex_rename_(varargin{:}); %@test:1 -%$ t = zeros(8,1); %$ ts = dseries([transpose(1:5), transpose(6:10)],'1950q1',{'Output'; 'Consumption'}, {'Y_t'; 'C_t'}); %$ try -%$ ts = tex_rename(ts,'Output','\\Delta Y_t'); -%$ t(1) = 1; -%$ catch -%$ t = 0; -%$ end -%$ -%$ if length(t)>1 -%$ t(2) = dassert(ts.freq,4); -%$ t(3) = dassert(ts.init.freq,4); -%$ t(4) = dassert(ts.init.time,[1950, 1]); -%$ t(5) = dassert(ts.vobs,2); -%$ t(6) = dassert(ts.nobs,5); -%$ t(7) = dassert(ts.name,{'Output'; 'Consumption'}); -%$ t(8) = dassert(ts.tex,{'\\Delta Y_t'; 'C_t'}); -%$ end -%$ -%$ T = all(t); -%@eof:1 - -%@test:2 -%$ t = zeros(8,1); -%$ ts = dseries([transpose(1:5), transpose(6:10)],'1950q1',{'Output'; 'Consumption'}, {'Y_t'; 'C_t'}); -%$ try -%$ ts = ts.tex_rename('Output','\\Delta Y_t'); -%$ t(1) = 1; -%$ catch -%$ t = 0; -%$ end -%$ -%$ if length(t)>1 -%$ t(2) = dassert(ts.freq,4); -%$ t(3) = dassert(ts.init.freq,4); -%$ t(4) = dassert(ts.init.time,[1950, 1]); -%$ t(5) = dassert(ts.vobs,2); -%$ t(6) = dassert(ts.nobs,5); -%$ t(7) = dassert(ts.name,{'Output'; 'Consumption'}); -%$ t(8) = dassert(ts.tex,{'\\Delta Y_t'; 'C_t'}); -%$ end -%$ -%$ T = all(t); -%@eof:2 - -%@test:3 -%$ t = zeros(2,1); -%$ ts = dseries(rand(3,3)); -%$ try -%$ ts = ts.tex_rename({'Output','\\Delta Y_t','\\theta_{-1}'}); +%$ ds = ts.tex_rename('Output','\\Delta Y_t'); %$ t(1) = 1; %$ catch -%$ t = 0; +%$ t(1) = 0; %$ end %$ -%$ if length(t)>1 -%$ t(2) = dassert(ts.tex,{'Output','\\Delta Y_t','\\theta_{-1}'}); +%$ if t(1) +%$ t(2) = dassert(ds.freq,4); +%$ t(3) = dassert(ds.init.freq,4); +%$ t(4) = dassert(ds.init.time,[1950, 1]); +%$ t(5) = dassert(ds.vobs,2); +%$ t(6) = dassert(ds.nobs,5); +%$ t(7) = dassert(ds.name,{'Output'; 'Consumption'}); +%$ t(8) = dassert(ds.tex,{'\\Delta Y_t'; 'C_t'}); +%$ t(9) = dassert(ts.tex,{'Y_t'; 'C_t'}); %$ end %$ %$ T = all(t); -%@eof:3 +%@eof:1 \ No newline at end of file diff --git a/src/@dseries/tex_rename_.m b/src/@dseries/tex_rename_.m new file mode 100644 index 0000000000000000000000000000000000000000..33450efe51f9c4d4367c138c19f159dbe88a0192 --- /dev/null +++ b/src/@dseries/tex_rename_.m @@ -0,0 +1,103 @@ +function o = tex_rename_(o, varargin) % --*-- Unitary tests --*-- + +% Changes TeX name of variables in a dseries object. +% +% INPUTS +% - o [dseries] +% - names_1 [string, cell] +% - names_2 [string] +% +% OUTPUTS +% - o [dseries] +% +% REMARKS +% If varargin has only one element (string or cell of strings) then this input defines the new +% texnames for all the variables in dseries object o. If varargin has two elements (strings) the +% first element is the name of the variable and the second argument is the new texname for this +% variable. + +% Copyright (C) 2013-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/>. + +assert(nargin <= 3, 'dseries::tex_rename: accepts at most three args'); + +if nargin == 2 + newtexname = varargin{1}; + assert(vobs(o) == 1 || vobs(o) == length(newtexname), ... + 'dseries::tex_rename: with one argument, the dseries contain only one variable.'); +else + newtexname = varargin{2}; + name = varargin{1}; + assert(ischar(name), 'dseries::tex_rename: second input argument (name) must be a string'); +end + +assert(ischar(newtexname) || (iscellstr(newtexname) && length(newtexname) == vobs(o)), ... + ['dseries::tex_rename: third input argument (newtexname) name must either be a string' ... + ' or a cell array of strings with the same number of entries as varibles in the dseries']); + +if nargin == 2 + idname = 1; +else + idname = find(strcmp(name, o.name)); + if isempty(idname) + error(['dseries::tex_rename: Variable ' name ' is unknown in dseries object ' inputname(1) '!']) + end +end + +if iscellstr(newtexname) + o.tex = newtexname; +else + o.tex(idname) = {newtexname}; +end + +%@test:1 +%$ ts = dseries([transpose(1:5), transpose(6:10)],'1950q1',{'Output'; 'Consumption'}, {'Y_t'; 'C_t'}); +%$ try +%$ ts.tex_rename_('Output','\\Delta Y_t'); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(ts.freq,4); +%$ t(3) = dassert(ts.init.freq,4); +%$ t(4) = dassert(ts.init.time,[1950, 1]); +%$ t(5) = dassert(ts.vobs,2); +%$ t(6) = dassert(ts.nobs,5); +%$ t(7) = dassert(ts.name,{'Output'; 'Consumption'}); +%$ t(8) = dassert(ts.tex,{'\\Delta Y_t'; 'C_t'}); +%$ end +%$ +%$ T = all(t); +%@eof:1 + +%@test:3 +%$ ts = dseries(rand(3,3)); +%$ try +%$ ts.tex_rename_({'Output','\\Delta Y_t','\\theta_{-1}'}); +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end +%$ +%$ if t(1) +%$ t(2) = dassert(ts.tex,{'Output','\\Delta Y_t','\\theta_{-1}'}); +%$ end +%$ +%$ T = all(t); +%@eof:3 diff --git a/src/@dseries/uminus.m b/src/@dseries/uminus.m index 6f0185311cae54dc8e61e8f903cc675c42e9cdfe..33fed6c44f7a559eefad51b8d709a0fae5361b1c 100644 --- a/src/@dseries/uminus.m +++ b/src/@dseries/uminus.m @@ -1,29 +1,14 @@ -function A = uminus(B) % --*-- Unitary tests --*-- +function o = uminus(o) % --*-- Unitary tests --*-- -%@info: -%! @deftypefn {Function File} {@var{A} =} plus (@var{B},@var{C}) -%! @anchor{@dseries/uminus} -%! @sp 1 -%! Overloads the uminus method for the Dynare time series class (@ref{dseries}). -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @ @var -%! @item B -%! Dynare time series object instantiated by @ref{dseries}. -%! @item C -%! Dynare time series object instantiated by @ref{dseries}. -%! @end table -%! @sp 1 -%! @strong{Outputs} -%! @sp 1 -%! @table @ @var -%! @item A -%! Dynare time series object. -%! @end deftypefn -%@eod: +% Overloads the uminus method for dseries objects. +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - o [dseries] -% Copyright (C) 2012-2013 Dynare Team +% Copyright (C) 2012-2017 Dynare Team % % This file is part of Dynare. % @@ -40,16 +25,13 @@ function A = uminus(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/>. -A = dseries(); +o = copy(o); -A.data = -(B.data); -A.dates = B.dates; +o.data = -(o.data); -A.name = cell(vobs(A),1); -A.tex = cell(vobs(A),1); -for i = 1:vobs(A) - A.name(i) = {[ '-' B.name{i}]}; - A.tex(i) = {[ '-' B.tex{i}]}; +for i = 1:vobs(o) + o.name(i) = {[ '-' o.name{i}]}; + o.tex(i) = {[ '-' o.tex{i}]}; end %@test:1 diff --git a/src/@dseries/vertcat.m b/src/@dseries/vertcat.m index 36aa733bdf02696a6f762d008738fd0265f09149..17d684f394d148a1b1a1e2d1f2a1fb428e98f487 100644 --- a/src/@dseries/vertcat.m +++ b/src/@dseries/vertcat.m @@ -1,36 +1,23 @@ -function a = vertcat(varargin) % --*-- Unitary tests --*-- +function o = vertcat(varargin) % --*-- Unitary tests --*-- -%@info: -%! @deftypefn {Function file} {@var{a} =} vertcat (@var{b},@var{c}, ...) -%! @anchor{horzcat} -%! @sp 1 -%! Method of the dseries class. -%! @sp 1 -%! This method overloads the vertical concatenation operator, so that -%! two (or more) time series objects containing the same variables -%! can be merged using the following syntax: -%! -%! a = [b; c; d]; -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @ @var -%! @item b -%! Dynare time series object, instantiated by @ref{dseries}. -%! @item c -%! Dynare time series object, instantiated by @ref{dseries}. -%! @end table -%! @sp 2 -%! @strong{Outputs} -%! @sp 1 -%! @table @var -%! @item a -%! Dynare time series object. -%! @end table -%! @end deftypefn -%@eod: +% Overloads vertcat method for dseries objects. +% +% INPUTS +% o o1 dseries object. +% o o2 dseries object. +% o ... +% +% OUTPUTS +% o o dseries object. +% +% EXAMPLE 1 +% If o1, o2 and o3 are dseries objects containing the same variables over different samples, the following syntax: +% +% o = [o1; o2; o3] ; +% +% merges the samples. -% Copyright (C) 2013 Dynare Team +% Copyright (C) 2013-2017 Dynare Team % % This file is part of Dynare. % @@ -48,13 +35,13 @@ function a = vertcat(varargin) % --*-- Unitary tests --*-- % along with Dynare. If not, see <http://www.gnu.org/licenses/>. if nargin==0 - a = DynSeries(); + o = dseries(); elseif nargin == 1 - a = varargin{1}; + o = varargin{1}; elseif nargin>1 - a = varargin{1}; + o = varargin{1}; for i=2:nargin - a = vertcat_(a,varargin{i}); + o = vertcat_(o, varargin{i}); end end @@ -103,15 +90,21 @@ function d = vertcat_(b, c) %$ ts2 = dseries(B,[],B_name,[]); %$ %$ % Call the tested method. -%$ ts3 = [ts1;ts2]; +%$ try +%$ ts3 = [ts1;ts2]; +%$ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end %$ %$ % Check the results. -%$ -%$ t(1) = dassert(ts3.init,e.init); -%$ t(2) = dassert(ts3.freq,e.freq); -%$ t(3) = dassert(ts3.data,e.data); -%$ t(4) = dassert(ts3.name,e.name); -%$ t(5) = dassert(ts3.nobs,20); +%$ if t(1) +%$ t(2) = dassert(ts3.init,e.init); +%$ t(3) = dassert(ts3.freq,e.freq); +%$ t(4) = dassert(ts3.data,e.data); +%$ t(5) = dassert(ts3.name,e.name); +%$ t(6) = dassert(ts3.nobs,20); +%$ end %$ T = all(t); %@eof:1 @@ -139,15 +132,22 @@ function d = vertcat_(b, c) %$ ts3 = dseries(C,[],C_name,[]); %$ %$ % Call the tested method. -%$ ts4 = [ts1; ts2; ts3]; +%$ try +%$ ts4 = [ts1; ts2; ts3]; +%£ t(1) = 1; +%$ catch +%$ t(1) = 0; +%$ end %$ -%$ % Check the results. %$ -%$ t(1) = dassert(ts4.init,e.init); -%$ t(2) = dassert(ts4.freq,e.freq); -%$ t(3) = dassert(ts4.data,e.data); -%$ t(4) = dassert(ts4.name,e.name); -%$ t(5) = dassert(ts4.nobs,30); +%$ % Check the results. +%$ if t(1) +%$ t(2) = dassert(ts4.init,e.init); +%$ t(3) = dassert(ts4.freq,e.freq); +%$ t(4) = dassert(ts4.data,e.data); +%$ t(5) = dassert(ts4.name,e.name); +%$ t(6) = dassert(ts4.nobs,30); +%$ end %$ T = all(t); %@eof:2 diff --git a/src/@dseries/vobs.m b/src/@dseries/vobs.m index 37c1c80dd31eb48cfdbeda31a70e8ef5988fc33e..5af584706d93cac1e3a01f29690d4b5a9b82024a 100644 --- a/src/@dseries/vobs.m +++ b/src/@dseries/vobs.m @@ -1,8 +1,14 @@ -function s = vobs(ts) +function s = vobs(o) % Returns the number of variables in a @dseries object. +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - s [integer] scalar, number of variables in o. -% Copyright (C) 2014 Dynare Team +% Copyright (C) 2014-2017 Dynare Team % % This file is part of Dynare. % @@ -19,4 +25,4 @@ function s = vobs(ts) % You should have received a copy of the GNU General Public License % along with Dynare. If not, see <http://www.gnu.org/licenses/>. -s = columns(ts.data); \ No newline at end of file +s = columns(o.data); \ No newline at end of file diff --git a/src/@dseries/ydiff.m b/src/@dseries/ydiff.m index 46159b9258e0bab20a4da1400f7610eb1ce9279c..e2983c3708dfc2e62d695f5976867d19d2f13f3b 100644 --- a/src/@dseries/ydiff.m +++ b/src/@dseries/ydiff.m @@ -1,28 +1,14 @@ -function us = ydiff(ts) % --*-- Unitary tests --*-- +function o = ydiff(o) % --*-- Unitary tests --*-- -%@info: -%! @deftypefn {Function File} {@var{us} =} ydiff (@var{ts}) -%! @anchor{ydiff} -%! @sp 1 -%! Computes annual differences. -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @var -%! @item ts -%! Dynare time series object, instantiated by @ref{dseries} -%! @end table -%! @sp 2 -%! @strong{Outputs} -%! @sp 1 -%! @table @var -%! @item us -%! Dynare time series object with transformed data field. -%! @end table -%! @end deftypefn -%@eod: +% Computes yearly differences. +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - o [dseries] -% Copyright (C) 2012-2015 Dynare Team +% Copyright (C) 2012-2017 Dynare Team % % This file is part of Dynare. % @@ -39,133 +25,28 @@ function us = ydiff(ts) % --*-- 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/>. -us = ts; - -switch frequency(ts) - case 1 - us.data(2:end,:) = ts.data(2:end,:)-ts.data(1:end-1,:); - us.data(1,:) = NaN; - for i = 1:vobs(ts) - us.name(i) = {['ydiff(' us.name{i} ')']}; - us.tex(i) = {['\Delta ' us.tex{i}]}; - end - case 4 - us.data(5:end,:) = ts.data(5:end,:)-ts.data(1:end-4,:); - us.data(1:4,:) = NaN; - for i = 1:vobs(ts) - us.name(i) = {['ydiff(' us.name{i} ')']}; - us.tex(i) = {['\Delta_4 ' us.tex{i}]}; - end - case 12 - us.data(13:end,:) = ts.data(13:end,:)-ts.data(1:end-12,:); - us.data(1:12,:) = NaN; - for i = 1:vobs(ts) - us.name(i) = {['ydiff(' us.name{i} ')']}; - us.tex(i) = {['\Delta_{12} ' us.tex{i}]}; - end - case 52 - us.data(53:end,:) = ts.data(53:end,:)-ts.data(1:end-52,:); - us.data(1:52,:) = NaN; - for i = 1:vobs(ts) - us.name(i) = {['ydiff(' us.name{i} ')']}; - us.tex(i) = {['\Delta_{52} ' us.tex{i}]}; - end - otherwise - error(['dseries::ydiff: object ' inputname(1) ' has unknown frequency']); -end +o = copy(o); +o.ydiff_(); %@test:1 -%$ t = zeros(4,1); -%$ %$ try %$ data = transpose(1:100); %$ ts = dseries(data,'1950Q1',{'A1'},{'A_1'}); -%$ ts = ts.ydiff; -%$ t(1) = 1; +%$ ds = ts.ydiff(); +%$ t(1) = true; %$ catch -%$ t = 0; +%$ t(1) = false; %$ end %$ %$ -%$ if length(t)>1 +%$ if t(1) %$ DATA = NaN(4,ts.vobs); %$ DATA = [DATA; 4*ones(ts.nobs-4,ts.vobs)]; -%$ t(2) = dassert(ts.data,DATA); -%$ t(3) = dassert(ts.name{1},['ydiff(A1)']); -%$ t(4) = dassert(ts.tex{1},['\\Delta_4 A_1']); -%$ end -%$ -%$ T = all(t); -%@eof:1 - -%@test:2 -%$ t = zeros(4,1); -%$ -%$ try -%$ data = transpose(1:100); -%$ ts = dseries(data,'1950M1',{'A1'},{'A_1'}); -%$ ts = ts.ydiff; -%$ t(1) = 1; -%$ catch -%$ t = 0; -%$ end -%$ -%$ -%$ if length(t)>1 -%$ DATA = NaN(12,ts.vobs); -%$ DATA = [DATA; 12*ones(ts.nobs-12,ts.vobs)]; -%$ t(2) = dassert(ts.data,DATA); -%$ t(3) = dassert(ts.name{1},['ydiff(A1)']); -%$ t(4) = dassert(ts.tex{1},['\\Delta_{12} A_1']); -%$ end -%$ -%$ T = all(t); -%@eof:2 - -%@test:3 -%$ t = zeros(2,1); -%$ -%$ try -%$ data = transpose(1:100); -%$ ts = dseries(data,'1950W1',{'A1'},{'A_1'}); -%$ ts = ts.ydiff; -%$ t(1) = 1; -%$ catch -%$ t = 0; -%$ end -%$ -%$ -%$ if length(t)>1 -%$ DATA = NaN(52,ts.vobs); -%$ DATA = [DATA; 52*ones(ts.nobs-52,ts.vobs)]; -%$ t(2) = dassert(ts.data,DATA); -%$ t(3) = dassert(ts.name{1},['ydiff(A1)']); -%$ t(4) = dassert(ts.tex{1},['\\Delta_{52} A_1']); -%$ end -%$ -%$ T = all(t); -%@eof:3 - -%@test:4 -%$ t = zeros(4,1); -%$ -%$ try -%$ data = transpose(1:100); -%$ ts = dseries(data,'1950Y',{'A1'},{'A_1'}); -%$ ts = ts.ydiff; -%$ t(1) = 1; -%$ catch -%$ t = 0; -%$ end -%$ -%$ -%$ if length(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.name{1},['ydiff(A1)']); -%$ t(4) = dassert(ts.tex{1},['\\Delta A_1']); +%$ t(2) = dassert(ds.data,DATA); +%$ t(3) = dassert(ds.name{1},['ydiff(A1)']); +%$ t(4) = dassert(ds.tex{1},['\\Delta_4 A_1']); +%$ t(5) = dassert(ts.data, data); %$ end %$ %$ T = all(t); -%@eof:4 +%@eof:1 \ No newline at end of file diff --git a/src/@dseries/ydiff_.m b/src/@dseries/ydiff_.m new file mode 100644 index 0000000000000000000000000000000000000000..d11d86f4eeb1ae0cbd0a1de8e271ec56d55ae389 --- /dev/null +++ b/src/@dseries/ydiff_.m @@ -0,0 +1,146 @@ +function o = ydiff_(o) % --*-- Unitary tests --*-- + +% Computes yearly differences. +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - o [dseries] + +% Copyright (C) 2012-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/>. + +switch frequency(o) + case 1 + o.data(2:end,:) = o.data(2:end,:)-o.data(1:end-1,:); + o.data(1,:) = NaN; + for i = 1:vobs(o) + o.name(i) = {['ydiff(' o.name{i} ')']}; + o.tex(i) = {['\Delta ' o.tex{i}]}; + end + case 4 + o.data(5:end,:) = o.data(5:end,:)-o.data(1:end-4,:); + o.data(1:4,:) = NaN; + for i = 1:vobs(o) + o.name(i) = {['ydiff(' o.name{i} ')']}; + o.tex(i) = {['\Delta_4 ' o.tex{i}]}; + end + case 12 + o.data(13:end,:) = o.data(13:end,:)-o.data(1:end-12,:); + o.data(1:12,:) = NaN; + for i = 1:vobs(o) + o.name(i) = {['ydiff(' o.name{i} ')']}; + o.tex(i) = {['\Delta_{12} ' o.tex{i}]}; + end + case 52 + o.data(53:end,:) = o.data(53:end,:)-o.data(1:end-52,:); + o.data(1:52,:) = NaN; + for i = 1:vobs(o) + o.name(i) = {['ydiff(' o.name{i} ')']}; + o.tex(i) = {['\Delta_{52} ' o.tex{i}]}; + end + otherwise + error(['dseries::ydiff: object ' inputname(1) ' has unknown frequency']); +end + +%@test:1 +%$ try +%$ data = transpose(1:100); +%$ ts = dseries(data,'1950Q1',{'A1'},{'A_1'}); +%$ ts.ydiff_; +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ +%$ if t(1) +%$ DATA = NaN(4,ts.vobs); +%$ DATA = [DATA; 4*ones(ts.nobs-4,ts.vobs)]; +%$ t(2) = dassert(ts.data,DATA); +%$ t(3) = dassert(ts.name{1},['ydiff(A1)']); +%$ t(4) = dassert(ts.tex{1},['\\Delta_4 A_1']); +%$ end +%$ +%$ T = all(t); +%@eof:1 + +%@test:2 +%$ try +%$ data = transpose(1:100); +%$ ts = dseries(data,'1950M1',{'A1'},{'A_1'}); +%$ ts.ydiff_; +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ +%$ if t(1) +%$ DATA = NaN(12,ts.vobs); +%$ DATA = [DATA; 12*ones(ts.nobs-12,ts.vobs)]; +%$ t(2) = dassert(ts.data,DATA); +%$ t(3) = dassert(ts.name{1},['ydiff(A1)']); +%$ t(4) = dassert(ts.tex{1},['\\Delta_{12} A_1']); +%$ end +%$ +%$ T = all(t); +%@eof:2 + +%@test:3 +%$ try +%$ data = transpose(1:100); +%$ ts = dseries(data,'1950W1',{'A1'},{'A_1'}); +%$ ts.ydiff_; +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ if t(1) +%$ DATA = NaN(52,ts.vobs); +%$ DATA = [DATA; 52*ones(ts.nobs-52,ts.vobs)]; +%$ t(2) = dassert(ts.data,DATA); +%$ t(3) = dassert(ts.name{1},['ydiff(A1)']); +%$ t(4) = dassert(ts.tex{1},['\\Delta_{52} A_1']); +%$ end +%$ +%$ T = all(t); +%@eof:3 + +%@test:4 +%$ try +%$ data = transpose(1:100); +%$ ts = dseries(data,'1950Y',{'A1'},{'A_1'}); +%$ ts.ydiff_; +%$ 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.name{1},['ydiff(A1)']); +%$ t(4) = dassert(ts.tex{1},['\\Delta A_1']); +%$ end +%$ +%$ T = all(t); +%@eof:4 diff --git a/src/@dseries/ygrowth.m b/src/@dseries/ygrowth.m index bc5deeb58a77a26bdf7e463082a8af5930084f99..aba3a062dd71fcf045aba9350bd4a9a70d4e1df3 100644 --- a/src/@dseries/ygrowth.m +++ b/src/@dseries/ygrowth.m @@ -1,28 +1,14 @@ -function us = ygrowth(ts) % --*-- Unitary tests --*-- +function o = ygrowth(o) % --*-- Unitary tests --*-- -%@info: -%! @deftypefn {Function File} {@var{us} =} ygrowth (@var{ts}) -%! @anchor{ygrowth} -%! @sp 1 -%! Computes annual growth rates. -%! @sp 2 -%! @strong{Inputs} -%! @sp 1 -%! @table @var -%! @item ts -%! Dynare time series object, instantiated by @ref{dseries} -%! @end table -%! @sp 2 -%! @strong{Outputs} -%! @sp 1 -%! @table @var -%! @item us -%! Dynare time series object with transformed data field. -%! @end table -%! @end deftypefn -%@eod: +% Computes yearly growth rates. +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - o [dseries] -% Copyright (C) 2012-2013 Dynare Team +% Copyright (C) 2012-2017 Dynare Team % % This file is part of Dynare. % @@ -39,40 +25,8 @@ function us = ygrowth(ts) % --*-- 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/>. -us = ts; - -switch frequency(ts) - case 1 - us.data(2:end,:) = ts.data(2:end,:)./ts.data(1:end-1,:) - 1; - us.data(1,:) = NaN; - for i = 1:vobs(ts) - us.name(i) = {['ygrowth(' us.name{i} ')']}; - us.tex(i) = {['\delta ' us.tex{i}]}; - end - case 4 - us.data(5:end,:) = ts.data(5:end,:)./ts.data(1:end-4,:) - 1; - us.data(1:4,:) = NaN; - for i = 1:vobs(ts) - us.name(i) = {['ygrowth(' us.name{i} ')']}; - us.tex(i) = {['\delta_4 ' us.tex{i}]}; - end - case 12 - us.data(13:end,:) = ts.data(13:end,:)./ts.data(1:end-12,:) - 1; - us.data(1:12,:) = NaN; - for i = 1:vobs(ts) - us.name(i) = {['ygrowth(' us.name{i} ')']}; - us.tex(i) = {['\delta_{12} ' us.tex{i}]}; - end - case 52 - us.data(53:end,:) = ts.data(53:end,:)./ts.data(1:end-52,:) - 1; - us.data(1:52,:) = NaN; - for i = 1:vobs(ts) - us.name(i) = {['ygrowth(' us.name{i} ')']}; - us.tex(i) = {['\delta_{52} ' us.tex{i}]}; - end - otherwise - error(['dseries::ygrowth: object ' inputname(1) ' has unknown frequency']); -end +o = copy(o); +o.ygrowth_(); %@test:1 %$ t = zeros(2,1); @@ -80,7 +34,7 @@ end %$ try %$ data = repmat(transpose(1:4),100,1); %$ ts = dseries(data,'1950Q1'); -%$ ts = ts.ygrowth; +%$ ds = ts.ygrowth; %$ t(1) = 1; %$ catch %$ t = 0; @@ -90,52 +44,9 @@ end %$ if length(t)>1 %$ DATA = NaN(4,ts.vobs); %$ DATA = [DATA; zeros(ts.nobs-4,ts.vobs)]; -%$ t(2) = dassert(ts.data,DATA); -%$ end -%$ -%$ T = all(t); -%@eof:1 - -%@test:2 -%$ t = zeros(2,1); -%$ -%$ try -%$ data = repmat(transpose(1:12),100,1); -%$ ts = dseries(data,'1950M1'); -%$ ts = ts.ygrowth; -%$ t(1) = 1; -%$ catch -%$ t = 0; -%$ end -%$ -%$ -%$ if length(t)>1 -%$ DATA = NaN(12,ts.vobs); -%$ DATA = [DATA; zeros(ts.nobs-12,ts.vobs)]; -%$ t(2) = dassert(ts.data,DATA); -%$ end -%$ -%$ T = all(t); -%@eof:2 - -%@test:3 -%$ t = zeros(2,1); -%$ -%$ try -%$ data = repmat(transpose(1:52),100,1); -%$ ts = dseries(data,'1950W1'); -%$ ts = ts.ygrowth; -%$ t(1) = 1; -%$ catch -%$ t = 0; -%$ end -%$ -%$ -%$ if length(t)>1 -%$ DATA = NaN(52,ts.vobs); -%$ DATA = [DATA; zeros(ts.nobs-52,ts.vobs)]; -%$ t(2) = dassert(ts.data,DATA); +%$ t(2) = dassert(ds.data,DATA); +%$ t(3) = dassert(ts.data,data); %$ end %$ %$ T = all(t); -%@eof:3 +%@eof:1 \ No newline at end of file diff --git a/src/@dseries/ygrowth_.m b/src/@dseries/ygrowth_.m new file mode 100644 index 0000000000000000000000000000000000000000..aa9934cc41560b01b3a76448ac35ff7e97b7ccea --- /dev/null +++ b/src/@dseries/ygrowth_.m @@ -0,0 +1,118 @@ +function o = ygrowth_(o) % --*-- Unitary tests --*-- + +% Computes yearly growth rates. +% +% INPUTS +% - o [dseries] +% +% OUTPUTS +% - o [dseries] + +% Copyright (C) 2012-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/>. + +switch frequency(o) + case 1 + o.data(2:end,:) = o.data(2:end,:)./o.data(1:end-1,:) - 1; + o.data(1,:) = NaN; + for i = 1:vobs(o) + o.name(i) = {['ygrowth(' o.name{i} ')']}; + o.tex(i) = {['\delta ' o.tex{i}]}; + end + case 4 + o.data(5:end,:) = o.data(5:end,:)./o.data(1:end-4,:) - 1; + o.data(1:4,:) = NaN; + for i = 1:vobs(o) + o.name(i) = {['ygrowth(' o.name{i} ')']}; + o.tex(i) = {['\delta_4 ' o.tex{i}]}; + end + case 12 + o.data(13:end,:) = o.data(13:end,:)./o.data(1:end-12,:) - 1; + o.data(1:12,:) = NaN; + for i = 1:vobs(o) + o.name(i) = {['ygrowth(' o.name{i} ')']}; + o.tex(i) = {['\delta_{12} ' o.tex{i}]}; + end + case 52 + o.data(53:end,:) = o.data(53:end,:)./o.data(1:end-52,:) - 1; + o.data(1:52,:) = NaN; + for i = 1:vobs(o) + o.name(i) = {['ygrowth(' o.name{i} ')']}; + o.tex(i) = {['\delta_{52} ' o.tex{i}]}; + end + otherwise + error(['dseries::ygrowth: object ' inputname(1) ' has unknown frequency']); +end + +%@test:1 +%$ try +%$ data = repmat(transpose(1:4),100,1); +%$ ts = dseries(data,'1950Q1'); +%$ ts.ygrowth_; +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ +%$ if t(1) +%$ DATA = NaN(4,ts.vobs); +%$ DATA = [DATA; zeros(ts.nobs-4,ts.vobs)]; +%$ t(2) = dassert(ts.data,DATA); +%$ end +%$ +%$ T = all(t); +%@eof:1 + +%@test:2 +%$ try +%$ data = repmat(transpose(1:12),100,1); +%$ ts = dseries(data,'1950M1'); +%$ ts.ygrowth_(); +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ if t(1) +%$ DATA = NaN(12,ts.vobs); +%$ DATA = [DATA; zeros(ts.nobs-12,ts.vobs)]; +%$ t(2) = dassert(ts.data,DATA); +%$ end +%$ +%$ T = all(t); +%@eof:2 + +%@test:3 +%$ try +%$ data = repmat(transpose(1:52),100,1); +%$ ts = dseries(data,'1950W1'); +%$ ts.ygrowth_(); +%$ t(1) = true; +%$ catch +%$ t(1) = false; +%$ end +%$ +%$ +%$ if t(1) +%$ DATA = NaN(52,ts.vobs); +%$ DATA = [DATA; zeros(ts.nobs-52,ts.vobs)]; +%$ t(2) = dassert(ts.data,DATA); +%$ end +%$ +%$ T = all(t); +%@eof:3