1function m = mean(o, geometric) % --*-- Unitary tests --*-- 2 3% Returns the mean of the variables in a @dseries object o. 4% 5% INPUTS 6% o o dseries object [mandatory]. 7% o geometric logical [default is false], if true returns the geometric mean. 8% 9% OUTPUTS 10% o m 1*vobs(o) vector of doubles. 11 12% Copyright (C) 2016 Dynare Team 13% 14% This file is part of Dynare. 15% 16% Dynare is free software: you can redistribute it and/or modify 17% it under the terms of the GNU General Public License as published by 18% the Free Software Foundation, either version 3 of the License, or 19% (at your option) any later version. 20% 21% Dynare is distributed in the hope that it will be useful, 22% but WITHOUT ANY WARRANTY; without even the implied warranty of 23% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 24% GNU General Public License for more details. 25% 26% You should have received a copy of the GNU General Public License 27% along with Dynare. If not, see <http://www.gnu.org/licenses/>. 28 29if nargin<2 30 geometric = false; 31end 32 33if geometric 34 m = prod(o.data, 1).^(1/nobs(o)); 35else 36 m = mean(o.data); 37end 38 39%@test:1 40%$ % Define a dataset. 41%$ A = repmat([1.005, 1.05], 10, 1); 42%$ 43%$ % Instantiate a time series object and compute the mean. 44%$ try 45%$ ts = dseries(A); 46%$ m = mean(ts, true); 47%$ t(1) = 1; 48%$ catch 49%$ t = 0; 50%$ end 51%$ 52%$ if t(1) 53%$ t(2) = dassert(isequal(size(m),[1, 2]), true); 54%$ t(3) = dassert(m, [1.005, 1.05]); 55%$ end 56%$ T = all(t); 57%@eof:1 58 59%@test:2 60%$ % Define a dataset. 61%$ A = repmat([1.005, 1.05], 10, 1); 62%$ 63%$ % Instantiate a time series object and compute the mean. 64%$ try 65%$ ts = dseries(A); 66%$ m = ts.mean(true); 67%$ t(1) = 1; 68%$ catch 69%$ t = 0; 70%$ end 71%$ 72%$ if t(1) 73%$ t(2) = dassert(isequal(size(m),[1, 2]), true); 74%$ t(3) = dassert(m, [1.005, 1.05]); 75%$ end 76%$ T = all(t); 77%@eof:2 78 79%@test:3 80%$ % Define a dataset. 81%$ A = bsxfun(@plus, randn(100000000,2)*.1, [.5, 2]); 82%$ 83%$ % Instantiate time series objects and compute the mean. 84%$ try 85%$ ts = dseries(A); 86%$ m1 = mean(ts); 87%$ m2 = mean(ts, true); 88%$ t(1) = 1; 89%$ catch 90%$ t = 0; 91%$ end 92%$ 93%$ if t(1) 94%$ t(2) = dassert(isequal(size(m1),[1, 2]), true); 95%$ t(3) = dassert(isequal(size(m2),[1, 2]), true); 96%$ t(4) = dassert(max(abs(m1-[.5, 2]))<.0001, true); 97%$ t(5) = isinf(m2(2)); 98%$ t(6) = isequal(m2(1), 0); 99%$ end 100%$ T = all(t); 101%@eof:3 102 103%@test:4 104%$ % Define a dataset. 105%$ A = bsxfun(@plus, randn(100000000,2)*.1, [.5, 2]); 106%$ 107%$ % Instantiate time series objects and compute the mean. 108%$ try 109%$ ts = dseries(A); 110%$ m1 = ts.mean(); 111%$ m2 = ts.mean(true); 112%$ m3 = ts.mean(false); 113%$ t(1) = 1; 114%$ catch 115%$ t = 0; 116%$ end 117%$ 118%$ if t(1) 119%$ t(2) = dassert(isequal(size(m1),[1, 2]), true); 120%$ t(3) = dassert(isequal(size(m2),[1, 2]), true); 121%$ t(4) = dassert(max(abs(m1-[.5, 2]))<.0001, true); 122%$ t(5) = isinf(m2(2)); 123%$ t(6) = isequal(m2(1), 0); 124%$ t(7) = isequal(m1, m3); 125%$ end 126%$ T = all(t); 127%@eof:4