Skip to content

use RandomState for random numbers #306

Open

Description

@josef-pkt

We should follow sklearn's example and use RandomState for any random variable or random process generation.

makes it easier to initialize with seed and will work better with parallel processing.

Activity

  1. jseabold commented on Jun 12, 2012

    @jseabold
    Member

    What's the difference to calling seed? Could you explain the parallel part or point to some discussions? I have written a decorator to do parallel monte carlo (will be in public soon) and at first I ran into some issues with setting the seeds for the workers.

  2. josef-pkt commented on Jun 12, 2012

    @josef-pkt
    MemberAuthor

    Mostly guessing:

    If we don't use RandomState, then it uses the global numpy RandomState and different processes might influence each other. I don't understand whether this is correct, or what the effects would be, but sklearn has more experience in this and I would just imitate them.

    My tendency so far was to rely on the global state and just to np.random.seed(...)

  3. jseabold commented on Jun 12, 2012

    @jseabold
    Member

    Ah, well this is good to know then. I will have a look around. Thanks.

  4. modified the milestones: 0.7, 0.6 on Sep 26, 2014
  5. modified the milestones: 0.8, 0.7 on Jul 17, 2015
  6. added a commit that references this issue on Jun 19, 2016
    d10fec2
  7. modified the milestones: 0.9, 0.8 on Jun 20, 2016
  8. josef-pkt commented on Jun 20, 2016

    @josef-pkt
    MemberAuthor

    revisit for 0.9 #3043 added local RandomState instance to current cases

    scikit-learn and scipy use a helper function to transform user input which can be None, a seed or a RandomState instance, IIRC.
    (a bit similar to how we get matplotlib ax and figure keyword handling)

    also we need a proper FAQ-D for it, as source for pattern and reminder.
    My multinomial proportion PR still uses global random state monte carlo probabilities.

  9. josef-pkt commented on Mar 27, 2017

    @josef-pkt
    MemberAuthor

    another usecase: continue a permutation or monte carlo test, confidence interval (?) or similar

    in an example for #3576, I used a generator sampling procedure with yield to be able to have an unlimited number of random samples.

    However, if we can combine Monte Carlo results like for p-values (number of replication above a value), then we could just continue with the same RandomState in a new function call. The overhead in duplicate computation will in most cases be small relative to the computation for all monte carlo cases.

    This might not work with updating confidence intervals or quantile unless we keep all previous monte carlo results available (i.e. online updating of e.g quantile for 5% largest and smallest values)

  10. josef-pkt commented on Mar 27, 2017

    @josef-pkt
    MemberAuthor

    related: this might be fragile

    the code snippet below is inside a permutation hyopthesis test function, the underlying random sample generator is not vectorized (cannot be vectorized except with loop)

    • simulate_table_conditional(n_row, n_col, seed) is a simple function. If we use the seed provided by the user in the main function, then we would call the random sample generator with the same seed each time. This will not work correctly. We would need to initialize a RandomState in the outer function if a seed was provided, and call the sampling function with the same RandomState instance.
    • simulate_table_permutation_gen(n_row, n_col, seed) is a generator that is set up once with whatever RandomState initialization was used, and then we just iterate with next. Using a class that holds on to the RandomState would be similar.
        if 0:
            # this is slower than simple permutation generator 
            # (around 30 to 40% in simple example)
    < see below >
            res_mc = [((simulate_table_conditional(n_row, n_col, seed) - count_ind)**2
                       / count_ind).sum()
                      for _ in range(n_repl)]
        else:
            gen = simulate_table_permutation_gen(n_row, n_col, seed)
            res_mc = [((next(gen) - count_ind)**2
                       / count_ind).sum()
                      for _ in range(n_repl)]
    

    <...>
    This needs
    rng = check_random_state(seed)
    and then replace seed by rng in simulate_table_conditional(n_row, n_col, rng)

    i.e. Never use a seed inside a sampling loop.

    In a more elaborate version where we have vectorized chunking we would run into the same problems.

  11. added a commit that references this issue on Mar 27, 2017
  12. jbrockmendel commented on May 14, 2019

    @jbrockmendel
    Contributor

    @bashtage does the reset_randomstate fixture make this closable?

  13. josef-pkt commented on May 14, 2019

    @josef-pkt
    MemberAuthor

    No, this is not for the unit tests.
    This is where we use random numbers in the regular code, e.g. for bootstrap, simulated (p-)values and randomized algorithms.

  14. josef-pkt commented on May 14, 2019

    @josef-pkt
    MemberAuthor

    for example the back-linked commit above 0fd2fd5

    edit
    that commit is in PR #3576

  15. removed this from the 0.9 milestone on Mar 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions