-
-
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
Option to supply a random seed #153
Comments
I think this is a good approach. I like the idea of using random state object as in a recent test I had to ensure numpy's internal state was consistent before running two different functions for comparison which required setting the seed twice. This would avoid this problem. We should wrap the instance checks into a utility or support function as |
@oyamad I have pushed a branch called from quantecon.util import check_random_state |
We need to rewrite |
@oyamad Thanks for pointing that out.
I have migrated the tests to a Note: I know this hasn't been done with |
@mmcky Thanks! It may be helpful if we have a template and provide it in a developer documentation. from .util import check_random_state
def some_function(main_arguments, random_state=None):
"""
Parameters
----------
...
random_state : int or np.random.RandomState, optional
Random seed (integer) or np.random.RandomState instance to set
the initial state of the random number generator for
reproducibility. If None, a randomly initialized RandomState is
used.
"""
random_state_ = check_random_state(random_state) # Or `rng` or something else?
# Generate a random number
u = random_state_.random_sample() (EDIT: according to #287 (comment)) |
@oyamad Good Idea - Agreed. I have added an action item in QuantEcon.site repo for addition of this to the developer documentation. |
@mmcky Great, thanks. Will you delete the comment "Or |
@oyamad I am not sure I follow why you would add a I would propose: random_state = check_random_state(random_state) If random_state is an An alternative name would be just to use |
@mmcky You are right. I may have mixed it up with the case of a class; in a function there will be no case where one wants to initialize What about a possible template for a class? Would it be like this? class SomeClass(object):
"""
Parameters
----------
...
random_state : int or np.random.RandomState, optional
Random seed (integer) or np.random.RandomState instance to set
the initial state of the random number generator for
reproducibility. If None, a randomly initialized RandomState is
used.
"""
def __init__(self, main_inputs, random_state=None):
self.random_state = random_state
def simulate(self, main_arguments):
random_state = check_random_state(self.random_state)
# Generate a random number
u = random_state.random_sample() (EDIT: according to #287 (comment)) An issue is what function(s) should accept a
|
@oyamad Good question - I think I would be more in favour of setting the seed once for an object. Therefore I would probably support including it in the SomeClassInstance.random_state = RandomState I also wouldn't be against leaving this up to the developer to implement in which way they see fit re: Do you know of a case where an object would require two or more RandomStates within a class? |
@mmcky Right, this shouldn't be considered as a "rule", and it will be up to the developer. This is just for discussion about what to write in a template on the developer documentation. For the moment let's just leave it as is, including the seed option only in One may want to give different seeds for different simulation trials, but in that case, he/she can do |
@oyamad Sounds good. I have added your example to an Issue on the QuantEcon.site() repository. |
Option to supply I don't know if we want to add
|
See: https://github.com/QuantEcon/QuantEcon.site/issues/25 for action related to QuantEcon.site |
* FEAT: Add random_state option to discrete_rv.py * TEST: Add test for discrete_rv.py random state option * FEAT: Add random_state option to lqcontrol.py * TEST: Add test for lqcontrol.py random state option * FEAT: Add random_state option to lqnash.py * FIX: Make formatting of test_lqnash.py consistent with other tests * FEAT: Add random_state option to lss.py * TEST: Add test for lss.py random state option * FEAT: Add random_state option to quad.py * TEST: Add test for quad.py random state option * FIX: Correct typo * FIX: Correct the docstring for the random_state options
Given the discussion in #147, I would like to discuss the way how to give the user the option to supply a random seed; we better fix a common interface and build a module that provides that interface.
A simple form would be something like this:
For example,
scikit-learn
has a guideline on random numbers and a common routine check_random_state that converts the input into anp.random.RandomState
instance.The text was updated successfully, but these errors were encountered: