The HPFilter Package (by Johannes Ludsteck)

A smooth trend y of a time series \(TraditionalForm\`x\_t, \ \(\(t\)\(\ \)\(∈\)\(\ \)\({1,  ... , T}\)\(\ \)\)\)can be found by minimization of the restricted sum of squares
\(TraditionalForm\`∑\+\(t = 1\)\%T\((x\_t - y\_t)\)\^2 + α\ \(∑\+\(t = 3\)\%T\((\(Δ\^2\) y\_t)\)\^2\)\)
where Δ denotes the difference operator (Δ(Subscript[y, t]) = (Subscript[y, t]) - (Subscript[y, t - 1]))and (Δ^n)denotes n-fold application of Δ, i.e. (Δ^2) 
          (Subscript[y, t]) = Δ(Δ(Subscript[y, t])) = Δ(
              (Subscript[y, t]) - (Subscript[y, t - 1])) = 
            (Subscript[y, t]) - 2 (Subscript[y, t - 1]) + (Subscript[y, t - 2]). (Δ^2) (Subscript[y, t])can be interpreted as the curvature of y. The smoothing parameter α controls the smoothnes of the resulting trend y. For α=0, the solution of the minimization problem is (Subscript[y, t]) = (Subscript[x, t])for all t. If α approaches infinity, y approaches a linear trend, i.e. (Subscript[y, t])->(Subscript[y, 0]) +  γ t.
A crucial and challenging problem with the filter regards the determination of a sensible value of the smooting parameter α. Schlicht (2004) proposes an estimation procedure for a specific stochastic formulation of the data generating process. He assumes that x is generated by the process
(Subscript[x, t]) = (Subscript[y, t]) +  (Subscript[u, t])
and that (Subscript[u, t])and (Subscript[v, t]) ≡ (Δ^2) (Subscript[y, t])are distributed normally with means 0 and variances (Subscript[σ, u]^2)and (Subscript[σ, v]^2). Furthermore, u and v are independent.
This Package provides functions to compute the filter.

This loads the package

<< HPFilter`HPFilter`

Here is a series with 50 elements

x = {0.8, -0.1854, -0.7096, 
        1.321, -1.624, -0.4312, -1.102, 1.543, 1.381, 
        0.08875, -1.035, 2.938, 1.637, 6.097, 8.055, 11.54, 10.79, 13.42, 
        14.51, 15.84, 18.37, 20.44, 17.63, 17.97, 20.71, 23.1, 23.59, 24.6, 
        25.34, 27.76, 33.36, 35.71, 41.18, 44.08, 45.84, 52.7, 53.23, 58.31, 
        57.37, 60.16, 58.4, 59.58, 61.82, 64.45, 62.66, 63.36, 66. , 69.85, 
        73.99, 77.3}

We Plot x and its first difference to ge a visual impression of the series.

diff[x_] := Rest[x] - Most[x];
Show[
      GraphicsArray[
{ListPlot[x, PlotJoined -> True, 
            DisplayFunction -> Identity], 
          ListPlot[diff[x], PlotJoined -> True, 
            DisplayFunction -> Identity]}
], 
      DisplayFunction -> $DisplayFunction, 
      ImageSize -> 500]

The following code calls HPFilter with smoothing parameter value α=50

y50 = HPFilter[x, 50.0]

Here is a plot of x and the HP-trend y50 (both series in differences)

<< Graphics`Graphics`

DisplayTogether[
      ListPlot[diff[x], PlotJoined -> True], 
ListPlot[
        diff[y50], PlotJoined -> True]]

\(\(If\ HPFilter\ is\ called\ without\ a\ value\ of\ the\ smooting\ constant\ it\ computes\ the\ optimum\ smoothing\ value\ according\ to\ the\ method\ proposed\ by\ Schlicht\ and\ returns\ a\ list\)\(\ \)\)
\({yopt, \ {α, σ\_u\%2, σ\_v\%2}}\ where\ yopt\ is\ the\ smooth\ trend, \ α\ the\ optimum\ smoothing\ parameter\ value, \ and\ \(σ\_u\%2\) and\ \(σ\_v\%2\) 
        are\ the\ variances\ of\ u\ and\ \(\(v\)\(.\)\)\)

{yopt, {α, vu, vv}} = HPFilter[x]

Here we show the series together with y50 and yopt (all variables in differences)

DisplayTogether[
      ListPlot[diff[x], PlotJoined -> True], 
ListPlot[
        diff[yopt], PlotJoined -> True], 
      ListPlot[diff[y50], PlotJoined -> True]]

Here we use this option.

{yopt, vyopt, {α, vu, vv}} = 
      HPFilter[x, ReportPredictedTrendVariance -> True]

HPFilter[x, AccuracyGoal -> 2]

In the cell above, NMaximize does not converge. HPFilter indicates convergence problems by returnin the object Null instead of a result list. Here is an additional example, together with a test for the flag

flag = HPFilter[x, MaxIterations -> 4]
flag === Null

Evaluate the following code to clear symbols

Clear[x, y50, yopt, vu, vv, α]