-
-
Notifications
You must be signed in to change notification settings - Fork 199
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
HARK.algos #1283
HARK.algos #1283
Conversation
Seb, This looks awesome! Thanks for taking the initiative and realizing there was something useful to be done on this front, and just doing it! |
Thanks Chris. You give me too much credit. I realized this was what needed to happen after hearing your concerns with what I was doing with BARK. I think this incremental way forward is going to work better. |
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## master #1283 +/- ##
==========================================
- Coverage 72.55% 71.27% -1.29%
==========================================
Files 78 83 +5
Lines 13009 13667 +658
==========================================
+ Hits 9439 9741 +302
- Misses 3570 3926 +356
☔ View full report in Codecov by Sentry. |
[ ] CDC: Potentially rename/document |
HARK/algos/egm.py
Outdated
@@ -0,0 +1,125 @@ | |||
|
|||
|
|||
def analytic_pi_star_y(self, |
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.
'acted'
'retro-policy'
What to call this?
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.
how about 'chosen'
The EGM test is now working! The core substance of this PR is, in my view, complete. Remaining TODOs:
Questions raised by this PR (hopefully not blockers, but wanted to bring them up)... Do we have a canonical way of representing grids over multiple variables?
What I've implemented in this PR fudges things a bit:
What I think might be a good move going forward is:
I wonder if @alanlujan91 @mnwhite or @Mv77 have any thoughts on grid representations at this point. |
EGM test works locally with Python 3.10, but apparently not in the automated Python 3.8 test. bummer. Will fix |
Neat! Yes, some thoughts on grid representations are that it would be good to have some I looked into this a bit and came out with your same impression that Something that I think would be huge in our goal of representing models using transition equations would be to have an integration between this state representation and our tools in |
The EGM algorithm now matches the target values up to a precision of codecov is very helpfully pointing out which lines of code are not covered by automated tests! However, the |
I saw that and I'm very glad that we are converging. I am interested in your transition matrix PR and will follow up with you on that once I've get a better sense of it.
I understand. I believe @alanlujan91 did some work on using xarrays in our distribution classes. Maybe we should move more robustly in that direction. Do you see stochastic transitions as ever being more complex than the application of a function, say What I'm hoping this PR lets us do is break down the existing models into their fundamental equations, which can then be passed around to construct new objects like this. |
We currently have a
I don't think so, but that depends on what we allow to be in the 'equation.' For instance, the transition from |
@Mv77 you might find this interesting
In that file, I take expectations of full transitions which contain many variables all at once, including value and marginal value
To do this, I represent the state as a DataArray, and all other functions that depend on the state are included in a Dataset. I do think xarray is a very important building block for future HARK iterations. |
Aha, @alanlujan91 thank you for reminding me about the ConsLabeledType, which is great work. I see now that you have picked out good names for a lot of the key features, and have a general EGM method as a class method:
I hope these standalone EGM and FOC algorithms still can stand as contributions. @alanlujan91 I wonder if you have any thoughts on how to improve this EGM implementation, given the one in ConsLabeledType. To some extent, making the change to use xarray as a base means deciding to refactor the old models and tools to use it, rather than putting 'Labeled' on everything. I personally would be +1 to working in that direction. I'll bring this up, and some related ideas, in some other issues. |
Looking at your implementation @alanlujan91 I'm noticing that there doesn't seem to be a very good reason for functions like this to be object methods, since they don't invoke If so, in order to reduce redundancy, I'd suggest that this sort of functionality get moved to I'll try to lift design ideas from your version when I do my code cleanup in this PR. |
I have written documentation for this feature and updated the CHANGELOG. Requesting review from @mnwhite . The docs look a little funny because for some reason we need to very selectively escape backslashes in the One question, which has come up before, is what mathematical notation to use for the |
My big piece of feedback here is that we need to be clear about taxonomy and labeling of variable types, and what they mean. The same piece of information can be two different "types" of variables at different "moments" in a problem. As written, algos.foc has the policy function map from STATES x SHOCKS to CONTROLS, but it should just map from STATES to CONTROLS. The information in an element of the STATES-space is (or more precisely, should be) the lowest dimensional sufficient statistic for the agent's payoff-relevant information set at the moment that they take an action. It might be that a piece of information that was randomly drawn is part of that information set-- e.g. a preference shock in the ConsPrefShock model. However, that piece of information is a STATE at the moment the action decision is made. In the opposite direction, for the base case you're working with (ConsIndShock), you don't want what would we would think of as the SHOCKS to be part of the domain of the policy function. That is, once I know m_t, I don't need to know theta_t nor psi_t-- they're not future-payoff-relevant information, and m_t is the sufficient statistic for future behavior. We want a policy function that maps from m_t to c_t, not one that maps from m_t X theta_t X psi_t to c_t. Here's my general concept of both models and the desired structure for a HARK "frame" or whatever we're calling it. Variable types include: CONTROLS : the set of actions the agent can take Generally, there is some function that maps from PRE-STATES x SHOCKS to STATES. Some policy function (optimal or otherwise) maps from STATES to CONTROLS. Then another function maps from STATES and CONTROLS to POST-STATES. In single-frame models, the space of POST-STATES is the same as the space of PRE-STATES, but in general there's some mapping to the PRE-STATES of the next frame, which might just be a re-labeling. Just as sometimes information that arose in SHOCKS is also explicitly part of STATES, in some models one or more CONTROLS needs to go in POST-STATE as well. This includes the portfolio choice model. |
Thanks @mnwhite . I follow all this. I think it shouldn't be hard to change the code to collapse what are current state and action spaces into one space. Let me get back to you about this once I've worked on that a bit. |
This PR aims to provide two general purpose algorithms for the optimization 'step' of consumer problems, with automated tests confirming their results replicate teh SolvingMicroDSOPs results to a high degree of precision.
I'm hoping that this:
expectations
method.) This provides an implementation path from HARK's current state to HARK 1.0.It introduces two new subdirectories for HARK modules:
HARK.algos
is intended to contain general purpose algorithms, and their tests. C.f.dolo.algos
(link)HARK.gothic
is a direct copy/fork of thegothic_class
andresources
files from SolvingMicroDSOPs. These are used in the automated tests for the algorithms.The two algorithms provided are:
foc.py
, solving the optimal policy decision based on numerically solving the first order conditionegm.py
, solving the optimal policy decision analytically using EGMAs of this writing, there is a passing test for the
optimal_policy_foc()
method. The substantive test for the EGM algorithm is still a work in progress. Most of this code is from the BARK PR #1186 . But what I have done here is separate out the algorithmic component, made it more general (see below), and provided more robust testing (relative to SolvingMicroDSOPs).The algorithms provided generalize from BARK in that they do not depend on a$X$ for states, $Z$ for shocks, and $A$ for actions. I think this leaves $Y$ as a natural choice for post-states. I suppose that could be debated.
Stage
object explicitly. Rather, they are stand-alone functions. However, they do rely on some notation that we've settled on in BARK. Notably, I'm following Sargent and Stachurski and using:I believe these algorithms can be used in existing HARK solvers, allowing some hard-coded mathematical statements to be replaced with mathematical functions. I think that moving in this direction will make it easier to move HARK towards HARK 1.0 while maintaining backwards compatibility.