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: cauchy_rnd (lambda, sigma [, r, c]) |
|
18 ## |
|
19 ## cauchy_rnd (lambda, sigma) returns a matrix of random samples from |
|
20 ## the Cauchy distribution with parameters lambda and sigma. The size |
|
21 ## of the matrix is the common size of the parameters. |
|
22 ## |
|
23 ## cauchy_rnd (lambda, sigma, r, c) returns an r by c matrix of random |
|
24 ## samples from the Cauchy distribution with parameters lambda and sigma |
|
25 ## which must both 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 Cauchy distribution |
|
29 |
|
30 function rnd = cauchy_rnd (l, scale, r, c) |
3426
|
31 |
3191
|
32 if (nargin == 4) |
|
33 if ( !(is_scalar (r) && (r > 0) && (r == round (r))) ) |
|
34 error ("cauchy_rnd: r must be a positive integer"); |
|
35 endif |
|
36 if ( !(is_scalar (c) && (c > 0) && (c == round (c))) ) |
|
37 error ("cauchy_rnd: c must be a positive integer"); |
|
38 endif |
|
39 [retval, l, scale] = common_size (l, scale, zeros (r, c)); |
|
40 if (retval > 0) |
|
41 error (strcat("cauchy_rnd: ", |
3426
|
42 "lambda and sigma must be scalar or of size", |
|
43 sprintf ("%d by %d.", r, c))); |
3191
|
44 endif |
|
45 elseif (nargin == 2) |
|
46 [retval, l, scale] = common_size (l, scale); |
|
47 if (retval > 0) |
|
48 error (["cauchy_rnd: ", ... |
3426
|
49 "lambda and sigma must be of common size or scalar"]); |
3191
|
50 endif |
|
51 [r, c] = size (l); |
|
52 else |
|
53 usage ("cauchy_rnd (lambda, sigma [, r, c])"); |
|
54 endif |
3426
|
55 |
3191
|
56 s = r * c; |
|
57 l = reshape (l, 1, s); |
|
58 scale = reshape (scale, 1, s); |
3426
|
59 |
3191
|
60 rnd = NaN * ones (1, s); |
3426
|
61 |
3191
|
62 k = find ((l > -Inf) & (l < Inf) & (scale > 0) & (scale < Inf)); |
|
63 if (any (k)) |
|
64 rnd(k) = l(k) - cot (pi * rand (1, length (k))) .* scale(k); |
|
65 endif |
|
66 |
|
67 rnd = reshape (rnd, r, c); |
3426
|
68 |
3191
|
69 endfunction |