[mix_model.md] Update np.random → Generator API#978
Merged
Conversation
56 tasks
📖 Netlify Preview Ready!Preview URL: https://pr-978--sunny-cactus-210e3e.netlify.app Commit: 📚 Changed LecturesBuild Info
|
Contributor
|
Many thanks @Chihiro2000GitHub , nice work. There is, however, an issue of overlapping PRs. I'll write separately to explain. |
jstac
pushed a commit
that referenced
this pull request
Jul 19, 2026
Rebased onto main on top of the style-guide branch (#642). Converts the Numba/NumPy simulation code to JAX (jax.random keys, jax.lax.scan, jax.vmap), replacing the interim np.random Generator migration from #978. Keeps #642's link/heading/prose fixes. Verified: lecture runs end-to-end (JAX cells + numpyro MCMC) on CPU.
jstac
pushed a commit
that referenced
this pull request
Jul 19, 2026
jstac
pushed a commit
that referenced
this pull request
Jul 19, 2026
Rebased onto main (now containing the #642 style pass). Converts the Numba/NumPy simulation code to JAX (jax.random keys, jax.lax.scan, jax.vmap), superseding the interim np.random Generator migration (#978). draw_lottery_MC now uses jax.scipy.stats.beta.cdf and a jax.random key instead of scipy.stats + np.random, so the lecture no longer touches NumPy's legacy RNG at all. Verified: full lecture (JAX cells + numpyro NUTS MCMC) runs end-to-end on CPU; MC draws match the target mixture density. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
jstac
pushed a commit
that referenced
this pull request
Jul 19, 2026
Rebased onto main (post #642 style pass). Converts the Numba/NumPy simulation code to JAX (jax.random keys, jax.lax.scan, jax.vmap), superseding the interim np.random Generator migration (#978). Randomness uses explicit jax.random.key threading (keys passed into simulate / draw_lottery / draw_lottery_MC / simulate_mixed / plot_π_seq / π_lim, split where needed) rather than deriving seeds from parameter values, matching the JAX conversion style guide. draw_lottery_MC uses jax.scipy.stats.beta.cdf, so the lecture no longer touches NumPy's legacy RNG. Single seed 42 throughout. Verified: full lecture (JAX cells + numpyro NUTS MCMC) runs end-to-end on CPU; MC draws match the target mixture density. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
jstac
added a commit
that referenced
this pull request
Jul 19, 2026
* [mix_model] Convert code to JAX Rebased onto main (post #642 style pass). Converts the Numba/NumPy simulation code to JAX (jax.random keys, jax.lax.scan, jax.vmap), superseding the interim np.random Generator migration (#978). Randomness uses explicit jax.random.key threading (keys passed into simulate / draw_lottery / draw_lottery_MC / simulate_mixed / plot_π_seq / π_lim, split where needed) rather than deriving seeds from parameter values, matching the JAX conversion style guide. draw_lottery_MC uses jax.scipy.stats.beta.cdf, so the lecture no longer touches NumPy's legacy RNG. Single seed 42 throughout. Verified: full lecture (JAX cells + numpyro NUTS MCMC) runs end-to-end on CPU; MC draws match the target mixture density. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> * [mix_model] Style-guide compliance pass Bring the lecture into full style-guide compliance: - figures: remove matplotlib titles (ax.set_title), drop hard-coded figsize, use lw=2 on all line charts, lower-case axis labels - math: \mathbb{E} / \mathbb{P} for expectation/probability, \mathrm{Beta} for the distribution name, \log instead of log - writing: IID (not i.i.d.), Bayes' law (not Bayes' Law / Bayes law) - doclinks: same-series links use the [](name) auto-title form and drop the generic 'this quantecon lecture' text - code: don't pip install jax at the top (GPU admonition covers it), remove the unused pandas import Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: John Stachurski <john.stachurski@gmail.com>
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
mix_model.mdas part of QuantEcon/meta#299.The lecture used the module-level
np.random.seed/@jit set_seed()idiom for reproducibility. It is replaced with an explicit seeded generatorrng = np.random.default_rng(142857)that is passed into the simulation functions.Details
np.random.seed(142857)/@jit def set_seed()mechanism withrng = np.random.default_rng(142857). The lecture reset the seed at two points (setup cell and before the long-horizon paths), 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,draw_lottery,draw_lottery_MC,simulate_mixed, andplot_π_seq, updating all call sites.142857); the original lecture already seeded for reproducibility, so no new seed was introduced.Note for reviewers
π_lim(used withnp.vectorize):π_limis wrapped byπ_lim_v = np.vectorize(π_lim)and callssimulate_mixed. Addingrngas a positional argument would makenp.vectorizetry to broadcast it, so I leftπ_lim's signature unchanged and let it reference the shared module-levelrnginside. This reuses the same single generator, just via closure rather than an argument. The explicit-argument alternative would be:rngexplicit everywhere.numpy.random.choicefunction to flip an unfair coin". I updated it to "use therng.choicemethod …" to match the Generator API. Note the actualdraw_lotterycode implements the coin flip withrng.random() <= prather thanrng.choice, so this is a conceptual description — let me know if you'd rather word it differently.x=0.5convergence figure: under the new generator the realized 200-step path in the "three different prior beliefs" figure (T_mix = 200) converges less visibly tox=0.5than before, though the text says the posterior means "eventually converge". The theoretical statement still holds; if you'd like the figure to match more tightly,T_mixcould be increased. I left it unchanged to keep this PR focused on the RNG migration.@jitfunctions (simulate,draw_lottery) have noparallel=True/prange, sorngis passed in explicitly (Case 2). The numpyro/JAX MCMC usesjax.random.PRNGKey, which is not annp.random.*call and is left unchanged. Verified locally that the notebook compiles and runs under Numba 0.62.1.Hi @mmcky and @HumphreyYang, I'd be grateful if you could take a look when you have time.