-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ENH: check_random_state: Accept np.random.Generator
#654
Conversation
There is incompatibility between Use |
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
quantecon/util/random.py
Outdated
return scipy_rng_integers(gen, low, high=high, size=size, | ||
dtype=dtype, endpoint=endpoint) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would say this is redundant. Isn't just importing scipy._lib._util.rng_integers
directly enough?
e36d515
to
6f97ffc
Compare
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.Generator
np.random.Generator
Let me merge this. Thanks for your help @Smit-create. |
Numpy's recommended procedure to generate random numbers now seems to be to use
np.random.Generator
instead ofnp.random.RandomState
: https://numpy.org/doc/stable/reference/random/index.htmlThis PR is to update
check_random_state
to 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.RandomState
but 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.RandomState
specific methods tonp.random.Generator
compatible 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()