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} {} poisson_rnd (@var{lambda}, @var{r}, @var{c}) |
|
22 ## Return an @var{r} by @var{c} matrix of random samples from the |
|
23 ## Poisson distribution with parameter @var{lambda}, which must be a |
|
24 ## scalar or of size @var{r} by @var{c}. |
3191
|
25 ## |
3456
|
26 ## If @var{r} and @var{c} are omitted, the size of the result matrix is |
|
27 ## the size of @var{lambda}. |
|
28 ## @end deftypefn |
3426
|
29 |
3456
|
30 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
31 ## Description: Random deviates from the Poisson distribution |
3426
|
32 |
3191
|
33 function rnd = poisson_rnd (l, r, c) |
|
34 |
|
35 if (nargin == 3) |
4030
|
36 if (! (isscalar (r) && (r > 0) && (r == round (r)))) |
3456
|
37 error ("poisson_rnd: r must be a positive integer"); |
3191
|
38 endif |
4030
|
39 if (! (isscalar (c) && (c > 0) && (c == round (c)))) |
3456
|
40 error ("poisson_rnd: c must be a positive integer"); |
3191
|
41 endif |
4859
|
42 sz = [r, c]; |
|
43 |
|
44 if (any (size (l) != 1) && |
|
45 ((length (size (l)) != length (sz)) || any (size (l) != sz))) |
|
46 error ("poisson_rnd: lambda must be scalar or of size [r, c]"); |
3191
|
47 endif |
4859
|
48 elseif (nargin == 2) |
|
49 if (isscalar (r) && (r > 0)) |
|
50 sz = [r, r]; |
|
51 elseif (isvector(r) && all (r > 0)) |
|
52 sz = r(:)'; |
|
53 else |
|
54 error ("poisson_rnd: r must be a postive integer or vector"); |
|
55 endif |
|
56 |
|
57 if (any (size (l) != 1) && |
|
58 ((length (size (l)) != length (sz)) || any (size (l) != sz))) |
|
59 error ("poisson_rnd: lambda must be scalar or of size sz"); |
|
60 endif |
|
61 elseif (nargin == 1) |
|
62 sz = size (l); |
|
63 else |
3456
|
64 usage ("poisson_rnd (lambda, r, c)"); |
3191
|
65 endif |
3426
|
66 |
4859
|
67 if (isscalar (l)) |
|
68 |
5154
|
69 if (!(l >= 0) | !(l < Inf)) |
4859
|
70 rnd = NaN * ones (sz); |
|
71 elseif ((l > 0) & (l < Inf)) |
|
72 num = zeros (sz); |
|
73 sum = - log (1 - rand (sz)) ./ l; |
|
74 while (1) |
|
75 ind = find (sum < 1); |
|
76 if (any (ind)) |
|
77 sum(ind) = (sum(ind) - log (1 - rand (size (ind))) / l); |
|
78 num(ind) = num(ind) + 1; |
|
79 else |
|
80 break; |
|
81 endif |
|
82 endwhile |
|
83 rnd = num; |
|
84 else |
|
85 rnd = zeros (sz); |
|
86 endif |
|
87 else |
|
88 rnd = zeros (sz); |
3426
|
89 |
5154
|
90 k = find (!(l >= 0) | !(l < Inf)); |
4859
|
91 if (any (k)) |
|
92 rnd(k) = NaN; |
|
93 endif |
|
94 |
|
95 k = find ((l > 0) & (l < Inf)); |
|
96 if (any (k)) |
|
97 l = l(k); |
|
98 num = zeros (size (k)); |
|
99 sum = - log (1 - rand (size (k))) ./ l; |
|
100 while (1) |
|
101 ind = find (sum < 1); |
|
102 if (any (ind)) |
|
103 sum(ind) = (sum(ind) |
5154
|
104 - log (1 - rand (size (ind))) ./ l(ind)); |
4859
|
105 num(ind) = num(ind) + 1; |
|
106 else |
|
107 break; |
|
108 endif |
|
109 endwhile |
|
110 rnd(k) = num; |
|
111 endif |
3191
|
112 endif |
3426
|
113 |
3191
|
114 endfunction |