Skip to content

mccall_q: draw from a seeded Generator, as intermediate's copy does - #78

Merged
mmcky merged 1 commit into
mainfrom
mccall-q-seeded-generator
Nov 22, 2026
Merged

mmcky merged 1 commit into
mainfrom
mccall-q-seeded-generator

Conversation

@mmcky

@mmcky mmcky commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor

What

The code change of QuantEcon/lecture-python.myst#959 (merged 2026-07-31), applied as it stands to dp's copy of mccall_q. The Q-learning code calls np.random.random() inside a numba jitclass, which np.random.seed(123) in the Python namespace does not reach. So the page's Q-learning results, and the figures drawn from them, change from build to build. intermediate's copy has drawn from a seeded Generator since QuantEcon/lecture-python.myst#959; dp's copy, which dp republishes from intermediate, did not follow.

Before After
np.random.seed(123) rng = np.random.default_rng(123)
np.random.random() (twice) rng.random()
draw_offer_index(self), temp_diff(self, qtable, state, accept), run_one_epoch(self, qtable, max_times=20000), run_epochs(N, qlmc, qtable) each takes rng, and every call passes it on

The patch is QuantEcon/lecture-python.myst#959's diff, which applies to dp's copy with no change. dp's copy differs from intermediate's in other ways too, which this leaves alone: intermediate has since renumbered the lecture (Job Search X), changed its headings to sentence case, corrected some typos, changed the legend of the plot_epochs figures, guarded its progress prints with n % max(1, N // 10), and dropped a repeated VFI computation. None of these affects the draws.

This is part of reporting lecture fixes upstream to each lecture's source of truth (QuantEcon/project-monorepo#77).

Validation

Done on 2026-11-22, a full validation: fresh builds with no cache of this pull request and of main, every notebook executed, both green. The evidence is in the comment below.

This changes lecture content, so it needs a person's review.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WT8bMacV9Lmy1VLUc6XJoL

The code change of QuantEcon/lecture-python.myst#959 (merged 2026-07-31),
applied to dp's copy of the lecture. The Q-learning code called
np.random.random() inside a numba jitclass, which np.random.seed(123) in
the Python namespace does not reach, so the page's results changed from
build to build. It now creates rng = np.random.default_rng(123) and passes
it to every method that draws.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01WT8bMacV9Lmy1VLUc6XJoL
@mmcky

mmcky commented Nov 22, 2026 •

Copy link
Copy Markdown
Contributor Author

Validation: a full validation

As for dp's last upgrade (#75): a fresh build with no cache of main and of this pull request, each executing all 51 notebooks in ghcr.io/quantecon/quantecon-build:latest (cache.yml). Each was then rendered by ci.yml, which restored exactly that build's cache (its key names the run) and executed nothing.

main at 5aaa712, 2026-11-22 this pull request at 3c81e7b, 2026-11-22
fresh build 45012310197 45012324588
render 45015664442 45015683422

mccall_q now gives the same results on every build. This pull request's own CI run when it opened, 45012239888, executed mccall_q independently of the fresh build above. The two executions agree:

  • every cell output is the same, apart from pip's notice in the first cell about its cache directory;
  • all six figures are byte-identical.

Against main. mccall_q's Q-learning results differ, as they must, since they now come from the seeded Generator rather than an unseeded stream:

  • the printed Q-table and value function;
  • the three figures drawn from them (the 3rd, 5th and 6th).

The other three figures, which do not depend on the draws, are byte-identical.

Build logs. No Sphinx warning on either side.

The rest of the HTML. 19 of 57 pages differ in all. Besides mccall_q, there are 17 pages of output noise, the same as between any two builds: unseeded draws, timings and solver printouts. There is also the status page, with its build times. The warnings in the cell outputs are the same on both sides. odu's solver prints one more "Error at iteration" progress line, because its unseeded draws converged in a few more iterations, as they vary between any two builds.

@mmcky
mmcky marked this pull request as ready for review November 22, 2026 11:17
Copilot AI adaptive review requested due to automatic review settings November 22, 2026 11:17

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

Regenerated Q-learning plots were unavailable for inspection, so a subject-matter reviewer must confirm that the seeded results still support the lecture’s convergence and quit/no-quit comparisons.

No open issues 🎉

What changed in this PR

Makes the McCall Q-learning lecture’s results and figures reproducible across builds by adopting the seeded random generator used in the upstream lecture.

Changes:

  • Replaces global NumPy seeding with np.random.default_rng(123).
  • Passes the generator through Q-learning methods and callers, using it for wage-offer and exploration draws.
File Description
lectures/​mccall_q.md Threads a seeded generator through Q-learning execution and plotting code.

Review effort: Balanced (adaptive)

@mmcky

mmcky commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Addendum: the new figures against intermediate's published page

intermediate's copy of this lecture has drawn from the same seeded Generator since QuantEcon/lecture-python.myst#959. Its published page, https://python.quantecon.org/mccall_q.html, read on 2026-11-22, can therefore be compared with this pull request's build.

  • The first figure (VFI against Q-learning) plots the same values as intermediate's.
  • The two plot_epochs figures plot the same curves, with the same mean errors:
Figure epochs 100 1,000 10,000 100,000 200,000
quitting allowed 2233.04 2150.79 1593.86 1264.33 1157.47
quitting not allowed 2147.81 1828.91 1547.65 1152.11 1094.10

Only the legend differs: intermediate has since moved it below the plot and rounds the errors to two places, a change this pull request does not bring.

What the prose says. The text after the first plot_epochs figure says the approximation "improves for longer epochs", and the error falls at every step in both figures. The section "Employed Worker Can't Quit" then says that "learning is slower" when quitting is not allowed. By mean error the figures do not show that: the error without quitting is lower at every epoch count. This is so on intermediate's published page too, and in dp's unseeded build on main. So it is a question about the lecture's text, which this pull request neither causes nor changes.

@mmcky

mmcky commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Validation correction: pages whose only difference is a figure

The evidence comment above says 19 of 57 pages differ. The comparison behind it compared a figure only when the page wraps it in a link, and this series doesn't, so a page whose only difference is a figure wasn't counted. With every figure now compared by content, 30 of 57 pages differ. The 11 added pages differ only in figures:

Page Figures that differ, and why
career figures 2 and 5 of 6, from qe.random.draw, which draws without a seed
jv figures 1 and 2 of 3, from unseeded draws (np.random.beta, np.random.uniform)
lq_inventories figures 1, 2, 4, 5, 6, 12, 15 and 16 of 16, from simulate, which calls LQ.compute_sequence without a seed
lqcontrol figures 1, 2, 8, 9 and 10 of 10, from LQ.compute_sequence, which draws its shocks without a seed
lqramsey figures 1 to 6 of 6, from paths drawn with numpy's unseeded randn
markov_jump_lq figures 2 and 51 of 52, from compute_sequence, which draws its shocks without a seed
perm_income figures 1 and 2 of 3, from unseeded draws (np.random.randn)
smoothing figures 1 and 2 of 2: figure 1 from a simulation whose seed is itself drawn at random (np.random.randint); figure 2 on 422 pixels, each by one shade level in 255 (anti-aliasing, invisible)
tax_smoothing_1 figures 1 to 3 of 3, from compute_sequence, which draws its shocks without a seed
tax_smoothing_2 figures 1 to 4 of 4, from compute_sequence, which draws its shocks without a seed
tax_smoothing_3 figures 1 and 2 of 2, from compute_sequence, which draws its shocks without a seed

This pull request's own pages. mccall_q, the page it changes: figures 3, 5 and 6 differ from main's because they are now drawn from the seeded generator, and the other three are byte-identical, as the evidence comment says. This pull request's two executions of the lecture give byte-identical figures.

None of the added pages is one this pull request changes, and each also differs when the same main build is compared with the build of #81, which changes only the conda channel's name and installs the same packages. The conclusion doesn't change: no other page's prose differs.

@mmcky
mmcky merged commit 915400d into main Nov 22, 2026
4 checks passed
@mmcky
mmcky deleted the mccall-q-seeded-generator branch November 22, 2026 23:50
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.

2 participants