]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/binornd.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / binornd.m
1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1995-2012 Kurt Hornik
3 ##
4 ## This file is part of Octave.
5 ##
6 ## Octave is free software; you can redistribute it and/or modify it
7 ## under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 3 of the License, or (at
9 ## your option) any later version.
10 ##
11 ## Octave is distributed in the hope that it will be useful, but
12 ## WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 ## General Public License for more details.
15 ##
16 ## You should have received a copy of the GNU General Public License
17 ## along with Octave; see the file COPYING.  If not, see
18 ## <http://www.gnu.org/licenses/>.
19
20 ## -*- texinfo -*-
21 ## @deftypefn  {Function File} {} binornd (@var{n}, @var{p})
22 ## @deftypefnx {Function File} {} binornd (@var{n}, @var{p}, @var{r})
23 ## @deftypefnx {Function File} {} binornd (@var{n}, @var{p}, @var{r}, @var{c}, @dots{})
24 ## @deftypefnx {Function File} {} binornd (@var{n}, @var{p}, [@var{sz}])
25 ## Return a matrix of random samples from the binomial distribution with
26 ## parameters @var{n} and @var{p}, where @var{n} is the number of trials
27 ## and @var{p} is the probability of success.
28 ##
29 ## When called with a single size argument, return a square matrix with
30 ## the dimension specified.  When called with more than one scalar argument the
31 ## first two arguments are taken as the number of rows and columns and any
32 ## further arguments specify additional matrix dimensions.  The size may also
33 ## be specified with a vector of dimensions @var{sz}.
34 ## 
35 ## If no size arguments are given then the result matrix is the common size of
36 ## @var{n} and @var{p}.
37 ## @end deftypefn
38
39 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
40 ## Description: Random deviates from the binomial distribution
41
42 function rnd = binornd (n, p, varargin)
43
44   if (nargin < 2)
45     print_usage ();
46   endif
47
48   if (!isscalar (n) || !isscalar (p))
49     [retval, n, p] = common_size (n, p);
50     if (retval > 0)
51       error ("binornd: N and P must be of common size or scalars");
52     endif
53   endif
54
55   if (iscomplex (n) || iscomplex (p))
56     error ("binornd: N and P must not be complex");
57   endif
58
59   if (nargin == 2)
60     sz = size (n);
61   elseif (nargin == 3)
62     if (isscalar (varargin{1}) && varargin{1} >= 0)
63       sz = [varargin{1}, varargin{1}];
64     elseif (isrow (varargin{1}) && all (varargin{1} >= 0))
65       sz = varargin{1};
66     else
67       error ("binornd: dimension vector must be row vector of non-negative integers");
68     endif
69   elseif (nargin > 3)
70     if (any (cellfun (@(x) (!isscalar (x) || x < 0), varargin)))
71       error ("binornd: dimensions must be non-negative integers");
72     endif
73     sz = [varargin{:}];
74   endif
75
76   if (!isscalar (n) && !isequal (size (n), sz))
77     error ("binornd: N and P must be scalar or of size SZ");
78   endif
79
80   if (isa (n, "single") || isa (p, "single"))
81     cls = "single";
82   else
83     cls = "double";
84   endif
85
86   if (isscalar (n) && isscalar (p))
87     if ((n > 0) && (n < Inf) && (n == fix (n)) && (p >= 0) && (p <= 1))
88       nel = prod (sz);
89       tmp = rand (n, nel);
90       rnd = sum (tmp < p, 1);
91       rnd = reshape (rnd, sz);
92       if (strcmp (cls, "single"))
93         rnd = single (rnd);
94       endif
95     elseif ((n == 0) && (p >= 0) && (p <= 1))
96       rnd = zeros (sz, cls);
97     else
98       rnd = NaN (sz, cls);
99     endif
100   else
101     rnd = zeros (sz, cls);
102
103     k = !(n >= 0) | !(n < Inf) | !(n == fix (n)) | !(p >= 0) | !(p <= 1);
104     rnd(k) = NaN;
105
106     k = (n > 0) & (n < Inf) & (n == fix (n)) & (p >= 0) & (p <= 1);
107     if (any (k(:)))
108       N = max (n(k));
109       L = sum (k(:));
110       tmp = rand (N, L);
111       ind = repmat ((1 : N)', 1, L);
112       rnd(k) = sum ((tmp < repmat (p(k)(:)', N, 1)) &
113                     (ind <= repmat (n(k)(:)', N, 1)), 1);
114     endif
115   endif
116
117 endfunction
118
119
120 %!assert (binornd (0, 0, 1), 0)
121 %!assert (binornd ([0, 0], [0, 0], 1, 2), [0, 0])
122
123 %!assert(size (binornd (2, 1/2)), [1, 1]);
124 %!assert(size (binornd (2*ones(2,1), 1/2)), [2, 1]);
125 %!assert(size (binornd (2*ones(2,2), 1/2)), [2, 2]);
126 %!assert(size (binornd (2, 1/2*ones(2,1))), [2, 1]);
127 %!assert(size (binornd (2, 1/2*ones(2,2))), [2, 2]);
128 %!assert(size (binornd (2, 1/2, 3)), [3, 3]);
129 %!assert(size (binornd (2, 1/2, [4 1])), [4, 1]);
130 %!assert(size (binornd (2, 1/2, 4, 1)), [4, 1]);
131
132 %% Test class of input preserved
133 %!assert(class (binornd (2, 0.5)), "double");
134 %!assert(class (binornd (single(2), 0.5)), "single");
135 %!assert(class (binornd (single([2 2]), 0.5)), "single");
136 %!assert(class (binornd (2, single(0.5))), "single");
137 %!assert(class (binornd (2, single([0.5 0.5]))), "single");
138
139 %% Test input validation
140 %!error binornd ()
141 %!error binornd (1)
142 %!error binornd (ones(3),ones(2))
143 %!error binornd (ones(2),ones(3))
144 %!error binornd (i, 2)
145 %!error binornd (2, i)
146 %!error binornd (1,2, -1)
147 %!error binornd (1,2, ones(2))
148 %!error binornd (1,2, [2 -1 2])
149 %!error binornd (1,2, 1, ones(2))
150 %!error binornd (1,2, 1, -1)
151 %!error binornd (ones(2,2), 2, 3)
152 %!error binornd (ones(2,2), 2, [3, 2])
153 %!error binornd (ones(2,2), 2, 2, 3)
154