You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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(...)
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.
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)
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.
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.
Activity
jseabold commentedon Jun 12, 2012
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.
josef-pkt commentedon Jun 12, 2012
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(...)
jseabold commentedon Jun 12, 2012
Ah, well this is good to know then. I will have a look around. Thanks.
CLN: Change use of global NumPy RandomState
josef-pkt commentedon Jun 20, 2016
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.
josef-pkt commentedon Mar 27, 2017
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)
josef-pkt commentedon Mar 27, 2017
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 withnext. Using a class that holds on to the RandomState would be similar.<...>
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.
ENH add random state (statsmodels#306), chisquare_contingency with pe…
jbrockmendel commentedon May 14, 2019
@bashtage does the reset_randomstate fixture make this closable?
josef-pkt commentedon May 14, 2019
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.
josef-pkt commentedon May 14, 2019
for example the back-linked commit above 0fd2fd5
edit
that commit is in PR #3576