1function o = hptrend_(o, lambda) % --*-- Unitary tests --*-- 2 3% Extracts the trend component from a dseries object using Hodrick Prescott filter. 4% 5% INPUTS 6% - o [dseries] Original time series. 7% - lambda [double] scalar, trend smoothness parameter. 8% 9% OUTPUTS 10% - o [dseries] Trend component of the original time series. 11 12% Copyright (C) 2013-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>1 30 if lambda<=0 31 error(['dseries::hptrend: Lambda must be a positive integer!']) 32 end 33else 34 lambda = []; 35end 36 37o.data = sample_hp_filter(o.data, lambda); 38 39for i=1:vobs(o) 40 if isempty(o.ops{i}) 41 if isempty(lambda) 42 o.ops(i) = {sprintf('hptrend(%s, [])', o.name{i})}; 43 else 44 o.ops(i) = {sprintf('hptrend(%s, %s)', o.name{i}, num2str(lambda))}; 45 end 46 else 47 if isempty(lambda) 48 o.ops(i) = {sprintf('hptrend(%s, [])', o.name{i})}; 49 else 50 o.ops(i) = {sprintf('hptrend(%s, %s)', o.name{i}, num2str(lambda))}; 51 end 52 end 53end 54 55 56%@test:1 57%$ plot_flag = 0; 58%$ 59%$ % Create a dataset. 60%$ e = .2*randn(200,1); 61%$ u = randn(200,1); 62%$ stochastic_trend = cumsum(e); 63%$ deterministic_trend = .1*transpose(1:200); 64%$ x = zeros(200,1); 65%$ for i=2:200 66%$ x(i) = .75*x(i-1) + e(i); 67%$ end 68%$ y = x + stochastic_trend + deterministic_trend; 69%$ 70%$ % Test the routine. 71%$ try 72%$ ts0 = dseries(y,'1950Q1'); 73%$ ts1 = dseries(stochastic_trend+deterministic_trend,'1950Q1'); 74%$ ts0.hptrend(1600); 75%$ t(1) = 1; 76%$ catch 77%$ t(1) = 0; 78%$ end 79%$ 80%$ if t(1) 81%$ t(2) = dassert(ts0.freq,4); 82%$ t(3) = dassert(ts0.init.freq,4); 83%$ t(4) = dassert(ts0.init.time,[1950, 1]); 84%$ t(5) = dassert(ts0.vobs,1); 85%$ t(6) = dassert(ts0.nobs,200); 86%$ end 87%$ 88%$ % Show results 89%$ if plot_flag 90%$ plot(ts1.data,'-k'); % Plot of the stationary component. 91%$ hold on 92%$ plot(ts2.data,'--r'); % Plot of the filtered y. 93%$ hold off 94%$ axis tight 95%$ id = get(gca,'XTick'); 96%$ set(gca,'XTickLabel',strings(ts1.dates(id))); 97%$ legend({'Nonstationary component of y', 'Estimated trend of y'}) 98%$ print('-depsc2','../doc/dynare.plots/HPTrend.eps') 99%$ system('convert -density 300 ../doc/dynare.plots/HPTrend.eps ../doc/dynare.plots/HPTrend.png'); 100%$ system('convert -density 300 ../doc/dynare.plots/HPTrend.eps ../doc/dynare.plots/HPTrend.pdf'); 101%$ system('convert -density 300 ../doc/dynare.plots/HPTrend.eps ../doc/dynare.plots/HPTrend.jpg'); 102%$ end 103%$ T = all(t); 104%@eof:1