-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtcdf.m
More file actions
73 lines (60 loc) · 2.16 KB
/
tcdf.m
File metadata and controls
73 lines (60 loc) · 2.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function p = tcdf(x,v)
%TCDF Student's T cumulative distribution function (cdf).
% P = TCDF(X,V) computes the cdf for Student's T distribution
% with V degrees of freedom, at the values in X.
%
% The size of P is the common size of X and V. A scalar input
% functions as a constant matrix of the same size as the other input.
%
% See also TINV, TPDF, TRND, TSTAT, CDF.
% References:
% [1] M. Abramowitz and I. A. Stegun, "Handbook of Mathematical
% Functions", Government Printing Office, 1964, 26.7.
% [2] L. Devroye, "Non-Uniform Random Variate Generation",
% Springer-Verlag, 1986
% [3] E. Kreyszig, "Introductory Mathematical Statistics",
% John Wiley, 1970, Section 10.3, pages 144-146.
% Copyright 1993-2009 The MathWorks, Inc.
% $Revision: 2.12.4.8 $ $Date: 2009/11/05 17:03:40 $
normcutoff = 1e7;
if nargin < 2,
error('stats:tcdf:TooFewInputs','Requires two input arguments.');
end
[errorcode x v] = distchck(2,x,v);
if errorcode > 0
error('stats:tcdf:InputSizeMismatch',...
'Requires non-scalar arguments to match in size.');
end
% Initialize P.
if isa(x,'single') || isa(v,'single')
p = NaN(size(x),'single');
else
p = NaN(size(x));
end
nans = (isnan(x) | ~(0<v)); % v==NaN ==> (0<v)==false
cauchy = (v == 1);
normal = (v > normcutoff);
% General case: first compute F(-|x|) < .5, the lower tail.
%
% See Abramowitz and Stegun, formulas and 26.7.1/26.5.27 and 26.5.2
general = ~(cauchy | normal | nans);
xsq = x.^2;
% For small v, form v/(v+x^2) to maintain precision
t = (v < xsq) & general;
if any(t(:))
p(t) = betainc(v(t) ./ (v(t) + xsq(t)), v(t)/2, 0.5, 'lower') / 2;
end
% For large v, form x^2/(v+x^2) to maintain precision
t = (v >= xsq) & general;
if any(t(:))
p(t) = betainc(xsq(t) ./ (v(t) + xsq(t)), 0.5, v(t)/2, 'upper') / 2;
end
% For x > 0, F(x) = 1 - F(-|x|).
xpos = (x > 0);
p(xpos) = 1 - p(xpos); % p < .5, cancellation not a problem
% Special case for Cauchy distribution. See Devroye pages 29 and 450.
p(cauchy) = .5 + atan(x(cauchy))/pi;
% Normal Approximation for very large nu.
p(normal) = normcdf(x(normal));
% Make the result exact for the median.
p(x == 0 & ~nans) = 0.5;