]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/geornd.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / geornd.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} {} geornd (@var{p})
22 ## @deftypefnx {Function File} {} geornd (@var{p}, @var{r})
23 ## @deftypefnx {Function File} {} geornd (@var{p}, @var{r}, @var{c}, @dots{})
24 ## @deftypefnx {Function File} {} geornd (@var{p}, [@var{sz}])
25 ## Return a matrix of random samples from the geometric distribution with
26 ## parameter @var{p}.
27 ##
28 ## When called with a single size argument, return a square matrix with
29 ## the dimension specified.  When called with more than one scalar argument the
30 ## first two arguments are taken as the number of rows and columns and any
31 ## further arguments specify additional matrix dimensions.  The size may also
32 ## be specified with a vector of dimensions @var{sz}.
33 ## 
34 ## If no size arguments are given then the result matrix is the size of
35 ## @var{p}.
36 ## @end deftypefn
37
38 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
39 ## Description: Random deviates from the geometric distribution
40
41 function rnd = geornd (p, varargin)
42
43   if (nargin < 1)
44     print_usage ();
45   endif
46
47   if (nargin == 1)
48     sz = size (p);
49   elseif (nargin == 2)
50     if (isscalar (varargin{1}) && varargin{1} >= 0)
51       sz = [varargin{1}, varargin{1}];
52     elseif (isrow (varargin{1}) && all (varargin{1} >= 0))
53       sz = varargin{1};
54     else
55       error ("geornd: dimension vector must be row vector of non-negative integers");
56     endif
57   elseif (nargin > 2)
58     if (any (cellfun (@(x) (!isscalar (x) || x < 0), varargin)))
59       error ("geornd: dimensions must be non-negative integers");
60     endif
61     sz = [varargin{:}];
62   endif
63
64   if (!isscalar (p) && !isequal (size (p), sz))
65     error ("geornd: P must be scalar or of size SZ");
66   endif
67
68   if (iscomplex (p))
69     error ("geornd: P must not be complex");
70   endif
71
72   if (isa (p, "single"))
73     cls = "single";
74   else
75     cls = "double";
76   endif
77
78   if (isscalar (p))
79     if (p > 0 && p < 1);
80       rnd = floor (- rande (sz) ./ log (1 - p));
81     elseif (p == 0)
82       rnd = Inf (sz, cls);
83     elseif (p == 1)
84       rnd = zeros (sz, cls);
85     elseif (p < 0 || p > 1)
86       rnd = NaN (sz, cls);
87     endif
88   else
89     rnd = floor (- rande (sz) ./ log (1 - p));
90
91     k = !(p >= 0) | !(p <= 1);
92   rnd(k) = NaN;
93
94     k = (p == 0);
95     rnd(k) = Inf;
96   endif
97
98 endfunction
99
100
101 %!assert(size (geornd (0.5)), [1, 1]);
102 %!assert(size (geornd (0.5*ones(2,1))), [2, 1]);
103 %!assert(size (geornd (0.5*ones(2,2))), [2, 2]);
104 %!assert(size (geornd (0.5, 3)), [3, 3]);
105 %!assert(size (geornd (0.5, [4 1])), [4, 1]);
106 %!assert(size (geornd (0.5, 4, 1)), [4, 1]);
107
108 %% Test class of input preserved
109 %!assert(class (geornd (0.5)), "double");
110 %!assert(class (geornd (single(0.5))), "single");
111 %!assert(class (geornd (single([0.5 0.5]))), "single");
112 %!assert(class (geornd (single(0))), "single");
113 %!assert(class (geornd (single(1))), "single");
114
115 %% Test input validation
116 %!error geornd ()
117 %!error geornd (ones(3),ones(2))
118 %!error geornd (ones(2),ones(3))
119 %!error geornd (i)
120 %!error geornd (1, -1)
121 %!error geornd (1, ones(2))
122 %!error geornd (1, [2 -1 2])
123 %!error geornd (ones(2,2), 2, 3)
124 %!error geornd (ones(2,2), 3, 2)
125