! Estimation of the Hodrick-Prescott stochastic trend;
! Based on a version by Eren Ocakverdi;
! The problem:  Given a series of observations, we want to
   derive a sequence of smoothed "forecasts" for which the
   trend from period to period does not vary too much but
   which also approximates the original data;
!  Ref: Hodrick, R., and E. Prescott (1997), "Postwar U.S. Business Cycles: 
     An Empirical Investigation," Journal of Money, Credit, and Banking,
     vol. 29, no. 1, pp. 2-16;

! Keywords: Hodrick-Prescott filtering, smoothing, filtering,
  time series, forecasting, trend forecasting, business cycle forecasting;

DATA: ! Suggested values for the penalty (or smoothing) parameter lambda are 100-400, 1600 and 14400 respectively for annual, quarterly and monthly time series; L=14400; ! Observed values of the time series; OBSERVED = 110.6 106.4 102.7 102.7 87.2 107.9 102.7 107.9 110.1 111.4 108.0 117.3 124.7 110.7 124.9; ENDDATA N = @SIZE(PERIODS); ! Number of periods in this data set; ! Minimize a weighted sum of squared errors and the squared 2nd differences from period to period; [OBJ] MIN= @SUM( PERIODS: CYCLE^2)+ L*@SUM( PERIODS: CHANGE^2); ! The change in trend; @FOR( PERIODS( T) | T #GT# 1 #AND# T #LT# N: CHANGE( T) = (TREND( T + 1) - TREND( T)) - (TREND( T)-TREND( T-1))); ! Fluctuations around the trend; @FOR( PERIODS: CYCLE = OBSERVED - TREND); ! Fluctuations and smoothed values can be either positive or negative; @FOR( PERIODS: @FREE( CYCLE); @FREE( TREND)); END