Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use the new NumPy random number generator?

The fact that NumPy now recommends that new code uses the defacult_rng() instance instead of numpy.random for new code has got me thinking about how it should be used to yield good results, both performance vice and statistically.

This first example is how I first wanted to write:

import numpy as np

class fancy_name():
  def __init__(self):
    self.rg = np.random.default_rng()
    self.gamma_shape = 1.0
    self.gamma_scale = 1.0

  def public_method(self, input):
    # Do intelligent stuff with input
    return self.rg.gamma(self.gamma_shape, slef.gamma_scale)

But I have also considered creating a new instance in every function call:

import numpy as np

class fancy_name():
  def __init__(self):
    self.gamma_shape = 1.0
    self.gamma_scale = 1.0

  def public_method(self, input):
    # Do intelligent stuff with input
    rg = np.random.default_rng()
    return rg.gamma(self.gamma_shape, slef.gamma_scale)

A third alternative would be to pass the rng as an argument in the function call. This way the same rng can be used in other parts of the code as well.

This is used in a simulation environment that is going to be called often to sample, for example, transition times.

I guess the question is if there are arguments for any of these three methods and if there exists some kind of praxis?

Also, any reference to more in-depth explanations of using these random number generators (except for the NumPy doc and Random Sampling article) is of great interest!

like image 830
Marcus Avatar asked May 08 '20 09:05

Marcus


People also ask

How do you generate a random number in Python with numpy?

Generate Random Number From Array The choice() method allows you to generate a random value based on an array of values. The choice() method takes an array as a parameter and randomly returns one of the values.

How does numpy random number generator work?

Numpy's random number routines produce pseudo random numbers using combinations of a BitGenerator to create sequences and a Generator to use those sequences to sample from different statistical distributions: BitGenerators: Objects that generate random numbers.

How do you generate random values from a numpy array?

choice() function is used to get random elements from a NumPy array. It is a built-in function in the NumPy package of python. Parameters: a: a one-dimensional array/list (random sample will be generated from its elements) or an integer (random samples will be generated in the range of this integer)


Video Answer


1 Answers

default_rng() isn't a singleton. It makes a new Generator backed by a new instance of the default BitGenerator class. Quoting the docs:

Construct a new Generator with the default BitGenerator (PCG64).

...

If seed is not a BitGenerator or a Generator, a new BitGenerator is instantiated. This function does not manage a default global instance.

This can also be tested empirically:

In [1]: import numpy

In [2]: numpy.random.default_rng() is numpy.random.default_rng()
Out[2]: False

This is expensive. You should usually call default_rng() once in your program and pass the generator to anything that needs it. (Yes, this is awkward.)

like image 120
user2357112 supports Monica Avatar answered Oct 21 '22 04:10

user2357112 supports Monica