import cupy
from cupy.random import generator
# TODO(beam2d): Implement many distributions
[docs]def gumbel(loc=0.0, scale=1.0, size=None, dtype=float):
"""Returns an array of samples drawn from a Gumbel distribution.
The samples are drawn from a Gumbel distribution with location ``loc``
and scale ``scale``.
Its probability density function is defined as
.. math::
f(x) = \\frac{1}{\\eta} \
\\exp\\left\\{ - \\frac{x - \\mu}{\\eta} \\right\\} \
\\exp\\left[-\\exp\\left\\{-\\frac{x - \\mu}{\\eta} \
\\right\\}\\right],
where :math:`\\mu` is ``loc`` and :math:`\\eta` is ``scale``.
Args:
loc (float): The location of the mode :math:`\\mu`.
scale (float): The scale parameter :math:`\\eta`.
size (int or tuple of ints): The shape of the array. If ``None``, a
zero-dimensional array is generated.
dtype: Data type specifier. Only :class:`numpy.float32` and
:class:`numpy.float64` types are allowed.
Returns:
cupy.ndarray: Samples drawn from the Gumbel destribution.
.. seealso:: :func:`numpy.random.gumbel`
"""
rs = uniform(size=size, dtype=dtype)
# We use `1 - x` as input of `log` method to prevent overflow.
# It obeys numpy implementation.
return cupy.ElementwiseKernel(
'T x, T loc, T scale', 'T y',
'y = loc - log(-log(1 - x)) * scale',
'gumbel_kernel'
)(rs, loc, scale, rs)
return rs
[docs]def lognormal(mean=0.0, sigma=1.0, size=None, dtype=float):
"""Returns an array of samples drawn from a log normal distribution.
The samples are natural log of samples drawn from a normal distribution
with mean ``mean`` and deviation ``sigma``.
Args:
mean (float): Mean of the normal distribution.
sigma (float): Standard deviation of the normal distribution.
size (int or tuple of ints): The shape of the array. If ``None``, a
zero-dimensional array is generated.
dtype: Data type specifier. Only :class:`numpy.float32` and
:class:`numpy.float64` types are allowed.
Returns:
cupy.ndarray: Samples drawn from the log normal distribution.
.. seealso:: :func:`numpy.random.lognormal`
"""
rs = generator.get_random_state()
return rs.lognormal(mean, sigma, size=size, dtype=dtype)
[docs]def normal(loc=0.0, scale=1.0, size=None, dtype=float):
"""Returns an array of normally distributed samples.
Args:
loc (float or array_like of floats): Mean of the normal distribution.
scale (float or array_like of floats):
Standard deviation of the normal distribution.
size (int or tuple of ints): The shape of the array. If ``None``, a
zero-dimensional array is generated.
dtype: Data type specifier. Only :class:`numpy.float32` and
:class:`numpy.float64` types are allowed.
Returns:
cupy.ndarray: Normally distributed samples.
.. seealso:: :func:`numpy.random.normal`
"""
rs = generator.get_random_state()
return (rs.normal(0, 1, size, dtype) * scale + loc).astype(dtype)
[docs]def standard_normal(size=None, dtype=float):
"""Returns an array of samples drawn from the standard normal distribution.
This is a variant of :func:`cupy.random.randn`.
Args:
size (int or tuple of ints): The shape of the array. If ``None``, a
zero-dimensional array is generated.
dtype: Data type specifier.
Returns:
cupy.ndarray: Samples drawn from the standard normal distribution.
.. seealso:: :func:`numpy.random.standard_normal`
"""
return normal(size=size, dtype=dtype)