]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/discrete_rnd.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / discrete_rnd.m
1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1996-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} {} discrete_rnd (@var{v}, @var{p})
22 ## @deftypefnx {Function File} {} discrete_rnd (@var{v}, @var{p}, @var{r})
23 ## @deftypefnx {Function File} {} discrete_rnd (@var{v}, @var{p}, @var{r}, @var{c}, @dots{})
24 ## @deftypefnx {Function File} {} discrete_rnd (@var{v}, @var{p}, [@var{sz}])
25 ## Return a matrix of random samples from the univariate distribution which
26 ## assumes the values in @var{v} with probabilities @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 common size of
35 ## @var{v} and @var{p}.
36 ## @end deftypefn
37
38 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
39 ## Description: Random deviates from a discrete distribution
40
41 function rnd = discrete_rnd (v, p, varargin)
42
43   if (nargin < 2)
44     print_usage ();
45   endif
46
47   if (! isvector (v))
48     error ("discrete_rnd: V must be a vector");
49   elseif (! isvector (p) || (length (p) != length (v)))
50     error ("discrete_rnd: P must be a vector with length (V) elements");
51   elseif (any (isnan (p)))
52     error ("discrete_rnd: P must not have any NaN elements");
53   elseif (! (all (p >= 0) && any (p)))
54     error ("discrete_rnd: P must be a nonzero, non-negative vector");
55   endif
56
57   if (nargin == 2)
58     sz = size (v);
59   elseif (nargin == 3)
60     if (isscalar (varargin{1}) && varargin{1} >= 0)
61       sz = [varargin{1}, varargin{1}];
62     elseif (isrow (varargin{1}) && all (varargin{1} >= 0))
63       sz = varargin{1};
64     else
65       error ("discrete_rnd: dimension vector must be row vector of non-negative integers");
66     endif
67   elseif (nargin > 3)
68     if (any (cellfun (@(x) (!isscalar (x) || x < 0), varargin)))
69       error ("discrete_rnd: dimensions must be non-negative integers");
70     endif
71     sz = [varargin{:}];
72   endif
73
74   rnd = v(lookup (cumsum (p(1:end-1)) / sum (p), rand (sz)) + 1);
75   rnd = reshape (rnd, sz);
76
77 endfunction
78
79
80 %!assert(size (discrete_rnd (1:2, 1:2, 3)), [3, 3]);
81 %!assert(size (discrete_rnd (1:2, 1:2, [4 1])), [4, 1]);
82 %!assert(size (discrete_rnd (1:2, 1:2, 4, 1)), [4, 1]);
83
84 %% Test class of input preserved
85 %!assert(class (discrete_rnd (1:2, 1:2)), "double");
86 %!assert(class (discrete_rnd (single(1:2), 1:2)), "single");
87 ## FIXME: Maybe this should work, maybe it shouldn't.
88 #%!assert(class (discrete_rnd (1:2, single(1:2))), "single");
89
90 %% Test input validation
91 %!error discrete_rnd ()
92 %!error discrete_rnd (1)
93 %!error discrete_rnd (1:2,1:2, -1)
94 %!error discrete_rnd (1:2,1:2, ones(2))
95 %!error discrete_rnd (1:2,1:2, [2 -1 2])
96 %!error discrete_rnd (1:2,1:2, 1, ones(2))
97 %!error discrete_rnd (1:2,1:2, 1, -1)
98 %% test v,p verification
99 %!error discrete_rnd (1, ones(2), ones(2,1))
100 %!error discrete_rnd (1, ones(2,1), ones(1,1))
101 %!error discrete_rnd (1, ones(2,1), [1 -1])
102 %!error discrete_rnd (1, ones(2,1), [1 NaN])
103 %!error discrete_rnd (1, ones(2,1), [0  0])
104