5410
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
|
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 |
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
|
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 |
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
|
19 |
|
20 ## -*- texinfo -*- |
5411
|
21 ## @deftypefn {Function File} {} frnd (@var{m}, @var{n}, @var{r}, @var{c}) |
|
22 ## @deftypefnx {Function File} {} frnd (@var{m}, @var{n}, @var{sz}) |
5410
|
23 ## Return an @var{r} by @var{c} matrix of random samples from the F |
|
24 ## distribution with @var{m} and @var{n} degrees of freedom. Both |
|
25 ## @var{m} and @var{n} must be scalar or of size @var{r} by @var{c}. |
|
26 ## If @var{sz} is a vector the random samples are in a matrix of |
|
27 ## size @var{sz}. |
|
28 ## |
|
29 ## If @var{r} and @var{c} are omitted, the size of the result matrix is |
|
30 ## the common size of @var{m} and @var{n}. |
|
31 ## @end deftypefn |
|
32 |
5428
|
33 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410
|
34 ## Description: Random deviates from the F distribution |
|
35 |
5411
|
36 function rnd = frnd (m, n, r, c) |
5410
|
37 |
|
38 if (nargin > 1) |
|
39 if (!isscalar(m) || !isscalar(n)) |
|
40 [retval, m, n] = common_size (m, n); |
|
41 if (retval > 0) |
5411
|
42 error ("frnd: m and n must be of common size or scalar"); |
5410
|
43 endif |
|
44 endif |
|
45 endif |
|
46 |
|
47 |
|
48 if (nargin == 4) |
|
49 if (! (isscalar (r) && (r > 0) && (r == round (r)))) |
5411
|
50 error ("frnd: r must be a positive integer"); |
5410
|
51 endif |
|
52 if (! (isscalar (c) && (c > 0) && (c == round (c)))) |
5411
|
53 error ("frnd: c must be a positive integer"); |
5410
|
54 endif |
|
55 sz = [r, c]; |
|
56 |
|
57 if (any (size (m) != 1) && |
|
58 ((length (size (m)) != length (sz)) || any (size (m) != sz))) |
5411
|
59 error ("frnd: m and n must be scalar or of size [r,c]"); |
5410
|
60 endif |
|
61 elseif (nargin == 3) |
|
62 if (isscalar (r) && (r > 0)) |
|
63 sz = [r, r]; |
|
64 elseif (isvector(r) && all (r > 0)) |
|
65 sz = r(:)'; |
|
66 else |
5411
|
67 error ("frnd: r must be a postive integer or vector"); |
5410
|
68 endif |
|
69 |
|
70 if (any (size (m) != 1) && |
|
71 ((length (size (m)) != length (sz)) || any (size (m) != sz))) |
5411
|
72 error ("frnd: m and n must be scalar or of size sz"); |
5410
|
73 endif |
|
74 elseif (nargin == 2) |
|
75 sz = size(a); |
|
76 else |
5411
|
77 usage ("frnd (m, n, r, c)"); |
5410
|
78 endif |
|
79 |
|
80 |
|
81 if (isscalar (m) && isscalar (n)) |
|
82 if ((m > 0) && (m < Inf) && (n > 0) && (n < Inf)) |
5482
|
83 rnd = finv (rand (sz), m, n); |
5410
|
84 else |
|
85 rnd = NaN * ones (sz); |
|
86 endif |
|
87 else |
|
88 rnd = zeros (sz); |
|
89 |
|
90 k = find (!(m > 0) | !(m < Inf) | |
|
91 !(n > 0) | !(n < Inf)); |
|
92 if (any (k)) |
|
93 rnd(k) = NaN; |
|
94 endif |
|
95 |
|
96 k = find ((m > 0) & (m < Inf) & |
|
97 (n > 0) & (n < Inf)); |
|
98 if (any (k)) |
5411
|
99 rnd(k) = finv (rand (size (k)), m(k), n(k)); |
5410
|
100 endif |
|
101 endif |
|
102 |
|
103 endfunction |