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 proper Krusell-Smith model #762

Merged
merged 9 commits into from
Jul 21, 2020
Merged

Add proper Krusell-Smith model #762

merged 9 commits into from
Jul 21, 2020

Conversation

mnwhite
Copy link
Contributor

@mnwhite mnwhite commented Jul 20, 2020

CDC has long wanted HARK to be able to solve the Krusell-Smith (1998) model verbatim; this PR adds that capability. Previously, we had "fake KS", as our Markov ConsAggShock model didn't have serially correlated unemployment.

This PR includes new AgentType and Market subclasses in ConsAggShockModel.py, as the Krusell-Smith model is so specialized that trying to shoehorn trivial versions of existing HARK model objects would just overcomplicate things. The one compromise with HARK is that the aggregate saving rule (regressing log A_t on log M_t conditional on the state of the economy) is still the equilibrium object, rather than a straight log K_t on log K_{t-1} like in the Krusell-Smith paper. HARK's existing models use the "A on M" approach because we have aggregate risk other than the binary state of the macroeconomy, so K_{t-1} is not a sufficient statistic for the aggregate state in period t.

Example has been added to the ConsAggShock example file, replacing the old "fake KS" example. Tests have also been added. Default parameters are set to those in the KS paper, with the exception of LbrInd (\tilde{\ell}), whose value is not provided in the paper as far as I can tell. Their results are nearly identically replicated.

This code does not handle the KS version of discount factor heterogeneity, as CDC did not reply to my query about whether he wanted that included.

mnwhite added 7 commits July 16, 2020 21:30
CDC really wants to be able to solve KS, so I wrote a solver just for that model.  Completely untested, and needs an AgentType and Market to go with it.  First 2/3 of the solution code can be done in pre-computational step.
Walked Mridul through KS model math and solver and began writing AgentType subclass for it.  Still need to do the obnoxious arithmetic of the good-bad bad bad-good idiosyncratic transitions.
Made tiny example file, solveKrusellSmith seems to work as intended.  Need to make a special Market subclass for it.
Had to force idiosyncratic shocks to exactly match specified probabilities, but it works.

Need to clean up model file and do a bit of work to speed up the process.
Default KS model parameters now more closely match version in their paper, but it also takes a shockingly long time to solve.  Moved dictionaries into model file, will move example code next.
Also deleted figure-producing progress code lines.
These should pass and don't add much to test time.
@alanlujan91
Copy link
Member

I have ell tilde in my class notes as 0.3271, if that helps

@mnwhite
Copy link
Contributor Author

mnwhite commented Jul 20, 2020 via email

@alanlujan91
Copy link
Member

I don't think the number is on the paper. As far as I remember, the source of this number is my prof. emailing one of the authors.

@mnwhite
Copy link
Contributor Author

mnwhite commented Jul 21, 2020 via email

# This can be written more concisely, but is intentionally explicit for
# the sake of making it very easy to read
if self.MrkvNow == 0: # Bad macroeconomic conditions
these = np.logical_not(self.EmpNow) # Unemployed agents
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe name this variable unemployed_agents instead of these.
Save you having to explain it in comment :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose, but this is used throughout HARK for binary indexing arrays.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair enough. just a suggestion.

# Add consumer-type specific objects
self.time_vary = []
self.time_inv = ['DiscFac', 'CRRA',]
self.poststate_vars = ['aNow','EmpNow']
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a critique of this code but a general question...

I see that the poststate_vars are listed explicitly here.
Would it be silly to list (a) the regular states, (b) the control variables, and (c) the shocks in lists like these?
I mean, in a future implementation.

That would make it easier to do things like #760 and #761

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That can be done. They're listed here and in all HARK models because initializeSim() initializes them.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool. I think I can demo how this would look in a PR for the next weekly meeting.

Mcount = self.Mgrid.size

# Make tiled array of end-of-period idiosyncratic assets (order: a, M, s, s')
aNow_tiled = np.tile(np.reshape(self.aGrid, [aCount, 1, 1, 1]), [1, Mcount, 4, 4]) # shape (aCount, Mcount, 4, 4)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general, I think the code formatting of this PR is really nice.
I was going to ask if you used black for it?

I'm surprised by this long line (in terms of number of characters) though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I didn't use black. All of my comments with the shape of arrays can be removed (will do that now). I put those in while explaining the code the Mridul.

@mnwhite
Copy link
Contributor Author

mnwhite commented Jul 21, 2020

Ack, just caught that I forgot to put pre-computed arrays into the solveKrusellSmith docstring.

@sbenthall
Copy link
Contributor

I've made a few minor comments.
Otherwise, this looks good to me.
I'm probably not the best person to judge the economics of it, but I'd be happy to merge if there's no objections, after Matt gets in his "ack!' fix.

Fixed and expanded some comments, added comparison of our results to KS' published numbers (in the example).  Adjusted LbrInd parameter to use value that Alan Lujan says his prof got from KS.  Future readers: please cite this chain for the parameter value missing from Krusell-Smith (1998).
self.makeGrid()
self.updateSolutionTerminal()

def getEconomyData(self, Economy):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be lower case -- see how the GitHub syntax highlighting misformats this as a class name because it is capitalized.

I see this is common to other uses of getEconomyData though.

self.MrkvIndArray = Economy.MrkvIndArray # Transition probabilities among discrete states
self.MrkvAggArray = Economy.MrkvArray # Transition probabilities among aggregate discrete states
self.addToTimeInv('Mgrid', 'AFunc', 'DeprFac', 'CapShare', 'UrateB', 'LbrInd',\
'UrateG', 'ProdB', 'ProdG', 'MrkvIndArray', 'MrkvAggArray')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See #764 . If those aspects of the economy that needed to be brought into the Agent were available in a list (as most of them are here), then it would be possible to write this method generically as a loop over those values, rather than writing out each one individually.

This is common across all uses of getEconomyData

@mnwhite
Copy link
Contributor Author

mnwhite commented Jul 21, 2020 via email

@sbenthall
Copy link
Contributor

Made #766 for that issue.
I'll work on it after this PR is in so as not to block it.

@sbenthall
Copy link
Contributor

Is this PR ready to merge, from your perspective, @mnwhite ?

@mnwhite
Copy link
Contributor Author

mnwhite commented Jul 21, 2020 via email

@sbenthall sbenthall merged commit 112a01a into master Jul 21, 2020
@mnwhite
Copy link
Contributor Author

mnwhite commented Jul 21, 2020 via email

@sbenthall
Copy link
Contributor

! Oh, perhaps I merged too soon !

Yes, I think what you are looking for is jupytext

https://jupytext.readthedocs.io/en/latest/using-cli.html

@mnwhite
Copy link
Contributor Author

mnwhite commented Jul 21, 2020 via email

@MridulS MridulS deleted the AggShockMicroMarkov branch August 24, 2020 18:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants