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