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