3
<!-- This HTML file has been created by texi2html 1.54+ (gsl)
4
from ../gsl-ref.texi -->
6
<TITLE>GNU Scientific Library -- Reference Manual - Random Number Distributions</TITLE>
7
<!-- <LINK rel="stylesheet" title="Default Style Sheet" href="/css/texinfo.css" type="text/css"> -->
8
<link href="gsl-ref_20.html" rel=Next>
9
<link href="gsl-ref_18.html" rel=Previous>
10
<link href="gsl-ref_toc.html" rel=ToC>
14
<p>Go to the <A HREF="gsl-ref_1.html">first</A>, <A HREF="gsl-ref_18.html">previous</A>, <A HREF="gsl-ref_20.html">next</A>, <A HREF="gsl-ref_50.html">last</A> section, <A HREF="gsl-ref_toc.html">table of contents</A>.
18
<H1><A NAME="SEC286" HREF="gsl-ref_toc.html#TOC286">Random Number Distributions</A></H1>
20
<A NAME="IDX1459"></A>
21
<A NAME="IDX1460"></A>
22
<A NAME="IDX1461"></A>
23
<A NAME="IDX1462"></A>
24
<A NAME="IDX1463"></A>
25
This chapter describes functions for generating random variates and
26
computing their probability distributions. Samples from the
27
distributions described in this chapter can be obtained using any of the
28
random number generators in the library as an underlying source of
29
randomness. In the simplest cases a non-uniform distribution can be
30
obtained analytically from the uniform distribution of a random number
31
generator by applying an appropriate transformation. This method uses
32
one call to the random number generator.
36
More complicated distributions are created by the
37
<I>acceptance-rejection</I> method, which compares the desired
38
distribution against a distribution which is similar and known
39
analytically. This usually requires several samples from the generator.
43
The library also provides cumulative distribution functions and inverse
44
cumulative distribution functions, sometimes referred to as quantile
45
functions. The cumulative distribution functions and their inverses are
46
computed separately for the upper and lower tails of the distribution,
47
allowing full accuracy to be retained for small results.
51
The functions for random variates and probability density functions
52
described in this section are declared in <TT>'gsl_randist.h'</TT>. The
53
corresponding cumulative distribution functions are declared in
60
<H2><A NAME="SEC287" HREF="gsl-ref_toc.html#TOC287">Introduction</A></H2>
63
Continuous random number distributions are defined by a probability
64
density function, p(x), such that the probability of x
65
occurring in the infinitesimal range x to x+dx is
70
The cumulative distribution function for the lower tail is defined by,
73
P(x) = \int_{-\infty}^{x} dx' p(x')
77
and gives the probability of a variate taking a value less than x.
81
The cumulative distribution function for the upper tail is defined by,
84
Q(x) = \int_{x}^{+\infty} dx' p(x')
88
and gives the probability of a variate taking a value greater than x.
89
The upper and lower cumulative distribution functions are related by
90
P(x) + Q(x) = 1 and satisfy
91
0 <= P(x) <= 1,
92
0 <= Q(x) <= 1.
96
The inverse cumulative distributions,
98
x=Q^{-1}(Q) give the values of x
99
which correspond to a specific value of P or {Q}.
100
They can be used to find confidence limits from probability values.
105
<H2><A NAME="SEC288" HREF="gsl-ref_toc.html#TOC288">The Gaussian Distribution</A></H2>
108
<DT><U>Random:</U> double <B>gsl_ran_gaussian</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>sigma</VAR>)</I>
109
<DD><A NAME="IDX1464"></A>
110
<A NAME="IDX1465"></A>
111
This function returns a Gaussian random variate, with mean zero and
112
standard deviation <VAR>sigma</VAR>. The probability distribution for
113
Gaussian random variates is,
117
<PRE class="example">
118
p(x) dx = {1 \over \sqrt{2 \pi \sigma^2}} \exp (-x^2 / 2\sigma^2) dx
122
for x in the range -\infty to +\infty. Use the
123
transformation z = \mu + x on the numbers returned by
124
<CODE>gsl_ran_gaussian</CODE> to obtain a Gaussian distribution with mean
125
\mu. This function uses the Box-Mueller algorithm which requires two
126
calls to the random number generator <VAR>r</VAR>.
132
<DT><U>Function:</U> double <B>gsl_ran_gaussian_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>sigma</VAR>)</I>
133
<DD><A NAME="IDX1466"></A>
134
This function computes the probability density p(x) at <VAR>x</VAR>
135
for a Gaussian distribution with standard deviation <VAR>sigma</VAR>, using
136
the formula given above.
144
<DT><U>Function:</U> double <B>gsl_ran_gaussian_ratio_method</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>sigma</VAR>)</I>
145
<DD><A NAME="IDX1467"></A>
146
This function computes a Gaussian random variate using the
147
Kinderman-Monahan ratio method.
153
<DT><U>Random:</U> double <B>gsl_ran_ugaussian</B> <I>(const gsl_rng * <VAR>r</VAR>)</I>
154
<DD><A NAME="IDX1468"></A>
155
<DT><U>Function:</U> double <B>gsl_ran_ugaussian_pdf</B> <I>(double <VAR>x</VAR>)</I>
156
<DD><A NAME="IDX1469"></A>
157
<DT><U>Random:</U> double <B>gsl_ran_ugaussian_ratio_method</B> <I>(const gsl_rng * <VAR>r</VAR>)</I>
158
<DD><A NAME="IDX1470"></A>
159
These functions compute results for the unit Gaussian distribution. They
160
are equivalent to the functions above with a standard deviation of one,
161
<VAR>sigma</VAR> = 1.
167
<DT><U>Function:</U> double <B>gsl_cdf_gaussian_P</B> <I>(double <VAR>x</VAR>, double <VAR>sigma</VAR>)</I>
168
<DD><A NAME="IDX1471"></A>
169
<DT><U>Function:</U> double <B>gsl_cdf_gaussian_Q</B> <I>(double <VAR>x</VAR>, double <VAR>sigma</VAR>)</I>
170
<DD><A NAME="IDX1472"></A>
171
<DT><U>Function:</U> double <B>gsl_cdf_gaussian_Pinv</B> <I>(double <VAR>P</VAR>, double <VAR>sigma</VAR>)</I>
172
<DD><A NAME="IDX1473"></A>
173
<DT><U>Function:</U> double <B>gsl_cdf_gaussian_Qinv</B> <I>(double <VAR>Q</VAR>, double <VAR>sigma</VAR>)</I>
174
<DD><A NAME="IDX1474"></A>
175
These functions compute the cumulative distribution functions
176
P(x), Q(x) and their inverses for the Gaussian
177
distribution with standard deviation <VAR>sigma</VAR>.
183
<DT><U>Function:</U> double <B>gsl_cdf_ugaussian_P</B> <I>(double <VAR>x</VAR>)</I>
184
<DD><A NAME="IDX1475"></A>
185
<DT><U>Function:</U> double <B>gsl_cdf_ugaussian_Q</B> <I>(double <VAR>x</VAR>)</I>
186
<DD><A NAME="IDX1476"></A>
187
<DT><U>Function:</U> double <B>gsl_cdf_ugaussian_Pinv</B> <I>(double <VAR>P</VAR>)</I>
188
<DD><A NAME="IDX1477"></A>
189
<DT><U>Function:</U> double <B>gsl_cdf_ugaussian_Qinv</B> <I>(double <VAR>Q</VAR>)</I>
190
<DD><A NAME="IDX1478"></A>
191
These functions compute the cumulative distribution functions
192
P(x), Q(x) and their inverses for the unit Gaussian
199
<H2><A NAME="SEC289" HREF="gsl-ref_toc.html#TOC289">The Gaussian Tail Distribution</A></H2>
202
<DT><U>Random:</U> double <B>gsl_ran_gaussian_tail</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>a</VAR>, double <VAR>sigma</VAR>)</I>
203
<DD><A NAME="IDX1479"></A>
204
<A NAME="IDX1480"></A>
205
This function provides random variates from the upper tail of a Gaussian
206
distribution with standard deviation <VAR>sigma</VAR>. The values returned
207
are larger than the lower limit <VAR>a</VAR>, which must be positive. The
208
method is based on Marsaglia's famous rectangle-wedge-tail algorithm (Ann
209
Math Stat 32, 894-899 (1961)), with this aspect explained in Knuth, v2,
210
3rd ed, p139,586 (exercise 11).
214
The probability distribution for Gaussian tail random variates is,
218
<PRE class="example">
219
p(x) dx = {1 \over N(a;\sigma)} \exp (- x^2/(2 \sigma^2)) dx
223
for x > a where N(a;\sigma) is the normalization constant,
227
<PRE class="example">
228
N(a;\sigma) = (1/2) erfc(a / sqrt(2 sigma^2)).
235
<DT><U>Function:</U> double <B>gsl_ran_gaussian_tail_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>sigma</VAR>)</I>
236
<DD><A NAME="IDX1481"></A>
237
This function computes the probability density p(x) at <VAR>x</VAR>
238
for a Gaussian tail distribution with standard deviation <VAR>sigma</VAR> and
239
lower limit <VAR>a</VAR>, using the formula given above.
247
<DT><U>Random:</U> double <B>gsl_ran_ugaussian_tail</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>a</VAR>)</I>
248
<DD><A NAME="IDX1482"></A>
249
<DT><U>Function:</U> double <B>gsl_ran_ugaussian_tail_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>)</I>
250
<DD><A NAME="IDX1483"></A>
251
These functions compute results for the tail of a unit Gaussian
252
distribution. They are equivalent to the functions above with a standard
253
deviation of one, <VAR>sigma</VAR> = 1.
260
<H2><A NAME="SEC290" HREF="gsl-ref_toc.html#TOC290">The Bivariate Gaussian Distribution</A></H2>
264
<DT><U>Random:</U> void <B>gsl_ran_bivariate_gaussian</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>sigma_x</VAR>, double <VAR>sigma_y</VAR>, double <VAR>rho</VAR>, double * <VAR>x</VAR>, double * <VAR>y</VAR>)</I>
265
<DD><A NAME="IDX1484"></A>
266
<A NAME="IDX1485"></A>
267
<A NAME="IDX1486"></A>
268
<A NAME="IDX1487"></A>
269
This function generates a pair of correlated gaussian variates, with
270
mean zero, correlation coefficient <VAR>rho</VAR> and standard deviations
271
<VAR>sigma_x</VAR> and <VAR>sigma_y</VAR> in the x and y directions.
272
The probability distribution for bivariate gaussian random variates is,
276
<PRE class="example">
277
p(x,y) dx dy = {1 \over 2 \pi \sigma_x \sigma_y \sqrt{1-\rho^2}} \exp (-(x^2/\sigma_x^2 + y^2/\sigma_y^2 - 2 \rho x y/(\sigma_x\sigma_y))/2(1-\rho^2)) dx dy
281
for x,y in the range -\infty to +\infty. The
282
correlation coefficient <VAR>rho</VAR> should lie between 1 and
289
<DT><U>Function:</U> double <B>gsl_ran_bivariate_gaussian_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>y</VAR>, double <VAR>sigma_x</VAR>, double <VAR>sigma_y</VAR>, double <VAR>rho</VAR>)</I>
290
<DD><A NAME="IDX1488"></A>
291
This function computes the probability density p(x,y) at
292
(<VAR>x</VAR>,<VAR>y</VAR>) for a bivariate gaussian distribution with standard
293
deviations <VAR>sigma_x</VAR>, <VAR>sigma_y</VAR> and correlation coefficient
294
<VAR>rho</VAR>, using the formula given above.
302
<H2><A NAME="SEC291" HREF="gsl-ref_toc.html#TOC291">The Exponential Distribution</A></H2>
305
<DT><U>Random:</U> double <B>gsl_ran_exponential</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>mu</VAR>)</I>
306
<DD><A NAME="IDX1489"></A>
307
<A NAME="IDX1490"></A>
308
This function returns a random variate from the exponential distribution
309
with mean <VAR>mu</VAR>. The distribution is,
313
<PRE class="example">
314
p(x) dx = {1 \over \mu} \exp(-x/\mu) dx
325
<DT><U>Function:</U> double <B>gsl_ran_exponential_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>mu</VAR>)</I>
326
<DD><A NAME="IDX1491"></A>
327
This function computes the probability density p(x) at <VAR>x</VAR>
328
for an exponential distribution with mean <VAR>mu</VAR>, using the formula
337
<DT><U>Function:</U> double <B>gsl_cdf_exponential_P</B> <I>(double <VAR>x</VAR>, double <VAR>mu</VAR>)</I>
338
<DD><A NAME="IDX1492"></A>
339
<DT><U>Function:</U> double <B>gsl_cdf_exponential_Q</B> <I>(double <VAR>x</VAR>, double <VAR>mu</VAR>)</I>
340
<DD><A NAME="IDX1493"></A>
341
<DT><U>Function:</U> double <B>gsl_cdf_exponential_Pinv</B> <I>(double <VAR>P</VAR>, double <VAR>mu</VAR>)</I>
342
<DD><A NAME="IDX1494"></A>
343
<DT><U>Function:</U> double <B>gsl_cdf_exponential_Qinv</B> <I>(double <VAR>Q</VAR>, double <VAR>mu</VAR>)</I>
344
<DD><A NAME="IDX1495"></A>
345
These functions compute the cumulative distribution functions
346
P(x), Q(x) and their inverses for the exponential
347
distribution with mean <VAR>mu</VAR>.
353
<H2><A NAME="SEC292" HREF="gsl-ref_toc.html#TOC292">The Laplace Distribution</A></H2>
356
<DT><U>Random:</U> double <B>gsl_ran_laplace</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>a</VAR>)</I>
357
<DD><A NAME="IDX1496"></A>
358
<A NAME="IDX1497"></A>
359
<A NAME="IDX1498"></A>
360
This function returns a random variate from the Laplace distribution
361
with width <VAR>a</VAR>. The distribution is,
365
<PRE class="example">
366
p(x) dx = {1 \over 2 a} \exp(-|x/a|) dx
370
for -\infty < x < \infty.
376
<DT><U>Function:</U> double <B>gsl_ran_laplace_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>)</I>
377
<DD><A NAME="IDX1499"></A>
378
This function computes the probability density p(x) at <VAR>x</VAR>
379
for a Laplace distribution with width <VAR>a</VAR>, using the formula
388
<DT><U>Function:</U> double <B>gsl_cdf_laplace_P</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>)</I>
389
<DD><A NAME="IDX1500"></A>
390
<DT><U>Function:</U> double <B>gsl_cdf_laplace_Q</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>)</I>
391
<DD><A NAME="IDX1501"></A>
392
<DT><U>Function:</U> double <B>gsl_cdf_laplace_Pinv</B> <I>(double <VAR>P</VAR>, double <VAR>a</VAR>)</I>
393
<DD><A NAME="IDX1502"></A>
394
<DT><U>Function:</U> double <B>gsl_cdf_laplace_Qinv</B> <I>(double <VAR>Q</VAR>, double <VAR>a</VAR>)</I>
395
<DD><A NAME="IDX1503"></A>
396
These functions compute the cumulative distribution functions
397
P(x), Q(x) and their inverses for the Laplace
398
distribution with width <VAR>a</VAR>.
405
<H2><A NAME="SEC293" HREF="gsl-ref_toc.html#TOC293">The Exponential Power Distribution</A></H2>
408
<DT><U>Random:</U> double <B>gsl_ran_exppow</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
409
<DD><A NAME="IDX1504"></A>
410
<A NAME="IDX1505"></A>
411
This function returns a random variate from the exponential power distribution
412
with scale parameter <VAR>a</VAR> and exponent <VAR>b</VAR>. The distribution is,
416
<PRE class="example">
417
p(x) dx = {1 \over 2 a \Gamma(1+1/b)} \exp(-|x/a|^b) dx
422
x >= 0. For b = 1 this reduces to the Laplace
423
distribution. For b = 2 it has the same form as a gaussian
424
distribution, but with
431
<DT><U>Function:</U> double <B>gsl_ran_exppow_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
432
<DD><A NAME="IDX1506"></A>
433
This function computes the probability density p(x) at <VAR>x</VAR>
434
for an exponential power distribution with scale parameter <VAR>a</VAR>
435
and exponent <VAR>b</VAR>, using the formula given above.
443
<H2><A NAME="SEC294" HREF="gsl-ref_toc.html#TOC294">The Cauchy Distribution</A></H2>
446
<DT><U>Random:</U> double <B>gsl_ran_cauchy</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>a</VAR>)</I>
447
<DD><A NAME="IDX1507"></A>
448
<A NAME="IDX1508"></A>
449
This function returns a random variate from the Cauchy distribution with
450
scale parameter <VAR>a</VAR>. The probability distribution for Cauchy
455
<PRE class="example">
456
p(x) dx = {1 \over a\pi (1 + (x/a)^2) } dx
460
for x in the range -\infty to +\infty. The Cauchy
461
distribution is also known as the Lorentz distribution.
467
<DT><U>Function:</U> double <B>gsl_ran_cauchy_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>)</I>
468
<DD><A NAME="IDX1509"></A>
469
This function computes the probability density p(x) at <VAR>x</VAR>
470
for a Cauchy distribution with scale parameter <VAR>a</VAR>, using the formula
479
<DT><U>Function:</U> double <B>gsl_cdf_cauchy_P</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>)</I>
480
<DD><A NAME="IDX1510"></A>
481
<DT><U>Function:</U> double <B>gsl_cdf_cauchy_Q</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>)</I>
482
<DD><A NAME="IDX1511"></A>
483
<DT><U>Function:</U> double <B>gsl_cdf_cauchy_Pinv</B> <I>(double <VAR>P</VAR>, double <VAR>a</VAR>)</I>
484
<DD><A NAME="IDX1512"></A>
485
<DT><U>Function:</U> double <B>gsl_cdf_cauchy_Qinv</B> <I>(double <VAR>Q</VAR>, double <VAR>a</VAR>)</I>
486
<DD><A NAME="IDX1513"></A>
487
These functions compute the cumulative distribution functions
488
P(x), Q(x) and their inverses for the Cauchy
489
distribution with scale parameter <VAR>a</VAR>.
496
<H2><A NAME="SEC295" HREF="gsl-ref_toc.html#TOC295">The Rayleigh Distribution</A></H2>
499
<DT><U>Random:</U> double <B>gsl_ran_rayleigh</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>sigma</VAR>)</I>
500
<DD><A NAME="IDX1514"></A>
501
<A NAME="IDX1515"></A>
502
This function returns a random variate from the Rayleigh distribution with
503
scale parameter <VAR>sigma</VAR>. The distribution is,
507
<PRE class="example">
508
p(x) dx = {x \over \sigma^2} \exp(- x^2/(2 \sigma^2)) dx
518
<DT><U>Function:</U> double <B>gsl_ran_rayleigh_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>sigma</VAR>)</I>
519
<DD><A NAME="IDX1516"></A>
520
This function computes the probability density p(x) at <VAR>x</VAR>
521
for a Rayleigh distribution with scale parameter <VAR>sigma</VAR>, using the
530
<DT><U>Function:</U> double <B>gsl_cdf_rayleigh_P</B> <I>(double <VAR>x</VAR>, double <VAR>sigma</VAR>)</I>
531
<DD><A NAME="IDX1517"></A>
532
<DT><U>Function:</U> double <B>gsl_cdf_rayleigh_Q</B> <I>(double <VAR>x</VAR>, double <VAR>sigma</VAR>)</I>
533
<DD><A NAME="IDX1518"></A>
534
<DT><U>Function:</U> double <B>gsl_cdf_rayleigh_Pinv</B> <I>(double <VAR>P</VAR>, double <VAR>sigma</VAR>)</I>
535
<DD><A NAME="IDX1519"></A>
536
<DT><U>Function:</U> double <B>gsl_cdf_rayleigh_Qinv</B> <I>(double <VAR>Q</VAR>, double <VAR>sigma</VAR>)</I>
537
<DD><A NAME="IDX1520"></A>
538
These functions compute the cumulative distribution functions
539
P(x), Q(x) and their inverses for the Rayleigh
540
distribution with scale parameter <VAR>sigma</VAR>.
547
<H2><A NAME="SEC296" HREF="gsl-ref_toc.html#TOC296">The Rayleigh Tail Distribution</A></H2>
550
<DT><U>Random:</U> double <B>gsl_ran_rayleigh_tail</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>a</VAR>, double <VAR>sigma</VAR>)</I>
551
<DD><A NAME="IDX1521"></A>
552
<A NAME="IDX1522"></A>
553
This function returns a random variate from the tail of the Rayleigh
554
distribution with scale parameter <VAR>sigma</VAR> and a lower limit of
555
<VAR>a</VAR>. The distribution is,
559
<PRE class="example">
560
p(x) dx = {x \over \sigma^2} \exp ((a^2 - x^2) /(2 \sigma^2)) dx
570
<DT><U>Function:</U> double <B>gsl_ran_rayleigh_tail_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>sigma</VAR>)</I>
571
<DD><A NAME="IDX1523"></A>
572
This function computes the probability density p(x) at <VAR>x</VAR>
573
for a Rayleigh tail distribution with scale parameter <VAR>sigma</VAR> and
574
lower limit <VAR>a</VAR>, using the formula given above.
582
<H2><A NAME="SEC297" HREF="gsl-ref_toc.html#TOC297">The Landau Distribution</A></H2>
585
<DT><U>Random:</U> double <B>gsl_ran_landau</B> <I>(const gsl_rng * <VAR>r</VAR>)</I>
586
<DD><A NAME="IDX1524"></A>
587
<A NAME="IDX1525"></A>
588
This function returns a random variate from the Landau distribution. The
589
probability distribution for Landau random variates is defined
590
analytically by the complex integral,
594
<PRE class="example">
595
p(x) = (1/(2 \pi i)) \int_{c-i\infty}^{c+i\infty} ds exp(s log(s) + x s)
599
For numerical purposes it is more convenient to use the following
600
equivalent form of the integral,
602
<PRE class="example">
603
p(x) = (1/\pi) \int_0^\infty dt \exp(-t \log(t) - x t) \sin(\pi t).
610
<DT><U>Function:</U> double <B>gsl_ran_landau_pdf</B> <I>(double <VAR>x</VAR>)</I>
611
<DD><A NAME="IDX1526"></A>
612
This function computes the probability density p(x) at <VAR>x</VAR>
613
for the Landau distribution using an approximation to the formula given
622
<H2><A NAME="SEC298" HREF="gsl-ref_toc.html#TOC298">The Levy alpha-Stable Distributions</A></H2>
625
<DT><U>Random:</U> double <B>gsl_ran_levy</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>c</VAR>, double <VAR>alpha</VAR>)</I>
626
<DD><A NAME="IDX1527"></A>
627
<A NAME="IDX1528"></A>
628
This function returns a random variate from the Levy symmetric stable
629
distribution with scale <VAR>c</VAR> and exponent <VAR>alpha</VAR>. The symmetric
630
stable probability distribution is defined by a fourier transform,
634
<PRE class="example">
635
p(x) = {1 \over 2 \pi} \int_{-\infty}^{+\infty} dt \exp(-it x - |c t|^alpha)
639
There is no explicit solution for the form of p(x) and the
640
library does not define a corresponding <CODE>pdf</CODE> function. For
641
\alpha = 1 the distribution reduces to the Cauchy distribution. For
642
\alpha = 2 it is a Gaussian distribution with
643
\sigma = \sqrt{2} c. For \alpha < 1 the tails of the
644
distribution become extremely wide.
648
The algorithm only works for
649
0 < alpha <= 2.
657
<H2><A NAME="SEC299" HREF="gsl-ref_toc.html#TOC299">The Levy skew alpha-Stable Distribution</A></H2>
661
<DT><U>Random:</U> double <B>gsl_ran_levy_skew</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>c</VAR>, double <VAR>alpha</VAR>, double <VAR>beta</VAR>)</I>
662
<DD><A NAME="IDX1529"></A>
663
<A NAME="IDX1530"></A>
664
<A NAME="IDX1531"></A>
665
This function returns a random variate from the Levy skew stable
666
distribution with scale <VAR>c</VAR>, exponent <VAR>alpha</VAR> and skewness
667
parameter <VAR>beta</VAR>. The skewness parameter must lie in the range
668
[-1,1]. The Levy skew stable probability distribution is defined
669
by a fourier transform,
673
<PRE class="example">
674
p(x) = {1 \over 2 \pi} \int_{-\infty}^{+\infty} dt \exp(-it x - |c t|^alpha (1-i beta sign(t) tan(pi alpha/2)))
678
When \alpha = 1 the term \tan(\pi \alpha/2) is replaced by
679
-(2/\pi)\log|t|. There is no explicit solution for the form of
680
p(x) and the library does not define a corresponding <CODE>pdf</CODE>
681
function. For \alpha = 2 the distribution reduces to a Gaussian
683
\sigma = \sqrt{2} c and the skewness parameter has no effect.
684
For \alpha < 1 the tails of the distribution become extremely
685
wide. The symmetric distribution corresponds to \beta =
690
The algorithm only works for
691
0 < alpha <= 2.
696
The Levy alpha-stable distributions have the property that if N
697
alpha-stable variates are drawn from the distribution p(c, \alpha,
698
\beta) then the sum Y = X_1 + X_2 + \dots + X_N will also be
699
distributed as an alpha-stable variate,
700
p(N^(1/\alpha) c, \alpha, \beta).
708
<H2><A NAME="SEC300" HREF="gsl-ref_toc.html#TOC300">The Gamma Distribution</A></H2>
711
<DT><U>Random:</U> double <B>gsl_ran_gamma</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
712
<DD><A NAME="IDX1532"></A>
713
<A NAME="IDX1533"></A>
714
This function returns a random variate from the gamma
715
distribution. The distribution function is,
719
<PRE class="example">
720
p(x) dx = {1 \over \Gamma(a) b^a} x^{a-1} e^{-x/b} dx
730
<DT><U>Function:</U> double <B>gsl_ran_gamma_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
731
<DD><A NAME="IDX1534"></A>
732
This function computes the probability density p(x) at <VAR>x</VAR>
733
for a gamma distribution with parameters <VAR>a</VAR> and <VAR>b</VAR>, using the
742
<DT><U>Function:</U> double <B>gsl_cdf_gamma_P</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
743
<DD><A NAME="IDX1535"></A>
744
<DT><U>Function:</U> double <B>gsl_cdf_gamma_Q</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
745
<DD><A NAME="IDX1536"></A>
746
<DT><U>Function:</U> double <B>gsl_cdf_gamma_Pinv</B> <I>(double <VAR>P</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
747
<DD><A NAME="IDX1537"></A>
748
<DT><U>Function:</U> double <B>gsl_cdf_gamma_Qinv</B> <I>(double <VAR>Q</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
749
<DD><A NAME="IDX1538"></A>
750
These functions compute the cumulative distribution functions
751
P(x), Q(x) and their inverses for the gamma
752
distribution with parameters <VAR>a</VAR> and <VAR>b</VAR>.
758
<H2><A NAME="SEC301" HREF="gsl-ref_toc.html#TOC301">The Flat (Uniform) Distribution</A></H2>
761
<DT><U>Random:</U> double <B>gsl_ran_flat</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
762
<DD><A NAME="IDX1539"></A>
763
<A NAME="IDX1540"></A>
764
<A NAME="IDX1541"></A>
765
This function returns a random variate from the flat (uniform)
766
distribution from <VAR>a</VAR> to <VAR>b</VAR>. The distribution is,
770
<PRE class="example">
771
p(x) dx = {1 \over (b-a)} dx
776
a <= x < b and 0 otherwise.
782
<DT><U>Function:</U> double <B>gsl_ran_flat_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
783
<DD><A NAME="IDX1542"></A>
784
This function computes the probability density p(x) at <VAR>x</VAR>
785
for a uniform distribution from <VAR>a</VAR> to <VAR>b</VAR>, using the formula
794
<DT><U>Function:</U> double <B>gsl_cdf_flat_P</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
795
<DD><A NAME="IDX1543"></A>
796
<DT><U>Function:</U> double <B>gsl_cdf_flat_Q</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
797
<DD><A NAME="IDX1544"></A>
798
<DT><U>Function:</U> double <B>gsl_cdf_flat_Pinv</B> <I>(double <VAR>P</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
799
<DD><A NAME="IDX1545"></A>
800
<DT><U>Function:</U> double <B>gsl_cdf_flat_Qinv</B> <I>(double <VAR>Q</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
801
<DD><A NAME="IDX1546"></A>
802
These functions compute the cumulative distribution functions
803
P(x), Q(x) and their inverses for a uniform distribution
804
from <VAR>a</VAR> to <VAR>b</VAR>.
811
<H2><A NAME="SEC302" HREF="gsl-ref_toc.html#TOC302">The Lognormal Distribution</A></H2>
814
<DT><U>Random:</U> double <B>gsl_ran_lognormal</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>zeta</VAR>, double <VAR>sigma</VAR>)</I>
815
<DD><A NAME="IDX1547"></A>
816
<A NAME="IDX1548"></A>
817
This function returns a random variate from the lognormal
818
distribution. The distribution function is,
822
<PRE class="example">
823
p(x) dx = {1 \over x \sqrt{2 \pi \sigma^2} } \exp(-(\ln(x) - \zeta)^2/2 \sigma^2) dx
833
<DT><U>Function:</U> double <B>gsl_ran_lognormal_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>zeta</VAR>, double <VAR>sigma</VAR>)</I>
834
<DD><A NAME="IDX1549"></A>
835
This function computes the probability density p(x) at <VAR>x</VAR>
836
for a lognormal distribution with parameters <VAR>zeta</VAR> and <VAR>sigma</VAR>,
837
using the formula given above.
845
<DT><U>Function:</U> double <B>gsl_cdf_lognormal_P</B> <I>(double <VAR>x</VAR>, double <VAR>zeta</VAR>, double <VAR>sigma</VAR>)</I>
846
<DD><A NAME="IDX1550"></A>
847
<DT><U>Function:</U> double <B>gsl_cdf_lognormal_Q</B> <I>(double <VAR>x</VAR>, double <VAR>zeta</VAR>, double <VAR>sigma</VAR>)</I>
848
<DD><A NAME="IDX1551"></A>
849
<DT><U>Function:</U> double <B>gsl_cdf_lognormal_Pinv</B> <I>(double <VAR>P</VAR>, double <VAR>zeta</VAR>, double <VAR>sigma</VAR>)</I>
850
<DD><A NAME="IDX1552"></A>
851
<DT><U>Function:</U> double <B>gsl_cdf_lognormal_Qinv</B> <I>(double <VAR>Q</VAR>, double <VAR>zeta</VAR>, double <VAR>sigma</VAR>)</I>
852
<DD><A NAME="IDX1553"></A>
853
These functions compute the cumulative distribution functions
854
P(x), Q(x) and their inverses for the lognormal
855
distribution with parameters <VAR>zeta</VAR> and <VAR>sigma</VAR>.
862
<H2><A NAME="SEC303" HREF="gsl-ref_toc.html#TOC303">The Chi-squared Distribution</A></H2>
864
The chi-squared distribution arises in statistics. If Y_i are
865
n independent gaussian random variates with unit variance then the
870
<PRE class="example">
875
has a chi-squared distribution with n degrees of freedom.
880
<DT><U>Random:</U> double <B>gsl_ran_chisq</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>nu</VAR>)</I>
881
<DD><A NAME="IDX1554"></A>
882
<A NAME="IDX1555"></A>
883
This function returns a random variate from the chi-squared distribution
884
with <VAR>nu</VAR> degrees of freedom. The distribution function is,
888
<PRE class="example">
889
p(x) dx = {1 \over 2 \Gamma(\nu/2) } (x/2)^{\nu/2 - 1} \exp(-x/2) dx
900
<DT><U>Function:</U> double <B>gsl_ran_chisq_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>nu</VAR>)</I>
901
<DD><A NAME="IDX1556"></A>
902
This function computes the probability density p(x) at <VAR>x</VAR>
903
for a chi-squared distribution with <VAR>nu</VAR> degrees of freedom, using
904
the formula given above.
912
<DT><U>Function:</U> double <B>gsl_cdf_chisq_P</B> <I>(double <VAR>x</VAR>, double <VAR>nu</VAR>)</I>
913
<DD><A NAME="IDX1557"></A>
914
<DT><U>Function:</U> double <B>gsl_cdf_chisq_Q</B> <I>(double <VAR>x</VAR>, double <VAR>nu</VAR>)</I>
915
<DD><A NAME="IDX1558"></A>
916
<DT><U>Function:</U> double <B>gsl_cdf_chisq_Pinv</B> <I>(double <VAR>P</VAR>, double <VAR>nu</VAR>)</I>
917
<DD><A NAME="IDX1559"></A>
918
<DT><U>Function:</U> double <B>gsl_cdf_chisq_Qinv</B> <I>(double <VAR>Q</VAR>, double <VAR>nu</VAR>)</I>
919
<DD><A NAME="IDX1560"></A>
920
These functions compute the cumulative distribution functions
921
P(x), Q(x) and their inverses for the chi-squared
922
distribution with <VAR>nu</VAR> degrees of freedom.
929
<H2><A NAME="SEC304" HREF="gsl-ref_toc.html#TOC304">The F-distribution</A></H2>
931
The F-distribution arises in statistics. If Y_1 and Y_2
932
are chi-squared deviates with \nu_1 and \nu_2 degrees of
933
freedom then the ratio,
937
<PRE class="example">
938
X = { (Y_1 / \nu_1) \over (Y_2 / \nu_2) }
942
has an F-distribution F(x;\nu_1,\nu_2).
947
<DT><U>Random:</U> double <B>gsl_ran_fdist</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>nu1</VAR>, double <VAR>nu2</VAR>)</I>
948
<DD><A NAME="IDX1561"></A>
949
<A NAME="IDX1562"></A>
950
This function returns a random variate from the F-distribution with degrees of freedom <VAR>nu1</VAR> and <VAR>nu2</VAR>. The distribution function is,
954
<PRE class="example">
956
{ \Gamma((\nu_1 + \nu_2)/2)
957
\over \Gamma(\nu_1/2) \Gamma(\nu_2/2) }
958
\nu_1^{\nu_1/2} \nu_2^{\nu_2/2}
959
x^{\nu_1/2 - 1} (\nu_2 + \nu_1 x)^{-\nu_1/2 -\nu_2/2}
970
<DT><U>Function:</U> double <B>gsl_ran_fdist_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>nu1</VAR>, double <VAR>nu2</VAR>)</I>
971
<DD><A NAME="IDX1563"></A>
972
This function computes the probability density p(x) at <VAR>x</VAR>
973
for an F-distribution with <VAR>nu1</VAR> and <VAR>nu2</VAR> degrees of freedom,
974
using the formula given above.
982
<DT><U>Function:</U> double <B>gsl_cdf_fdist_P</B> <I>(double <VAR>x</VAR>, double <VAR>nu1</VAR>, double <VAR>nu2</VAR>)</I>
983
<DD><A NAME="IDX1564"></A>
984
<DT><U>Function:</U> double <B>gsl_cdf_fdist_Q</B> <I>(double <VAR>x</VAR>, double <VAR>nu1</VAR>, double <VAR>nu2</VAR>)</I>
985
<DD><A NAME="IDX1565"></A>
986
These functions compute the cumulative distribution functions
987
P(x) and Q(x) for the F-distribution with <VAR>nu1</VAR> and
988
<VAR>nu2</VAR> degrees of freedom.
994
<H2><A NAME="SEC305" HREF="gsl-ref_toc.html#TOC305">The t-distribution</A></H2>
996
The t-distribution arises in statistics. If Y_1 has a normal
997
distribution and Y_2 has a chi-squared distribution with
998
\nu degrees of freedom then the ratio,
1002
<PRE class="example">
1003
X = { Y_1 \over \sqrt{Y_2 / \nu} }
1007
has a t-distribution t(x;\nu) with \nu degrees of freedom.
1012
<DT><U>Random:</U> double <B>gsl_ran_tdist</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>nu</VAR>)</I>
1013
<DD><A NAME="IDX1566"></A>
1014
<A NAME="IDX1567"></A>
1015
<A NAME="IDX1568"></A>
1016
This function returns a random variate from the t-distribution. The
1017
distribution function is,
1021
<PRE class="example">
1022
p(x) dx = {\Gamma((\nu + 1)/2) \over \sqrt{\pi \nu} \Gamma(\nu/2)}
1023
(1 + x^2/\nu)^{-(\nu + 1)/2} dx
1027
for -\infty < x < +\infty.
1033
<DT><U>Function:</U> double <B>gsl_ran_tdist_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>nu</VAR>)</I>
1034
<DD><A NAME="IDX1569"></A>
1035
This function computes the probability density p(x) at <VAR>x</VAR>
1036
for a t-distribution with <VAR>nu</VAR> degrees of freedom, using the formula
1045
<DT><U>Function:</U> double <B>gsl_cdf_tdist_P</B> <I>(double <VAR>x</VAR>, double <VAR>nu</VAR>)</I>
1046
<DD><A NAME="IDX1570"></A>
1047
<DT><U>Function:</U> double <B>gsl_cdf_tdist_Q</B> <I>(double <VAR>x</VAR>, double <VAR>nu</VAR>)</I>
1048
<DD><A NAME="IDX1571"></A>
1049
<DT><U>Function:</U> double <B>gsl_cdf_tdist_Pinv</B> <I>(double <VAR>P</VAR>, double <VAR>nu</VAR>)</I>
1050
<DD><A NAME="IDX1572"></A>
1051
<DT><U>Function:</U> double <B>gsl_cdf_tdist_Qinv</B> <I>(double <VAR>Q</VAR>, double <VAR>nu</VAR>)</I>
1052
<DD><A NAME="IDX1573"></A>
1053
These functions compute the cumulative distribution functions
1054
P(x), Q(x) and their inverses for the t-distribution
1055
with <VAR>nu</VAR> degrees of freedom.
1061
<H2><A NAME="SEC306" HREF="gsl-ref_toc.html#TOC306">The Beta Distribution</A></H2>
1064
<DT><U>Random:</U> double <B>gsl_ran_beta</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1065
<DD><A NAME="IDX1574"></A>
1066
<A NAME="IDX1575"></A>
1067
This function returns a random variate from the beta
1068
distribution. The distribution function is,
1072
<PRE class="example">
1073
p(x) dx = {\Gamma(a+b) \over \Gamma(a) \Gamma(b)} x^{a-1} (1-x)^{b-1} dx
1078
0 <= x <= 1.
1084
<DT><U>Function:</U> double <B>gsl_ran_beta_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1085
<DD><A NAME="IDX1576"></A>
1086
This function computes the probability density p(x) at <VAR>x</VAR>
1087
for a beta distribution with parameters <VAR>a</VAR> and <VAR>b</VAR>, using the
1088
formula given above.
1096
<DT><U>Function:</U> double <B>gsl_cdf_beta_P</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1097
<DD><A NAME="IDX1577"></A>
1098
<DT><U>Function:</U> double <B>gsl_cdf_beta_Q</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1099
<DD><A NAME="IDX1578"></A>
1100
These functions compute the cumulative distribution functions
1101
P(x) and Q(x) for the beta distribution with
1102
parameters <VAR>a</VAR> and <VAR>b</VAR>.
1108
<H2><A NAME="SEC307" HREF="gsl-ref_toc.html#TOC307">The Logistic Distribution</A></H2>
1112
<DT><U>Random:</U> double <B>gsl_ran_logistic</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>a</VAR>)</I>
1113
<DD><A NAME="IDX1579"></A>
1114
<A NAME="IDX1580"></A>
1115
This function returns a random variate from the logistic
1116
distribution. The distribution function is,
1120
<PRE class="example">
1121
p(x) dx = { \exp(-x/a) \over a (1 + \exp(-x/a))^2 } dx
1125
for -\infty < x < +\infty.
1131
<DT><U>Function:</U> double <B>gsl_ran_logistic_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>)</I>
1132
<DD><A NAME="IDX1581"></A>
1133
This function computes the probability density p(x) at <VAR>x</VAR>
1134
for a logistic distribution with scale parameter <VAR>a</VAR>, using the
1135
formula given above.
1143
<DT><U>Function:</U> double <B>gsl_cdf_logistic_P</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>)</I>
1144
<DD><A NAME="IDX1582"></A>
1145
<DT><U>Function:</U> double <B>gsl_cdf_logistic_Q</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>)</I>
1146
<DD><A NAME="IDX1583"></A>
1147
<DT><U>Function:</U> double <B>gsl_cdf_logistic_Pinv</B> <I>(double <VAR>P</VAR>, double <VAR>a</VAR>)</I>
1148
<DD><A NAME="IDX1584"></A>
1149
<DT><U>Function:</U> double <B>gsl_cdf_logistic_Qinv</B> <I>(double <VAR>Q</VAR>, double <VAR>a</VAR>)</I>
1150
<DD><A NAME="IDX1585"></A>
1151
These functions compute the cumulative distribution functions
1152
P(x), Q(x) and their inverses for the logistic
1153
distribution with scale parameter <VAR>a</VAR>.
1159
<H2><A NAME="SEC308" HREF="gsl-ref_toc.html#TOC308">The Pareto Distribution</A></H2>
1162
<DT><U>Random:</U> double <B>gsl_ran_pareto</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1163
<DD><A NAME="IDX1586"></A>
1164
<A NAME="IDX1587"></A>
1165
This function returns a random variate from the Pareto distribution of
1166
order <VAR>a</VAR>. The distribution function is,
1170
<PRE class="example">
1171
p(x) dx = (a/b) / (x/b)^{a+1} dx
1182
<DT><U>Function:</U> double <B>gsl_ran_pareto_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1183
<DD><A NAME="IDX1588"></A>
1184
This function computes the probability density p(x) at <VAR>x</VAR>
1185
for a Pareto distribution with exponent <VAR>a</VAR> and scale <VAR>b</VAR>, using
1186
the formula given above.
1194
<DT><U>Function:</U> double <B>gsl_cdf_pareto_P</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1195
<DD><A NAME="IDX1589"></A>
1196
<DT><U>Function:</U> double <B>gsl_cdf_pareto_Q</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1197
<DD><A NAME="IDX1590"></A>
1198
<DT><U>Function:</U> double <B>gsl_cdf_pareto_Pinv</B> <I>(double <VAR>P</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1199
<DD><A NAME="IDX1591"></A>
1200
<DT><U>Function:</U> double <B>gsl_cdf_pareto_Qinv</B> <I>(double <VAR>Q</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1201
<DD><A NAME="IDX1592"></A>
1202
These functions compute the cumulative distribution functions
1203
P(x), Q(x) and their inverses for the Pareto
1204
distribution with exponent <VAR>a</VAR> and scale <VAR>b</VAR>.
1210
<H2><A NAME="SEC309" HREF="gsl-ref_toc.html#TOC309">Spherical Vector Distributions</A></H2>
1213
The spherical distributions generate random vectors, located on a
1214
spherical surface. They can be used as random directions, for example in
1215
the steps of a random walk.
1220
<DT><U>Random:</U> void <B>gsl_ran_dir_2d</B> <I>(const gsl_rng * <VAR>r</VAR>, double *<VAR>x</VAR>, double *<VAR>y</VAR>)</I>
1221
<DD><A NAME="IDX1593"></A>
1222
<DT><U>Random:</U> void <B>gsl_ran_dir_2d_trig_method</B> <I>(const gsl_rng * <VAR>r</VAR>, double *<VAR>x</VAR>, double *<VAR>y</VAR>)</I>
1223
<DD><A NAME="IDX1594"></A>
1224
<A NAME="IDX1595"></A>
1225
<A NAME="IDX1596"></A>
1226
<A NAME="IDX1597"></A>
1227
This function returns a random direction vector v =
1228
(<VAR>x</VAR>,<VAR>y</VAR>) in two dimensions. The vector is normalized such that
1229
|v|^2 = x^2 + y^2 = 1. The obvious way to do this is to take a
1230
uniform random number between 0 and 2\pi and let <VAR>x</VAR> and
1231
<VAR>y</VAR> be the sine and cosine respectively. Two trig functions would
1232
have been expensive in the old days, but with modern hardware
1233
implementations, this is sometimes the fastest way to go. This is the
1234
case for the Pentium (but not the case for the Sun Sparcstation).
1235
One can avoid the trig evaluations by choosing <VAR>x</VAR> and
1236
<VAR>y</VAR> in the interior of a unit circle (choose them at random from the
1237
interior of the enclosing square, and then reject those that are outside
1238
the unit circle), and then dividing by
1240
A much cleverer approach, attributed to von Neumann (See Knuth, v2, 3rd
1241
ed, p140, exercise 23), requires neither trig nor a square root. In
1242
this approach, <VAR>u</VAR> and <VAR>v</VAR> are chosen at random from the
1243
interior of a unit circle, and then x=(u^2-v^2)/(u^2+v^2) and
1250
<DT><U>Random:</U> void <B>gsl_ran_dir_3d</B> <I>(const gsl_rng * <VAR>r</VAR>, double *<VAR>x</VAR>, double *<VAR>y</VAR>, double * <VAR>z</VAR>)</I>
1251
<DD><A NAME="IDX1598"></A>
1252
<A NAME="IDX1599"></A>
1253
<A NAME="IDX1600"></A>
1254
<A NAME="IDX1601"></A>
1255
This function returns a random direction vector v =
1256
(<VAR>x</VAR>,<VAR>y</VAR>,<VAR>z</VAR>) in three dimensions. The vector is normalized
1257
such that |v|^2 = x^2 + y^2 + z^2 = 1. The method employed is
1258
due to Robert E. Knop (CACM 13, 326 (1970)), and explained in Knuth, v2,
1259
3rd ed, p136. It uses the surprising fact that the distribution
1260
projected along any axis is actually uniform (this is only true for 3
1267
<DT><U>Random:</U> void <B>gsl_ran_dir_nd</B> <I>(const gsl_rng * <VAR>r</VAR>, size_t <VAR>n</VAR>, double *<VAR>x</VAR>)</I>
1268
<DD><A NAME="IDX1602"></A>
1269
<A NAME="IDX1603"></A>
1270
<A NAME="IDX1604"></A>
1271
<A NAME="IDX1605"></A>
1275
This function returns a random direction vector
1276
v = (x_1,x_2,...,x_n) in <VAR>n</VAR> dimensions. The vector is normalized
1278
|v|^2 = x_1^2 + x_2^2 + ... + x_n^2 = 1. The method
1279
uses the fact that a multivariate gaussian distribution is spherically
1280
symmetric. Each component is generated to have a gaussian distribution,
1281
and then the components are normalized. The method is described by
1282
Knuth, v2, 3rd ed, p135-136, and attributed to G. W. Brown, Modern
1283
Mathematics for the Engineer (1956).
1289
<H2><A NAME="SEC310" HREF="gsl-ref_toc.html#TOC310">The Weibull Distribution</A></H2>
1292
<DT><U>Random:</U> double <B>gsl_ran_weibull</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1293
<DD><A NAME="IDX1606"></A>
1294
<A NAME="IDX1607"></A>
1295
This function returns a random variate from the Weibull distribution. The
1296
distribution function is,
1300
<PRE class="example">
1301
p(x) dx = {b \over a^b} x^{b-1} \exp(-(x/a)^b) dx
1312
<DT><U>Function:</U> double <B>gsl_ran_weibull_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1313
<DD><A NAME="IDX1608"></A>
1314
This function computes the probability density p(x) at <VAR>x</VAR>
1315
for a Weibull distribution with scale <VAR>a</VAR> and exponent <VAR>b</VAR>,
1316
using the formula given above.
1324
<DT><U>Function:</U> double <B>gsl_cdf_weibull_P</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1325
<DD><A NAME="IDX1609"></A>
1326
<DT><U>Function:</U> double <B>gsl_cdf_weibull_Q</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1327
<DD><A NAME="IDX1610"></A>
1328
<DT><U>Function:</U> double <B>gsl_cdf_weibull_Pinv</B> <I>(double <VAR>P</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1329
<DD><A NAME="IDX1611"></A>
1330
<DT><U>Function:</U> double <B>gsl_cdf_weibull_Qinv</B> <I>(double <VAR>Q</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1331
<DD><A NAME="IDX1612"></A>
1332
These functions compute the cumulative distribution functions
1333
P(x), Q(x) and their inverses for the Weibull
1334
distribution with scale <VAR>a</VAR> and exponent <VAR>b</VAR>.
1341
<H2><A NAME="SEC311" HREF="gsl-ref_toc.html#TOC311">The Type-1 Gumbel Distribution</A></H2>
1344
<DT><U>Random:</U> double <B>gsl_ran_gumbel1</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1345
<DD><A NAME="IDX1613"></A>
1346
<A NAME="IDX1614"></A>
1347
<A NAME="IDX1615"></A>
1348
This function returns a random variate from the Type-1 Gumbel
1349
distribution. The Type-1 Gumbel distribution function is,
1353
<PRE class="example">
1354
p(x) dx = a b \exp(-(b \exp(-ax) + ax)) dx
1358
for -\infty < x < \infty.
1364
<DT><U>Function:</U> double <B>gsl_ran_gumbel1_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1365
<DD><A NAME="IDX1616"></A>
1366
This function computes the probability density p(x) at <VAR>x</VAR>
1367
for a Type-1 Gumbel distribution with parameters <VAR>a</VAR> and <VAR>b</VAR>,
1368
using the formula given above.
1376
<DT><U>Function:</U> double <B>gsl_cdf_gumbel1_P</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1377
<DD><A NAME="IDX1617"></A>
1378
<DT><U>Function:</U> double <B>gsl_cdf_gumbel1_Q</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1379
<DD><A NAME="IDX1618"></A>
1380
<DT><U>Function:</U> double <B>gsl_cdf_gumbel1_Pinv</B> <I>(double <VAR>P</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1381
<DD><A NAME="IDX1619"></A>
1382
<DT><U>Function:</U> double <B>gsl_cdf_gumbel1_Qinv</B> <I>(double <VAR>Q</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1383
<DD><A NAME="IDX1620"></A>
1384
These functions compute the cumulative distribution functions
1385
P(x), Q(x) and their inverses for the Type-1 Gumbel
1386
distribution with parameters <VAR>a</VAR> and <VAR>b</VAR>.
1393
<H2><A NAME="SEC312" HREF="gsl-ref_toc.html#TOC312">The Type-2 Gumbel Distribution</A></H2>
1396
<DT><U>Random:</U> double <B>gsl_ran_gumbel2</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1397
<DD><A NAME="IDX1621"></A>
1398
<A NAME="IDX1622"></A>
1399
<A NAME="IDX1623"></A>
1400
This function returns a random variate from the Type-2 Gumbel
1401
distribution. The Type-2 Gumbel distribution function is,
1405
<PRE class="example">
1406
p(x) dx = a b x^{-a-1} \exp(-b x^{-a}) dx
1410
for 0 < x < \infty.
1416
<DT><U>Function:</U> double <B>gsl_ran_gumbel2_pdf</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1417
<DD><A NAME="IDX1624"></A>
1418
This function computes the probability density p(x) at <VAR>x</VAR>
1419
for a Type-2 Gumbel distribution with parameters <VAR>a</VAR> and <VAR>b</VAR>,
1420
using the formula given above.
1428
<DT><U>Function:</U> double <B>gsl_cdf_gumbel2_P</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1429
<DD><A NAME="IDX1625"></A>
1430
<DT><U>Function:</U> double <B>gsl_cdf_gumbel2_Q</B> <I>(double <VAR>x</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1431
<DD><A NAME="IDX1626"></A>
1432
<DT><U>Function:</U> double <B>gsl_cdf_gumbel2_Pinv</B> <I>(double <VAR>P</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1433
<DD><A NAME="IDX1627"></A>
1434
<DT><U>Function:</U> double <B>gsl_cdf_gumbel2_Qinv</B> <I>(double <VAR>Q</VAR>, double <VAR>a</VAR>, double <VAR>b</VAR>)</I>
1435
<DD><A NAME="IDX1628"></A>
1436
These functions compute the cumulative distribution functions
1437
P(x), Q(x) and their inverses for the Type-2 Gumbel
1438
distribution with parameters <VAR>a</VAR> and <VAR>b</VAR>.
1445
<H2><A NAME="SEC313" HREF="gsl-ref_toc.html#TOC313">The Dirichlet Distribution</A></H2>
1448
<DT><U>Random:</U> void <B>gsl_ran_dirichlet</B> <I>(const gsl_rng * <VAR>r</VAR>, size_t <VAR>K</VAR>, const double <VAR>alpha</VAR>[], double <VAR>theta</VAR>[])</I>
1449
<DD><A NAME="IDX1629"></A>
1450
<A NAME="IDX1630"></A>
1451
This function returns an array of <VAR>K</VAR> random variates from a Dirichlet
1452
distribution of order <VAR>K</VAR>-1. The distribution function is
1456
<PRE class="example">
1457
p(\theta_1, ..., \theta_K) d\theta_1 ... d\theta_K =
1458
(1/Z) \prod_{i=1}^K \theta_i^{\alpha_i - 1} \delta(1 -\sum_{i=1}^K \theta_i) d\theta_1 ... d\theta_K
1465
alpha_i >= 0. The delta function ensures that \sum \theta_i = 1.
1466
The normalization factor Z is
1470
<PRE class="example">
1471
Z = {\prod_{i=1}^K \Gamma(\alpha_i)} / {\Gamma( \sum_{i=1}^K \alpha_i)}
1475
The random variates are generated by sampling <VAR>K</VAR> values
1476
from gamma distributions with parameters
1479
See A.M. Law, W.D. Kelton, <CITE>Simulation Modeling and Analysis</CITE> (1991).
1485
<DT><U>Function:</U> double <B>gsl_ran_dirichlet_pdf</B> <I>(size_t <VAR>K</VAR>, const double <VAR>alpha</VAR>[], const double <VAR>theta</VAR>[])</I>
1486
<DD><A NAME="IDX1631"></A>
1487
This function computes the probability density
1488
p(\theta_1, ... , \theta_K)
1489
at <VAR>theta</VAR>[<VAR>K</VAR>] for a Dirichlet distribution with parameters
1490
<VAR>alpha</VAR>[<VAR>K</VAR>], using the formula given above.
1496
<DT><U>Function:</U> double <B>gsl_ran_dirichlet_lnpdf</B> <I>(size_t <VAR>K</VAR>, const double <VAR>alpha</VAR>[], const double <VAR>theta</VAR>[])</I>
1497
<DD><A NAME="IDX1632"></A>
1498
This function computes the logarithm of the probability density
1499
p(\theta_1, ... , \theta_K)
1500
for a Dirichlet distribution with parameters
1501
<VAR>alpha</VAR>[<VAR>K</VAR>].
1507
<H2><A NAME="SEC314" HREF="gsl-ref_toc.html#TOC314">General Discrete Distributions</A></H2>
1510
Given K discrete events with different probabilities P[k],
1511
produce a random value k consistent with its probability.
1515
The obvious way to do this is to preprocess the probability list by
1516
generating a cumulative probability array with K+1 elements:
1520
<PRE class="example">
1526
Note that this construction produces C[K]=1. Now choose a
1527
uniform deviate u between 0 and 1, and find the value of k
1529
C[k] <= u < C[k+1].
1530
Although this in principle requires of order \log K steps per
1531
random number generation, they are fast steps, and if you use something
1532
like \lfloor uK \rfloor as a starting point, you can often do
1537
But faster methods have been devised. Again, the idea is to preprocess
1538
the probability list, and save the result in some form of lookup table;
1539
then the individual calls for a random discrete event can go rapidly.
1540
An approach invented by G. Marsaglia (Generating discrete random numbers
1541
in a computer, Comm ACM 6, 37-38 (1963)) is very clever, and readers
1542
interested in examples of good algorithm design are directed to this
1543
short and well-written paper. Unfortunately, for large K,
1544
Marsaglia's lookup table can be quite large.
1548
A much better approach is due to Alastair J. Walker (An efficient method
1549
for generating discrete random variables with general distributions, ACM
1550
Trans on Mathematical Software 3, 253-256 (1977); see also Knuth, v2,
1551
3rd ed, p120-121,139). This requires two lookup tables, one floating
1552
point and one integer, but both only of size K. After
1553
preprocessing, the random numbers are generated in O(1) time, even for
1554
large K. The preprocessing suggested by Walker requires
1555
O(K^2) effort, but that is not actually necessary, and the
1556
implementation provided here only takes O(K) effort. In general,
1557
more preprocessing leads to faster generation of the individual random
1558
numbers, but a diminishing return is reached pretty early. Knuth points
1559
out that the optimal preprocessing is combinatorially difficult for
1564
This method can be used to speed up some of the discrete random number
1565
generators below, such as the binomial distribution. To use it for
1566
something like the Poisson Distribution, a modification would have to
1567
be made, since it only takes a finite set of K outcomes.
1572
<DT><U>Function:</U> gsl_ran_discrete_t * <B>gsl_ran_discrete_preproc</B> <I>(size_t <VAR>K</VAR>, const double * <VAR>P</VAR>)</I>
1573
<DD><A NAME="IDX1633"></A>
1574
<A NAME="IDX1634"></A>
1575
<A NAME="IDX1635"></A>
1576
This function returns a pointer to a structure that contains the lookup
1577
table for the discrete random number generator. The array <VAR>P</VAR>[] contains
1578
the probabilities of the discrete events; these array elements must all be
1579
positive, but they needn't add up to one (so you can think of them more
1580
generally as "weights")---the preprocessor will normalize appropriately.
1581
This return value is used
1582
as an argument for the <CODE>gsl_ran_discrete</CODE> function below.
1588
<DT><U>Random:</U> size_t <B>gsl_ran_discrete</B> <I>(const gsl_rng * <VAR>r</VAR>, const gsl_ran_discrete_t * <VAR>g</VAR>)</I>
1589
<DD><A NAME="IDX1636"></A>
1590
<A NAME="IDX1637"></A>
1591
After the preprocessor, above, has been called, you use this function to
1592
get the discrete random numbers.
1598
<DT><U>Function:</U> double <B>gsl_ran_discrete_pdf</B> <I>(size_t <VAR>k</VAR>, const gsl_ran_discrete_t * <VAR>g</VAR>)</I>
1599
<DD><A NAME="IDX1638"></A>
1600
<A NAME="IDX1639"></A>
1601
Returns the probability P[k] of observing the variable <VAR>k</VAR>.
1602
Since P[k] is not stored as part of the lookup table, it must be
1603
recomputed; this computation takes O(K), so if <VAR>K</VAR> is large
1604
and you care about the original array P[k] used to create the
1605
lookup table, then you should just keep this original array P[k]
1612
<DT><U>Function:</U> void <B>gsl_ran_discrete_free</B> <I>(gsl_ran_discrete_t * <VAR>g</VAR>)</I>
1613
<DD><A NAME="IDX1640"></A>
1614
<A NAME="IDX1641"></A>
1615
De-allocates the lookup table pointed to by <VAR>g</VAR>.
1621
<H2><A NAME="SEC315" HREF="gsl-ref_toc.html#TOC315">The Poisson Distribution</A></H2>
1624
<DT><U>Random:</U> unsigned int <B>gsl_ran_poisson</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>mu</VAR>)</I>
1625
<DD><A NAME="IDX1642"></A>
1626
<A NAME="IDX1643"></A>
1627
This function returns a random integer from the Poisson distribution
1628
with mean <VAR>mu</VAR>. The probability distribution for Poisson variates is,
1632
<PRE class="example">
1633
p(k) = {\mu^k \over k!} \exp(-\mu)
1644
<DT><U>Function:</U> double <B>gsl_ran_poisson_pdf</B> <I>(unsigned int <VAR>k</VAR>, double <VAR>mu</VAR>)</I>
1645
<DD><A NAME="IDX1644"></A>
1646
This function computes the probability p(k) of obtaining <VAR>k</VAR>
1647
from a Poisson distribution with mean <VAR>mu</VAR>, using the formula
1656
<H2><A NAME="SEC316" HREF="gsl-ref_toc.html#TOC316">The Bernoulli Distribution</A></H2>
1659
<DT><U>Random:</U> unsigned int <B>gsl_ran_bernoulli</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>p</VAR>)</I>
1660
<DD><A NAME="IDX1645"></A>
1661
<A NAME="IDX1646"></A>
1662
This function returns either 0 or 1, the result of a Bernoulli trial
1663
with probability <VAR>p</VAR>. The probability distribution for a Bernoulli
1668
<PRE class="example">
1677
<DT><U>Function:</U> double <B>gsl_ran_bernoulli_pdf</B> <I>(unsigned int <VAR>k</VAR>, double <VAR>p</VAR>)</I>
1678
<DD><A NAME="IDX1647"></A>
1679
This function computes the probability p(k) of obtaining
1680
<VAR>k</VAR> from a Bernoulli distribution with probability parameter
1681
<VAR>p</VAR>, using the formula given above.
1689
<H2><A NAME="SEC317" HREF="gsl-ref_toc.html#TOC317">The Binomial Distribution</A></H2>
1692
<DT><U>Random:</U> unsigned int <B>gsl_ran_binomial</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>p</VAR>, unsigned int <VAR>n</VAR>)</I>
1693
<DD><A NAME="IDX1648"></A>
1694
<A NAME="IDX1649"></A>
1695
This function returns a random integer from the binomial distribution,
1696
the number of successes in <VAR>n</VAR> independent trials with probability
1697
<VAR>p</VAR>. The probability distribution for binomial variates is,
1701
<PRE class="example">
1702
p(k) = {n! \over k! (n-k)! } p^k (1-p)^{n-k}
1707
0 <= k <= n.
1713
<DT><U>Function:</U> double <B>gsl_ran_binomial_pdf</B> <I>(unsigned int <VAR>k</VAR>, double <VAR>p</VAR>, unsigned int <VAR>n</VAR>)</I>
1714
<DD><A NAME="IDX1650"></A>
1715
This function computes the probability p(k) of obtaining <VAR>k</VAR>
1716
from a binomial distribution with parameters <VAR>p</VAR> and <VAR>n</VAR>, using
1717
the formula given above.
1725
<H2><A NAME="SEC318" HREF="gsl-ref_toc.html#TOC318">The Multinomial Distribution</A></H2>
1728
<DT><U>Random:</U> void <B>gsl_ran_multinomial</B> <I>(const gsl_rng * <VAR>r</VAR>, size_t <VAR>K</VAR>, unsigned int <VAR>N</VAR>, const double <VAR>p</VAR>[], unsigned int <VAR>n</VAR>[])</I>
1729
<DD><A NAME="IDX1651"></A>
1730
<A NAME="IDX1652"></A>
1734
This function returns an array of <VAR>K</VAR> random variates from a
1735
multinomial distribution. The distribution function is,
1739
<PRE class="example">
1740
P(n_1, n_2, ..., n_K) =
1741
(N!/(n_1! n_2! ... n_K!)) p_1^n_1 p_2^n_2 ... p_K^n_K
1746
(n_1, n_2, ..., n_K)
1747
are nonnegative integers with
1748
sum_{k=1}^K n_k = N,
1750
(p_1, p_2, ..., p_K)
1751
is a probability distribution with \sum p_i = 1.
1752
If the array <VAR>p</VAR>[<VAR>K</VAR>] is not normalized then its entries will be
1753
treated as weights and normalized appropriately.
1757
Random variates are generated using the conditional binomial method (see
1758
C.S. David, <CITE>The computer generation of multinomial random
1759
variates</CITE>, Comp. Stat. Data Anal. 16 (1993) 205-217 for details).
1765
<DT><U>Function:</U> double <B>gsl_ran_multinomial_pdf</B> <I>(size_t <VAR>K</VAR>, const double <VAR>p</VAR>[], const unsigned int <VAR>n</VAR>[])</I>
1766
<DD><A NAME="IDX1653"></A>
1767
This function computes the probability
1768
P(n_1, n_2, ..., n_K)
1769
of sampling <VAR>n</VAR>[<VAR>K</VAR>] from a multinomial distribution
1770
with parameters <VAR>p</VAR>[<VAR>K</VAR>], using the formula given above.
1776
<DT><U>Function:</U> double <B>gsl_ran_multinomial_lnpdf</B> <I>(size_t <VAR>K</VAR>, const double <VAR>p</VAR>[], const unsigned int <VAR>n</VAR>[])</I>
1777
<DD><A NAME="IDX1654"></A>
1778
This function returns the logarithm of the probability for the
1779
multinomial distribution
1780
P(n_1, n_2, ..., n_K) with parameters <VAR>p</VAR>[<VAR>K</VAR>].
1787
<H2><A NAME="SEC319" HREF="gsl-ref_toc.html#TOC319">The Negative Binomial Distribution</A></H2>
1790
<DT><U>Random:</U> unsigned int <B>gsl_ran_negative_binomial</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>p</VAR>, double <VAR>n</VAR>)</I>
1791
<DD><A NAME="IDX1655"></A>
1792
<A NAME="IDX1656"></A>
1793
This function returns a random integer from the negative binomial
1794
distribution, the number of failures occurring before <VAR>n</VAR> successes
1795
in independent trials with probability <VAR>p</VAR> of success. The
1796
probability distribution for negative binomial variates is,
1800
<PRE class="example">
1801
p(k) = {\Gamma(n + k) \over \Gamma(k+1) \Gamma(n) } p^n (1-p)^k
1805
Note that n is not required to be an integer.
1811
<DT><U>Function:</U> double <B>gsl_ran_negative_binomial_pdf</B> <I>(unsigned int <VAR>k</VAR>, double <VAR>p</VAR>, double <VAR>n</VAR>)</I>
1812
<DD><A NAME="IDX1657"></A>
1813
This function computes the probability p(k) of obtaining <VAR>k</VAR>
1814
from a negative binomial distribution with parameters <VAR>p</VAR> and
1815
<VAR>n</VAR>, using the formula given above.
1823
<H2><A NAME="SEC320" HREF="gsl-ref_toc.html#TOC320">The Pascal Distribution</A></H2>
1827
<DT><U>Random:</U> unsigned int <B>gsl_ran_pascal</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>p</VAR>, unsigned int <VAR>n</VAR>)</I>
1828
<DD><A NAME="IDX1658"></A>
1829
This function returns a random integer from the Pascal distribution. The
1830
Pascal distribution is simply a negative binomial distribution with an
1835
<PRE class="example">
1836
p(k) = {(n + k - 1)! \over k! (n - 1)! } p^n (1-p)^k
1847
<DT><U>Function:</U> double <B>gsl_ran_pascal_pdf</B> <I>(unsigned int <VAR>k</VAR>, double <VAR>p</VAR>, unsigned int <VAR>n</VAR>)</I>
1848
<DD><A NAME="IDX1659"></A>
1849
This function computes the probability p(k) of obtaining <VAR>k</VAR>
1850
from a Pascal distribution with parameters <VAR>p</VAR> and
1851
<VAR>n</VAR>, using the formula given above.
1859
<H2><A NAME="SEC321" HREF="gsl-ref_toc.html#TOC321">The Geometric Distribution</A></H2>
1862
<DT><U>Random:</U> unsigned int <B>gsl_ran_geometric</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>p</VAR>)</I>
1863
<DD><A NAME="IDX1660"></A>
1864
<A NAME="IDX1661"></A>
1865
This function returns a random integer from the geometric distribution,
1866
the number of independent trials with probability <VAR>p</VAR> until the
1867
first success. The probability distribution for geometric variates
1872
<PRE class="example">
1873
p(k) = p (1-p)^(k-1)
1884
<DT><U>Function:</U> double <B>gsl_ran_geometric_pdf</B> <I>(unsigned int <VAR>k</VAR>, double <VAR>p</VAR>)</I>
1885
<DD><A NAME="IDX1662"></A>
1886
This function computes the probability p(k) of obtaining <VAR>k</VAR>
1887
from a geometric distribution with probability parameter <VAR>p</VAR>, using
1888
the formula given above.
1896
<H2><A NAME="SEC322" HREF="gsl-ref_toc.html#TOC322">The Hypergeometric Distribution</A></H2>
1898
<A NAME="IDX1663"></A>
1900
<DT><U>Random:</U> unsigned int <B>gsl_ran_hypergeometric</B> <I>(const gsl_rng * <VAR>r</VAR>, unsigned int <VAR>n1</VAR>, unsigned int <VAR>n2</VAR>, unsigned int <VAR>t</VAR>)</I>
1901
<DD><A NAME="IDX1664"></A>
1902
<A NAME="IDX1665"></A>
1903
This function returns a random integer from the hypergeometric
1904
distribution. The probability distribution for hypergeometric
1909
<PRE class="example">
1910
p(k) = C(n_1, k) C(n_2, t - k) / C(n_1 + n_2, t)
1914
where C(a,b) = a!/(b!(a-b)!) and
1915
t <= n_1 + n_2. The
1917
max(0,t-n_2), ..., min(t,n_1).
1923
<DT><U>Function:</U> double <B>gsl_ran_hypergeometric_pdf</B> <I>(unsigned int <VAR>k</VAR>, unsigned int <VAR>n1</VAR>, unsigned int <VAR>n2</VAR>, unsigned int <VAR>t</VAR>)</I>
1924
<DD><A NAME="IDX1666"></A>
1925
This function computes the probability p(k) of obtaining <VAR>k</VAR>
1926
from a hypergeometric distribution with parameters <VAR>n1</VAR>, <VAR>n2</VAR>,
1927
<VAR>t</VAR>, using the formula given above.
1935
<H2><A NAME="SEC323" HREF="gsl-ref_toc.html#TOC323">The Logarithmic Distribution</A></H2>
1938
<DT><U>Random:</U> unsigned int <B>gsl_ran_logarithmic</B> <I>(const gsl_rng * <VAR>r</VAR>, double <VAR>p</VAR>)</I>
1939
<DD><A NAME="IDX1667"></A>
1940
<A NAME="IDX1668"></A>
1941
This function returns a random integer from the logarithmic
1942
distribution. The probability distribution for logarithmic random variates
1947
<PRE class="example">
1948
p(k) = {-1 \over \log(1-p)} {(p^k \over k)}
1959
<DT><U>Function:</U> double <B>gsl_ran_logarithmic_pdf</B> <I>(unsigned int <VAR>k</VAR>, double <VAR>p</VAR>)</I>
1960
<DD><A NAME="IDX1669"></A>
1961
This function computes the probability p(k) of obtaining <VAR>k</VAR>
1962
from a logarithmic distribution with probability parameter <VAR>p</VAR>,
1963
using the formula given above.
1971
<H2><A NAME="SEC324" HREF="gsl-ref_toc.html#TOC324">Shuffling and Sampling</A></H2>
1974
The following functions allow the shuffling and sampling of a set of
1975
objects. The algorithms rely on a random number generator as a source of
1976
randomness and a poor quality generator can lead to correlations in the
1977
output. In particular it is important to avoid generators with a short
1978
period. For more information see Knuth, v2, 3rd ed, Section 3.4.2,
1979
"Random Sampling and Shuffling".
1984
<DT><U>Random:</U> void <B>gsl_ran_shuffle</B> <I>(const gsl_rng * <VAR>r</VAR>, void * <VAR>base</VAR>, size_t <VAR>n</VAR>, size_t <VAR>size</VAR>)</I>
1985
<DD><A NAME="IDX1670"></A>
1989
This function randomly shuffles the order of <VAR>n</VAR> objects, each of
1990
size <VAR>size</VAR>, stored in the array <VAR>base</VAR>[0..<VAR>n</VAR>-1]. The
1991
output of the random number generator <VAR>r</VAR> is used to produce the
1992
permutation. The algorithm generates all possible n!
1993
permutations with equal probability, assuming a perfect source of random
1998
The following code shows how to shuffle the numbers from 0 to 51,
2002
<PRE class="example">
2005
for (i = 0; i < 52; i++)
2010
gsl_ran_shuffle (r, a, 52, sizeof (int));
2017
<DT><U>Random:</U> int <B>gsl_ran_choose</B> <I>(const gsl_rng * <VAR>r</VAR>, void * <VAR>dest</VAR>, size_t <VAR>k</VAR>, void * <VAR>src</VAR>, size_t <VAR>n</VAR>, size_t <VAR>size</VAR>)</I>
2018
<DD><A NAME="IDX1671"></A>
2019
This function fills the array <VAR>dest</VAR>[k] with <VAR>k</VAR> objects taken
2020
randomly from the <VAR>n</VAR> elements of the array
2021
<VAR>src</VAR>[0..<VAR>n</VAR>-1]. The objects are each of size <VAR>size</VAR>. The
2022
output of the random number generator <VAR>r</VAR> is used to make the
2023
selection. The algorithm ensures all possible samples are equally
2024
likely, assuming a perfect source of randomness.
2028
The objects are sampled <EM>without</EM> replacement, thus each object can
2029
only appear once in <VAR>dest</VAR>[k]. It is required that <VAR>k</VAR> be less
2030
than or equal to <CODE>n</CODE>. The objects in <VAR>dest</VAR> will be in the
2031
same relative order as those in <VAR>src</VAR>. You will need to call
2032
<CODE>gsl_ran_shuffle(r, dest, n, size)</CODE> if you want to randomize the
2037
The following code shows how to select a random sample of three unique
2038
numbers from the set 0 to 99,
2042
<PRE class="example">
2043
double a[3], b[100];
2045
for (i = 0; i < 100; i++)
2050
gsl_ran_choose (r, a, 3, b, 100, sizeof (double));
2057
<DT><U>Random:</U> void <B>gsl_ran_sample</B> <I>(const gsl_rng * <VAR>r</VAR>, void * <VAR>dest</VAR>, size_t <VAR>k</VAR>, void * <VAR>src</VAR>, size_t <VAR>n</VAR>, size_t <VAR>size</VAR>)</I>
2058
<DD><A NAME="IDX1672"></A>
2059
This function is like <CODE>gsl_ran_choose</CODE> but samples <VAR>k</VAR> items
2060
from the original array of <VAR>n</VAR> items <VAR>src</VAR> with replacement, so
2061
the same object can appear more than once in the output sequence
2062
<VAR>dest</VAR>. There is no requirement that <VAR>k</VAR> be less than <VAR>n</VAR>
2070
<H2><A NAME="SEC325" HREF="gsl-ref_toc.html#TOC325">Examples</A></H2>
2073
The following program demonstrates the use of a random number generator
2074
to produce variates from a distribution. It prints 10 samples from the
2075
Poisson distribution with a mean of 3.
2079
<PRE class="example">
2080
#include <stdio.h>
2081
#include <gsl/gsl_rng.h>
2082
#include <gsl/gsl_randist.h>
2087
const gsl_rng_type * T;
2093
/* create a generator chosen by the
2094
environment variable GSL_RNG_TYPE */
2096
gsl_rng_env_setup();
2098
T = gsl_rng_default;
2099
r = gsl_rng_alloc (T);
2101
/* print n random variates chosen from
2102
the poisson distribution with mean
2105
for (i = 0; i < n; i++)
2107
unsigned int k = gsl_ran_poisson (r, mu);
2117
If the library and header files are installed under <TT>'/usr/local'</TT>
2118
(the default location) then the program can be compiled with these
2123
<PRE class="example">
2124
gcc demo.c -lgsl -lgslcblas -lm
2128
Here is the output of the program,
2132
<PRE class="example">
2138
The variates depend on the seed used by the generator. The seed for the
2139
default generator type <CODE>gsl_rng_default</CODE> can be changed with the
2140
<CODE>GSL_RNG_SEED</CODE> environment variable to produce a different stream
2145
<PRE class="example">
2146
$ GSL_RNG_SEED=123 ./a.out
2152
The following program generates a random walk in two dimensions.
2156
<PRE class="example">
2157
#include <stdio.h>
2158
#include <gsl/gsl_rng.h>
2159
#include <gsl/gsl_randist.h>
2165
double x = 0, y = 0, dx, dy;
2167
const gsl_rng_type * T;
2170
gsl_rng_env_setup();
2171
T = gsl_rng_default;
2172
r = gsl_rng_alloc (T);
2174
printf ("%g %g\n", x, y);
2176
for (i = 0; i < 10; i++)
2178
gsl_ran_dir_2d (r, &dx, &dy);
2180
printf ("%g %g\n", x, y);
2187
Example output from the program, three 10-step random walks from the origin.
2193
The following program computes the upper and lower cumulative
2194
distribution functions for the standard normal distribution at
2199
<PRE class="example">
2200
#include <stdio.h>
2201
#include <gsl/gsl_cdf.h>
2209
P = gsl_cdf_ugaussian_P (x);
2210
printf ("prob(x < %f) = %f\n", x, P);
2212
Q = gsl_cdf_ugaussian_Q (x);
2213
printf ("prob(x > %f) = %f\n", x, Q);
2215
x = gsl_cdf_ugaussian_Pinv (P);
2216
printf ("Pinv(%f) = %f\n", P, x);
2218
x = gsl_cdf_ugaussian_Qinv (Q);
2219
printf ("Qinv(%f) = %f\n", Q, x);
2226
Here is the output of the program,
2230
<PRE class="example">
2231
prob(x < 2.000000) = 0.977250
2232
prob(x > 2.000000) = 0.022750
2233
Pinv(0.977250) = 2.000000
2234
Qinv(0.022750) = 2.000000
2239
<H2><A NAME="SEC326" HREF="gsl-ref_toc.html#TOC326">References and Further Reading</A></H2>
2242
For an encyclopaedic coverage of the subject readers are advised to
2243
consult the book <CITE>Non-Uniform Random Variate Generation</CITE> by Luc
2244
Devroye. It covers every imaginable distribution and provides hundreds
2249
<UL class="itemize">
2252
Luc Devroye, <CITE>Non-Uniform Random Variate Generation</CITE>,
2253
Springer-Verlag, ISBN 0-387-96305-7.
2257
The subject of random variate generation is also reviewed by Knuth, who
2258
describes algorithms for all the major distributions.
2262
<UL class="itemize">
2265
Donald E. Knuth, <CITE>The Art of Computer Programming: Seminumerical
2266
Algorithms</CITE> (Vol 2, 3rd Ed, 1997), Addison-Wesley, ISBN 0201896842.
2270
The Particle Data Group provides a short review of techniques for
2271
generating distributions of random numbers in the "Monte Carlo"
2272
section of its Annual Review of Particle Physics.
2276
<UL class="itemize">
2279
<CITE>Review of Particle Properties</CITE>
2280
R.M. Barnett et al., Physical Review D54, 1 (1996)
2281
<A HREF="http://pdg.lbl.gov/">http://pdg.lbl.gov/</A>.
2285
The Review of Particle Physics is available online in postscript and pdf
2290
An overview of methods used to compute cumulative distribution functions
2291
can be found in <CITE>Statistical Computing</CITE> by W.J. Kennedy and
2292
J.E. Gentle. Another general reference is <CITE>Elements of Statistical
2293
Computing</CITE> by R.A. Thisted.
2297
<UL class="itemize">
2300
William E. Kennedy and James E. Gentle, <CITE>Statistical Computing</CITE> (1980),
2301
Marcel Dekker, ISBN 0-8247-6898-1.
2305
<UL class="itemize">
2308
Ronald A. Thisted, <CITE>Elements of Statistical Computing</CITE> (1988),
2309
Chapman & Hall, ISBN 0-412-01371-1.
2313
The cumulative distribution functions for the Gaussian distribution
2314
are based on the following papers,
2318
<UL class="itemize">
2321
<CITE>Rational Chebyshev Approximations Using Linear Equations</CITE>,
2322
W.J. Cody, W. Fraser, J.F. Hart. Numerische Mathematik 12, 242-251 (1968).
2326
<UL class="itemize">
2329
<CITE>Rational Chebyshev Approximations for the Error Function</CITE>,
2330
W.J. Cody. Mathematics of Computation 23, n107, 631-637 (July 1969).
2334
<p>Go to the <A HREF="gsl-ref_1.html">first</A>, <A HREF="gsl-ref_18.html">previous</A>, <A HREF="gsl-ref_20.html">next</A>, <A HREF="gsl-ref_50.html">last</A> section, <A HREF="gsl-ref_toc.html">table of contents</A>.