pints icon indicating copy to clipboard operation
pints copied to clipboard

Seeding of priors

Open DavAug opened this issue 4 years ago • 4 comments

At the moment it's very easy to sample from priors, but we do not have any way to seed these random samples. I find seeding especially useful when exploring the prior predictive model. If you want to explore particular realisations of the prior predictive model, you need to be able to seed the prior samples to find them again.

Most prior sample methods in pints rely on np.random pseudo numbers. I would suggest to

  • [ ] Add an optional seed input argument to all prior sample methods, which default to None.
  • [ ] Follow numpys deprecation and change all samplers to np.random.default_rng which can be seeded directly
  • [ ] And sample from this pseudo-random number generator, i.e.
# Create random number generator
rng = np.random.default_rng(seed=seed)

# Sample whatever distribution we are interested in, e.g. Normal
sample = rng.normal()

DavAug avatar Dec 29 '20 11:12 DavAug

Hi David!

We usually do this just by calling np.random.seed(1). That seeds the random number generator used by all of numpy's random generators.

We could maybe make it more future proof by adding a pints.seed method that calls numpy, so that we can change the call when necessary?

MichaelClerx avatar Jan 04 '21 08:01 MichaelClerx

Hi @MichaelClerx , happy new year! 🥳

I think a pints.seed would be a cool idea! Would that effectively just be a wrapper around np.random.seed?

What I like about the np.random.default_rng(seed=seed) is that the seed is specific to this random number generator, and it doesn't fool around with other np.random calls. But I am happy to keep using np.random.seed.

DavAug avatar Jan 04 '21 08:01 DavAug

Happy new year! We'll need someone to carefully read https://numpy.org/doc/stable/reference/random/new-or-different.html#new-or-different I guess, and work out

  • whether we can use the new methods, or if we need to support older numpys for a while
  • what the differences between old & new style are
  • which one we prefer

MichaelClerx avatar Jan 04 '21 09:01 MichaelClerx

Generator stuff was introduced in https://numpy.org/doc/stable/release/1.17.0-notes.html

which was released July 26th, 2019 https://github.com/numpy/numpy/releases/tag/v1.17.0

MichaelClerx avatar Feb 23 '21 09:02 MichaelClerx