0

Currently, I am programming a large simulation that uses random variates from multiple distributions that come from both numpy and scipy.stats, and where the distributions should also be independent. Seeking a way to ensure reproducibility, I luckily stumbled upon Abhinav's response here, where they provide an amazing example. Nevertheless, it notably only seeds a single distribution from scipy, whereas my code has multiple scipy distributions. Is there a way to seed all scipy distributions at once (while still seeding the numpy distributions)? If not all at once, is it possible to seed all of the continuous distributions? (It just seems inefficient to seed every single distribution separately). Thank you very much!

Edit: A minimal reproducible example can be found below (it is similar to Abhinav's example):

from numpy.random import Generator, PCG64
from scipy.stats import binom, norm

n, p, size, seed = 10, 0.5, 10, 12345

numpy_randomGen = Generator(PCG64(seed))
scipy_randomGen = binom
scipy_randomGen2 = norm

numpy_randomGen = Generator(PCG64(seed))

# this is the part I want to simplify, as I have many distributions from scipy
# maybe there is a convention that simplifies it?
scipy_randomGen.random_state=numpy_randomGen
scipy_randomGen2.random_state=numpy_randomGen


print(scipy_randomGen.rvs(n, p, size=size))
print(scipy_randomGen2.rvs(size=size))
print(numpy_randomGen.binomial(n, p, size))
rianko
  • 29
  • 3

1 Answers1

0

Not sure what you're after. You still need to seed each distribution separately. So there's unlikely to be anything simpler then providing a random_state arg to each rvs call as in

.rvs(n, p, size=size, random_state=...)

Here the random_state argument can be a Generator or an integer seed (however in the latter case it's constructing an old-style RandomState object and seeds it under the hood.

ev-br
  • 24,968
  • 9
  • 65
  • 78
  • I was exactly asking if I need to seed each ``scipy.stats`` distribution separately, especially when seeding ``numpy`` and ``scipy.stats`` distributions! That's great to know! Thank you! – rianko Jan 08 '22 at 19:30
  • Well, you either seed each of them independently, or you rely on an implicit seeding via `np.random.seed`. The latter is discouraged. – ev-br Jan 08 '22 at 23:26