/
distributions.py
138 lines (99 loc) · 4.35 KB
/
distributions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import cupy
from cupy import core
from cupy.random import generator
# TODO(beam2d): Implement many distributions
_gumbel_kernel = core.ElementwiseKernel(
'T x, T loc, T scale', 'T y',
'y = loc - log(-log(1 - x)) * scale',
'gumbel_kernel'
)
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:`cupy.random.RandomState.gumbel`
:func:`numpy.random.gumbel`
"""
rs = generator.get_random_state()
return rs.gumbel(loc, scale, size, dtype)
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)
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()
x = rs.normal(0, 1, size, dtype)
cupy.multiply(x, scale, out=x)
cupy.add(x, loc, out=x)
return x
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)
def uniform(low=0.0, high=1.0, size=None, dtype=float):
"""Returns an array of uniformly-distributed samples over an interval.
Samples are drawn from a uniform distribution over the half-open interval
``[low, high)``.
Args:
low (float): Lower end of the interval.
high (float): Upper end of the interval.
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 uniform distribution.
.. seealso:: :func:`numpy.random.uniform`
"""
rs = generator.get_random_state()
return rs.uniform(low, high, size=size, dtype=dtype)