root/trunk/random/multinomial.lisp

Revision 34, 3.2 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;; Multinomial distribution
2;; Liam Healy, Sat Nov 25 2006 - 16:00
3;; Time-stamp: <2008-03-09 19:29:15EDT multinomial.lisp>
4;; $Id$
5
6(in-package :gsl)
7
8(defmfun multinomial (generator sum p n)
9  "gsl_ran_multinomial"
10  (((generator generator) :pointer)
11   ((dim0 p) size)
12   (sum size)
13   ((gsl-array p) :pointer)
14   ;; technically, n should be a uint array, but integers work
15   ((gsl-array n) :pointer))
16  :c-return :void
17  :documentation                        ; FDL
18  "Returns an array n of K random variates from a
19   multinomial distribution.  The sum of the array n is specified
20   by sum=N.  The distribution function is
21   P(n_1, n_2, ..., n_K) =
22   (N!/(n_1! n_2! ... n_K!)) p_1^n_1 p_2^n_2 ... p_K^n_K
23   where (n_1, n_2, ..., n_K) are nonnegative integers with
24   sum_{k=1}^K n_k = N, and (p_1, p_2, ..., p_K)
25   is a probability distribution with \sum p_i = 1. 
26   If the array p[K] is not normalized then its entries will be
27   treated as weights and normalized appropriately.
28   Random variates are generated using the conditional binomial method (see
29   C.S. David, \"The computer generation of multinomial random
30   variates,\" Comp. Stat. Data Anal. 16 (1993) 205--217 for details).")
31
32(defmfun multinomial-pdf (p n)
33  "gsl_ran_multinomial_pdf"
34  (((dim0 p) :uint) ((gsl-array p) :pointer) ((gsl-array n) :pointer))
35  :c-return :double
36  :documentation                        ; FDL
37  "Compute the probability P(n_1, n_2, ..., n_K)
38   of sampling n[K] from a multinomial distribution
39   with parameters p[K], using the formula given for #'multinomial.")
40
41(defmfun multinomial-log-pdf (p n)
42  "gsl_ran_multinomial_lnpdf"
43  (((dim0 p) :uint) ((gsl-array p) :pointer) ((gsl-array n) :pointer))
44  :c-return :double
45  :documentation                        ; FDL
46  "Compute the natural logarithm of the probability P(n_1, n_2, ..., n_K)
47   of sampling n[K] from a multinomial distribution
48   with parameters p[K], using the formula given for #'multinomial.")
49
50;;; Examples and unit test
51#|
52(make-tests multinomial
53  (letm ((rng (random-number-generator *mt19937* 0))
54          (p (vector-double-float #(0.1d0 0.2d0 0.3d0 0.4d0)))
55          (n (vector-fixnum 4)))
56     (multinomial rng 8 p n)
57     (data n))
58  (letm ((p (vector-double-float #(0.1d0 0.2d0 0.3d0 0.4d0)))
59          (n (vector-fixnum 4)))
60     (setf (data n) #(5 0 1 2))
61     (multinomial-pdf p N))
62  (letm ((p (vector-double-float #(0.1d0 0.2d0 0.3d0 0.4d0)))
63          (n (vector-fixnum 4)))
64     (setf (data n) #(5 0 1 2))
65     (multinomial-log-pdf p n)))
66|#
67
68(LISP-UNIT:DEFINE-TEST MULTINOMIAL
69  (LISP-UNIT::ASSERT-NUMERICAL-EQUAL
70   (LIST #(5 0 1 2))
71   (MULTIPLE-VALUE-LIST
72    (LETM ((RNG (RANDOM-NUMBER-GENERATOR *MT19937* 0))
73         (P (VECTOR-DOUBLE-FLOAT #(0.1d0 0.2d0 0.3d0 0.4d0)))
74         (N (VECTOR-FIXNUM 4)))
75      (MULTINOMIAL RNG 8 P N)
76      (DATA N))))
77  (LISP-UNIT::ASSERT-NUMERICAL-EQUAL
78   (LIST 8.064000000000026d-5)
79   (MULTIPLE-VALUE-LIST
80    (LETM
81        ((P (VECTOR-DOUBLE-FLOAT #(0.1d0 0.2d0 0.3d0 0.4d0)))
82         (N (VECTOR-FIXNUM 4)))
83      (SETF (DATA N) #(5 0 1 2)) (MULTINOMIAL-PDF P N))))
84  (LISP-UNIT::ASSERT-NUMERICAL-EQUAL
85   (LIST -9.425515753641212d0)
86   (MULTIPLE-VALUE-LIST
87    (LETM
88        ((P (VECTOR-DOUBLE-FLOAT #(0.1d0 0.2d0 0.3d0 0.4d0)))
89         (N (VECTOR-FIXNUM 4)))
90      (SETF (DATA N) #(5 0 1 2))
91      (MULTINOMIAL-LOG-PDF P N)))))
92
Note: See TracBrowser for help on using the browser.