Skip to content

[samuelson.md] Update np.random → Generator API#1002

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

[samuelson.md] Update np.random → Generator API#1002
Chihiro2000GitHub wants to merge 1 commit into
mainfrom
update-rng-samuelson

Conversation

@Chihiro2000GitHub

Copy link
Copy Markdown
Contributor

Summary

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

The lecture's uses of np.random.seed(...) are replaced with an explicit seeded generator rng = np.random.default_rng(seed), and the one remaining call that relied on NumPy's global state is given an explicit generator too.

Related PRs and issues

I checked for open PRs and issues related to this lecture. No open PR modifies samuelson.md. Issue #579 (a matplotlib deprecation warning in this lecture, also reported by #587) concerns a different part of the file, and this PR does not attempt to address it.

Details

  • In simulate_samuelson, np.random.seed(seed) + np.random.normal(0, 1, n)rng = np.random.default_rng(seed) + rng.normal(0, 1, n).
  • In SamuelsonLSS.plot_simulation, np.random.seed(seed) was seeding NumPy's global state so that the inherited LinearStateSpace.simulate would be reproducible. It now creates a local generator and passes it in: self.simulate(ts_length, random_state=rng). LinearStateSpace.simulate takes a random_state argument and accepts a Generator.
  • The sam_t.simulate(ts_length=n) call in "Using the LinearStateSpace class" passed no random_state and so drew from NumPy's global singleton. It is now given an explicit generator as well. That call was not seeded before and is not seeded now, so its output is as reproducible as it was.
  • The existing seeds (42 at the call sites, 0 as the method default) are kept, and no new seed was introduced.
  • After these changes the lecture no longer reads or writes NumPy's global random state.
  • No prose changes were needed: the text does not refer to np.random, and it does not describe specific realized paths.
  • The lecture contains no Numba (@jit, @njit, @jitclass, parallel=True, prange) code.
  • A full local build completed successfully and samuelson.md executed without errors.

Note for reviewers

This migration changes some of the figures: the shocks are plotted directly as simulated paths, so a different stream gives different lines even with the seeds unchanged. I checked the rebuilt lecture and each figure still shows the behaviour it is meant to illustrate.

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

Commit: 33e1c5f

📚 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