Added mean and std methods.

Returns the arithmetic or geometric mean and standard deviation.
parent 85868195
function m = mean(o, geometric) % --*-- Unitary tests --*--
% Returns the mean of the variables in a @dseries object o.
%
% INPUTS
% o o dseries object [mandatory].
% o geometric logical [default is false], if true returns the geometric mean.
%
% OUTPUTS
% o m 1*vobs(o) vector of doubles.
% Copyright (C) 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/>.
if nargin<2
geometric = false;
end
if geometric
m = prod(o.data, 1).^(1/nobs(o));
else
m = mean(o.data);
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);
%$ m = mean(ts, true);
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(isequal(size(m),[1, 2]), true);
%$ t(3) = dassert(m, [1.005, 1.05]);
%$ end
%$ T = all(t);
%@eof:1
%@test:2
%$ % Define a dataset.
%$ A = repmat([1.005, 1.05], 10, 1);
%$
%$ % Instantiate a time series object and compute the mean.
%$ try
%$ ts = dseries(A);
%$ m = ts.mean(true);
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(isequal(size(m),[1, 2]), true);
%$ t(3) = dassert(m, [1.005, 1.05]);
%$ end
%$ T = all(t);
%@eof:2
%@test:3
%$ % Define a dataset.
%$ A = bsxfun(@plus, randn(100000000,2)*.1, [.5, 2]);
%$
%$ % Instantiate time series objects and compute the mean.
%$ try
%$ ts = dseries(A);
%$ m1 = mean(ts);
%$ m2 = mean(ts, true);
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(isequal(size(m1),[1, 2]), true);
%$ t(3) = dassert(isequal(size(m2),[1, 2]), true);
%$ t(4) = dassert(max(abs(m1-[.5, 2]))<.0001, true);
%$ t(5) = isinf(m2(2));
%$ t(6) = isequal(m2(1), 0);
%$ end
%$ T = all(t);
%@eof:3
%@test:4
%$ % Define a dataset.
%$ A = bsxfun(@plus, randn(100000000,2)*.1, [.5, 2]);
%$
%$ % Instantiate time series objects and compute the mean.
%$ try
%$ ts = dseries(A);
%$ m1 = ts.mean();
%$ m2 = ts.mean(true);
%$ m3 = ts.mean(false);
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(isequal(size(m1),[1, 2]), true);
%$ t(3) = dassert(isequal(size(m2),[1, 2]), true);
%$ t(4) = dassert(max(abs(m1-[.5, 2]))<.0001, true);
%$ t(5) = isinf(m2(2));
%$ t(6) = isequal(m2(1), 0);
%$ t(7) = isequal(m1, m3);
%$ end
%$ T = all(t);
%@eof:4
\ No newline at end of file
function s = std(o, geometric) % --*-- Unitary tests --*--
% Returns the standard deviation of the variables in a @dseries object o.
% 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.
%
% OUTPUTS
% o s 1*vobs(o) vector of doubles.
% Copyright (C) 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/>.
if nargin<2
geometric = false;
end
if geometric
m = mean(o, true);
s = exp(sqrt(sum(log(bsxfun(@rdivide, o.data, m)).^2, 1)/nobs(o)));
else
s = std(o.data);
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);
%$ s1 = std(ts, true);
%$ s2 = std(ts);
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(isequal(size(s1),[1, 2]), true);
%$ t(3) = dassert(isequal(size(s2),[1, 2]), true);
%$ t(4) = dassert(s1, [1, 1]);
%$ t(4) = all(abs(s2)<1e-12);
%$ end
%$ T = all(t);
%@eof:1
%@test:2
%$ % Define a dataset.
%$ A = repmat([1.005, 1.05], 10, 1);
%$
%$ % Instantiate a time series object and compute the mean.
%$ try
%$ ts = dseries(A);
%$ s1 = ts.std(true);
%$ s2 = ts.std();
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(isequal(size(s1),[1, 2]), true);
%$ t(3) = dassert(isequal(size(s2),[1, 2]), true);
%$ t(4) = dassert(s1, [1, 1]);
%$ t(4) = all(abs(s2)<1e-12);
%$ end
%$ T = all(t);
%@eof:2
%@test:3
%$ % Define a dataset.
%$ A = bsxfun(@plus, randn(100000000,2)*.1, [.5, 2]);
%$
%$ % Instantiate time series objects and compute the mean.
%$ try
%$ ts = dseries(A);
%$ s = std(ts);
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(isequal(size(s),[1, 2]), true);
%$ t(3) = dassert(max(abs(s-[.1, .1]))<.0001, true);
%$ end
%$ T = all(t);
%@eof:3
%@test:4
%$ % Define a dataset.
%$ A = bsxfun(@plus, randn(100000000,2)*.1, [.5, 2]);
%$
%$ % Instantiate time series objects and compute the mean.
%$ try
%$ ts = dseries(A);
%$ s = ts.std();
%$ t(1) = 1;
%$ catch
%$ t = 0;
%$ end
%$
%$ if t(1)
%$ t(2) = dassert(isequal(size(s),[1, 2]), true);
%$ t(3) = dassert(max(abs(s-[.1, .1]))<.0001, true);
%$ end
%$ T = all(t);
%@eof:4
\ No newline at end of file
......@@ -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'} % Methods with less than two arguments.
case {'lag','lead','hptrend','hpcycle','chain','detrend','exist','mean','std'} % 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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment