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