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 exercise05 with julia without for/if #8

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

Shunsuke-Hori
Copy link
Collaborator

It is not exactly what is asked for because I don't use for/if

@vikjam
Copy link
Owner

vikjam commented Mar 2, 2018

Hi @Shunsuke-Hori, I added the loops as the exercise mentions. Do you have any thoughts on the differences in computing time?

@vikjam vikjam requested a review from jrpabraham March 2, 2018 04:45
@vikjam vikjam self-assigned this Mar 2, 2018
@vikjam vikjam added the enhancement New feature or request label Mar 2, 2018
@Shunsuke-Hori
Copy link
Collaborator Author

@vikjam Now, loop version is using dense matrix while vectorized one is using sparse matrix. Let's use the same one and compare them again.

Also, we should try with huge data as well.

(Having said that, I am not sure if the performance is a serious concern for HP filter because we don't iterate filtering. Typically, we use it just once, right?)

@Shunsuke-Hori
Copy link
Collaborator Author

@vikjam OK, I figured out. My code with sparse matrix is performing very poorly now because it is type unstable.

H = zeros(N, N) in your code implicitly assumes that every computation is operated with Float64, which is almost surely appropriate for HP filter.

H in my code can have either Int or Float depending on the input λ. If I pass 1600. instead of 1600, my code with vectorized sparse matrix is by far the fastest no matter how large the sample size is.

I'll fix it and push.

@Shunsuke-Hori
Copy link
Collaborator Author

Fixed. Now, even with 1600 as input, sparse matrix without loop is the best

@Shunsuke-Hori
Copy link
Collaborator Author

One more note: the vectorized dense matrix code is not so efficient. I am creating a bunch of garbage by diagm. If we really want to use dense matrix, loop would be better.

@vikjam
Copy link
Owner

vikjam commented Mar 4, 2018

Hi @Shunsuke-Hori, could you explain this cell? I wanted to add a bit more explanation to the notebook:

abstract type MatrixType end
struct Dense <: MatrixType end
struct Sparse <: MatrixType end
abstract type AssignMethod end
struct Loop <: AssignMethod end
struct Vectorized <: AssignMethod end

@Shunsuke-Hori
Copy link
Collaborator Author

@vikjam I am making use of multiple dispatch. I wanted to avoid repeating the same code for loop/vectorize/dense/sparse but if/else makes code slow and ugly.

Thanks to multiple dispatch, Julia recognizes functions differently if input types are different. So, by defining those types, I let julia to construct four functions with same name and same first two arguments but different 3rd and 4th arguments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants