]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/tcdf.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / tcdf.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} {} 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}).
25 ## @end deftypefn
26
27 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
28 ## Description: CDF of the t distribution
29
30 function cdf = tcdf (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 ("tcdf: X and N must be of common size or scalars");
40     endif
41   endif
42
43   if (iscomplex (x) || iscomplex (n))
44     error ("tcdf: X and N must not be complex");
45   endif
46
47   if (isa (x, "single") || isa (n, "single"))
48     cdf = zeros (size (x), "single");
49   else
50     cdf = zeros (size (x));
51   endif
52
53   k = !isinf (x) & (n > 0);
54   if (isscalar (n))
55     cdf(k) = betainc (1 ./ (1 + x(k) .^ 2 / n), n/2, 1/2) / 2;
56   else
57     cdf(k) = betainc (1 ./ (1 + x(k) .^ 2 ./ n(k)), n(k)/2, 1/2) / 2;
58   endif
59   k &= (x > 0);
60   if (any (k(:)))
61     cdf(k) = 1 - cdf(k);
62   endif
63
64   k = isnan (x) | !(n > 0);
65   cdf(k) = NaN;
66
67   k = (x == Inf) & (n > 0);
68   cdf(k) = 1;
69
70 endfunction
71
72
73 %!shared x,y
74 %! x = [-Inf 0 1 Inf];
75 %! y = [0 1/2 3/4 1];
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);
80
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"));
85
86 %% Test input validation
87 %!error tcdf ()
88 %!error tcdf (1)
89 %!error tcdf (1,2,3)
90 %!error tcdf (ones(3),ones(2))
91 %!error tcdf (ones(2),ones(3))
92 %!error tcdf (i, 2)
93 %!error tcdf (2, i)
94