Skip to content

[monte_carlo.md] Update np.random → Generator API#741

Merged
jstac merged 5 commits into
mainfrom
update-rng-monte-carlo
May 28, 2026
Merged

[monte_carlo.md] Update np.random → Generator API#741
jstac merged 5 commits into
mainfrom
update-rng-monte-carlo

Conversation

@Chihiro2000GitHub
Copy link
Copy Markdown
Contributor

Summary

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

  • Replaced from numpy.random import randn with rng = np.random.default_rng() in the imports block.
  • Replaced all randn(), randn(n), np.random.randn(M), and np.random.randn(2, M) calls with the corresponding Generator API methods (rng.standard_normal()).

Details

rng is defined once at module level in the imports block and reused throughout the lecture, including in the exercise solution blocks. In previous lectures, exercise solution blocks were typically treated as self-contained and rng was redefined within each solution block. In this lecture, however, the exercise solutions were already not self-contained — they relied on the module-level randn import rather than defining their own random source — so defining rng at module level and reusing it throughout is the more natural and consistent pattern here.

No fixed seed was introduced. No Numba-related code was present.

Hi @mmcky and @HumphreyYang, I'd be grateful if you could take a look when you have time.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@netlify
Copy link
Copy Markdown

netlify Bot commented May 18, 2026

Deploy Preview for taupe-gaufre-c4e660 ready!

Name Link
🔨 Latest commit c432df6
🔍 Latest deploy log https://app.netlify.com/projects/taupe-gaufre-c4e660/deploys/6a17892437145d0008dd6659
😎 Deploy Preview https://deploy-preview-741--taupe-gaufre-c4e660.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify project configuration.

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 18, 2026

@github-actions github-actions Bot temporarily deployed to pull request May 18, 2026 08:12 Inactive
@github-actions github-actions Bot temporarily deployed to pull request May 18, 2026 08:13 Inactive
Copy link
Copy Markdown
Member

@HumphreyYang HumphreyYang left a comment

Choose a reason for hiding this comment

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

Many thanks @Chihiro2000GitHub! It looks nice. Just one comment:

In this PR, a number of functions rely on the global rng object.

In some other PRs, rng is passed in as an argument instead. I think that approach is preferable here because it makes the source of randomness explicit and avoids hidden global state, which should make the functions easier to test and reason about.

So it would be nice to make a few minor edits so that these functions accept rng as an argument.

@github-actions github-actions Bot temporarily deployed to pull request May 24, 2026 04:52 Inactive
@github-actions github-actions Bot temporarily deployed to pull request May 24, 2026 04:52 Inactive
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Chihiro2000GitHub
Copy link
Copy Markdown
Contributor Author

Thank you for the feedback, @HumphreyYang! I've updated all functions in monte_carlo.md to accept rng as an explicit argument with rng=None as the default, following the pattern used in other PRs. Each function now creates its own generator via np.random.default_rng() if none is provided, and all call sites in the lecture have been updated to pass rng=rng explicitly.

@github-actions github-actions Bot temporarily deployed to pull request May 25, 2026 02:57 Inactive
@github-actions github-actions Bot temporarily deployed to pull request May 25, 2026 03:02 Inactive
Copy link
Copy Markdown
Member

@HumphreyYang HumphreyYang left a comment

Choose a reason for hiding this comment

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

Many thanks, @Chihiro2000GitHub!

It is nice that you have this:

if rng is None:
    rng = np.random.default_rng(

which is important for a mature Python program.

However, I think that for here, since we are always going to pass an rng to the function, we can safely do what you did in:

PR #874 in lecture-python.myst

and remove this repetitive rng check for simplicity.

It's important that edits across PRs are using the same convention.

What do you think?

Also just one more small comment on a line that exceeds 80 characters.

Comment thread lectures/monte_carlo.md Outdated

```{code-cell} ipython3
def simulate_asset_price_path(μ=default_μ, S0=default_S0, h0=default_h0, n=default_n, ρ=default_ρ, ν=default_ν):
def simulate_asset_price_path(μ=default_μ, S0=default_S0, h0=default_h0, n=default_n, ρ=default_ρ, ν=default_ν, rng=None):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This line is over 80 characters.

@jstac
Copy link
Copy Markdown
Contributor

jstac commented May 27, 2026

many thanks for the nice review @HumphreyYang .

@Chihiro2000GitHub , please address those review comments when you have time.

Per reviewer feedback, remove repetitive `if rng is None` checks and
change `rng=None` to `rng=rng` across all 7 functions, following the
convention established in lecture-python.myst PR #874.

Also reformat `simulate_asset_price_path` signature to one-arg-per-line
style (consistent with other multi-argument functions in the file) to
fix the 80-character line length violation.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@Chihiro2000GitHub
Copy link
Copy Markdown
Contributor Author

Thank you for the suggestion, @HumphreyYang!

I've removed the if rng is None guard from all functions and reformatted the simulate_asset_price_path signature to one-argument-per-line style.

For the default value, I've used rng=rng (referencing the global rng defined at the top of the notebook) rather than no default. The reason is a Python syntax constraint: since the other parameters already have defaults (e.g. n=default_n), a bare rng with no default would need to come before them or be written as *, rng (keyword-only) — see Python docs on compound statements. I wanted to keep the argument order intact and avoid introducing *, so rng=rng seemed like the simplest viable option.

That said, I'm happy to adjust. Would you prefer *, rng (required keyword-only argument) or moving rng to the first position instead?

Thanks also for the reminder, @jstac!

@github-actions github-actions Bot temporarily deployed to pull request May 27, 2026 23:00 Inactive
@github-actions github-actions Bot temporarily deployed to pull request May 27, 2026 23:33 Inactive
@github-actions github-actions Bot temporarily deployed to pull request May 28, 2026 00:27 Inactive
@github-actions github-actions Bot temporarily deployed to pull request May 28, 2026 00:28 Inactive
@jstac
Copy link
Copy Markdown
Contributor

jstac commented May 28, 2026

thanks @Chihiro2000GitHub @HumphreyYang . merging

@jstac jstac merged commit 34a37be into main May 28, 2026
7 checks passed
@jstac jstac deleted the update-rng-monte-carlo branch May 28, 2026 00:30
@jstac jstac mentioned this pull request May 28, 2026
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