Skip to content

Commit

Permalink
hamilton_filter: Clarify the API (#634)
Browse files Browse the repository at this point in the history
  • Loading branch information
rht committed Nov 22, 2022
1 parent 1edd6b2 commit a3f38a9
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions quantecon/filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,25 @@
"""
import numpy as np

def hamilton_filter(data, h, *args):

def hamilton_filter(data, h, p=None):
r"""
This function applies "Hamilton filter" to the data
http://econweb.ucsd.edu/~jhamilto/hp.pdf
Parameters
----------
data : array or dataframe
h : integer
Time horizon that we are likely to predict incorrectly.
Original paper recommends 2 for annual data, 8 for quarterly data,
24 for monthly data.
*args : integer
If supplied, it is p in the paper. Number of lags in regression.
p : integer (optional)
If supplied, it is p in the paper. Number of lags in regression.
Must be greater than h.
If not supplied, random walk process is assumed.
Note: For seasonal data, it's desirable for p and h to be integer multiples
of the number of obsevations in a year.
e.g. For quarterly data, h = 8 and p = 4 are recommended.
Expand All @@ -38,8 +39,7 @@ def hamilton_filter(data, h, *args):
# sample size
T = len(y)

if len(args) == 1: # if p is supplied
p = args[0]
if p is not None: # if p is supplied
# construct X matrix of lags
X = np.ones((T-p-h+1, p+1))
for j in range(1, p+1):
Expand All @@ -51,12 +51,7 @@ def hamilton_filter(data, h, *args):
trend = np.append(np.zeros(p+h-1)+np.nan, X@b)
# cyclical component
cycle = y - trend

elif len(args) == 0: # if p is not supplied (random walk)
else: # if p is not supplied (random walk)
cycle = np.append(np.zeros(h)+np.nan, y[h:T] - y[0:T-h])
trend = y - cycle

return cycle, trend



0 comments on commit a3f38a9

Please sign in to comment.