1function [hptrend,hpcycle] = sample_hp_filter(y,s)
2% HP filters a collection of time series.
3%
4% INPUTS
5%   y                        [double]   T*n matrix of data (n is the number of variables)
6%   s                        [double]   scalar, smoothing parameter.
7%
8% OUTPUTS
9%   hptrend                  [double]   T*n matrix, trend component of y.
10%   hpcycle                  [double]   T*n matrix, cycle component of y.
11%
12% SPECIAL REQUIREMENTS
13%
14
15% Copyright (C) 2010-2017 Dynare Team
16%
17% This file is part of Dynare.
18%
19% Dynare is free software: you can redistribute it and/or modify
20% it under the terms of the GNU General Public License as published by
21% the Free Software Foundation, either version 3 of the License, or
22% (at your option) any later version.
23%
24% Dynare is distributed in the hope that it will be useful,
25% but WITHOUT ANY WARRANTY; without even the implied warranty of
26% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27% GNU General Public License for more details.
28%
29% You should have received a copy of the GNU General Public License
30% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
31
32[T,n] = size(y);
33
34if nargin<2 || isempty(s)
35    s = 1600;
36end
37
38D = spdiags(repmat([s, -4.0*s, (1 + 6.0*s), -4.0*s, s], T, 1), -2:2, T, T);% Sparse matrix.
39D(1,1) = 1.0+s; D(T,T) = D(1,1);
40D(1,2) = -2.0*s; D(2,1) = D(1,2); D(T-1,T) = D(1,2); D(T,T-1) = D(1,2);
41D(2,2) = 1.0+5.0*s; D(T-1,T-1) = D(2,2);
42
43hptrend = D\y;
44
45if nargout>1
46    hpcycle = y-hptrend;
47end