Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hamilton_filter: Clarify the API #634

Merged
merged 1 commit into from
Nov 22, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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