]> Creatis software - CreaPhase.git/blob - octave_packages/nan-2.5.5/tinv.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / nan-2.5.5 / tinv.m
1 function y = tinv(x,n)
2 % TINV returns inverse cumulative function of the student distribution
3 %
4 % x = tinv(p,v);
5 %
6 % Computes the quantile (inverse of the CDF) of a the student
7 %    cumulative distribution with mean m and standard deviation s
8 % p,v must be matrices of same size, or any one can be a scalar. 
9 %
10 % see also: TPDF, TCDF, NORMPDF, NORMCDF, NORMINV 
11
12 % Reference(s):
13
14 %       $Id: tinv.m 9033 2011-11-08 20:58:07Z schloegl $
15 %       Copyright (C) 2000-2003,2009 by Alois Schloegl <alois.schloegl@gmail.com>       
16 %       This function is part of the NaN-toolbox
17 %       http://pub.ist.ac.at/~schloegl/matlab/NaN/
18
19 %    This program is free software; you can redistribute it and/or modify
20 %    it under the terms of the GNU General Public License as published by
21 %    the Free Software Foundation; either version 2 of the License, or
22 %    (at your option) any later version.
23 %
24 %    This program is distributed in the hope that it will be useful,
25 %    but WITHOUT ANY WARRANTY; without even the implied warranty of
26 %    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27 %    GNU General Public License for more details.
28 %
29 %    You should have received a copy of the GNU General Public License
30 %    along with this program; If not, see <http://www.gnu.org/licenses/>.
31
32
33 % allocate output memory and check size of arguments
34 if all(size(x)==1)
35         x = repmat(x,size(n));
36 elseif all(size(n)==1)
37         n = repmat(n,size(x));
38 elseif all(size(x)==size(n))
39         ;       %% OK, do nothing
40 else
41         error('size of input arguments must be equal or scalar')
42 end;    
43
44 y = norminv(x); % do special cases, like x<=0, x>=1, isnan(x), n > 10000;
45 y(~(n>0)) = NaN; 
46
47 ix = find(~isnan(x) & (n>0) & (n<10000));
48 if ~isempty(ix)
49         y(ix) = (sign(x(ix) - 1/2).*sqrt(n(ix)./betainv(2*min(x(ix), 1-x(ix)), n(ix)/2, 1/2) - n(ix)));
50 end;
51
52 y = reshape(y,size(x));
53
54 %!assert(tinv(NaN,4),NaN)