' --------------------------------------------------------------------------------------------------------------------
'	DESCRIPTION: 	Two state-space implementations of the HP Filter.
'
'	DATE: 				 03 / 03 / 2017
'
'	AUTHOR:			Graeme Walsh
' --------------------------------------------------------------------------------------------------------------------

' ********************************************************************************************************************
' Pre-liminary set-up
' ********************************************************************************************************************
  
  wfcreate(wf=example) Q 1980Q1 2017Q4

' ********************************************************************************************************************
' Create Simulated Data
' ********************************************************************************************************************

  rndseed 123456

  series e1 = nrnd
  series Y = 0

  model AR2
  AR2.append Y = 3.20 + 0.22 * Y(-1) + 0.15 * Y(-2) + e1
  AR2.solveopt(s=d,d=d)
  AR2.solve

  series Y = Y_0

' *******************************************************************************************************************
' EViews Built-in HP Filter function
' *******************************************************************************************************************

  hpf(lambda=1600,power=2) Y HPT @ HPC

' ********************************************************************************************************************
' State-space model of HP Filter - Version 1
' ********************************************************************************************************************

' HP filter smoothing parameter
  scalar lambda = 1600
  
' Create the sspace object
  sspace hp_mod1  

' Measurement equation
  hp_mod1.append @signal Y = 1 * TAU + 0 * BETA + EPS 
  hp_mod1.append @ename EPS 
  hp_mod1.append @evar var(EPS) = 1

' State equation (TAU)
  hp_mod1.append @state TAU = 1 * TAU(-1) + 1 * BETA(-1) + ETA_TAU 
  hp_mod1.append @ename ETA_TAU 
  hp_mod1.append @evar var(ETA_TAU) = 0  

' State equation (BETA)
  hp_mod1.append @state BETA = 0 * TAU(-1) + 1 * BETA(-1) + ETA_BETA 
  hp_mod1.append @ename ETA_BETA 
  hp_mod1.append @evar var(ETA_BETA) = 1 / lambda
    
' Estimate model by maximum likelihood
  hp_mod1.ml

' Create state estimates
  hp_mod1.makestates(t=smooth) *_S1
  hp_mod1.makestates(t=pred) *_P1
  hp_mod1.makestates(t=filt) *_F1

' Create gap estimates
  series CYCLE_S1 = Y - TAU_S1 		' Smooth gap
  series CYCLE_P1 = Y - TAU_P1		' Predicted gap
  series CYCLE_F1 = Y - TAU_F1		' Filtered gap

' ********************************************************************************************************************
' State-space model of HP Filter - version 2
' ********************************************************************************************************************

' HP filter smoothing parameter
  scalar lambda = 1600
  
' Create the sspace object
  sspace hp_mod2  

' Measurement equation
  hp_mod2.append @signal Y = Y_STAR + EPS 
  hp_mod2.append @ename EPS 
  hp_mod2.append @evar var(EPS) = 1
  
' State equation (Y_STAR)
  hp_mod2.append @state Y_STAR = 2 * Y_STAR(-1) - 1 * Y_STAR2(-1) + ETA_Y_STAR 
  hp_mod2.append @ename ETA_Y_STAR 
  hp_mod2.append @evar var(ETA_Y_STAR) = 1 / lambda
  
' State equation (Y_STAR2) - this is called an "augmented" state variable
  hp_mod2.append @state Y_STAR2 = Y_STAR(-1) 
   
' Estimate model by maximum likelihood
  hp_mod2.ml

' Create state estimates
  hp_mod2.makestates(t=smooth) *_S2
  hp_mod2.makestates(t=pred) *_P2
  hp_mod2.makestates(t=filt) *_F2

' Create gap estimates
  series CYCLE_S2 = Y - Y_STAR_S2 		' Smooth gap
  series CYCLE_P2 = Y - Y_STAR_P2 		' Preditced gap
  series CYCLE_F2 = Y - Y_STAR_F2  		' Filtered gap

' ********************************************************************************************************************
' Compare EViews built-in with State-Space Models
' ********************************************************************************************************************

  show HPT TAU_S1 Y_STAR_S2		' Trend comparison
  show HPC CYCLE_S1 CYCLE_S2	' Cycle comparison
  
' -------------------------------------------------------------------------------------------------------------------
'	END OF PROGRAM
' -------------------------------------------------------------------------------------------------------------------
