[numpy.md] Update np.random → Generator API#549
Conversation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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>
|
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! |
|
Many thanks @Chihiro2000GitHub! Merged! |
✅ Translation sync completed (zh-cn)Target repo: QuantEcon/lecture-python-programming.zh-cn
|
✅ Translation sync completed (fa)Target repo: QuantEcon/lecture-python-programming.fa
|
|
thanks @Chihiro2000GitHub and @HumphreyYang |
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.