Page 1 of 1

HP Filter in Rolling Window mode

Posted: Fri May 31, 2019 6:13 am
Hello, everyone. I want to share the programming that performs the HP Filter on a Rolling Window mode. It has been coded for one variable, the user can set the window, parameter size (lambda), step of the rolling window of its preference and, of course, get multiple trends and gaps per time span.

Please, note that the code could also be adapted for other univariate filters like the Band-Pass, HP one sided, Kalman Filter, etc. Moreover, a further step could be to adapt it for several groups of variables.

Code: Select all

' HP Filter Rolling Window

close @objects
smpl @all

' ------------------------------------------------------------------
' Set your order here
string tohp =       "x1"         ' Set here the name of your variable
string inidate =    "1995q1"     ' Sample (and rolling window) start date
string windate =    "2005q4"     ' Rolling window end date
string enddate =    "2019q1"    ' Sample end date
!step = 1    ' Set the step size (1, 2, 3, 4, …)
!lambda =  1601  ' Set the lambda
'           ---> Now you may run the code <---
' ------------------------------------------------------------------

' Do not modify this block from here on
smpl {inidate} {enddate}
!tspan = @obssmpl
smpl {inidate} {windate}
!win = @obssmpl
!length= !tspan -  !win
smpl {inidate}-1 {windate}-1
delete hptt* hpcy*
group _g  {tohp}
for !i = 0 to @round(@abs(!length+!step-1))  step !step    
for !v =1 to _g.@count   
   %y = _g.@seriesname(!v)
   {%y}.hpf(lambda=!lambda)    {%y}_hp  @   {%y}_hpc   
series hptt{!i} = {%y}_hp
series hpcy{!i} = {%y}_hpc
delete {%y}_hp {%y}_hpc
smpl {inidate}+!i  {windate}+!i

' ------------------------------------------------------------------
'Get your results here
group _g hptt*   ' Trends

group _gc hpcy*    ' Cycles
' ------------------------------------------------------------------