hpfilter.m (1552B)
1 function [s]=hpfilter(y,w) 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 % Author: Ivailo Izvorski, 4 % Department of Economics 5 % Yale University. 6 % izvorski@econ.yale.edu 7 % This code has been used and seems to be free of error. 8 % However, it carries no explicit or implicit guarantee. 9 % 10 % function [s]=hpfilter(y,w) 11 % Hondrick Prescott filter where: 12 % w - smoothing parameter; w=1600 for quarterly data 13 % y - the original series that has to be smoothed 14 % s - the filtered series 15 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 16 % 17 % Chad 9/2/03 18 % Downloaded from 19 % http://econpapers.hhs.se/software/dgeqmrbcd/1.htm 20 % (Do a search for hodrick prescott matlab) 21 % 22 % Papers describing the filter: 23 % http://www.bepress.com/snde/vol4/iss1/art1/ 24 % 25 % King and Rebelo (1993 JEDC) not on web? 26 % Baxter and King (1995) working paper. 27 % 28 % In general, HP filtering seeks to minimize a weighted average of the Sum 29 %of Squared residuals between trend and series and a sum of squared second 30 %differences. Lambda (w here) is the weight on the second differences 31 %term. No really good justification for picking Lambda, and this filter is 32 %frowned on by the experts... 33 34 if size(y,1)<size(y,2) 35 y=y'; 36 end 37 t=size(y,1); 38 a=6*w+1; 39 b=-4*w; 40 c=w; 41 d=[c,b,a]; 42 d=ones(t,1)*d; 43 m=diag(d(:,3))+diag(d(1:t-1,2),1)+diag(d(1:t-1,2),-1); 44 m=m+diag(d(1:t-2,1),2)+diag(d(1:t-2,1),-2); 45 % 46 m(1,1)=1+w; m(1,2)=-2*w; 47 m(2,1)=-2*w; m(2,2)=5*w+1; 48 m(t-1,t-1)=5*w+1; m(t-1,t)=-2*w; 49 m(t,t-1)=-2*w; m(t,t)=1+w; 50 % 51 s=inv(m)*y;