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

The change in how distributions are handled seems to have messed simulations up #750

Merged
merged 3 commits into from
Jul 3, 2020

Conversation

Mv77
Copy link
Contributor

@Mv77 Mv77 commented Jul 2, 2020

In my restoration of the CGMPortfolio remark, I think I've run into a couple of issues with RNG in simulations.

This PR deals with the fact that the random seed now has to be passed to the distribution's init method and not the draw method.

However, my current simulated income paths look like this, which does not seem right
image
It looks as if there was invididual-level variation in growth rates, but they stayed constant over time?

I'm looking into it but the income distribution RNG is more complicated so I wanted to ask if there is some obvious answer to what is going on there.

@project-bot project-bot bot added this to Needs Triage in Issues & PRs Jul 2, 2020
@mnwhite
Copy link
Contributor

mnwhite commented Jul 2, 2020 via email

@mnwhite
Copy link
Contributor

mnwhite commented Jul 2, 2020 via email

@mnwhite
Copy link
Contributor

mnwhite commented Jul 2, 2020

Ha, I didn't realize this was a PR-- I misread the email I received and thought it was an issue. I made the two line fixes you have here, and the example file now works correctly for me-- I'm not getting the pattern you are.

In example_ConsPortfolioModel.py in your branch, add 'pLvlNow' to track_vars for MyType (whoops, I need to rename that first example) and solve and simulate that type (for speed/laziness, just throw a breakhere after the simulation so it doesn't do the rest of the examples). Then do plt.plot(MyType.history['pLvlNow'][:,0:5], '.') and you'll see some example permanent income paths like the figure you have above; they move around exactly as you'd expect.

What code are you running that gives that bad picture?

@Mv77
Copy link
Contributor Author

Mv77 commented Jul 2, 2020

I just checked the example file as you suggested and it works, pLvls do vary erratically as expected.

The strange picture comes from running this file: https://github.com/Mv77/REMARK/blob/fixCGMPortfolio/REMARKs/CGMPortfolio/Code/Python/Simulations/FewAgents.py, which used to work before. Seeing that the example works now makes me think it might be a particular issue, maybe with the calibration.

I'll have a dig at it after dinner. (this is the last file that needs to work before the CGM remark is completely restored)

@mnwhite
Copy link
Contributor

mnwhite commented Jul 2, 2020 via email

@Mv77
Copy link
Contributor Author

Mv77 commented Jul 2, 2020

I'm getting closer to the bug. It seems to be the "draw_events" method of the income_dstn that is getting used in line 2027 of ConsIndShockModel.py. Here are the events being drawn for 5 different agents (columns) through their lives (rows)
image
Indeed, the same permanent shock is being drawn every period for a given agent. I will keep looking to fix it myself but wanted to share in case you do look at it.

My hypothesis of why example_ConsPortfolioModel.py works and this one does not is that in the example, the same distribution object is being used to draw events for every simulation period. In my file, being a life-cycle problem, there is a list of distribution objects, one for every period. Maybe the seed of each object is being set to the same number. I'll investigate!

@Mv77
Copy link
Contributor Author

Mv77 commented Jul 3, 2020

Yep. All are being created with the default seed (0). Fixing now!

@mnwhite
Copy link
Contributor

mnwhite commented Jul 3, 2020

Your hypothesis might be correct, but this feature isn't showing up in the lifecycle example in example_ConsIndShock.py ... Permanent income shocks jump around for each agent just as you'd expect.

@mnwhite
Copy link
Contributor

mnwhite commented Jul 3, 2020

Same thing with the portfolio lifecycle example in example_ConsPortfolioModel.py (with completely cockamamie parameters)-- income shocks draw as expected. This is weird.

@mnwhite
Copy link
Contributor

mnwhite commented Jul 3, 2020

But then why is this only happening for that one case? It's a one line fix when the IncomeDstn elements are created (which it looks like you're doing now), but this should come up in the example files as well as the CGM REMARK file.

@Mv77
Copy link
Contributor Author

Mv77 commented Jul 3, 2020

My last commit seems to fix the issue.

To the best of my understanding, the issue arose when combining the transitory and permanent distributions of the income process using 'combineIndepDstns'. The method creates the combined distribution without a seed, so it just defaults to 0. I added the option to pass a seed and used it.

I tested both my file and the example and they work.

@Mv77
Copy link
Contributor Author

Mv77 commented Jul 3, 2020

But then why is this only happening for that one case? It's a one line fix when the IncomeDstn elements are created (which it looks like you're doing now), but this should come up in the example files as well as the CGM REMARK file.

The example file (at least the portfolio one) works because T_cycle = 1. So there is a single distribution object. The seed starts at 0, HARK takes a draw, and next period it takes a draw from the same object. So the draws are different.

The life-cycle problem from my file creates a list of 60(?) incomeDstn's, all initialized with seed 0. So HARK takes a draw from the 1st element when t_cycle=0, and from the second when t_cycle = 1. Since both objects start out with the same seed, the draws are the same?

@mnwhite
Copy link
Contributor

mnwhite commented Jul 3, 2020 via email

@mnwhite
Copy link
Contributor

mnwhite commented Jul 3, 2020

Anyway, your code changes look correct, and I'm glad this fixed the problem. I'll need to adjust the simulation-based targets for some tests, as the seeds have changed, then I'll merge this branch.

@llorracc
Copy link
Collaborator

llorracc commented Jul 3, 2020 via email

@Mv77
Copy link
Contributor Author

Mv77 commented Jul 3, 2020

I get all that. But I ran other lifecycle examples and they simulated correctly. That's what's confusing.

Got it!

In the example, the default parameters set mortality rates quite high, to illustrate the life-cycle point. So the number of alive agents changes a lot, and the varying number of draws is what generates variation in the shocks.

You can check this by setting init_lifecycle['LivPrb'] = [1]*10 in the life cycle example.

That one was hard to catch 😩 .

@llorracc
Copy link
Collaborator

llorracc commented Jul 3, 2020

That one was hard to catch

and a perfect example of why the default should be to always initialize with a time-based seed. Like the Spanish Inquisition, nobody expects that the results of their simulations will be importantly different if the probability of living is 0.9999 instead of 1.000

PS. But the build is still failing ...

@mnwhite
Copy link
Contributor

mnwhite commented Jul 3, 2020 via email

Mateo's fixes to the RNG seeding (obviously) changed seeds, so some simulation-based tests had their targets changed.
@mnwhite
Copy link
Contributor

mnwhite commented Jul 3, 2020

Tests will pass after latest commit, will merge. No release notes are necessary, as this just makes tiny adjustments to account for RNG changes in Seb's prior PR; release notes for that PR would encompass these changes.

That said, we might want to look through other RNG-using methods and make sure these kinds of changes are made there. In particular, the medical shocks model and the Markov shocks.

@mnwhite mnwhite merged commit d302243 into econ-ark:master Jul 3, 2020
Issues & PRs automation moved this from Needs Triage to Done Jul 3, 2020
@llorracc
Copy link
Collaborator

llorracc commented Jul 3, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Issues & PRs
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

3 participants