From b8547bc8408b00f5c2a5cca9a44bb39b32ac6604 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Hermes=29?= <stephane.adjemian@univ-lemans.fr> Date: Thu, 18 Feb 2016 00:42:51 +0100 Subject: [PATCH] Added mean and std methods. Returns the arithmetic or geometric mean and standard deviation. --- src/@dseries/mean.m | 127 +++++++++++++++++++++++++++++++++++++++++ src/@dseries/std.m | 125 ++++++++++++++++++++++++++++++++++++++++ src/@dseries/subsref.m | 2 +- 3 files changed, 253 insertions(+), 1 deletion(-) create mode 100644 src/@dseries/mean.m create mode 100644 src/@dseries/std.m diff --git a/src/@dseries/mean.m b/src/@dseries/mean.m new file mode 100644 index 0000000..0488765 --- /dev/null +++ b/src/@dseries/mean.m @@ -0,0 +1,127 @@ +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 diff --git a/src/@dseries/std.m b/src/@dseries/std.m new file mode 100644 index 0000000..7cedf39 --- /dev/null +++ b/src/@dseries/std.m @@ -0,0 +1,125 @@ +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 diff --git a/src/@dseries/subsref.m b/src/@dseries/subsref.m index 6a165a3..ec731ef 100644 --- a/src/@dseries/subsref.m +++ b/src/@dseries/subsref.m @@ -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); -- GitLab