-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
ENH: Adding Discrete Approximation of VAR Methods #640
Conversation
Many thanks @crondonm , this will be a very valuable contribution. @chien-y and @shlff, would you mind to take a first pass at reviewing this PR? You'll need to quickly review the underlying paper, get some idea of what the code does, and write up a small notebook that tries to explain it. Also,
@Smit-create , I know that you have quite a lot on but I'm hoping that you can finish this PR in the next month or two, reviewing whether the code can be made more efficient and turning the example into a test. |
Thanks, @jstac. Once the code is reviewed and verified, I can look into this and try to modify and make the APIs more user-friendly and clean ups. |
Thanks @jstac and @Smit-create . @chien-y and I chatted about this just now. We will work on this, meet again on Monday and find a time to report it to you later. |
Thanks @jstac . Here is a list of reviewing tasks:
|
@shlff and @chien-y, please update me on your progress. |
Hey @jstac please refer to the list above, I've designed and been implementing some experiments to test different parts of the code: Please find code with shu's comments, breakdowns and experiments on the code: https://colab.research.google.com/drive/1116BYYOw0XiJjvedRNUSf5DrSQkeRchK?usp=sharing |
Docs need to be reformulated in line with QuantEcon.py standards -- see, e.g., https://github.com/QuantEcon/QuantEcon.py/blob/master/quantecon/markov/ddp.py Note |
Adding to the last comment, for Python we need to be using row major. |
|
The function |
quantecon/discrete_var.py
Outdated
mean = [0] * m | ||
|
||
for t in range(T+Tburn): | ||
drw = multivariate_normal(mean, omega).reshape(1,2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@crondonm , does this line assume that m = 2
? I'm guessing 2
should be m
in my edit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @jstac,
m should be equal to the number of states you are discretizing. In the example, m = 2.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @crondonm . In the specified example that's true but for the code to work for arbitrary examples we need to replace 2 by m. I've taken care of this.
quantecon/discrete_var.py
Outdated
return Pi, Xvec, S | ||
|
||
|
||
def mom(gx, hx, varshock, J=0, method=True): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@crondonm , FYI, this code is already available in quantecon
as solve_discrete_lyapunov
. I've changed it to use that function (after confirming identical results for the test matrices you use.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Noted. Thanks for the suggestion.
@shlff I couldn't see your code (due to lack of access). I edited @crondonm's code and pushed my own commits. Pleaese comment on the new version if I've missed obvious changes. We still need to accelerate the simulation, which is very slow (as well as add tests, etc.). I suggest that we also return a multi-index version of the transition probability matrix P. Then S doesn't need to contain a lot of unnecessary values --- we can just return the grid. |
Thanks @jstac . I've updated the access and now you and others should be able to view, edit and comment on it: I've updated the new-version code, commented on them (please see comments 1-7), broke down the code by parts and identified the most time-consuming parts in the simulation. For example this part:
|
@crondonm, FYI, the documentation states that the code discretizes (Note that If this is a mistake in the original MATLAB documentation perhaps we should let the authors know. |
@crondonm , FYI, following up from our conversation at the CBC, to avoid tiling and hence creating unnecessary arrays, xx = np.tile(x.T, (n, 1))
d = np.sum((S - xx)**2, axis=1) can be replaced by xx = np.reshape(x, (1, m))
d = np.sum((S - xx)**2, axis=1) |
Efficiency issues are now solved. I chose Numba to accelerate the code because it's largely serial. With jit compilation and some memory savings mentioned above ( |
Hi @jstac, This is fantastic. Thanks a lot for all your work optimizing the code. Thanks a lot for pointing out the issue with the variance-covariance matrix, In fact, the MATLAB code does compute the variance-covariance as Regarding your question about XVEC, I find it helpful to get it as it saves time when you are simulating the full model. However, in fairness, it is not always needed and it could be an option. I guess the minimum output should be the states and the stochastic matrix. |
Good idea, I'll make that change now. |
Python tradition suggests to use long descriptive names for functions (unlike MATLAB). Hence I propose that we change the name of |
Sounds great! |
OK @crondonm , I've changed the name and also provided options to construct the state space using row major order, as well as column major order. (The first will be a better fit for most Python code, whereas the second is consistent with the original). |
This is about ready. I've added tests within the file so the code just needs to be reviewed and then embedded properly in the library. I suggest that we add the code to All help for these last steps is appreciated (CC @Smit-create @mmcky) |
Sure, will try to add the final touch-ups this weekend. |
4e89bd8
to
ca862d6
Compare
Given the rest of the description. It seems to me that the last sentence is redundant: QuantEcon.py/quantecon/markov/approximation.py Lines 278 to 279 in 03e1a8d
|
It's not easy to find other tests for this function. |
@mmcky Yes. Remaining are:
|
Hi @mmcky, CI on the windows platform has some issues with
Many thanks in advance. |
Hi @oyamad, Many thanks for editing the docstring. It is a wonderful exemplar for me to follow in the future. I have added the three tests and fixed a small bug in the code while testing. For the record, tests with u = random_state.standard_normal(size=(sim_length-1, 2))
u = np.insert(u, 2, u[:, 0], axis=1) The test result is the same as the previous Could you please kindly review these changes when you are available? Many thanks in advance. |
quantecon/markov/approximation.py
Outdated
drawn from a multivariate t-distribution | ||
|
||
>>> mc = discrete_var(A, C, grid_sizes, | ||
... rv=scipy.stats.multivariate_t(shape=np.identity(2)), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @HumphreyYang, but it is the covariance matrix that must be the identity matrix.
Many thanks @oyamad. Sorry I forgot the constraint on the covariance matrix and accidentally removed your comment.
Do you think we should add these lines below into the docstring as well, in addition to my update to emphasize our constraint on the distribution?
>>> df = 100
>>> Sigma = np.diag(np.tile((df-2)/df, 2))
>>> t_dis = scipy.stats.multivariate_t(shape=Sigma, df=df).rvs(10_000_000)
>>> np.round(np.cov(t_dis.T), 2)
array([[1., 0.],
[0., 1.]])
…Econ.py into VAR-approximation-2
No, I think the update in a85a6cd is just fine. Thanks! |
@HumphreyYang Will you cherry-pick the minor chnages in 496942d? Thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tests all green. I think this is ready to merge.
Many thanks @oyamad, @crondonm, @HumphreyYang and all other contributers. @mmcky , I think this is ready to merge. |
thanks everyone for your input on this. LGTM. |
Hi,
As discussed with @jstac, I am submitting the routine discrete_var.py discussed in #639.
The routine is based on the work `Finite-State Approximation Of VAR Processes: A Simulation Approach'' by Stephanie Schmitt-Grohé and Martín Uribe, July 11, 2010.
I am attaching the Authors' Matlab file for comparison. Also, I am attaching a notebook to facilitate testing
Discrete_VAR.zip