Skip to content

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

Merged
mmcky merged 2 commits into
mainfrom
update-rng-likelihood-bayes
Aug 12, 2026
Merged

mmcky merged 2 commits 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.beta → rng.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 prange → range 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

github-actions Bot commented Jul 18, 2026 •

Copy link
Copy Markdown

📖 Netlify Preview Ready!

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

Commit: 0e9433a

📚 Changed Lectures


Build Info

Comment thread lectures/likelihood_bayes.md Resolved
Comment thread lectures/likelihood_bayes.md Resolved
@mmcky

mmcky commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Co-authored-by: mmcky <8263752+mmcky@users.noreply.github.com>

Copilot AI commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Resolved the merge conflicts in this PR in commit 0e9433a (merged origin/main and reconciled lectures/likelihood_bayes.md).

@Chihiro2000GitHub

Copy link
Copy Markdown
Contributor Author

Thanks @mmcky — sorry these were mine to answer and you got there first.

Both replies are exactly what I would have said, so I have nothing to add; much appreciated, including the conflict resolution.

I'll take it from here and check the rebuilt preview once the build finishes.

@mmcky

mmcky commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Thanks @mmcky — sorry these were mine to answer and you got there first.

Both replies are exactly what I would have said, so I have nothing to add; much appreciated, including the conflict resolution.

I'll take it from here and check the rebuilt preview once the build finishes.

Thanks so much @Chihiro2000GitHub. Appreciate the review.

@Chihiro2000GitHub

Copy link
Copy Markdown
Contributor Author

@mmcky -- I've now gone through the rebuilt preview myself and it looks good.

The one visible difference from main is Fig. 29.3 (fig-posterior-mixture), where $\pi_t$ converges to 1 noticeably earlier. The migration changes the draw sequence, so the realized path behind that figure is different. It doesn't contradict the prose that follows ("Evidently, $\pi_t$ converges to 1"), so I'd leave it as is.

Everything else builds and reads exactly as it should, so no concerns at all from my side.

@mmcky

mmcky commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

thanks @Chihiro2000GitHub.

@kp992 if you disagree -- feel free to open an issue. I'll merge this now.

@mmcky
mmcky merged commit 06c784f into main Aug 12, 2026
1 check passed
@mmcky
mmcky deleted the update-rng-likelihood-bayes branch August 12, 2026 10:53
@mmcky

mmcky commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

✅ Translation sync completed (zh-cn)

Target repo: QuantEcon/lecture-python.zh-cn
Translation PR: QuantEcon/lecture-python.zh-cn#248
Files synced (1):

  • lectures/likelihood_bayes.md

@kp992

kp992 commented Aug 12, 2026

Copy link
Copy Markdown
Contributor

Thanks @mmcky and @Chihiro2000GitHub. I am fine with the current PR as it is aligning with the style guide.

mmcky added a commit to QuantEcon/lecture-python.zh-cn that referenced this pull request Oct 22, 2026
…la.solve port (#301)

* Advance four state files whose lectures already carry the source change

career, jv and odu: QuantEcon/lecture-python.myst#760 changed only the
H1 in each (Job Search renumbering), and #273 applied it here. The
H1s on main already read VII, VIII and IX.

likelihood_bayes: QuantEcon/lecture-python.myst#977 replaced the
global np.random seed with default_rng; #248 delivered that change, and
the file uses np.random.default_rng(142857) at the same two sites as
the source, but the state kept the older sha.

source-sha advanced to each file's last source commit and synced-at
stamped; model, mode, section-count and tool-version are left as they
were, since nothing was regenerated (the fa#160 convention).
`translate status` at v0.29.6 now reports all four ALIGNED.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* Advance mccall_q's state file too: its one outstanding change is applied

The state sha is 46cf999 (QuantEcon/lecture-python.myst#624). The only
later source change is QuantEcon/lecture-python.myst#760, which changed
just the H1 (Job Search IX -> X); #273 applied it, and the H1 here
reads 工作搜寻 X. The earlier default_rng change (QuantEcon/lecture-python.myst#959) and
QuantEcon/lecture-python.myst#624's typo, legend and loop-guard edits
are present.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* harrison_kreps: port the la.solve refactor by hand, and advance its state

QuantEcon/lecture-python.myst#669 replaced the explicit inverse in
price_single_beliefs with a linear solve. Its sync failed before it
could file anything (a missing anthropic-api-key input on 2026-08-05),
so the change never arrived. The source diff is seven lines of code in
one cell, so it is ported by hand rather than regenerating a lecture
the zh-cn editor has worked on: the code is the source's, and the three
comments are translated as this edition translates code comments.

The cell's code now matches the source (ASTs with string literals
masked); the lecture executes with numerical output identical to the
English. source-sha advanced and synced-at stamped; model, mode,
section-count and tool-version unchanged, as nothing was regenerated.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
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.

4 participants