3191
|
1 ## Copyright (C) 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3922
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3191
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3191
|
15 ## You should have received a copy of the GNU General Public License |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3191
|
18 |
3456
|
19 ## -*- texinfo -*- |
|
20 ## @deftypefn {Function File} {} discrete_rnd (@var{n}, @var{v}, @var{p}) |
4859
|
21 ## @deftypefnx {Function File} {} discrete_rnd (@var{v}, @var{p}, @var{r}, @var{c}) |
|
22 ## @deftypefnx {Function File} {} discrete_rnd (@var{v}, @var{p}, @var{sz}) |
3456
|
23 ## Generate a row vector containing a random sample of size @var{n} from |
|
24 ## the univariate distribution which assumes the values in @var{v} with |
4859
|
25 ## probabilities @var{p}. @var{n} must be a scalar. |
3191
|
26 ## |
4859
|
27 ## If @var{r} and @var{c} are given create a matrix with @var{r} rows and |
|
28 ## @var{c} columns. Or if @var{sz} is a vector, create a matrix of size |
|
29 ## @var{sz}. |
3456
|
30 ## @end deftypefn |
3191
|
31 |
5428
|
32 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3456
|
33 ## Description: Random deviates from a discrete distribution |
3191
|
34 |
4859
|
35 function rnd = discrete_rnd (v, p, r, c) |
|
36 |
|
37 if (nargin == 4) |
|
38 if (! (isscalar (r) && (r > 0) && (r == round (r)))) |
|
39 error ("discrete_rnd: r must be a positive integer"); |
|
40 endif |
|
41 if (! (isscalar (c) && (c > 0) && (c == round (c)))) |
|
42 error ("discrete_rnd: c must be a positive integer"); |
|
43 endif |
|
44 sz = [r, c]; |
|
45 elseif (nargin == 3) |
|
46 ## A potential problem happens here if all args are scalar, as |
|
47 ## we can't distiguish between the command syntax. Thankfully this |
|
48 ## case doesn't make much sense. So we assume the first syntax |
|
49 ## if the first arg is scalar |
3426
|
50 |
4859
|
51 if (isscalar (v)) |
|
52 sz = [1, floor(v)]; |
|
53 v = p; |
|
54 p = r; |
|
55 else |
|
56 if (isscalar (r) && (r > 0)) |
|
57 sz = [r, r]; |
|
58 elseif (isvector(r) && all (r > 0)) |
|
59 sz = r(:)'; |
|
60 else |
7007
|
61 error ("discrete_rnd: r must be a positive integer or vector"); |
4859
|
62 endif |
|
63 endif |
|
64 else |
6046
|
65 print_usage (); |
3191
|
66 endif |
|
67 |
4030
|
68 if (! isvector (v)) |
3456
|
69 error ("discrete_rnd: v must be a vector"); |
4030
|
70 elseif (! isvector (p) || (length (p) != length (v))) |
3456
|
71 error ("discrete_rnd: p must be a vector with length (v) elements"); |
|
72 elseif (! (all (p >= 0) && any (p))) |
|
73 error ("discrete_rnd: p must be a nonzero, nonnegative vector"); |
3191
|
74 endif |
|
75 |
6350
|
76 rnd = v (lookup (cumsum (p (1 : end-1)) / sum(p), rand (sz)) + 1); |
3191
|
77 endfunction |