onesidedhptrend_.m 3.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
function o = onesidedhptrend_(o, lambda) % --*-- Unitary tests --*--

% Extracts the trend component from a dseries object using a one sided HP filter.
%
% INPUTS
% - o         [dseries]   Original time series.
% - lambda    [double]    scalar, trend smoothness parameter.
%
% OUTPUTS
% - o         [dseries]   Trend component of the original time series.

% Copyright (C) 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/>.

if nargin>1
    if lambda<=0
        error(['dseries::onesidedhptrend: Lambda must be a positive integer!'])
    end
33 34 35 36 37
    if nargin>2
        if ~isequal(init, 'hpfilter')
            error('dseries::onesidedhptrend: Unknown option!')
        end
    end
38 39 40 41 42
else
    lambda = [];
end

for i=1:vobs(o)
43 44
    if isempty(o.ops{i})
        if isempty(lambda)
45 46 47 48 49
            if nargin>2
                o.ops(i) = {sprintf('onesidedhptrend(%s, [], ''%s'')', o.name{i}, init)};
            else
                o.ops(i) = {sprintf('onesidedhptrend(%s, [])', o.name{i})};
            end
50
        else
51 52 53 54 55
            if nargin>2
                o.ops(i) = {sprintf('onesidedhptrend(%s, %s, ''%s'')', o.name{i}, num2str(lambda), init)};
            else
                o.ops(i) = {sprintf('onesidedhptrend(%s, %s)', o.name{i}, num2str(lambda))};
            end
56 57 58
        end
    else
        if isempty(lambda)
59 60 61 62 63
            if nargin>2
                o.ops(i) = {sprintf('onesidedhptrend(%s, [], ''%s'')', o.ops{i}, init)};
            else
                o.ops(i) = {sprintf('onesidedhptrend(%s, [])', o.ops{i})};
            end
64
        else
65 66 67 68 69
            if nargin>2
                o.ops(i) = {sprintf('onesidedhptrend(%s, %s, ''%s'')', o.ops{i}, num2str(lambda), init)};
            else
                o.ops(i) = {sprintf('onesidedhptrend(%s, %s)', o.ops{i}, num2str(lambda))};
            end
70 71
        end
    end
72 73
end

74 75 76 77 78 79 80
if nargin>2
    trend = o.hptrend(lambda);
    x0 = trend.data(1:2,:);
    o.data = one_sided_hp_filter(o.data, lambda, x0);
else    
    o.data = one_sided_hp_filter(o.data, lambda);
end
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

return

%@test:1
e = .2*randn(200,1);
u = randn(200,1);
stochastic_trend = cumsum(e);
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;

try
    ts0 = dseries(y,'1950Q1');
    ts0.onesidedhptrend_(1600);
    t(1) = 1;
catch
    t(1) = 0;
end

T = all(t);
%@eof:1

%@test:2
e = .2*randn(200,2);
u = randn(200,2);
stochastic_trends = cumsum(e);
deterministic_trends = transpose(1:200)*[.1, -.2];
x = zeros(200,2);
for i=2:200
    x(i, 1) = .75*x(i-1, 1) + e(i, 1);
    x(i, 2) = -.10*x(i-1, 2) + e(i, 2);
end
y = x + stochastic_trends + deterministic_trends;

try
    ts0 = dseries(y,'1950Q1');
    ts0.onesidedhptrend_(1600);
    t(1) = 1;
catch
    t(1) = 0;
end

T = all(t);
%@eof:2