]> Creatis software - CreaPhase.git/blob - octave_packages/statistics-1.1.3/raylcdf.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / statistics-1.1.3 / raylcdf.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{p} =} raylcdf (@var{x}, @var{sigma})
18 ## Compute the cumulative distribution function of the Rayleigh
19 ## distribution.
20 ##
21 ## @subheading Arguments
22 ##
23 ## @itemize @bullet
24 ## @item
25 ## @var{x} is the support. The elements of @var{x} must be non-negative.
26 ##
27 ## @item
28 ## @var{sigma} is the parameter of the Rayleigh distribution. The elements
29 ## of @var{sigma} must be positive.
30 ## @end itemize
31 ## @var{x} and @var{sigma} must be of common size or one of them must be
32 ## scalar.
33 ##
34 ## @subheading Return values
35 ##
36 ## @itemize @bullet
37 ## @item
38 ## @var{p} is the cumulative distribution of the Rayleigh distribution at
39 ## each element of @var{x} and corresponding parameter @var{sigma}.
40 ## @end itemize
41 ##
42 ## @subheading Examples
43 ##
44 ## @example
45 ## @group
46 ## x = 0:0.5:2.5;
47 ## sigma = 1:6;
48 ## p = raylcdf (x, sigma)
49 ## @end group
50 ##
51 ## @group
52 ## p = raylcdf (x, 0.5)
53 ## @end group
54 ## @end example
55 ##
56 ## @subheading References
57 ##
58 ## @enumerate
59 ## @item
60 ## Wendy L. Martinez and Angel R. Martinez. @cite{Computational Statistics
61 ## Handbook with MATLAB}. Appendix E, pages 547-557, Chapman & Hall/CRC,
62 ## 2001.
63 ##
64 ## @item
65 ## Athanasios Papoulis. @cite{Probability, Random Variables, and Stochastic
66 ## Processes}. pages 104 and 148, McGraw-Hill, New York, second edition,
67 ## 1984.
68 ## @end enumerate
69 ## @end deftypefn
70
71 ## Author: Arno Onken <asnelt@asnelt.org>
72 ## Description: CDF of the Rayleigh distribution
73
74 function p = raylcdf (x, sigma)
75
76   # Check arguments
77   if (nargin != 2)
78     print_usage ();
79   endif
80
81   if (! isempty (x) && ! ismatrix (x))
82     error ("raylcdf: x must be a numeric matrix");
83   endif
84   if (! isempty (sigma) && ! ismatrix (sigma))
85     error ("raylcdf: sigma must be a numeric matrix");
86   endif
87
88   if (! isscalar (x) || ! isscalar (sigma))
89     [retval, x, sigma] = common_size (x, sigma);
90     if (retval > 0)
91       error ("raylcdf: x and sigma must be of common size or scalar");
92     endif
93   endif
94
95   # Calculate cdf
96   p = 1 - exp ((-x .^ 2) ./ (2 * sigma .^ 2));
97
98   # Continue argument check
99   k = find (! (x >= 0) | ! (x < Inf) | ! (sigma > 0));
100   if (any (k))
101     p(k) = NaN;
102   endif
103
104 endfunction
105
106 %!test
107 %! x = 0:0.5:2.5;
108 %! sigma = 1:6;
109 %! p = raylcdf (x, sigma);
110 %! expected_p = [0.0000, 0.0308, 0.0540, 0.0679, 0.0769, 0.0831];
111 %! assert (p, expected_p, 0.001);
112
113 %!test
114 %! x = 0:0.5:2.5;
115 %! p = raylcdf (x, 0.5);
116 %! expected_p = [0.0000, 0.3935, 0.8647, 0.9889, 0.9997, 1.0000];
117 %! assert (p, expected_p, 0.001);