1function o = hpcycle_(o, lambda) % --*-- Unitary tests --*--
2
3% Extracts the cycle 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]  Cyclical 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::hpcycle: Lambda must be a positive integer!'])
32    end
33else
34    lambda = [];
35end
36
37[~, data] = sample_hp_filter(o.data,lambda);
38o.data = data;
39
40for i=1:vobs(o)
41    if isempty(o.ops{i})
42        if isempty(lambda)
43            o.ops(i) = {sprintf('hpcycle(%s, [])', o.name{i})};
44        else
45            o.ops(i) = {sprintf('hpcycle(%s, %s)', o.name{i}, num2str(lambda))};
46        end
47    else
48        if isempty(lambda)
49            o.ops(i) = {sprintf('hpcycle(%s, [])', o.ops{i})};
50        else
51            o.ops(i) = {sprintf('hpcycle(%s, %s)', o.ops{i}, num2str(lambda))};
52        end
53    end
54end
55
56%@test:1
57%$ plot_flag = false;
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) = .9*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(x,'1950Q1');
74%$     ts0.hpcycle_();
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(ts0.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({'Stationary component of y', 'Filtered y'})
98%$     print('-depsc2','../doc/dynare.plots/HPCycle.eps')
99%$     system('convert -density 300 ../doc/dynare.plots/HPCycle.eps ../doc/dynare.plots/HPCycle.png');
100%$     system('convert -density 300 ../doc/dynare.plots/HPCycle.eps ../doc/dynare.plots/HPCycle.pdf');
101%$     system('convert -density 300 ../doc/dynare.plots/HPCycle.eps ../doc/dynare.plots/HPCycle.jpg');
102%$ end
103%$
104%$ T = all(t);
105%@eof:1