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 |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
3191
|
19 |
3456
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} exponential_rnd (@var{lambda}, @var{r}, @var{c}) |
4862
|
22 ## @deftypefnx {Function File} {} exponential_rnd (@var{lambda}, @var{sz}) |
3456
|
23 ## Return an @var{r} by @var{c} matrix of random samples from the |
|
24 ## exponential distribution with parameter @var{lambda}, which must be a |
4859
|
25 ## scalar or of size @var{r} by @var{c}. Or if @var{sz} is a vector, |
|
26 ## create a matrix of size @var{sz}. |
3191
|
27 ## |
3456
|
28 ## If @var{r} and @var{c} are omitted, the size of the result matrix is |
|
29 ## the size of @var{lambda}. |
|
30 ## @end deftypefn |
3426
|
31 |
3456
|
32 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
33 ## Description: Random deviates from the exponential distribution |
3426
|
34 |
3191
|
35 function rnd = exponential_rnd (l, r, c) |
|
36 |
|
37 if (nargin == 3) |
4030
|
38 if (! (isscalar (r) && (r > 0) && (r == round (r)))) |
3456
|
39 error ("exponential_rnd: r must be a positive integer"); |
3191
|
40 endif |
4030
|
41 if (! (isscalar (c) && (c > 0) && (c == round (c)))) |
3456
|
42 error ("exponential_rnd: c must be a positive integer"); |
3191
|
43 endif |
4859
|
44 sz = [r, c]; |
|
45 |
|
46 if (any (size (l) != 1) && |
|
47 ((length (size (nl)) != length (sz)) || any (size (l) != sz))) |
|
48 error ("exponential_rnd: lambda must be scalar or of size [r, c]"); |
3191
|
49 endif |
4859
|
50 elseif (nargin == 2) |
|
51 if (isscalar (r) && (r > 0)) |
|
52 sz = [r, r]; |
|
53 elseif (isvector(r) && all (r > 0)) |
|
54 sz = r(:)'; |
|
55 else |
|
56 error ("exponential_rnd: r must be a postive integer or vector"); |
|
57 endif |
|
58 |
|
59 if (any (size (l) != 1) && |
|
60 ((length (size (l)) != length (sz)) || any (size (l) != sz))) |
|
61 error ("exponential_rnd: lambda must be scalar or of size sz"); |
|
62 endif |
|
63 elseif (nargin == 1) |
|
64 sz = size (l); |
|
65 else |
3456
|
66 usage ("exponential_rnd (lambda, r, c)"); |
3191
|
67 endif |
3426
|
68 |
|
69 |
4859
|
70 if (isscalar (l)) |
|
71 if ((l > 0) && (l < Inf)) |
|
72 rnd = - log (1 - rand (sz)) ./ l; |
|
73 else |
|
74 rnd = NaN * ones (sz); |
|
75 endif |
|
76 else |
|
77 rnd = zeros (sz); |
|
78 k = find (!(l > 0) | !(l < Inf)); |
|
79 if (any (k)) |
|
80 rnd(k) = NaN; |
|
81 endif |
|
82 k = find ((l > 0) & (l < Inf)); |
|
83 if (any (k)) |
|
84 rnd(k) = - log (1 - rand (size (k))) ./ l(k); |
|
85 endif |
3191
|
86 endif |
3426
|
87 |
3191
|
88 endfunction |