3191
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3922
|
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 |
3191
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3191
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3191
|
15 ## You should have received a copy of the GNU General Public License |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
3191
|
19 |
3456
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} normal_inv (@var{x}, @var{m}, @var{v}) |
|
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}. |
3191
|
25 ## |
3456
|
26 ## Default values are @var{m} = 0, @var{v} = 1. |
|
27 ## @end deftypefn |
3426
|
28 |
3456
|
29 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
30 ## Description: Quantile function of the normal distribution |
3191
|
31 |
|
32 function inv = normal_inv (x, m, v) |
|
33 |
4854
|
34 if (nargin != 1 && nargin != 3) |
3456
|
35 usage ("normal_inv (x, m, v)"); |
3191
|
36 endif |
|
37 |
|
38 if (nargin == 1) |
|
39 m = 0; |
|
40 v = 1; |
|
41 endif |
|
42 |
4854
|
43 if (!isscalar (m) || !isscalar(v)) |
|
44 [retval, x, m, v] = common_size (x, m, v); |
|
45 if (retval > 0) |
|
46 error ("normal_inv: x, m and v must be of common size or scalars"); |
|
47 endif |
3191
|
48 endif |
|
49 |
4854
|
50 sz = size (x); |
|
51 inv = zeros (sz); |
3191
|
52 |
4854
|
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 |
3426
|
64 |
4854
|
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 |
3191
|
69 endif |
|
70 |
4079
|
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 |
3191
|
79 endfunction |