[inventory_q.md] Update np.random → Generator API - #1012
Open
Chihiro2000GitHub wants to merge 1 commit into
Open
[inventory_q.md] Update np.random → Generator API#1012Chihiro2000GitHub wants to merge 1 commit into
Chihiro2000GitHub wants to merge 1 commit into
Conversation
56 tasks
📖 Netlify Preview Ready!Preview URL: https://pr-1012--sunny-cactus-210e3e.netlify.app Commit: 📚 Changed LecturesBuild Info
|
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
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
inventory_q.md. Issues #881 and #882 concern other aspects of this lecture (the demand grid and the Q-learning loop), and this PR does not attempt to address them.Details
sim_inventoriestakesrngin place ofseed, and draws withrng.geometric(p).q_learning_kerneltakesrngin place ofseed, and draws withrng.geometric(p),rng.integers(...)andrng.random().q_learningwrapper 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 optimal policy and the Q-learning snapshots still face the same demand sequence, as they did when each call re-seeded.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. On a 5-million-iteration benchmark the Generator version ran at the same speed as the legacy calls.inventory_q.mdexecuted without errors.Note for reviewers
Both jitted functions take
rngin place ofseed, which changes their signatures.q_learningkeeps 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.