3191
|
1 ## Copyright (C) 1995, 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 |
|
17 ## usage: lognormal_rnd (a, v [, r, c]) |
|
18 ## |
|
19 ## lognormal_rnd (a, v) returns a matrix of random samples from the |
|
20 ## lognormal distribution with parameters a and v. The size of the |
|
21 ## matrix is the common size of a and v. |
|
22 ## |
|
23 ## lognormal_rnd (a, v, r, c) returns an r by c matrix of random samples |
|
24 ## from the lognormal distribution with parameters a and v. Both a and v |
|
25 ## must be scalar or of size r by c. |
3426
|
26 |
3191
|
27 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
28 ## Description: Random deviates from the log normal distribution |
3426
|
29 |
3191
|
30 function rnd = lognormal_rnd (a, v, r, c) |
|
31 |
|
32 if (nargin == 4) |
|
33 if ( !(is_scalar (r) && (r > 0) && (r == round (r))) ) |
|
34 error ("lognormal_rnd: r must be a positive integer"); |
|
35 endif |
|
36 if ( !(is_scalar (c) && (c > 0) && (c == round (c))) ) |
|
37 error ("lognormal_rnd: c must be a positive integer"); |
|
38 endif |
|
39 [retval, a, v] = common_size (a, v, zeros (r, c)); |
|
40 if (retval > 0) |
|
41 error (strcat("lognormal_rnd: ", |
3426
|
42 "a and v must be scalar or of size ", |
|
43 sprintf ("%d by %d", r, c))); |
3191
|
44 endif |
|
45 elseif (nargin == 2) |
|
46 [retval, a, v] = common_size (a, v); |
|
47 if (retval > 0) |
|
48 error (strcat("lognormal_rnd: ", |
3426
|
49 "a and v must be of common size or scalar")); |
3191
|
50 endif |
|
51 else |
|
52 usage ("lognormal_rnd (a, v [, r, c])"); |
|
53 endif |
|
54 |
|
55 [r, c] = size (a); |
|
56 s = r * c; |
|
57 a = reshape (a, 1, s); |
|
58 v = reshape (v, 1, s); |
|
59 rnd = zeros (1, s); |
3426
|
60 |
3191
|
61 k = find (!(a > 0) | !(a < Inf) | !(v > 0) | !(v < Inf)); |
|
62 if any (k) |
|
63 rnd(k) = NaN * ones (1, length (k)); |
|
64 endif |
3426
|
65 |
3191
|
66 k = find ((a > 0) & (a < Inf) & (v > 0) & (v < Inf)); |
|
67 if any (k) |
|
68 rnd(k) = a(k) .* exp (sqrt (v(k)) .* randn (1, length (k))); |
|
69 endif |
3426
|
70 |
3191
|
71 rnd = reshape (rnd, r, c); |
3426
|
72 |
3191
|
73 endfunction |