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