hptrend_.m 3.1 KB
Newer Older
1 2 3 4
function o = hptrend_(o, lambda) % --*-- Unitary tests --*--

% Extracts the trend component from a dseries object using Hodrick Prescott filter.
%
Stéphane Adjemian's avatar
Stéphane Adjemian committed
5
% INPUTS
6 7 8
% - o         [dseries]   Original time series.
% - lambda    [double]    scalar, trend smoothness parameter.
%
Stéphane Adjemian's avatar
Stéphane Adjemian committed
9
% OUTPUTS
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
% - o         [dseries]   Trend component of the original time series.

% Copyright (C) 2013-2017 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.

Stéphane Adjemian's avatar
Stéphane Adjemian committed
29
if nargin>1
30 31 32 33 34 35 36
    if lambda<=0
        error(['dseries::hptrend: Lambda must be a positive integer!'])
    end
else
    lambda = [];
end

Stéphane Adjemian's avatar
Stéphane Adjemian committed
37 38
o.data = sample_hp_filter(o.data, lambda);

39
for i=1:vobs(o)
Stéphane Adjemian's avatar
Stéphane Adjemian committed
40 41 42 43 44 45 46 47 48 49 50 51 52
    if isempty(o.ops{i})
        if isempty(lambda)
            o.ops(i) = {sprintf('hptrend(%s, [])', o.name{i})};
        else
            o.ops(i) = {sprintf('hptrend(%s, %s)', o.name{i}, num2str(lambda))};
        end
    else
        if isempty(lambda)
            o.ops(i) = {sprintf('hptrend(%s, [])', o.name{i})};
        else
            o.ops(i) = {sprintf('hptrend(%s, %s)', o.name{i}, num2str(lambda))};
        end
    end
53 54 55 56
end


%@test:1
Stéphane Adjemian's avatar
Stéphane Adjemian committed
57
%$ plot_flag = 0;
58 59 60 61
%$
%$ % Create a dataset.
%$ e = .2*randn(200,1);
%$ u = randn(200,1);
Stéphane Adjemian's avatar
Stéphane Adjemian committed
62
%$ stochastic_trend = cumsum(e);
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
%$ deterministic_trend = .1*transpose(1:200);
%$ x = zeros(200,1);
%$ for i=2:200
%$    x(i) = .75*x(i-1) + e(i);
%$ end
%$ y = x + stochastic_trend + deterministic_trend;
%$
%$ % Test the routine.
%$ try
%$     ts0 = dseries(y,'1950Q1');
%$     ts1 = dseries(stochastic_trend+deterministic_trend,'1950Q1');
%$     ts0.hptrend(1600);
%$     t(1) = 1;
%$ catch
%$     t(1) = 0;
%$ end
%$
%$ if t(1)
%$     t(2) = dassert(ts0.freq,4);
%$     t(3) = dassert(ts0.init.freq,4);
%$     t(4) = dassert(ts0.init.time,[1950, 1]);
%$     t(5) = dassert(ts0.vobs,1);
%$     t(6) = dassert(ts0.nobs,200);
%$ end
%$
%$ % Show results
%$ if plot_flag
%$     plot(ts1.data,'-k'); % Plot of the stationary component.
%$     hold on
%$     plot(ts2.data,'--r');          % Plot of the filtered y.
%$     hold off
%$     axis tight
%$     id = get(gca,'XTick');
%$     set(gca,'XTickLabel',strings(ts1.dates(id)));
%$     legend({'Nonstationary component of y', 'Estimated trend of y'})
%$     print('-depsc2','../doc/dynare.plots/HPTrend.eps')
%$     system('convert -density 300 ../doc/dynare.plots/HPTrend.eps ../doc/dynare.plots/HPTrend.png');
%$     system('convert -density 300 ../doc/dynare.plots/HPTrend.eps ../doc/dynare.plots/HPTrend.pdf');
%$     system('convert -density 300 ../doc/dynare.plots/HPTrend.eps ../doc/dynare.plots/HPTrend.jpg');
%$ end
%$ T = all(t);
%@eof:1