/*------------------------------------------------------------------------- HPFILTER.PRC Purpose: Generates Cyclical and Trend Component from Univariate Time Series using Hodrick-Prescott Filter Written by Hyeongwoo Kim (Mar 1, 2004) ------------------------------------------------------------------------- Format: {xc,xt} = hpfilter(x,l); Input : x (TX1) Vector of a Time Series l (1x1) Penalty Parameter l = 1,600 for Quarterly Data 100,000 < l < 150,000 for Monthly Data 6 < l < 14 for Annual Data Output: xc (Tx1) Vector of Cyclical Component xt (Tx1) Vector of Trend Component -------------------------------------------------------------------------*/ proc(2) = hpfilter(x,l); local nob,F,f1,f2,f3,f4,i,FT,xt,xc; nob = rows(x); F = zeros(nob,nob); // declare pre-filter matrix let f1 = 1 -2 1; let f2 = -2 5 -4 1; let f3 = 1 -4 5 -2; let f4 = 1 -4 6 -4 1; F[1,1:3] = f1'; F[2,1:4] = f2'; F[nob-1,nob-3:nob]= f3'; F[nob,nob-2:nob] = f1'; i = 3; do until i > (nob-2); F[i,i-2:i+2] = f4'; i = i + 1; endo; FT = inv(l*F+eye(nob)); // T x T filter matrix xt = FT*x; // trend component xc = x - xt; // cyclical component retp(xc,xt); endp;