7017
|
1 ## Copyright (C) 1995, 1996, 1997, 2005, 2006, 2007 Kurt Hornik |
5410
|
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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) any later version. |
5410
|
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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
5410
|
18 |
|
19 ## -*- texinfo -*- |
5411
|
20 ## @deftypefn {Function File} {} poissinv (@var{x}, @var{lambda}) |
5410
|
21 ## For each component of @var{x}, compute the quantile (the inverse of |
|
22 ## the CDF) at @var{x} of the Poisson distribution with parameter |
|
23 ## @var{lambda}. |
|
24 ## @end deftypefn |
|
25 |
5428
|
26 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410
|
27 ## Description: Quantile function of the Poisson distribution |
|
28 |
5411
|
29 function inv = poissinv (x, l) |
5410
|
30 |
|
31 if (nargin != 2) |
6046
|
32 print_usage (); |
5410
|
33 endif |
|
34 |
|
35 if (!isscalar (l)) |
|
36 [retval, x, l] = common_size (x, l); |
|
37 if (retval > 0) |
5411
|
38 error ("poissinv: x and lambda must be of common size or scalar"); |
5410
|
39 endif |
|
40 endif |
|
41 |
|
42 inv = zeros (size (x)); |
|
43 |
|
44 k = find ((x < 0) | (x > 1) | isnan (x) | !(l > 0)); |
|
45 if (any (k)) |
|
46 inv(k) = NaN; |
|
47 endif |
|
48 |
|
49 k = find ((x == 1) & (l > 0)); |
|
50 if (any (k)) |
|
51 inv(k) = Inf; |
|
52 endif |
|
53 |
|
54 k = find ((x > 0) & (x < 1) & (l > 0)); |
|
55 if (any (k)) |
|
56 if (isscalar (l)) |
|
57 cdf = exp (-l) * ones (size (k)); |
|
58 else |
|
59 cdf = exp (-l(k)); |
|
60 endif |
|
61 while (1) |
|
62 m = find (cdf < x(k)); |
|
63 if (any (m)) |
|
64 inv(k(m)) = inv(k(m)) + 1; |
|
65 if (isscalar (l)) |
5482
|
66 cdf(m) = cdf(m) + poisspdf (inv(k(m)), l); |
5410
|
67 else |
5482
|
68 cdf(m) = cdf(m) + poisspdf (inv(k(m)), l(k(m))); |
5410
|
69 endif |
|
70 else |
|
71 break; |
|
72 endif |
|
73 endwhile |
|
74 endif |
|
75 |
|
76 endfunction |