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

Add hamilton filter #405

Merged
merged 10 commits into from
Mar 29, 2018
Merged

Conversation

Shunsuke-Hori
Copy link
Contributor

@Shunsuke-Hori Shunsuke-Hori commented Mar 23, 2018

This PR adds "Hamilton filter" from Why You Should Never Use the Hodrick-Prescott Filter

Proposition 4 in the paper shows that the error term v_{t+h} of the regression

y_{t+h} = beta_0 + beta_1 y_{t} + ... + beta_p y{t-p+1} + v_{t+h}

gives cyclical component.

If *arg (p in the paper) is not passed, the true process is assumed to be a random walk. Then, we no longer need to do regression and we just take the difference.

Example:

df['y_c'], df['y_t'] = hamilton_filter(df['y'], 8, 4) # apply to data `y` in pd.dataframe
df['y_c'], df['y_t'] = hamilton_filter(y, 8, 4) # apply to data `y` in np.array

# random walk
df['y_c'], df['y_t'] = hamilton_filter(df['y'], 8) # apply to data `y` in pd.dataframe
df['y_c'], df['y_t'] = hamilton_filter(y, 8) # apply to data `y` in np.array

@jstac
Copy link
Contributor

jstac commented Mar 23, 2018

@Shunsuke-Hori I didn't know you were writing Python code now :-)

This looks good.

Random thought: Will we end up with a few filters? Should we have a subpackage? I guess we can created one later on if others are added...

@coveralls
Copy link

coveralls commented Mar 23, 2018

Coverage Status

Coverage increased (+0.03%) to 95.165% when pulling bacc9c0 on Shunsuke-Hori:add-hamilton-filter into e5e76bd on QuantEcon:master.

@mmcky mmcky added the in-work label Mar 23, 2018
@Shunsuke-Hori
Copy link
Contributor Author

@jstac If more filters are added, we may be better to make it. Al least, I don't have any plan to add more filters now.

Two questions:

  • Is there any way to test locally? Would anyone mind telling me how?
  • Below is the difference between python and matlab output; python gives larger output for all periods. Is it wierd? too large? If so, does anyone know why?
    dif

@mmcky
Copy link
Contributor

mmcky commented Mar 23, 2018

Hi @Shunsuke-Hori if you are not using windows you can use make test at the base level of QuantEcon.py. Otherwise you can use nose directly using nosetests -v assuming nose is on your path.

@Shunsuke-Hori
Copy link
Contributor Author

Thank you @mmcky, I can run the test locally now.

It's ready for review.

@Shunsuke-Hori
Copy link
Contributor Author

Regarding the difference with matlab output, original matlab code is using inv(X'*X)*X'y instead of (X'*X)\X'y. With \, output becomes close.

@mmcky
Copy link
Contributor

mmcky commented Mar 26, 2018

@Shunsuke-Hori can you please update the description to the PR to document what this routine does, and any references of how it was implemented etc, a simple example for how it can be used etc. Thanks.

@Shunsuke-Hori
Copy link
Contributor Author

@mmcky Thank you for pointing it out. It's updated.

@mmcky mmcky added ready and removed in-work labels Mar 26, 2018
@mmcky
Copy link
Contributor

mmcky commented Mar 26, 2018

thanks @Shunsuke-Hori this is looking good. I think we can leave it in filter for now and when the time comes to add new filters we may setup a filters sub-module. I have marked this as ready and will allow a day for comments.

@mmcky
Copy link
Contributor

mmcky commented Mar 29, 2018

thanks @Shunsuke-Hori for this contribution. Merging now.

@mmcky mmcky merged commit c9a7670 into QuantEcon:master Mar 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants