Skip to content

[likelihood_bayes.md] Update np.random → Generator API#977

Open
Chihiro2000GitHub wants to merge 1 commit into
mainfrom
update-rng-likelihood-bayes
Open

[likelihood_bayes.md] Update np.random → Generator API#977
Chihiro2000GitHub wants to merge 1 commit into
mainfrom
update-rng-likelihood-bayes

Conversation

@Chihiro2000GitHub

Copy link
Copy Markdown
Contributor

Summary

This PR migrates legacy NumPy random API usage in likelihood_bayes.md as part of QuantEcon/meta#299.

The lecture used the @jit-based set_seed() / np.random.seed idiom for reproducibility. It is replaced with an explicit seeded generator rng = np.random.default_rng(142857) that is passed into the simulation functions.

Details

  • Replaced the @jit def set_seed(): np.random.seed(142857) mechanism with rng = np.random.default_rng(142857). The lecture reset the seed at two points (setup cell and before the mixture path), so rng is (re)created at both points to preserve that behaviour.
  • Migrated np.random.betarng.beta and np.random.rand()rng.random(), and threaded rng as an explicit argument through simulate, simulate_mixture_path, martingale_simulate (and its fraction_0_1 / create_table wrappers), and compute_cond_var, updating the call sites.
  • Kept the existing fixed seed (142857); the original lecture already seeded for reproducibility, so no new seed was introduced.

Note for reviewers

  • compute_cond_var (the prange case): the original used @jit with a prange loop containing the RNG draws, but there is no parallel=True anywhere 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 jitted prange kernel" pattern), I changed prangerange so the function becomes an ordinary serial @jit function like the others, and passed rng in 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. prange is now unused, so I also removed it from the from numba import ... line.
  • The other jitted functions (simulate, simulate_mixture_path, martingale_simulate) have no parallel=True/prange, so rng is passed in explicitly (Case 2). Verified locally that passing a Generator into these @jit functions and calling rng.beta / rng.random compiles and runs under Numba 0.62.1.
  • The fixed seed 142857 is retained, so results remain reproducible.

Hi @mmcky and @HumphreyYang, I'd be grateful if you could take a look when you have time.

@github-actions

Copy link
Copy Markdown

📖 Netlify Preview Ready!

Preview URL: https://pr-977--sunny-cactus-210e3e.netlify.app

Commit: 0b2fe2c

📚 Changed Lectures


Build Info

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant