Skip to content

[rs_inventory_q.md] Update np.random → Generator API - #1013

Merged
kp992 merged 1 commit into
mainfrom
update-rng-rs-inventory-q
Jul 31, 2026
Merged

[rs_inventory_q.md] Update np.random → Generator API#1013
kp992 merged 1 commit into
mainfrom
update-rng-rs-inventory-q

Conversation

@Chihiro2000GitHub

Copy link
Copy Markdown
Contributor

Summary

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

All of the random draws happen inside @numba.jit(nopython=True) functions, so the two of them now take a rng argument and the generator is built by the calling Python code.

Related PRs and issues

I checked for open PRs and issues related to this lecture. No open PR modifies rs_inventory_q.md. Issue #882 concerns the convergence message and the snapshot timing in the Q-learning loop, and this PR does not attempt to address it.

Details

  • sim_inventories takes rng in place of seed, and draws with rng.geometric(p).
  • q_learning_rs_kernel takes rng in place of seed, and draws with rng.geometric(p), rng.integers(...) and rng.random().
  • The q_learning_rs wrapper is ordinary Python, so it keeps its seed=1234 argument and builds the generator itself. Its call site is unchanged.
  • The existing seeds (0, 1234, 5678) are all kept, and no new seed was introduced.
  • Both simulation figures build a fresh np.random.default_rng(sim_seed) for each call, so the panels still face the same demand sequence, as they did when each call re-seeded. This matters for the figure comparing risk sensitivities and for the figure comparing the Q-learning snapshots against the VFI policy.
  • Numba supports geometric, integers and random on a Generator passed in as an argument, but does not support constructing one inside a jitted function, which is why the generator is built by the caller. There is no prange or parallel=True in this lecture, so a single generator is safe here.
  • No prose changes were needed.
  • A full local build completed successfully and rs_inventory_q.md executed without errors.

Note for reviewers

Both jitted functions take rng in place of seed, which changes their signatures. q_learning_rs keeps its seed argument so the call in the lecture is unchanged, but sim_inventories is called directly and now receives a generator at each call site.

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-1013--sunny-cactus-210e3e.netlify.app

Commit: efa349c

📚 Changed Lectures


Build Info

@kp992
kp992 merged commit 2ab2056 into main Jul 31, 2026
1 check passed
@kp992
kp992 deleted the update-rng-rs-inventory-q branch July 31, 2026 23:17
@mmcky

mmcky commented Jul 31, 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#215
Files synced (1):

  • lectures/rs_inventory_q.md

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.

3 participants