1function o = center_(o, geometric) % --*-- Unitary tests --*-- 2 3% Centers dseries object o around its mean (arithmetic or geometric). 4% 5% INPUTS 6% - o dseries object [mandatory]. 7% - geometric logical [default is false], if true returns the geometric mean. 8% 9% OUTPUTS 10% - o dseries object. 11 12% Copyright (C) 2016-2017 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 33m = mean(o.data); 34 35if geometric 36 o.data = bsxfun(@rdivide, o.data, m); 37else 38 o.data = bsxfun(@minus, o.data, m); 39end 40 41for i=1:o.vobs 42 if isempty(o.ops{i}) 43 o.ops(i) = {sprintf('center(%s, %s)', o.name{i}, num2str(geometric))}; 44 else 45 o.ops(i) = {sprintf('center(%s, %s)', o.ops{i}, num2str(geometric))}; 46 end 47end 48 49%@test:1 50%$ % Define a dataset. 51%$ A = repmat([1.005, 1.05], 10, 1); 52%$ 53%$ % Instantiate a time series object and compute the mean. 54%$ try 55%$ ts = dseries(A); 56%$ ts.center_(true); 57%$ t(1) = 1; 58%$ catch 59%$ t = 0; 60%$ end 61%$ 62%$ if t(1) 63%$ t(2) = all(all(abs(ts.data-ones(10,2))<1e-12)); 64%$ t(3) = dassert(ts.ops{1}, 'center(Variable_1, 1)'); 65%$ t(4) = dassert(ts.name{1}, 'Variable_1'); 66%$ end 67%$ T = all(t); 68%@eof:1 69