|
2 | 2 |
|
3 | 3 | (in-package #:cl-random)
|
4 | 4 |
|
5 |
| -;; TODO: Move bernoulli to univeriate, and implement as an rv. Idem for binomial, geometric, negative-binomial, poisson, ... |
6 | 5 |
|
7 |
| -;; (declaim (inline draw-bernoulli)) |
8 |
| -;; (defun draw-bernoulli (p &key (rng *random-state*)) |
9 |
| -;; "Return T with probability p, otherwise NIL. Rationals are handled exactly." |
10 |
| -;; (etypecase p |
11 |
| -;; (integer (ecase p |
12 |
| -;; (0 NIL) |
13 |
| -;; (1 T))) |
14 |
| -;; (rational (let+ (((&accessors-r/o numerator denominator) p)) |
15 |
| -;; (assert (<= numerator denominator)) |
16 |
| -;; (< (next denominator rng) numerator))) |
17 |
| -;; (float (< (next (float 1 p) rng) p)))) |
18 |
| - |
19 |
| -;; (defun draw-bernoulli-bit (p &key (rng *random-state*)) |
20 |
| -;; (if (draw-bernoulli p :rng rng) 1 0)) |
21 |
| - |
22 |
| -;; (declaim (inline draw-binomial)) |
23 |
| -;; (defun draw-binomial (p n &key (rng *random-state*)) |
24 |
| -;; "Return the number of successes out of N bernoulli trials with probability of success P." |
25 |
| -;; (let ((successes 0)) |
26 |
| -;; (dotimes (i n successes) |
27 |
| -;; (when (draw-bernoulli p :rng rng) |
28 |
| -;; (incf successes))))) |
29 |
| - |
30 |
| -;; (declaim (inline draw-geometric)) |
31 |
| -;; (defun draw-geometric (p &key (rng *random-state*)) |
32 |
| -;; "Return the number of Bernoulli trials, with probability of success P, that were needed to reach the first success. This is >= 1." |
33 |
| -;; (do ((trials 1 (1+ trials))) |
34 |
| -;; ((draw-bernoulli p :rng rng) trials))) |
35 |
| - |
36 |
| -;; (declaim (inline draw-poison)) |
37 |
| -;; (defun draw-poisson (lamda &key (rng *random-state*)) |
38 |
| -;; "Return the number of events that occur with probability LAMDA. The algorithm is from Donald E. Knuth (1969). Seminumerical Algorithms. The Art of Computer Programming, Volume 2. Addison Wesley. WARNING: It's simple but only linear in the return value K and is numerically unstable for large LAMDA." |
39 |
| -;; (do ((l (exp (- lamda))) |
40 |
| -;; (k 0 (1+ k)) |
41 |
| -;; (p 1d0 (* p u)) |
42 |
| -;; (u (next 1d0 rng) (next 1d0 rng))) |
43 |
| -;; ((<= p l) k))) |
44 | 6 |
|
45 | 7 | ;; TODO: Add shuffle! and elt (see :alexandria).
|
46 | 8 |
|
|
0 commit comments