1 ## Copyright (C) 2012 Rik Wehbring
2 ## Copyright (C) 1995-2012 Kurt Hornik
4 ## This file is part of Octave.
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.
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.
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/>.
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.
27 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
28 ## Description: PDF of the t distribution
30 function pdf = tpdf (x, n)
37 [retval, x, n] = common_size (x, n);
39 error ("tpdf: X and N must be of common size or scalars");
43 if (iscomplex (x) || iscomplex (n))
44 error ("tpdf: X and N must not be complex");
47 if (isa (x, "single") || isa (n, "single"))
48 pdf = zeros (size (x), "single");
50 pdf = zeros (size (x));
53 k = isnan (x) | !(n > 0) | !(n < Inf);
56 k = !isinf (x) & !isnan (x) & (n > 0) & (n < Inf);
58 pdf(k) = (exp (- (n + 1) * log (1 + x(k) .^ 2 / n)/2)
59 / (sqrt (n) * beta (n/2, 1/2)));
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)));
70 %! y = 1./(pi * (1 + x.^2));
71 %! assert(tpdf (x, 1), y, 5*eps);
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);
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"));
85 %% Test input validation
89 %!error tpdf (ones(3),ones(2))
90 %!error tpdf (ones(2),ones(3))