Mercurial > octave-antonio
annotate scripts/statistics/distributions/binornd.m @ 11587:c792872f8942
all script files: untabify and strip trailing whitespace
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 17:35:29 -0500 |
parents | fd0a3ac60b0e |
children | 19b9f17d22af |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1995-2011 Kurt Hornik |
5410 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
5410 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
5410 | 18 |
19 ## -*- texinfo -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {} binornd (@var{n}, @var{p}, @var{r}, @var{c}) |
5411 | 21 ## @deftypefnx {Function File} {} binornd (@var{n}, @var{p}, @var{sz}) |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
22 ## Return an @var{r} by @var{c} or a @code{size (@var{sz})} matrix of |
5410 | 23 ## random samples from the binomial distribution with parameters @var{n} |
24 ## and @var{p}. Both @var{n} and @var{p} must be scalar or of size | |
25 ## @var{r} by @var{c}. | |
26 ## | |
27 ## If @var{r} and @var{c} are omitted, the size of the result matrix is | |
28 ## the common size of @var{n} and @var{p}. | |
29 ## @end deftypefn | |
30 | |
5428 | 31 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410 | 32 ## Description: Random deviates from the binomial distribution |
33 | |
5411 | 34 function rnd = binornd (n, p, r, c) |
5410 | 35 |
36 if (nargin > 1) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
37 if (!isscalar(n) || !isscalar(p)) |
5410 | 38 [retval, n, p] = common_size (n, p); |
39 if (retval > 0) | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
40 error ("binornd: N and P must be of common size or scalar"); |
5410 | 41 endif |
42 endif | |
43 endif | |
44 | |
45 if (nargin == 4) | |
46 if (! (isscalar (r) && (r > 0) && (r == round (r)))) | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
47 error ("binornd: R must be a positive integer"); |
5410 | 48 endif |
49 if (! (isscalar (c) && (c > 0) && (c == round (c)))) | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
50 error ("binornd: C must be a positive integer"); |
5410 | 51 endif |
52 sz = [r, c]; | |
53 | |
54 if (any (size (n) != 1) | |
10549 | 55 && (length (size (n)) != length (sz) || any (size (n) != sz))) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
56 error ("binornd: N and must be scalar or of size [R, C]"); |
5410 | 57 endif |
58 elseif (nargin == 3) | |
59 if (isscalar (r) && (r > 0)) | |
60 sz = [r, r]; | |
61 elseif (isvector(r) && all (r > 0)) | |
62 sz = r(:)'; | |
63 else | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
64 error ("binornd: R must be a positive integer or vector"); |
5410 | 65 endif |
66 | |
67 if (any (size (n) != 1) | |
10549 | 68 && (length (size (n)) != length (sz) || any (size (n) != sz))) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
10793
diff
changeset
|
69 error ("binornd: N and must be scalar or of size SZ"); |
5410 | 70 endif |
71 elseif (nargin == 2) | |
72 sz = size(n); | |
73 else | |
6046 | 74 print_usage (); |
5410 | 75 endif |
76 | |
77 if (isscalar (n) && isscalar (p)) | |
8086
83646120b54c
Handle zero values of n in binornd correctly
Mark van Rossum <mvanross@inf.ed.ac.uk>
parents:
7017
diff
changeset
|
78 if (find (!(n >= 0) | !(n < Inf) | !(n == round (n)) | |
5410 | 79 !(p >= 0) | !(p <= 1))) |
10525
3306cfcb856e
Replace constructs like "NaN * one()" with "NaN()" and "Inf * ones ()" with "Inf()"
David Bateman <dbateman@free.fr>
parents:
8920
diff
changeset
|
80 rnd = NaN (sz); |
8086
83646120b54c
Handle zero values of n in binornd correctly
Mark van Rossum <mvanross@inf.ed.ac.uk>
parents:
7017
diff
changeset
|
81 elseif (n == 0) |
83646120b54c
Handle zero values of n in binornd correctly
Mark van Rossum <mvanross@inf.ed.ac.uk>
parents:
7017
diff
changeset
|
82 rnd = zeros (sz); |
5410 | 83 else |
84 nel = prod (sz); | |
85 tmp = rand (n, nel); | |
86 rnd = sum(tmp < ones (n, nel) * p, 1); | |
87 rnd = reshape(rnd, sz); | |
88 endif | |
89 else | |
90 rnd = zeros (sz); | |
91 | |
8086
83646120b54c
Handle zero values of n in binornd correctly
Mark van Rossum <mvanross@inf.ed.ac.uk>
parents:
7017
diff
changeset
|
92 k = find (!(n >= 0) | !(n < Inf) | !(n == round (n)) | |
5410 | 93 !(p >= 0) | !(p <= 1)); |
94 if (any (k)) | |
95 rnd(k) = NaN; | |
96 endif | |
97 | |
98 k = find ((n > 0) & (n < Inf) & (n == round (n)) & (p >= 0) & (p <= 1)); | |
99 if (any (k)) | |
100 N = max (n(k)); | |
101 L = length (k); | |
102 tmp = rand (N, L); | |
103 ind = (1 : N)' * ones (1, L); | |
104 rnd(k) = sum ((tmp < ones (N, 1) * p(k)(:)') & | |
105 (ind <= ones (N, 1) * n(k)(:)'),1); | |
106 endif | |
107 endif | |
108 | |
109 endfunction | |
8086
83646120b54c
Handle zero values of n in binornd correctly
Mark van Rossum <mvanross@inf.ed.ac.uk>
parents:
7017
diff
changeset
|
110 |
83646120b54c
Handle zero values of n in binornd correctly
Mark van Rossum <mvanross@inf.ed.ac.uk>
parents:
7017
diff
changeset
|
111 %!assert (binornd(0, 0, 1), 0) |
83646120b54c
Handle zero values of n in binornd correctly
Mark van Rossum <mvanross@inf.ed.ac.uk>
parents:
7017
diff
changeset
|
112 %!assert (binornd([0, 0], [0, 0], 1, 2), [0, 0]) |