-
Notifications
You must be signed in to change notification settings - Fork 10
/
cl-randist.html
393 lines (242 loc) · 12.9 KB
/
cl-randist.html
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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>RANDOM-DISTRIBUTIONS - a cool library</title>
<style type="text/css">
pre { padding:5px; background-color:#e0e0e0 }
h3, h4 { text-decoration: underline; }
a { text-decoration: none; padding: 1px 2px 1px 2px; }
a:visited { text-decoration: none; padding: 1px 2px 1px 2px; }
a:hover { text-decoration: none; padding: 1px 1px 1px 1px; border: 1px solid #000000; }
a:focus { text-decoration: none; padding: 1px 2px 1px 2px; border: none; }
a.none { text-decoration: none; padding: 0; }
a.none:visited { text-decoration: none; padding: 0; }
a.none:hover { text-decoration: none; border: none; padding: 0; }
a.none:focus { text-decoration: none; border: none; padding: 0; }
a.noborder { text-decoration: none; padding: 0; }
a.noborder:visited { text-decoration: none; padding: 0; }
a.noborder:hover { text-decoration: none; border: none; padding: 0; }
a.noborder:focus { text-decoration: none; border: none; padding: 0; }
pre.none { padding:5px; background-color:#ffffff }
</style>
</head>
<body bgcolor=white>
<h2> RANDOM-DISTRIBUTIONS - A library for random numbers generation</h2>
<blockquote>
<br> <br><h3><a name=abstract class=none>Abstract</a></h3>
<p>This a manual translation from C to common lisp of some random number generation functions of <a href="http://www.gnu.org/software/gsl/">GSL</a> library.</p>
<p> This package also include the alias method implementation of <a href="http://prxq.wordpress.com/2006/04/23/more-on-the-alias-method/">Mario S. Mommer</a> and my implementation of <a href="http://links.jstor.org/sici?sici=0003-1305(198408)38%3A3%3C219%3ASFADDW%3E2.0.CO%3B2-5">cut point method</a> for discrete random variable generation.</p>
<p>The code comes with a <a href="http://www.opensource.org/licenses/bsd-license.php">BSD-style license</a>
so you can basically do with it whatever you want.</p>
<p>
<font color=red>Download shortcut:</font> <a
href="http://cl-randist.googlecode.com/files/cl-randist.tar.gz">http://cl-randist.googlecode.com/files/cl-randist.tar.gz</a>.
<p> Latest development version can by download with git: git clone http://lambdatau.com/git/cl-randist</p>
</blockquote>
<br> <br><h3><a class=none name="contents">Contents</a></h3>
<ol>
<li><a href="#download">Download</a>
<li><a href="#dictionary">The RANDOM-DISTRIBUTIONS dictionary</a>
<ol>
<li><a href="#make-discrete-monotone-random-var"><code>make-discrete-monotone-random-var</code></a>
<li><a href="#make-discrete-random-var"><code>make-discrete-random-var</code></a>
<li><a href="#random-beta"><code>random-beta</code></a>
<li><a href="#random-binomial"><code>random-binomial</code></a>
<li><a href="#random-chi-square"><code>random-chi-square</code></a>
<li><a href="#random-exponential"><code>random-exponential</code></a>
<li><a href="#random-f"><code>random-f</code></a>
<li><a href="#random-gamma"><code>random-gamma</code></a>
<li><a href="#random-gamma-int"><code>random-gamma-int</code></a>
<li><a href="#random-gamma-mt"><code>random-gamma-mt</code></a>
<li><a href="#random-gamma1"><code>random-gamma1</code></a>
<li><a href="#random-multinomial"><code>random-multinomial</code></a>
<li><a href="#random-negative-binomial"><code>random-negative-binomial</code></a>
<li><a href="#random-normal"><code>random-normal</code></a>
<li><a href="#random-normal-ziggurat"><code>random-normal-ziggurat</code></a>
<li><a href="#random-pareto"><code>random-pareto</code></a>
<li><a href="#random-poisson"><code>random-poisson</code></a>
<li><a href="#random-uniform"><code>random-uniform</code></a>
</ol>
<li><a href="#ack">Acknowledgements</a>
</ol>
<br> <br><h3><a class=none name="download">Download</a></h3>
RANDOM-DISTRIBUTIONS together with this documentation can be downloaded from <a
href="http://cl-randist.googlecode.com/files/cl-randist.tar.gz">http://cl-randist.googlecode.com/files/cl-randist.tar.gz</a>. The
current version is 0.3.0.
<br> <br><h3><a class=none name="dictionary">The RANDOM-DISTRIBUTIONS dictionary</a></h3>
<!-- Entry for MAKE-DISCRETE-MONOTONE-RANDOM-VAR -->
<p><br>[Function]<br><a class=none name='make-discrete-monotone-random-var'><b>make-discrete-monotone-random-var</b> <i>p</i> => <i>result</i></a>
<blockquote><br>
The function MAKE-DISCRETE-MONOTONE-RANDOM-VAR takes an array of
probabilities. Produces a function which returns each of the
corresponding integer with the specified probability.
</blockquote>
<!-- End of entry for MAKE-DISCRETE-MONOTONE-RANDOM-VAR -->
<!-- Entry for MAKE-DISCRETE-RANDOM-VAR -->
<p><br>[Function]<br><a class=none name='make-discrete-random-var'><b>make-discrete-random-var</b> <i>probabilities <tt>&optional</tt> values</i> => <i>result</i></a>
<blockquote><br>
The function MAKE-DISCRETE-RANDOM-VAR takes an array of
probabilities and an (optional) array of values. Produces a
function which returns each of the values with the specified
probability (or the corresponding integer no values have been
given).
</blockquote>
<!-- End of entry for MAKE-DISCRETE-RANDOM-VAR -->
<!-- Entry for RANDOM-BETA -->
<p><br>[Function]<br><a class=none name='random-beta'><b>random-beta</b> <i>a b</i> => <i>result</i></a>
<blockquote><br>
The beta distribution has the form
p(x) dx = (Gamma(a + b)/(Gamma(a) Gamma(b))) x^(a-1) (1-x)^(b-1) dx
The method used here is the one described in Knuth
</blockquote>
<!-- End of entry for RANDOM-BETA -->
<!-- Entry for RANDOM-BINOMIAL -->
<p><br>[Function]<br><a class=none name='random-binomial'><b>random-binomial</b> <i></i> => <i>result</i></a>
<blockquote><br>
The binomial distribution has the form,
prob(k) = n!/(k!(n-k)!) * p^k (1-p)^(n-k) for k = 0, 1, ..., n
This is the algorithm from Knuth
</blockquote>
<!-- End of entry for RANDOM-BINOMIAL -->
<!-- Entry for RANDOM-CHI-SQUARE -->
<p><br>[Function]<br><a class=none name='random-chi-square'><b>random-chi-square</b> <i></i> => <i>result</i></a>
<blockquote><br>
Generate random variable for chi square distribution:
p(x) dx = (1/(2*Gamma(nu/2))) (x/2)^(nu/2 - 1) exp(-x/2) dx
</blockquote>
<!-- End of entry for RANDOM-CHI-SQUARE -->
<!-- Entry for RANDOM-EXPONENTIAL -->
<p><br>[Function]<br><a class=none name='random-exponential'><b>random-exponential</b> <i></i> => <i>result</i></a>
<blockquote><br>
Random values for:
p(x) dx = exp(-x/mu) dx/mu
</blockquote>
<!-- End of entry for RANDOM-EXPONENTIAL -->
<!-- Entry for RANDOM-F -->
<p><br>[Function]<br><a class=none name='random-f'><b>random-f</b> <i></i> => <i>result</i></a>
<blockquote><br>
Random value for:
p(x) dx = (nu1^(nu1/2) nu2^(nu2/2) Gamma((nu1 + nu2)/2) /
Gamma(nu1/2) Gamma(nu2/2)) *
x^(nu1/2 - 1) (nu2 + nu1 * x)^(-nu1/2 -nu2/2) dx
</blockquote>
<!-- End of entry for RANDOM-F -->
<!-- Entry for RANDOM-GAMMA -->
<p><br>[Function]<br><a class=none name='random-gamma'><b>random-gamma</b> <i>a <tt>&optional</tt> b</i> => <i>result</i></a>
<blockquote><br>
[syntax suggar] Generate a random variable with gamma distribution using the MT method (see random-gamma-mt)
</blockquote>
<!-- End of entry for RANDOM-GAMMA -->
<!-- Entry for RANDOM-GAMMA-INT -->
<p><br>[Function]<br><a class=none name='random-gamma-int'><b>random-gamma-int</b> <i>a</i> => <i>result</i></a>
<blockquote><br>
Random variable with gamma distribution with integer parameter.
</blockquote>
<!-- End of entry for RANDOM-GAMMA-INT -->
<!-- Entry for RANDOM-GAMMA-MT -->
<p><br>[Function]<br><a class=none name='random-gamma-mt'><b>random-gamma-mt</b> <i>a b</i> => <i>result</i></a>
<blockquote><br>
New version based on Marsaglia and Tsang, 'A Simple Method for
generating gamma variables', ACM Transactions on Mathematical
Software, Vol 26, No 3 (2000), p363-372.
</blockquote>
<!-- End of entry for RANDOM-GAMMA-MT -->
<!-- Entry for RANDOM-GAMMA1 -->
<p><br>[Function]<br><a class=none name='random-gamma1'><b>random-gamma1</b> <i>a b</i> => <i>result</i></a>
<blockquote><br>
The Gamma distribution of order a>0 is defined by:
p(x) dx = {1 / Gamma(a) b^a } x^{a-1} e^{-x/b} dx
for x>0. If X and Y are independent gamma-distributed random
variables of order a1 and a2 with the same scale parameter b, then
X+Y has gamma distribution of order a1+a2.
The algorithms below are from Knuth, vol 2, 2nd ed, p. 129.
Works only if a > 1, and is most efficient if a is large
This algorithm, reported in Knuth, is attributed to Ahrens. A
faster one, we are told, can be found in: J. H. Ahrens and
U. Dieter, Computing 12 (1974) 223-246.
</blockquote>
<!-- End of entry for RANDOM-GAMMA1 -->
<!-- Entry for RANDOM-MULTINOMIAL -->
<p><br>[Function]<br><a class=none name='random-multinomial'><b>random-multinomial</b> <i>nn p</i> => <i>result</i></a>
<blockquote><br>
The multinomial distribution has the form
N! n_1 n_2 n_K
prob(n_1, n_2, ... n_K) = -------------------- p_1 p_2 ... p_K
(n_1! n_2! ... n_K!)
where n_1, n_2, ... n_K are nonnegative integers, sum_{k=1,K} n_k = N,
and p = (p_1, p_2, ..., p_K) is a probability distribution.
Random variates are generated using the conditional binomial method.
This scales well with N and does not require a setup step.
Ref:
C.S. David, The computer generation of multinomial random variates,
Comp. Stat. Data Anal. 16 (1993) 205-217
</blockquote>
<!-- End of entry for RANDOM-MULTINOMIAL -->
<!-- Entry for RANDOM-NEGATIVE-BINOMIAL -->
<p><br>[Function]<br><a class=none name='random-negative-binomial'><b>random-negative-binomial</b> <i>p n</i> => <i>result</i></a>
<blockquote><br>
The negative binomial distribution has the form,
prob(k) = Gamma(n + k)/(Gamma(n) Gamma(k + 1)) p^n (1-p)^k
for k = 0, 1, ... . Note that n does not have to be an integer.
This is the Leger's algorithm (given in the answers in Knuth)
</blockquote>
<!-- End of entry for RANDOM-NEGATIVE-BINOMIAL -->
<!-- Entry for RANDOM-NORMAL -->
<p><br>[Function]<br><a class=none name='random-normal'><b>random-normal</b> <i><tt>&optional</tt> mean sigma</i> => <i>result</i></a>
<blockquote><br>
[sintax suggar] Generate random variable with normal distribution using ziggurat method
</blockquote>
<!-- End of entry for RANDOM-NORMAL -->
<!-- Entry for RANDOM-NORMAL-ZIGGURAT -->
<p><br>[Function]<br><a class=none name='random-normal-ziggurat'><b>random-normal-ziggurat</b> <i>mean sigma</i> => <i>result</i></a>
<blockquote><br>
This routine is based on the following article, with a couple of
modifications which simplify the implementation.
George Marsaglia, Wai Wan Tsang
The Ziggurat Method for Generating Random Variables
Journal of Statistical Software, vol. 5 (2000), no. 8
http://www.jstatsoft.org/v05/i08/
The modifications are:
1) use 128 steps instead of 256 to decrease the amount of static
data necessary.
2) use an acceptance sampling from an exponential wedge
exp(-R*(x-R/2)) for the tail of the base strip to simplify the
implementation. The area of exponential wedge is used in
calculating 'v' and the coefficients in ziggurat table, so the
coefficients differ slightly from those in the Marsaglia and Tsang
paper.
See also Leong et al, 'A Comment on the Implementation of the
Ziggurat Method', Journal of Statistical Software, vol 5 (2005), no 7.
</blockquote>
<!-- End of entry for RANDOM-NORMAL-ZIGGURAT -->
<!-- Entry for RANDOM-PARETO -->
<p><br>[Function]<br><a class=none name='random-pareto'><b>random-pareto</b> <i></i> => <i>result</i></a>
<blockquote><br>
Random value for parato distribution:
p(x) dx = (a/b) / (x/b)^(a+1) dx for x >= b
</blockquote>
<!-- End of entry for RANDOM-PARETO -->
<!-- Entry for RANDOM-POISSON -->
<p><br>[Function]<br><a class=none name='random-poisson'><b>random-poisson</b> <i>mu</i> => <i>result</i></a>
<blockquote><br>
The poisson distribution has the form
p(n) = (mu^n / n!) exp(-mu)
for n = 0, 1, 2, ... . The method used here is the one from Knuth.
</blockquote>
<!-- End of entry for RANDOM-POISSON -->
<!-- Entry for RANDOM-UNIFORM -->
<p><br>[Macro]<br><a class=none name='random-uniform'><b>random-uniform</b> <i></i> => <i>result</i></a>
<blockquote><br>
[syntax suggar] Random variable with uniform distribution in interval [0,1]
</blockquote>
<!-- End of entry for RANDOM-UNIFORM -->
<br> <br><h3><a class=none name="ack">Acknowledgements</a></h3>
<p>
This documentation was prepared with <a href="http://weitz.de/documentation-template/">DOCUMENTATION-TEMPLATE</a>.
</p>
<p>
$Header: /usr/local/cvsrep/documentation-template/output.lisp,v 1.13 2007/04/17 19:29:52 edi Exp $
<p><a href="http://weitz.de/index.html">BACK TO MY HOMEPAGE</a>
</body>
</html>