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