root/trunk/random/discrete.lisp

Revision 34, 3.1 kB (checked in by lhealy, 9 months ago)

The classes/types in the different contexts are now gathered together
in one place, in *type-names* for the types and in *data-class-name*
for data classes, populated by #'add-data-class. Both defdata and
defmfun-all use the table and so mapping between various names is
consistent. The data class names are now different, *-double-float
and *-single-float replaces *-double and *-single. The regression
tests give the same results as before.

  • Property svn:keywords set to Id
Line 
1;; Discrete random variables
2;; Liam Healy, Sat Nov 11 2006 - 21:51
3;; Time-stamp: <2008-03-09 19:29:15EDT discrete.lisp>
4;; $Id$
5
6(in-package :gsl)
7
8(cffi:defcstruct discrete-t
9  "Structure for Walker algorithm."
10  (K size)
11  (A :pointer)
12  (F :pointer))
13
14(defgo-s (discrete-random probabilities) discrete-preprocess discrete-free)
15
16(defmfun discrete-preprocess (probabilities)
17  "gsl_ran_discrete_preproc"
18  (((dim0 probabilities) size) ((gsl-array probabilities) :pointer))
19  :c-return :pointer
20  :export nil
21  :index (letm discrete-random)
22  :documentation                        ; FDL
23  "A pointer to a structure that contains the lookup
24  table for the discrete random number generator.  The array probabilities contains
25  the probabilities of the discrete events; these array elements must all be
26  positive, but they needn't add up to one (so you can think of them more
27  generally as ``weights'')---the preprocessor will normalize appropriately.
28  This return value is used as an argument to #'discrete.")
29
30(defmfun discrete-free (table)
31  "gsl_ran_discrete_free" ((table :pointer))
32  :c-return :void
33  :export nil
34  :index (letm discrete-random)
35  :documentation                        ; FDL
36  "De-allocates the lookup table created by #'discrete-preprocess.")
37
38(defmfun discrete (generator table)
39  "gsl_ran_discrete"
40  (((generator generator) :pointer) (table :pointer))
41  :c-return size
42  :documentation                        ; FDL
43  "Generate discrete random numbers after running #'discrete-preprocess;
44   the argument 'table is the value returned by #'discrete-preprocess.")
45
46(defmfun discrete-pdf (k table)
47  "gsl_ran_discrete_pdf"
48  ((k size) (table :pointer))
49  :c-return :double
50  :documentation                        ; FDL
51  "The probability P[k] of observing the variable k.
52   Since P[k] is not stored as part of the lookup table, it must be
53   recomputed; this computation takes O(K), so if K is large
54   and you care about the original array P[k] used to create the
55   lookup table, then you should just keep this original array P[k]
56   around.")
57
58;;; Examples and unit test
59#|
60(make-tests discrete
61   ;; Must have two letms because the vector value is not set until
62   ;; the body, but the discrete-random needs that set value.
63   (letm ((probabilities (vector-double-float #(0.25d0 0.5d0 0.25d0))))
64     (letm ((table (discrete-random probabilities))
65            (rng (random-number-generator *mt19937* 0)))
66       (loop for i from 0 to 10
67             collect
68             (discrete rng table))))
69   (letm ((probabilities (vector-double-float #(0.25d0 0.5d0 0.25d0))))
70      (letm ((table (discrete-random probabilities)))
71        (discrete-pdf 1 table))))
72|#
73
74(LISP-UNIT:DEFINE-TEST DISCRETE
75  (LISP-UNIT::ASSERT-NUMERICAL-EQUAL
76   (LIST (LIST 1 0 1 1 0 1 1 2 1 2 2))
77   (MULTIPLE-VALUE-LIST
78    (LETM ((PROBABILITIES
79            (VECTOR-DOUBLE-FLOAT #(0.25d0 0.5d0 0.25d0))))
80      (LETM ((TABLE (DISCRETE-RANDOM PROBABILITIES))
81             (RNG (RANDOM-NUMBER-GENERATOR *MT19937* 0)))
82        (LOOP FOR I FROM 0 TO 10 COLLECT
83              (DISCRETE RNG TABLE))))))
84  (LISP-UNIT::ASSERT-NUMERICAL-EQUAL
85   (LIST 0.5d0)
86   (MULTIPLE-VALUE-LIST
87    (LETM ((PROBABILITIES (VECTOR-DOUBLE-FLOAT #(0.25d0 0.5d0 0.25d0))))
88      (LETM ((TABLE (DISCRETE-RANDOM PROBABILITIES)))
89        (DISCRETE-PDF 1 TABLE))))))
90
Note: See TracBrowser for help on using the browser.