[rs_inventory_q.md] Update np.random → Generator API - #1013
Merged
Conversation
56 tasks
📖 Netlify Preview Ready!Preview URL: https://pr-1013--sunny-cactus-210e3e.netlify.app Commit: 📚 Changed LecturesBuild Info
|
kp992
approved these changes
Jul 31, 2026
Contributor
✅ Translation sync completed (zh-cn)Target repo: QuantEcon/lecture-python.zh-cn
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR migrates legacy NumPy random API usage in
rs_inventory_q.mdas part of QuantEcon/meta#299.All of the random draws happen inside
@numba.jit(nopython=True)functions, so the two of them now take arngargument and the generator is built by the calling Python code.Related PRs and issues
I checked for open PRs and issues related to this lecture. No open PR modifies
rs_inventory_q.md. Issue #882 concerns the convergence message and the snapshot timing in the Q-learning loop, and this PR does not attempt to address it.Details
sim_inventoriestakesrngin place ofseed, and draws withrng.geometric(p).q_learning_rs_kerneltakesrngin place ofseed, and draws withrng.geometric(p),rng.integers(...)andrng.random().q_learning_rswrapper is ordinary Python, so it keeps itsseed=1234argument and builds the generator itself. Its call site is unchanged.0,1234,5678) are all kept, and no new seed was introduced.np.random.default_rng(sim_seed)for each call, so the panels still face the same demand sequence, as they did when each call re-seeded. This matters for the figure comparing risk sensitivities and for the figure comparing the Q-learning snapshots against the VFI policy.geometric,integersandrandomon aGeneratorpassed in as an argument, but does not support constructing one inside a jitted function, which is why the generator is built by the caller. There is noprangeorparallel=Truein this lecture, so a single generator is safe here.rs_inventory_q.mdexecuted without errors.Note for reviewers
Both jitted functions take
rngin place ofseed, which changes their signatures.q_learning_rskeeps itsseedargument so the call in the lecture is unchanged, butsim_inventoriesis called directly and now receives a generator at each call site.Hi @mmcky and @HumphreyYang, I'd be grateful if you could take a look when you have time.