[monte_carlo.md] Update np.random → Generator API#741
Conversation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
✅ Deploy Preview for taupe-gaufre-c4e660 ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
HumphreyYang
left a comment
There was a problem hiding this comment.
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.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
|
Thank you for the feedback, @HumphreyYang! I've updated all functions in |
There was a problem hiding this comment.
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.
|
|
||
| ```{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): |
There was a problem hiding this comment.
This line is over 80 characters.
|
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>
|
Thank you for the suggestion, @HumphreyYang! I've removed the For the default value, I've used That said, I'm happy to adjust. Would you prefer Thanks also for the reminder, @jstac! |
|
thanks @Chihiro2000GitHub @HumphreyYang . merging |
Summary
This PR migrates legacy NumPy random API usage in
monte_carlo.mdas part of QuantEcon/meta#299.from numpy.random import randnwithrng = np.random.default_rng()in the imports block.randn(),randn(n),np.random.randn(M), andnp.random.randn(2, M)calls with the corresponding Generator API methods (rng.standard_normal()).Details
rngis 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 andrngwas redefined within each solution block. In this lecture, however, the exercise solutions were already not self-contained — they relied on the module-levelrandnimport rather than defining their own random source — so definingrngat 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.