1function o = baxter_king_filter_(o, high_frequency, low_frequency, K) % --*-- Unitary tests --*-- 2 3% Implementation of Baxter and King (1999) band pass filter for dseries objects. The code is adapted from 4% the one provided by Baxter and King. This filter isolates business cycle fluctuations with a period of length 5% ranging between high_frequency to low_frequency (quarters). 6% 7% INPUTS 8% - o dseries object. 9% - high_frequency positive scalar, period length (default value is 6). 10% - low_frequency positive scalar, period length (default value is 32). 11% - K positive scalar integer, truncation parameter (default value is 12). 12% 13% OUTPUTS 14% - o dseries object. 15% 16% REMARKS 17% This filter use a (symmetric) moving average smoother, so that K observations at the beginning and at the end of the 18% sample are lost in the computation of the filter. 19 20% Copyright (C) 2013-2017 Dynare Team 21% 22% This file is part of Dynare. 23% 24% Dynare is free software: you can redistribute it and/or modify 25% it under the terms of the GNU General Public License as published by 26% the Free Software Foundation, either version 3 of the License, or 27% (at your option) any later version. 28% 29% Dynare is distributed in the hope that it will be useful, 30% but WITHOUT ANY WARRANTY; without even the implied warranty of 31% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 32% GNU General Public License for more details. 33% 34% You should have received a copy of the GNU General Public License 35% along with Dynare. If not, see <http://www.gnu.org/licenses/>. 36 37if nargin<4 || isempty(K) 38 K = 12; 39 if nargin<3 || isempty(low_frequency) 40 % Set default number of periods corresponding to the lowest frequency. 41 low_frequency = 32; 42 if nargin<2 || isempty(high_frequency) 43 % Set default number of periods corresponding to the highest frequency. 44 high_frequency = 6; 45 if nargin<1 46 error('dseries::baxter_king_filter: I need at least one argument') 47 end 48 else 49 if high_frequency<2 50 error('dseries::baxter_king_filter: Second argument must be greater than 2!') 51 end 52 if high_frequency>low_frequency 53 error('dseries::baxter_king_filter: Second argument must be smaller than the third argument!') 54 end 55 end 56 end 57end 58 59% translate periods into frequencies. 60hf=2.0*pi/high_frequency; 61lf=2.0*pi/low_frequency; 62 63% Set weights for the band-pass filter's lag polynomial. 64weights = zeros(K+1,1); lpowers = transpose(1:K); 65weights(2:K+1) = (sin(lpowers*hf)-sin(lpowers*lf))./(lpowers*pi); 66weights(1) = (hf-lf)/pi; 67 68% Set the constraint on the sum of weights. 69if low_frequency>1000 70 % => low pass filter. 71 sum_of_weights_constraint = 1.0; 72else 73 sum_of_weights_constraint = 0.0; 74end 75 76% Compute the sum of weights. 77sum_of_weights = weights(1) + 2*sum(weights(2:K+1)); 78 79% Correct the weights. 80weights = weights + (sum_of_weights_constraint - sum_of_weights)/(2*K+1); 81 82% Weights are symmetric! 83weights = [flipud(weights(2:K+1)); weights]; 84 85tmp = zeros(size(o.data)); 86 87% Filtering step. 88for t = K+1:nobs(o)-K 89 tmp(t,:) = weights'*o.data(t-K:t+K,:); 90end 91 92% Update dseries object. 93o.data = tmp(K+1:end-K,:); 94init = firstdate(o)+K; 95o.dates = init:init+(nobs(o)-1); 96for i=1:vobs(o) 97 if isempty(o.ops{i}) 98 o.ops(i) = {sprintf('baxter_king_filter(%s, %s, %s, %s)', ... 99 o.name{i}, ... 100 num2str(high_frequency), ... 101 num2str(low_frequency), ... 102 num2str(K))}; 103 else 104 o.ops(i) = {sprintf('baxter_king_filter(%s, %s, %s, %s)', ... 105 o.ops{i}, ... 106 num2str(high_frequency), ... 107 num2str(low_frequency), ... 108 num2str(K))}; 109 end 110end 111 112 113%@test:1 114%$ plot_flag = false; 115%$ 116%$ % Create a dataset. 117%$ e = .2*randn(200,1); 118%$ u = randn(200,1); 119%$ stochastic_trend = cumsum(e); 120%$ deterministic_trend = .1*transpose(1:200); 121%$ x = zeros(200,1); 122%$ for i=2:200 123%$ x(i) = .75*x(i-1) + e(i); 124%$ end 125%$ y = x + stochastic_trend + deterministic_trend; 126%$ 127%$ % Test the routine. 128%$ try 129%$ ts = dseries(y,'1950Q1'); 130%$ ts.baxter_king_filter_(); 131%$ xx = dseries(x,'1950Q1'); 132%$ t(1) = 1; 133%$ catch 134%$ t(1) = 0; 135%$ end 136%$ 137%$ if t(1) 138%$ t(2) = dassert(ts.freq,4); 139%$ t(3) = dassert(ts.init.freq,4); 140%$ t(4) = dassert(ts.init.time,[1953, 1]); 141%$ t(5) = dassert(ts.vobs,1); 142%$ t(6) = dassert(ts.nobs,176); 143%$ t(7) = dassert(ts.name{1},'Variable_1'); 144%$ t(8) = dassert(ts.ops{1},'baxter_king_filter(Variable_1, 6, 32, 12)'); 145%$ end 146%$ 147%$ % Show results 148%$ if plot_flag 149%$ plot(xx(ts.dates).data,'-k'); 150%$ hold on 151%$ plot(ts.data,'--r'); 152%$ hold off 153%$ axis tight 154%$ id = get(gca,'XTick'); 155%$ set(gca,'XTickLabel',strings(ts.dates(id))); 156%$ legend({'Stationary component of y', 'Filtered y'}) 157%$ print('-depsc2','../doc/dynare.plots/BaxterKingFilter.eps') 158%$ system('convert -density 300 ../doc/dynare.plots/BaxterKingFilter.eps ../doc/dynare.plots/BaxterKingFilter.png'); 159%$ system('convert -density 300 ../doc/dynare.plots/BaxterKingFilter.eps ../doc/dynare.plots/BaxterKingFilter.pdf'); 160%$ system('convert -density 300 ../doc/dynare.plots/BaxterKingFilter.eps ../doc/dynare.plots/BaxterKingFilter.jpg'); 161%$ end 162%$ 163%$ T = all(t); 164%@eof:1 165