]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/fpdf.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / fpdf.m
1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1995-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} {} fpdf (@var{x}, @var{m}, @var{n})
22 ## For each element of @var{x}, compute the probability density function
23 ## (PDF) at @var{x} of the F distribution with @var{m} and @var{n}
24 ## degrees of freedom.
25 ## @end deftypefn
26
27 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
28 ## Description: PDF of the F distribution
29
30 function pdf = fpdf (x, m, n)
31
32   if (nargin != 3)
33     print_usage ();
34   endif
35
36   if (!isscalar (m) || !isscalar (n))
37     [retval, x, m, n] = common_size (x, m, n);
38     if (retval > 0)
39       error ("fpdf: X, M, and N must be of common size or scalars");
40     endif
41   endif
42
43   if (iscomplex (x) || iscomplex (m) || iscomplex (n))
44     error ("fpdf: X, M, and N must not be complex");
45   endif
46
47   if (isa (x, "single") || isa (m, "single") || isa (n, "single"))
48     pdf = zeros (size (x), "single");
49   else
50     pdf = zeros (size (x));
51   endif
52
53   k = isnan (x) | !(m > 0) | !(m < Inf) | !(n > 0) | !(n < Inf);
54   pdf(k) = NaN;
55
56   k = (x > 0) & (x < Inf) & (m > 0) & (m < Inf) & (n > 0) & (n < Inf);
57   if (isscalar (m) && isscalar (n))
58     tmp = m / n * x(k);
59     pdf(k) = (exp ((m/2 - 1) * log (tmp)
60                    - ((m + n) / 2) * log (1 + tmp))
61               * (m / n) ./ beta (m/2, n/2));
62   else
63     tmp = m(k) .* x(k) ./ n(k);
64     pdf(k) = (exp ((m(k)/2 - 1) .* log (tmp)
65                    - ((m(k) + n(k)) / 2) .* log (1 + tmp))
66               .* (m(k) ./ n(k)) ./ beta (m(k)/2, n(k)/2));
67   endif
68
69 endfunction
70
71
72 %% F (x, 1, m) == T distribution (sqrt (x), m) / sqrt (x)
73 %!test
74 %! x = rand (10,1);
75 %! x = x(x > 0.1 & x < 0.9);
76 %! y = tpdf (sqrt (x), 2) ./ sqrt (x);
77 %! assert(fpdf (x, 1, 2), y, 5*eps);
78
79 %!shared x,y
80 %! x = [-1 0 0.5 1 2];
81 %! y = [0 0 4/9 1/4 1/9];
82 %!assert(fpdf (x, 2*ones(1,5), 2*ones(1,5)), y, eps);
83 %!assert(fpdf (x, 2, 2*ones(1,5)), y, eps);
84 %!assert(fpdf (x, 2*ones(1,5), 2), y, eps);
85 %!assert(fpdf (x, [0 NaN Inf 2 2], 2), [NaN NaN NaN y(4:5)], eps);
86 %!assert(fpdf (x, 2, [0 NaN Inf 2 2]), [NaN NaN NaN y(4:5)], eps);
87 %!assert(fpdf ([x, NaN], 2, 2), [y, NaN], eps);
88
89 %% Test class of input preserved
90 %!assert(fpdf (single([x, NaN]), 2, 2), single([y, NaN]), eps("single"));
91 %!assert(fpdf ([x, NaN], single(2), 2), single([y, NaN]), eps("single"));
92 %!assert(fpdf ([x, NaN], 2, single(2)), single([y, NaN]), eps("single"));
93
94 %% Test input validation
95 %!error fpdf ()
96 %!error fpdf (1)
97 %!error fpdf (1,2)
98 %!error fpdf (1,2,3,4)
99 %!error fpdf (ones(3),ones(2),ones(2))
100 %!error fpdf (ones(2),ones(3),ones(2))
101 %!error fpdf (ones(2),ones(2),ones(3))
102 %!error fpdf (i, 2, 2)
103 %!error fpdf (2, i, 2)
104 %!error fpdf (2, 2, i)
105