Skip to content

[inventory_q.md] Update np.random → Generator API - #1012

Open
Chihiro2000GitHub wants to merge 1 commit into
mainfrom
update-rng-inventory-q
Open

[inventory_q.md] Update np.random → Generator API#1012
Chihiro2000GitHub wants to merge 1 commit into
mainfrom
update-rng-inventory-q

Conversation

@Chihiro2000GitHub

Copy link
Copy Markdown
Contributor

Summary

This PR migrates legacy NumPy random API usage in 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 inventory_q.md. Issues #881 and #882 concern other aspects of this lecture (the demand grid and the Q-learning loop), and this PR does not attempt to address them.

Details

  • sim_inventories takes rng in place of seed, and draws with rng.geometric(p).
  • q_learning_kernel takes rng in place of seed, and draws with rng.geometric(p), rng.integers(...) and rng.random().
  • The q_learning 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.
  • The comparison figure builds a fresh np.random.default_rng(sim_seed) for each call, so the optimal policy and the Q-learning snapshots still face the same demand sequence, as they did when each call re-seeded.
  • 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. On a 5-million-iteration benchmark the Generator version ran at the same speed as the legacy calls.
  • No prose changes were needed.
  • A full local build completed successfully and inventory_q.md executed without errors.

Note for reviewers

Both jitted functions take rng in place of seed, which changes their signatures. q_learning 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-1012--sunny-cactus-210e3e.netlify.app

Commit: c98c2fb

📚 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