1function o = detrend_(o, model) % --*-- Unitary tests --*-- 2 3% Detrends a dseries object with a polynomial of order model. 4% 5% INPUTS 6% - o [dseries] time series to be detrended. 7% - model [integer] scalar, order of the fitted polynomial. 8% 9% OUTPUTS 10% - o [dseries] detrended time series. 11 12% Copyright (C) 2014-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 29% Set default for the order of the polynomial trend (constant). 30if nargin<2 31 model = 0; 32end 33 34if isnumeric(model) 35 if isscalar(model) && isint(model) 36 switch model 37 case 0 38 o.data = demean(o.data); 39 otherwise 40 x = NaN(nobs(o), model+1); 41 x(:,1) = ones(nobs(o), 1); 42 x(:,2) = transpose(1:nobs(o)); 43 for c=3:model+1 44 x(:,c) = x(:,c-1).*x(:,2); 45 end 46 o.data = o.data - x*(x\o.data); 47 end 48 else 49 error('dseries::detrend: Second argument must be a positive integer scalar!') 50 end 51else 52 error('dseries::detrend: Second argument must be a positive integer scalar!') 53end 54 55for i=1:vobs(o) 56 if isempty(o.ops{i}) 57 o.ops(i) = {sprintf('detrend(%s, %s)', o.name{i}, num2str(model))}; 58 else 59 o.ops(i) = {sprintf('detrend(%s, %s)', o.ops{i}, num2str(model))}; 60 end 61end 62 63%@test:1 64%$ % Define a dataset. 65%$ a = dseries(randn(1000,3)); 66%$ 67%$ % detrend (default). 68%$ try 69%$ b = copy(a); 70%$ b1 = b.detrend_(); 71%$ t(1) = 1; 72%$ catch 73%$ t(1) = 0; 74%$ end 75%$ 76%$ % detrend (constant). 77%$ if t(1) 78%$ try 79%$ b = copy(a); 80%$ b2 = b.detrend_(0); 81%$ t(2) = 1; 82%$ catch 83%$ t(2) = 0; 84%$ end 85%$ end 86%$ 87%$ % detrend (linear). 88%$ if t(1) && t(2) 89%$ try 90%$ b = copy(a); 91%$ b3 = b.detrend_(1); 92%$ t(3) = 1; 93%$ catch 94%$ t(3) = 0; 95%$ end 96%$ end 97%$ 98%$ % detrend (quadratic). 99%$ if t(1) && t(2) && t(3) 100%$ try 101%$ b = copy(a); 102%$ b4 = b.detrend_(2); 103%$ t(4) = 1; 104%$ catch 105%$ t(4) = 0; 106%$ end 107%$ end 108%$ 109%$ % detrend (cubic). 110%$ if t(1) && t(2) && t(3) && t(4) 111%$ try 112%$ b = copy(a); 113%$ b5 = b.detrend_(3); 114%$ t(5) = 1; 115%$ catch 116%$ t(5) = 0; 117%$ end 118%$ end 119%$ 120%$ if t(1) && t(2) && t(3) && t(4) && t(5) 121%$ t(6) = dassert(max(mean(b1.data)), 0,1e-12); 122%$ t(7) = dassert(max(mean(b2.data)), 0,1e-12); 123%$ t(8) = dassert(max(mean(b3.data)), 0,1e-12); 124%$ t(9) = dassert(max(mean(b4.data)), 0,1e-12); 125%$ t(10) = dassert(max(mean(b5.data)), 0,1e-9); 126%$ end 127%$ T = all(t); 128%@eof:1 129 130%@test:2 131%$ % Define a dataset. 132%$ A = randn(1000,3); 133%$ a = dseries(randn(1000,3)); 134%$ 135%$ try 136%$ b = a.detrend_(); 137%$ t(1) = 1; 138%$ catch 139%$ t(1) = 0; 140%$ end 141%$ 142%$ if t(1) 143%$ t(2) = max(max(abs(a.data-A)))>1e-8; 144%$ end 145%$ 146%$ T = all(t); 147%@eof:2