3191
|
1 ## Copyright (C) 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3191
|
3 ## This program is free software; you can redistribute it and/or modify |
|
4 ## it under the terms of the GNU General Public License as published by |
|
5 ## the Free Software Foundation; either version 2, or (at your option) |
|
6 ## any later version. |
3426
|
7 ## |
3191
|
8 ## This program is distributed in the hope that it will be useful, but |
|
9 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
11 ## General Public License for more details. |
|
12 ## |
3191
|
13 ## You should have received a copy of the GNU General Public License |
|
14 ## along with this file. If not, write to the Free Software Foundation, |
|
15 ## 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
16 |
3456
|
17 ## -*- texinfo -*- |
|
18 ## @deftypefn {Function File} {} discrete_inv (@var{x}, @var{v}, @var{p}) |
|
19 ## For each component of @var{x}, compute the quantile (the inverse of |
|
20 ## the CDF) at @var{x} of the univariate distribution which assumes the |
|
21 ## values in @var{v} with probabilities @var{p}. |
|
22 ## @end deftypefn |
3191
|
23 |
3456
|
24 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
25 ## Description: Quantile function of a discrete distribution |
3191
|
26 |
3456
|
27 function inv = discrete_inv (x, v, p) |
3426
|
28 |
3191
|
29 if (nargin != 3) |
3456
|
30 usage ("discrete_inv (x, v, p)"); |
3191
|
31 endif |
|
32 |
3456
|
33 [r, c] = size (x); |
3191
|
34 |
3456
|
35 if (! is_vector (v)) |
|
36 error ("discrete_inv: v must be a vector"); |
|
37 elseif (! is_vector (p) || (length (p) != length (v))) |
|
38 error ("discrete_inv: p must be a vector with length (v) elements"); |
|
39 elseif (! (all (p >= 0) && any (p))) |
|
40 error ("discrete_inv: p must be a nonzero, nonnegative vector"); |
3191
|
41 endif |
|
42 |
|
43 n = r * c; |
3456
|
44 x = reshape (x, 1, n); |
|
45 m = length (v); |
|
46 [v, ind] = sort (v); |
|
47 s = reshape (cumsum (p / sum (p)), m, 1); |
3191
|
48 |
|
49 inv = NaN * ones (n, 1); |
3456
|
50 if (any (k = find (x == 0))) |
3191
|
51 inv(k) = -Inf * ones (1, length (k)); |
|
52 endif |
3456
|
53 if (any (k = find (x == 1))) |
|
54 inv(k) = v(m) * ones (1, length (k)); |
3191
|
55 endif |
3456
|
56 if (any (k = find ((x > 0) & (x < 1)))) |
3191
|
57 n = length (k); |
|
58 ## --FIXME-- |
|
59 ## This does not work! |
3456
|
60 inv(k) = v(sum ((ones (m, 1) * x(k)) > (s * ones (1, n))) + 1); |
3191
|
61 endif |
|
62 |
|
63 inv = reshape (inv, r, c); |
|
64 |
|
65 endfunction |
|
66 |
|
67 |