ENH: check_random_state: Accept np.random.Generator#654
Merged
Conversation
Member
Author
|
There is incompatibility between Use |
Member
Author
|
As demonstrated by #655, some calls can be replaced independent of this PR, without any breaking of the code (except for |
730582d to
ec511d2
Compare
oyamad
commented
Nov 24, 2022
quantecon/util/random.py
Outdated
Comment on lines
87
to
88
| return scipy_rng_integers(gen, low, high=high, size=size, | ||
| dtype=dtype, endpoint=endpoint) |
Member
Author
There was a problem hiding this comment.
I would say this is redundant. Isn't just importing scipy._lib._util.rng_integers directly enough?
e36d515 to
6f97ffc
Compare
Member
Author
|
I propose to update the docstrings as follows (to keep them short): def some_function(main_arguments, random_state=None):
"""
Parameters
----------
...
random_state : int or np.random.RandomState/Generator, optional
Random seed (integer) or np.random.RandomState or Generator
instance to set the initial state of the random number generator
for reproducibility. If None, a randomly initialized RandomState
is used.
"""
...class SomeClass:
def some_method(main_arguments, random_state=None):
"""
Parameters
----------
...
random_state : int or np.random.RandomState/Generator, optional
Random seed (integer) or np.random.RandomState or Generator
instance to set the initial state of the random number
generator for reproducibility. If None, a randomly
initialized RandomState is used.
"""
... |
75a1c43 to
2511646
Compare
np.random.Generatornp.random.Generator
Member
Author
|
Let me merge this. Thanks for your help @Smit-create. |
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.
Numpy's recommended procedure to generate random numbers now seems to be to use
np.random.Generatorinstead ofnp.random.RandomState: https://numpy.org/doc/stable/reference/random/index.htmlThis PR is to update
check_random_stateto acceptnp.random.Generator, following the corresponding code in scipy:https://github.com/scipy/scipy/blob/b4a970bf4eba1065ea31f8d0988cd4ec523a07d4/scipy/_lib/_util.py#L174-L203
There are discussions on this at scipy and scikit-learn:
The issue is that some methods are present for
np.random.RandomStatebut not fornp.random.Generator: see the table in here. Thus, for example,random_sample()andrand()have to be replaced withrandom(), andrandn()withstandard_normal()and so on.These can be done as commits added to this PR or as independent PRs.
TODOs:
np.random.RandomStatespecific methods tonp.random.Generatorcompatible methodsrandom_sample()->random()rand()->random(), butrand(d0, d1, ..., dn)->random((d0, d1, ..., dn))(orrandom(size=(d0, d1, ..., dn)))randn()->standard_normal(), butrandn(d0, d1, ..., dn)->standard_normal((d0, d1, ..., dn))(orstandard_normal(size=(d0, d1, ..., dn)))randint()->util.rng_integers()