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} {} tcdf (@var{x}, @var{n})
22 ## For each element of @var{x}, compute the cumulative distribution
23 ## function (CDF) at @var{x} of the t (Student) distribution with
24 ## @var{n} degrees of freedom, i.e., PROB (t(@var{n}) @leq{} @var{x}).
27 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
28 ## Description: CDF of the t distribution
30 function cdf = tcdf (x, n)
37 [retval, x, n] = common_size (x, n);
39 error ("tcdf: X and N must be of common size or scalars");
43 if (iscomplex (x) || iscomplex (n))
44 error ("tcdf: X and N must not be complex");
47 if (isa (x, "single") || isa (n, "single"))
48 cdf = zeros (size (x), "single");
50 cdf = zeros (size (x));
53 k = !isinf (x) & (n > 0);
55 cdf(k) = betainc (1 ./ (1 + x(k) .^ 2 / n), n/2, 1/2) / 2;
57 cdf(k) = betainc (1 ./ (1 + x(k) .^ 2 ./ n(k)), n(k)/2, 1/2) / 2;
64 k = isnan (x) | !(n > 0);
67 k = (x == Inf) & (n > 0);
74 %! x = [-Inf 0 1 Inf];
76 %!assert(tcdf (x, ones(1,4)), y, eps);
77 %!assert(tcdf (x, 1), y, eps);
78 %!assert(tcdf (x, [0 1 NaN 1]), [NaN 1/2 NaN 1], eps);
79 %!assert(tcdf ([x(1:2) NaN x(4)], 1), [y(1:2) NaN y(4)], eps);
81 %% Test class of input preserved
82 %!assert(tcdf ([x, NaN], 1), [y, NaN], eps);
83 %!assert(tcdf (single([x, NaN]), 1), single([y, NaN]), eps("single"));
84 %!assert(tcdf ([x, NaN], single(1)), single([y, NaN]), eps("single"));
86 %% Test input validation
90 %!error tcdf (ones(3),ones(2))
91 %!error tcdf (ones(2),ones(3))