5410
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
|
2 ## |
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
9 ## |
|
10 ## Octave is distributed in the hope that it will be useful, but |
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
13 ## General Public License for more details. |
|
14 ## |
|
15 ## You should have received a copy of the GNU General Public License |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
5411
|
21 ## @deftypefn {Function File} {} norminv (@var{x}, @var{m}, @var{v}) |
5410
|
22 ## For each element of @var{x}, compute the quantile (the inverse of the |
|
23 ## CDF) at @var{x} of the normal distribution with mean @var{m} and |
|
24 ## variance @var{v}. |
|
25 ## |
|
26 ## Default values are @var{m} = 0, @var{v} = 1. |
|
27 ## @end deftypefn |
|
28 |
|
29 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
30 ## Description: Quantile function of the normal distribution |
|
31 |
5411
|
32 function inv = norminv (x, m, v) |
5410
|
33 |
|
34 if (nargin != 1 && nargin != 3) |
5411
|
35 usage ("norminv (x, m, v)"); |
5410
|
36 endif |
|
37 |
|
38 if (nargin == 1) |
|
39 m = 0; |
|
40 v = 1; |
|
41 endif |
|
42 |
|
43 if (!isscalar (m) || !isscalar(v)) |
|
44 [retval, x, m, v] = common_size (x, m, v); |
|
45 if (retval > 0) |
5411
|
46 error ("norminv: x, m and v must be of common size or scalars"); |
5410
|
47 endif |
|
48 endif |
|
49 |
|
50 sz = size (x); |
|
51 inv = zeros (sz); |
|
52 |
|
53 if (isscalar (m) && isscalar(v)) |
|
54 if (find (isinf (m) | isnan (m) | !(v > 0) | !(v < Inf))) |
|
55 inv = NaN * ones (sz); |
|
56 else |
|
57 inv = m + sqrt (v) .* stdnormal_inv (x); |
|
58 endif |
|
59 else |
|
60 k = find (isinf (m) | isnan (m) | !(v > 0) | !(v < Inf)); |
|
61 if (any (k)) |
|
62 inv(k) = NaN; |
|
63 endif |
|
64 |
|
65 k = find (!isinf (m) & !isnan (m) & (v > 0) & (v < Inf)); |
|
66 if (any (k)) |
|
67 inv(k) = m(k) + sqrt (v(k)) .* stdnormal_inv (x(k)); |
|
68 endif |
|
69 endif |
|
70 |
|
71 k = find ((v == 0) & (x > 0) & (x < 1)); |
|
72 if (any (k)) |
|
73 inv(k) = m(k); |
|
74 endif |
|
75 |
|
76 inv((v == 0) & (x == 0)) = -Inf; |
|
77 inv((v == 0) & (x == 1)) = Inf; |
|
78 |
|
79 endfunction |