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 -*- |
6617
|
21 ## @deftypefn {Function File} {} norminv (@var{x}, @var{m}, @var{s}) |
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 |
6617
|
24 ## standard deviation @var{s}. |
5410
|
25 ## |
6617
|
26 ## Default values are @var{m} = 0, @var{s} = 1. |
5410
|
27 ## @end deftypefn |
|
28 |
5428
|
29 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410
|
30 ## Description: Quantile function of the normal distribution |
|
31 |
6617
|
32 function inv = norminv (x, m, s) |
5410
|
33 |
|
34 if (nargin != 1 && nargin != 3) |
6046
|
35 print_usage (); |
5410
|
36 endif |
|
37 |
|
38 if (nargin == 1) |
|
39 m = 0; |
6617
|
40 s = 1; |
5410
|
41 endif |
|
42 |
6617
|
43 if (!isscalar (m) || !isscalar (s)) |
|
44 [retval, x, m, s] = common_size (x, m, s); |
5410
|
45 if (retval > 0) |
6617
|
46 error ("norminv: x, m and s must be of common size or scalars"); |
5410
|
47 endif |
|
48 endif |
|
49 |
|
50 sz = size (x); |
|
51 inv = zeros (sz); |
|
52 |
6617
|
53 if (isscalar (m) && isscalar (s)) |
|
54 if (find (isinf (m) | isnan (m) | !(s > 0) | !(s < Inf))) |
5410
|
55 inv = NaN * ones (sz); |
|
56 else |
6617
|
57 inv = m + s .* stdnormal_inv (x); |
5410
|
58 endif |
|
59 else |
6617
|
60 k = find (isinf (m) | isnan (m) | !(s > 0) | !(s < Inf)); |
5410
|
61 if (any (k)) |
|
62 inv(k) = NaN; |
|
63 endif |
|
64 |
6617
|
65 k = find (!isinf (m) & !isnan (m) & (s > 0) & (s < Inf)); |
5410
|
66 if (any (k)) |
6617
|
67 inv(k) = m(k) + s(k) .* stdnormal_inv (x(k)); |
5410
|
68 endif |
|
69 endif |
|
70 |
6617
|
71 k = find ((s == 0) & (x > 0) & (x < 1)); |
5410
|
72 if (any (k)) |
|
73 inv(k) = m(k); |
|
74 endif |
|
75 |
6617
|
76 inv((s == 0) & (x == 0)) = -Inf; |
|
77 inv((s == 0) & (x == 1)) = Inf; |
5410
|
78 |
|
79 endfunction |