[numpy.md] Update np.random → Generator API#549
Open
Chihiro2000GitHub wants to merge 2 commits into
Open
Conversation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Member
HumphreyYang
left a comment
There was a problem hiding this comment.
This looks great to me! The only small suggestion I have is that
We’ve already seen how we can generate random variables using `np.random`
seems a bit stale because we are specifically using the Generator API. I think
We've already seen how we can generate random variables using NumPy's Generator API.
would be a better description.
Another minor point is that we can potentially allow for the seed key to be passed for the exercise solution by:
class DiscreteRV:
def __init__(self, q, seed=None):
self.q = q
self.Q = cumsum(q)
self.rng = np.random.default_rng(seed)
def draw(self, k=1):
return self.Q.searchsorted(self.rng.uniform(0, 1, size=k))
Please let me know what you think!
- Update prose to reference NumPy's Generator API instead of np.random - Add optional seed parameter to DiscreteRV.__init__ for reproducibility Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Collaborator
Author
|
Hi @HumphreyYang, Thank you for the feedback! Both of your points make perfect sense. I've addressed both points:
Please let me know if anything else needs adjusting! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR migrates legacy NumPy random API calls in
numpy.mdas part of QuantEcon/meta#299.All
np.random.randn,np.random.binomial, andfrom numpy.random import uniformusages are replaced with the Generator API vianp.random.default_rng().Details
rng = np.random.default_rng()is introduced at the first point of use (Mutability section) and reused throughout the main lecture text.np_ex3,np_ex4) define their own localrngto remain self-contained.np_ex4(Part 1 and Part 2, exercise and solution) retains seed 123 vianp.random.default_rng(123), matching the original intent of reproducible comparison between broadcasting and for-loop results.DiscreteRVin thenp_ex2solution storesself.rng = np.random.default_rng()in__init__and usesself.rng.uniform(...)indraw.jb build lectures);numpy.mdexecuted without errors in 30 seconds.Notes for reviewers
One item was intentionally left unchanged and would benefit from reviewer judgment:
The prose in the Sub-packages section currently reads:
After this migration, the code directly below uses
rng.standard_normal()andrng.binomial()rather thannp.random.xxx()directly. Technicallyrngis still obtained fromnp.random.default_rng(), so the sentence is not wrong — but it may feel slightly mismatched to a reader. Happy to update the wording if you think that would be clearer.Hi @mmcky and @HumphreyYang, I'd be grateful if you could take a look when you have time.