]> Creatis software - CreaPhase.git/blob - octave_packages/statistics-1.1.3/raylrnd.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / statistics-1.1.3 / raylrnd.m
1 ## Copyright (C) 2006, 2007 Arno Onken <asnelt@asnelt.org>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {@var{x} =} raylrnd (@var{sigma})
18 ## @deftypefnx {Function File} {@var{x} =} raylrnd (@var{sigma}, @var{sz})
19 ## @deftypefnx {Function File} {@var{x} =} raylrnd (@var{sigma}, @var{r}, @var{c})
20 ## Generate a matrix of random samples from the Rayleigh distribution.
21 ##
22 ## @subheading Arguments
23 ##
24 ## @itemize @bullet
25 ## @item
26 ## @var{sigma} is the parameter of the Rayleigh distribution. The elements
27 ## of @var{sigma} must be positive.
28 ##
29 ## @item
30 ## @var{sz} is the size of the matrix to be generated. @var{sz} must be a
31 ## vector of non-negative integers.
32 ##
33 ## @item
34 ## @var{r} is the number of rows of the matrix to be generated. @var{r} must
35 ## be a non-negative integer.
36 ##
37 ## @item
38 ## @var{c} is the number of columns of the matrix to be generated. @var{c}
39 ## must be a non-negative integer.
40 ## @end itemize
41 ##
42 ## @subheading Return values
43 ##
44 ## @itemize @bullet
45 ## @item
46 ## @var{x} is a matrix of random samples from the Rayleigh distribution with
47 ## corresponding parameter @var{sigma}. If neither @var{sz} nor @var{r} and
48 ## @var{c} are specified, then @var{x} is of the same size as @var{sigma}.
49 ## @end itemize
50 ##
51 ## @subheading Examples
52 ##
53 ## @example
54 ## @group
55 ## sigma = 1:6;
56 ## x = raylrnd (sigma)
57 ## @end group
58 ##
59 ## @group
60 ## sz = [2, 3];
61 ## x = raylrnd (0.5, sz)
62 ## @end group
63 ##
64 ## @group
65 ## r = 2;
66 ## c = 3;
67 ## x = raylrnd (0.5, r, c)
68 ## @end group
69 ## @end example
70 ##
71 ## @subheading References
72 ##
73 ## @enumerate
74 ## @item
75 ## Wendy L. Martinez and Angel R. Martinez. @cite{Computational Statistics
76 ## Handbook with MATLAB}. Appendix E, pages 547-557, Chapman & Hall/CRC,
77 ## 2001.
78 ##
79 ## @item
80 ## Athanasios Papoulis. @cite{Probability, Random Variables, and Stochastic
81 ## Processes}. pages 104 and 148, McGraw-Hill, New York, second edition,
82 ## 1984.
83 ## @end enumerate
84 ## @end deftypefn
85
86 ## Author: Arno Onken <asnelt@asnelt.org>
87 ## Description: Random samples from the Rayleigh distribution
88
89 function x = raylrnd (sigma, r, c)
90
91   # Check arguments
92   if (nargin == 1)
93     sz = size (sigma);
94   elseif (nargin == 2)
95     if (! isvector (r) || any ((r < 0) | round (r) != r))
96       error ("raylrnd: sz must be a vector of non-negative integers")
97     endif
98     sz = r(:)';
99     if (! isscalar (sigma) && ! isempty (sigma) && (length (size (sigma)) != length (sz) || any (size (sigma) != sz)))
100       error ("raylrnd: sigma must be scalar or of size sz");
101     endif
102   elseif (nargin == 3)
103     if (! isscalar (r) || any ((r < 0) | round (r) != r))
104       error ("raylrnd: r must be a non-negative integer")
105     endif
106     if (! isscalar (c) || any ((c < 0) | round (c) != c))
107       error ("raylrnd: c must be a non-negative integer")
108     endif
109     sz = [r, c];
110     if (! isscalar (sigma) && ! isempty (sigma) && (length (size (sigma)) != length (sz) || any (size (sigma) != sz)))
111       error ("raylrnd: sigma must be scalar or of size [r, c]");
112     endif
113   else
114     print_usage ();
115   endif
116
117   if (! isempty (sigma) && ! ismatrix (sigma))
118     error ("raylrnd: sigma must be a numeric matrix");
119   endif
120
121   if (isempty (sigma))
122     x = [];
123   elseif (isscalar (sigma) && ! (sigma > 0))
124     x = NaN .* ones (sz); 
125   else
126     # Draw random samples
127     x = sqrt (-2 .* log (1 - rand (sz)) .* sigma .^ 2);
128
129     # Continue argument check
130     k = find (! (sigma > 0));
131     if (any (k))
132       x(k) = NaN;
133     endif
134   endif
135
136 endfunction
137
138 %!test
139 %! sigma = 1:6;
140 %! x = raylrnd (sigma);
141 %! assert (size (x), size (sigma));
142 %! assert (all (x >= 0));
143
144 %!test
145 %! sigma = 0.5;
146 %! sz = [2, 3];
147 %! x = raylrnd (sigma, sz);
148 %! assert (size (x), sz);
149 %! assert (all (x >= 0));
150
151 %!test
152 %! sigma = 0.5;
153 %! r = 2;
154 %! c = 3;
155 %! x = raylrnd (sigma, r, c);
156 %! assert (size (x), [r, c]);
157 %! assert (all (x >= 0));