lightcurvelynx.math_nodes.np_random
Wrapper classes for calling numpy random number generators.
Classes
The base class for numpy random number generators. |
|
As specific wrapper for the multivariate normal function. This is needed because it does not |
Module Contents
- class NumpyRandomFunc(func_name, size=1, seed=None, **kwargs)[source]
Bases:
lightcurvelynx.base_models.FunctionNodeThe base class for numpy random number generators.
- sample_size
The shape of the array to generate for each sample. The actual returned value will be
(num_samples, *size). If an empty tuple will generate a single value per sample.- Type:
tuple
- Parameters:
func_name (str) – The name of the random function to use.
size (int or tuple, optional) – The shape of the array to generate for each sample. Actual returned value will be
(num_samples, *size). Default: None (single values for each sample)seed (int, optional) – The seed to use.
Note
Since we need to create a new random number generator for this object and use that generator’s functions, we cannot pass in the function directly. Instead we need to pass in the function’s name.
The NumpyRandomFunc node does not support the choice function.
Examples
>>> # Create a uniform random number generator between 100.0 and 150.0 >>> func_node = NumpyRandomFunc("uniform", low=100.0, high=150.0)
>>> # Create a normal random number generator with mean=5.0 and std=1.0 >>> func_node = NumpyRandomFunc("normal", loc=5.0, scale=1.0)
- set_seed(new_seed)[source]
Update the random number generator’s seed to a given value.
- Parameters:
new_seed (int) – The given seed
- compute(graph_state, rng_info=None, **kwargs)[source]
Execute the wrapped function.
The input arguments are taken from the current graph_state and the outputs are written to graph_state.
- Parameters:
graph_state (GraphState) – An object mapping graph parameters to their values. This object is modified in place as it is sampled.
rng_info (numpy.random._generator.Generator, optional) – A given numpy random number generator to use for this computation. If not provided, the function uses the node’s random number generator.
**kwargs (dict, optional) – Additional function arguments.
- Returns:
results – The result of the computation. This return value is provided so that testing functions can easily access the results.
- Return type:
any
- Raises:
ValueError is func attribute is None. –
- class NumpyMultivariateNormalFunc(mean, cov, seed=None, **kwargs)[source]
Bases:
lightcurvelynx.base_models.FunctionNodeAs specific wrapper for the multivariate normal function. This is needed because it does not support vectorizing over multiple input parameters (lists of means) in the same way.
Only a single mean and covariance matrix can be provided (instead of one per sample).
- func_name
The name of the random function to use.
- Type:
str
- sample_size
The shape of the array to generate for each sample. The actual returned value will be
(num_samples, *size). If an empty tuple will generate a single value per sample.- Type:
tuple
- Parameters:
mean (array-like) – A length D array with the mean of the distribution for each sample.
cov (array-like) – A D x D array with the covariance matrix of the distribution for each sample.
seed (int, optional) – The seed to use.
Examples
>>> # Create a uniform random number generator between 100.0 and 150.0 >>> func_node = NumpyRandomFunc("uniform", low=100.0, high=150.0)
>>> # Create a normal random number generator with mean=5.0 and std=1.0 >>> func_node = NumpyRandomFunc("normal", loc=5.0, scale=1.0)
- set_seed(new_seed)[source]
Update the random number generator’s seed to a given value.
- Parameters:
new_seed (int) – The given seed
- compute(graph_state, rng_info=None, **kwargs)[source]
Sample from the multivariate normal distribution.
- Parameters:
graph_state (GraphState) – An object mapping graph parameters to their values. This object is modified in place as it is sampled.
rng_info (numpy.random._generator.Generator, optional) – A given numpy random number generator to use for this computation. If not provided, the function uses the node’s random number generator.
**kwargs (dict, optional) – Additional function arguments.
- Returns:
results – The result of the computation. If num_samples > 1, this will be an array of shape (num_samples, dims). Otherwise it will be a 1D array of length dims.
- Return type:
np.ndarray