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 59o = copy(o); 60o.baxter_king_filter_(high_frequency, low_frequency, K); 61 62%@test:1 63%$ plot_flag = false; 64%$ 65%$ % Create a dataset. 66%$ e = .2*randn(200,1); 67%$ u = randn(200,1); 68%$ stochastic_trend = cumsum(e); 69%$ deterministic_trend = .1*transpose(1:200); 70%$ x = zeros(200,1); 71%$ for i=2:200 72%$ x(i) = .75*x(i-1) + e(i); 73%$ end 74%$ y = x + stochastic_trend + deterministic_trend; 75%$ 76%$ % Test the routine. 77%$ try 78%$ ts = dseries(y,'1950Q1'); 79%$ ds = ts.baxter_king_filter(); 80%$ xx = dseries(x,'1950Q1'); 81%$ t(1) = 1; 82%$ catch 83%$ t(1) = 0; 84%$ end 85%$ 86%$ if t(1) 87%$ t(2) = dassert(ds.freq, 4); 88%$ t(3) = dassert(ds.init.freq, 4); 89%$ t(4) = dassert(ds.init.time, [1953, 1]); 90%$ t(5) = dassert(ds.vobs, 1); 91%$ t(6) = dassert(ds.nobs, 176); 92%$ t(7) = dassert(ds.name{1}, 'Variable_1'); 93%$ t(8) = dassert(ds.ops{1}, 'baxter_king_filter(Variable_1, 6, 32, 12)'); 94%$ t(9) = dassert(ts.freq, 4); 95%$ t(10) = dassert(ts.name{1}, 'Variable_1'); 96%$ t(11) = dassert(ts.init.freq, 4); 97%$ t(12) = dassert(ts.init.time, [1950, 1]); 98%$ t(13) = dassert(ts.vobs, 1); 99%$ t(14) = dassert(ts.nobs, length(y)); 100%$ t(15) = dassert(ts.data, y); 101%$ t(16) = isempty(ts.ops{1}); 102%$ end 103%$ 104%$ % Show results 105%$ if plot_flag 106%$ plot(xx(ts.dates).data,'-k'); 107%$ hold on 108%$ plot(ts.data,'--r'); 109%$ hold off 110%$ axis tight 111%$ id = get(gca,'XTick'); 112%$ set(gca,'XTickLabel',strings(ts.dates(id))); 113%$ legend({'Stationary component of y', 'Filtered y'}) 114%$ print('-depsc2','../doc/dynare.plots/BaxterKingFilter.eps') 115%$ system('convert -density 300 ../doc/dynare.plots/BaxterKingFilter.eps ../doc/dynare.plots/BaxterKingFilter.png'); 116%$ system('convert -density 300 ../doc/dynare.plots/BaxterKingFilter.eps ../doc/dynare.plots/BaxterKingFilter.pdf'); 117%$ system('convert -density 300 ../doc/dynare.plots/BaxterKingFilter.eps ../doc/dynare.plots/BaxterKingFilter.jpg'); 118%$ end 119%$ 120%$ T = all(t); 121%@eof:1 122