[wealth_dynamics.md] Update np.random → Generator API - #989
Conversation
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Rs2tDEcDXLnfMnM88v8e1D
📖 Netlify Preview Ready!Preview URL: https://pr-989--sunny-cactus-210e3e.netlify.app Commit: 📚 Changed LecturesBuild Info
|
|
Thanks @Chihiro2000GitHub . This looks good. Can you please confirm that no earlier PRs also touch this lecture. |
|
Thanks @jstac! Confirmed — no other PRs touch this lecture. The earlier PRs that touched it are either already merged into main (#574, #581, both reflected in this branch) or were closed without merging (#513, #626, #628, #630, #632, #634, #636, #638, #639 — earlier style-guide / JAX-conversion attempts). I also re-checked open issues: the only one relevant to this migration is #970 (random seed in the parallel #512 also touches this lecture, but it's just a title-formatting issue, unrelated to this RNG migration. For context, I did survey open PRs and issues when I created this PR, but the PR body didn't make that survey explicit — I've now lightly edited it to state it clearly. |
|
Hi @Chihiro2000GitHub , thanks for the update. I'm a bit confused. Do these statements contradict each other?
|
|
Hi @jstac Sorry, my wording was not clear. There is no contradiction — I was talking about two different things: ・No other PR touches this lecture. So there is no open PR that overlaps with this one. |
|
Thanks @Chihiro2000GitHub . I should have looked at #512 myself. In this context 'touching' refers to changing, at least for our team, but the English is unusual and probably confusing. This PR looks good to me. It's good that you left the parallel code. Merging. |
|
Thanks @jstac! I understand now — since an issue does not change anything, saying an issue "touches" the lecture is confusing. I see what you mean now, and I will keep it in mind going forward. Thanks for the review and for merging! |
Summary
This PR migrates legacy NumPy random API usage in
wealth_dynamics.mdas part of QuantEcon/meta#299.The migration is limited to the ordinary-Python code cells. The code around
update_cross_sectionis intentionally left unchanged — see Details.Related PRs and issues
Before opening this PR I surveyed open PRs and issues touching this lecture:
wealth_dynamics.md.main(reflected in this branch): #574, #581.update_cross_sectioncode; deliberately left for a follow-up (see Details).Details
In the plain Python cells,
np.random.*calls are replaced with an explicitrng = np.random.default_rng().rngis defined at its first use in the main text and reused afterwards; thewd_ex1solution block defines its ownrngto stay self-contained. No fixed seed is introduced.Code around
update_cross_sectionleft unchanged (deliberate). While reviewing this file I found the pre-existing issue #970 ("Add random seed to wealth dynamics lecture"). It concernsupdate_cross_section, which is@jit(parallel=True)and draws unseedednp.random.randn()inside aprangeloop, causing the Gini-vs-μ_rfigure to change on every rebuild. Since that discussion is still open, I've deliberately left that code — and the related code that depends on it — untouched in this PR:update_cross_sectiondraws inside aprangeloop, where Numba's Generator support is not thread-safe.update_statesis used by both the parallelupdate_cross_sectionand the non-parallelwealth_time_series, so it cannot be migrated on its own.wealth_time_series(line ~368) would leave the per-period shocks (drawn viaupdate_states) on the legacy globalnp.randomstate, mixing two RNG sources within a single simulation — inconsistent, and it would not actually remove the hidden global state from that code path.If you'd like, I'm happy to handle this
update_cross_sectioncode (RNG migration and/or the #970 reproducibility fix) in a separate follow-up — just let me know how you'd prefer to proceed.Hi @mmcky and @HumphreyYang, I'd be grateful if you could take a look when you have time.