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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
.. highlight:: lua
.. include:: <isogrk1.txt>
.. include:: <isotech.txt>
:mod:`rnd` -- Random Number Distributions
=========================================
.. module:: rnd
This chapter describes functions for generating random variates and
computing their probability distributions. Samples from the
distributions described in this chapter can be obtained using any of
the random number generators in the library as an underlying source of
randomness.
In the simplest cases a non-uniform distribution can be obtained
analytically from the uniform distribution of a random number
generator by applying an appropriate transformation. This method uses
one call to the random number generator. More complicated
distributions are created by the "acceptance-rejection" method, which
compares the desired distribution against a distribution which is
similar and known analytically. This usually requires several samples
from the generator.
The library also provides cumulative distribution functions and
inverse cumulative distribution functions, sometimes referred to as
quantile functions. The cumulative distribution functions and their
inverses are computed separately for the upper and lower tails of the
distribution, allowing full accuracy to be retained for small results. See the chapter :ref:`cdf-section` for more details.
.. _rnd_gaussian:
.. function:: gaussian(r [, sigma])
This function returns a Gaussian random variate, with mean zero and
standard deviation 'sigma'. The probability distribution for
Gaussian random variates is,
.. math::
p(x) dx = {1 \over \sqrt{2 \pi \sigma^2}} \exp (-x^2 / 2\sigma^2) dx
for x in the range -\ |infin| to +\ |infin| . Use the transformation z =
|mgr| + x on the numbers returned by 'gsl_ran_gaussian' to obtain a
Gaussian distribution with mean |mgr|. This function uses the
Box-Mueller algorithm which requires two calls to the random
number generator R.
.. figure:: gaussian-pdf.png
Probability distribution function
.. figure:: gaussian-cdf.png
Cumulative distribution function
.. _rnd_exponential:
.. function:: exponential(r [, mu])
This function returns a random variate from the exponential
distribution with mean 'mu'. The distribution is,
.. math::
p(x) dx = {1 \over \mu} \exp(-x/\mu) dx
for x >= 0.
.. _rnd_chisq:
.. function:: chisq(r [, nu])
The chi-squared distribution arises in statistics. If Y\ :sub:`i` are n
independent gaussian random variates with unit variance then the
sum-of-squares,
.. math::
X_i = \sum_i Y_i^2
has a chi-squared distribution with n degrees of freedom.
This function returns a random variate from the chi-squared
distribution with |ngr| degrees of freedom. The distribution function
is,
.. math::
p(x) dx = {1 \over 2 \Gamma(\nu/2) } (x/2)^{\nu/2 - 1} \exp(-x/2) dx
for x >= 0.
.. figure:: chisq-pdf.png
Probability distribution function
.. _rnd_laplace:
.. function:: laplace(r [, a])
This function returns a random variate from the Laplace
distribution with width ``a``. The distribution is,
.. math::
p(x) dx = {1 \over 2 a} \exp(-|x/a|) dx
for -\ |infin| < x < +\ |infin|.
.. _rnd_tdist:
.. function:: tdist(r [, nu])
The t-distribution arises in statistics. If Y\ :sub:`1` has a
normal distribution and Y\ :sub:`2` has a chi-squared distribution
with \nu degrees of freedom then the ratio,
.. math::
X = { Y_1 \over \sqrt{Y_2 / \nu} }
has a t-distribution t(x; |ngr|) with |ngr| degrees of freedom.
This function returns a random variate from the t-distribution.
The distribution function is,
.. math::
p(x) dx = {\Gamma((\nu + 1)/2) \over \sqrt{\pi \nu} \Gamma(\nu/2)}
(1 + x^2/\nu)^{-(\nu + 1)/2} dx
for -\ |infin| < x < +\ |infin|.
.. _rnd_cauchy:
.. function:: cauchy(r [, a])
This function returns a random variate from the Cauchy
distribution with scale parameter A. The probability distribution
for Cauchy random variates is,
.. math::
p(x) dx = {1 \over a\pi (1 + (x/a)^2) } dx
for x in the range - |infin| to + |infin| . The Cauchy distribution is
also known as the Lorentz distribution.
.. _rnd_rayleigh:
.. function:: rayleigh(r [, sigma])
This function returns a random variate from the Rayleigh
distribution with scale parameter |sgr|. The distribution is,
.. math::
p(x) dx = {x \over \sigma^2} \exp(- x^2/(2 \sigma^2)) dx
for x > 0.
.. _rnd_fdist:
.. function:: fdist(r, nu1, nu2)
The F-distribution arises in statistics. If Y\ :sub:`1` and Y\
:sub:`2` are chi-squared deviates with |ngr| :sub:`1` and
|ngr|\ :sub:`2` degrees of freedom then the ratio,
.. math::
X = { (Y_1 / \nu_1) \over (Y_2 / \nu_2) }
has an F-distribution F(x; |ngr|\ :sub:`1`, |ngr|\ :sub:`2`).
This function returns a random variate from the F-distribution with
degrees of freedom |ngr|\ :sub:`1` and |ngr|\ :sub:`2`. The
distribution function is,
.. math::
p(x) dx =
{ \Gamma((\nu_1 + \nu_2)/2)
\over \Gamma(\nu_1/2) \Gamma(\nu_2/2) }
\nu_1^{\nu_1/2} \nu_2^{\nu_2/2}
x^{\nu_1/2 - 1} (\nu_2 + \nu_1 x)^{-\nu_1/2 -\nu_2/2}
for x >= 0.
.. _rnd_gamma:
.. function:: gamma(r, a, b)
This function returns a random variate from the gamma
distribution. The distribution function is,
.. math::
p(x) dx = {1 \over \Gamma(a) b^a} x^{a-1} e^{-x/b} dx
for x > 0.
The gamma distribution with an integer parameter a is known as the
Erlang distribution. The variates are computed using the
Marsaglia-Tsang fast gamma method.
.. _rnd_beta:
.. function:: beta(r, a, b)
This function returns a random variate from the beta
distribution. The distribution function is,
.. math::
p(x) dx = {\Gamma(a+b) \over \Gamma(a) \Gamma(b)} x^{a-1} (1-x)^{b-1} dx
for 0 <= x <= 1.
.. _rnd_gaussian_tail:
.. function:: gaussian_tail(r, a, sigma)
This function provides random variates from the upper tail of a
Gaussian distribution with standard deviation sigma. The values
returned are larger than the lower limit a, which must be
positive. The method is based on Marsaglia's famous
rectangle-wedge-tail algorithm (Ann. Math. Stat. 32, 894–899
(1961)), with this aspect explained in Knuth, v2, 3rd ed, p139,586
(exercise 11).
The probability distribution for Gaussian tail random variates is,
.. math::
p(x) dx = {1 \over N(a;\sigma) \sqrt{2 \pi \sigma^2}}
\exp \left(- \frac{x^2}{2 \sigma^2}\right) dx
for x > a where N(a; |sgr|) is the normalization constant,
.. math::
N(a; \sigma) = (1/2) \textrm{erfc}(a / \sqrt{2 \sigma^2}).
.. _rnd_exppow:
.. function:: exppow(r, a, b)
This function returns a random variate from the exponential power
distribution with scale parameter a and exponent b. The
distribution is,
.. math::
p(x) dx = {1 \over 2 a \Gamma(1+1/b)} \exp(-|x/a|^b) dx
for x >= 0. For b = 1 this reduces to the Laplace distribution. For
b = 2 it has the same form as a gaussian distribution, but with
:math:`a = \sqrt{2} \sigma`.
.. _rnd_lognormal:
.. function:: lognormal(r, zeta, sigma)
This function returns a random variate from the lognormal
distribution. The distribution function is,
.. math::
p(x) dx = {1 \over x \sqrt{2 \pi \sigma^2} }
\exp(-(\ln(x) - \zeta)^2/2 \sigma^2) dx
for x > 0.
.. _rnd_binomial:
.. function:: binomial(r, p, n)
This function returns a random integer from the binomial
distribution, the number of successes in n independent trials with
probability p. The probability distribution for binomial variates
is,
.. math::
p(k) = {n! \over k! (n-k)! } p^k (1-p)^{n-k}
for 0 <= k <= n.
.. _rnd_poisson:
.. function:: poisson(r [, mu])
This function returns a random integer from the Poisson
distribution with mean mu. The probability distribution for Poisson
variates is,
.. math::
p(k) = {\mu^k \over k!} \exp(-\mu)
for k >= 0.
|