[likelihood_bayes.md] Update np.random → Generator API#977
Open
Chihiro2000GitHub wants to merge 1 commit into
Open
[likelihood_bayes.md] Update np.random → Generator API#977Chihiro2000GitHub wants to merge 1 commit into
Chihiro2000GitHub wants to merge 1 commit into
Conversation
40 tasks
📖 Netlify Preview Ready!Preview URL: https://pr-977--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
likelihood_bayes.mdas part of QuantEcon/meta#299.The lecture used the
@jit-basedset_seed()/np.random.seedidiom for reproducibility. It is replaced with an explicit seeded generatorrng = np.random.default_rng(142857)that is passed into the simulation functions.Details
@jit def set_seed(): np.random.seed(142857)mechanism withrng = np.random.default_rng(142857). The lecture reset the seed at two points (setup cell and before the mixture path), sorngis (re)created at both points to preserve that behaviour.np.random.beta→rng.betaandnp.random.rand()→rng.random(), and threadedrngas an explicit argument throughsimulate,simulate_mixture_path,martingale_simulate(and itsfraction_0_1/create_tablewrappers), andcompute_cond_var, updating the call sites.142857); the original lecture already seeded for reproducibility, so no new seed was introduced.Note for reviewers
compute_cond_var(theprangecase): the original used@jitwith aprangeloop containing the RNG draws, but there is noparallel=Trueanywhere in the file, so the loop already ran serially. Rather than lifting the draws out (which, since the loop's only content was the sampling, would dissolve the loop entirely and defeat the "pass a pre-drawn array into a jittedprangekernel" pattern), I changedprange→rangeso the function becomes an ordinary serial@jitfunction like the others, and passedrngin as an argument. This changes no runtime behaviour (it was already serial). If you intended this to be parallelized, the RNG should instead be drawn outside and passed in — happy to switch to that.prangeis now unused, so I also removed it from thefrom numba import ...line.simulate,simulate_mixture_path,martingale_simulate) have noparallel=True/prange, sorngis passed in explicitly (Case 2). Verified locally that passing aGeneratorinto these@jitfunctions and callingrng.beta/rng.randomcompiles and runs under Numba 0.62.1.142857is retained, so results remain reproducible.Hi @mmcky and @HumphreyYang, I'd be grateful if you could take a look when you have time.