]> Creatis software - CreaPhase.git/blob - octave_packages/m/statistics/distributions/norminv.m
update packages
[CreaPhase.git] / octave_packages / m / statistics / distributions / norminv.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} {} norminv (@var{x})
22 ## @deftypefnx {Function File} {} norminv (@var{x}, @var{mu}, @var{sigma})
23 ## For each element of @var{x}, compute the quantile (the inverse of the
24 ## CDF) at @var{x} of the normal distribution with mean @var{mu} and
25 ## standard deviation @var{sigma}.
26 ##
27 ## Default values are @var{mu} = 0, @var{sigma} = 1.
28 ## @end deftypefn
29
30 ## Author: KH <Kurt.Hornik@wu-wien.ac.at>
31 ## Description: Quantile function of the normal distribution
32
33 function inv = norminv (x, mu = 0, sigma = 1)
34
35   if (nargin != 1 && nargin != 3)
36     print_usage ();
37   endif
38
39   if (!isscalar (mu) || !isscalar (sigma))
40     [retval, x, mu, sigma] = common_size (x, mu, sigma);
41     if (retval > 0)
42       error ("norminv: X, MU, and SIGMA must be of common size or scalars");
43     endif
44   endif
45
46   if (iscomplex (x) || iscomplex (mu) || iscomplex (sigma))
47     error ("norminv: X, MU, and SIGMA must not be complex");
48   endif
49
50   if (isa (x, "single") || isa (mu, "single") || isa (sigma, "single"))
51     inv = NaN (size (x), "single");
52   else
53     inv = NaN (size (x));
54   endif
55
56   if (isscalar (mu) && isscalar (sigma))
57     if (!isinf (mu) && !isnan (mu) && (sigma > 0) && (sigma < Inf))
58       inv =  mu + sigma * stdnormal_inv (x);
59     endif
60   else
61     k = !isinf (mu) & !isnan (mu) & (sigma > 0) & (sigma < Inf);
62     inv(k) = mu(k) + sigma(k) .* stdnormal_inv (x(k));
63   endif
64
65 endfunction
66
67
68 %!shared x
69 %! x = [-1 0 0.5 1 2];
70 %!assert(norminv (x, ones(1,5), ones(1,5)), [NaN -Inf 1 Inf NaN]);
71 %!assert(norminv (x, 1, ones(1,5)), [NaN -Inf 1 Inf NaN]);
72 %!assert(norminv (x, ones(1,5), 1), [NaN -Inf 1 Inf NaN]);
73 %!assert(norminv (x, [1 -Inf NaN Inf 1], 1), [NaN NaN NaN NaN NaN]);
74 %!assert(norminv (x, 1, [1 0 NaN Inf 1]), [NaN NaN NaN NaN NaN]);
75 %!assert(norminv ([x(1:2) NaN x(4:5)], 1, 1), [NaN -Inf NaN Inf NaN]);
76
77 %% Test class of input preserved
78 %!assert(norminv ([x, NaN], 1, 1), [NaN -Inf 1 Inf NaN NaN]);
79 %!assert(norminv (single([x, NaN]), 1, 1), single([NaN -Inf 1 Inf NaN NaN]));
80 %!assert(norminv ([x, NaN], single(1), 1), single([NaN -Inf 1 Inf NaN NaN]));
81 %!assert(norminv ([x, NaN], 1, single(1)), single([NaN -Inf 1 Inf NaN NaN]));
82
83 %% Test input validation
84 %!error norminv ()
85 %!error norminv (1,2)
86 %!error norminv (1,2,3,4)
87 %!error norminv (ones(3),ones(2),ones(2))
88 %!error norminv (ones(2),ones(3),ones(2))
89 %!error norminv (ones(2),ones(2),ones(3))
90 %!error norminv (i, 2, 2)
91 %!error norminv (2, i, 2)
92 %!error norminv (2, 2, i)
93