5410
|
1 ## Copyright (C) 1997 Kurt Hornik |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
5691
|
21 ## @deftypefn {Function File} {} hygernd (@var{m}, @var{t}, @var{n}, @var{r}, @var{c}) |
5411
|
22 ## @deftypefnx {Function File} {} hygernd (@var{m}, @var{t}, @var{n}, @var{sz}) |
5691
|
23 ## Return an @var{r} by @var{c} matrix of random samples from the |
|
24 ## hypergeometric distribution with parameters @var{m}, @var{t}, |
5410
|
25 ## and @var{n}. |
|
26 ## |
|
27 ## The parameters @var{m}, @var{t}, and @var{n} must positive integers |
|
28 ## with @var{m} and @var{n} not greater than @var{t}. |
|
29 ## @end deftypefn |
|
30 |
5411
|
31 ## function rnd = hygernd (N, m, t, n) |
|
32 function rnd = hygernd (m, t, n, r, c) |
5410
|
33 |
|
34 if (nargin == 5) |
|
35 if (! (isscalar (r) && (r > 0) && (r == round (r)))) |
5411
|
36 error ("hygernd: r must be a positive integer"); |
5410
|
37 endif |
|
38 if (! (isscalar (c) && (c > 0) && (c == round (c)))) |
5411
|
39 error ("hygernd: c must be a positive integer"); |
5410
|
40 endif |
|
41 sz = [r, c]; |
|
42 elseif (nargin == 4) |
|
43 ## A potential problem happens here if all args are scalar, as |
|
44 ## we can distiguish between the command syntax. This is quite |
|
45 ## ambigous! I assume that if the last arg is a vector then |
|
46 ## then third form is assumed. This means that you can't define |
|
47 ## and r-by-r matrix with a single scalar! |
|
48 |
|
49 if (isscalar (r)) |
|
50 sz = [1, floor(m)]; |
|
51 m = t; |
|
52 t = n; |
|
53 n = r; |
|
54 elseif (isvector(r) && all (r > 0)) |
|
55 sz = r(:)'; |
|
56 else |
5411
|
57 error ("hygernd: r must be a vector of positive integers"); |
5410
|
58 endif |
|
59 else |
6046
|
60 print_usage (); |
5410
|
61 endif |
|
62 |
|
63 if (!isscalar (m) || !isscalar (t) || !isscalar (n)) |
5411
|
64 error ("hygernd: m, t and n must all be positive integers"); |
5410
|
65 endif |
|
66 |
|
67 if ((m < 0) | (t < 0) | (n <= 0) | (m != round (m)) | |
|
68 (t != round (t)) | (n != round (n)) | (m > t) | (n > t)) |
|
69 rnd = NaN * ones (sz) |
|
70 else |
5411
|
71 rnd = discrete_rnd (0 : n, hygepdf (0 : n, m, t, n), sz); |
5410
|
72 endif |
|
73 |
|
74 endfunction |