Mercurial > octave-nkf
annotate scripts/statistics/distributions/frnd.m @ 11587:c792872f8942
all script files: untabify and strip trailing whitespace
author | John W. Eaton <jwe@octave.org> |
---|---|
date | Thu, 20 Jan 2011 17:35:29 -0500 |
parents | fd0a3ac60b0e |
children | c21eb9c02c31 |
rev | line source |
---|---|
11523 | 1 ## Copyright (C) 1995-2011 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 -*- | |
10793
be55736a0783
Grammarcheck the documentation from m-files.
Rik <octave@nomad.inbox5.com>
parents:
10549
diff
changeset
|
20 ## @deftypefn {Function File} {} frnd (@var{m}, @var{n}, @var{r}, @var{c}) |
5411 | 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}. | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
25 ## If @var{sz} is a vector the random samples are in a matrix of |
5410 | 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) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
38 if (!isscalar(m) || !isscalar(n)) |
5410 | 39 [retval, m, n] = common_size (m, n); |
40 if (retval > 0) | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
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)))) | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
49 error ("frnd: R must be a positive integer"); |
5410 | 50 endif |
51 if (! (isscalar (c) && (c > 0) && (c == round (c)))) | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
52 error ("frnd: C must be a positive integer"); |
5410 | 53 endif |
54 sz = [r, c]; | |
55 | |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
56 if (any (size (m) != 1) |
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
57 && ((length (size (m)) != length (sz)) || any (size (m) != sz))) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
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 | |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
66 error ("frnd: R must be a positive integer or vector"); |
5410 | 67 endif |
68 | |
11149
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
69 if (any (size (m) != 1) |
fe3c3dfc07eb
style fix: break lines before && and ||, not after
John W. Eaton <jwe@octave.org>
parents:
10793
diff
changeset
|
70 && ((length (size (m)) != length (sz)) || any (size (m) != sz))) |
11472
1740012184f9
Use uppercase for variable names in error() strings to match Info documentation. Only m-files done.
Rik <octave@nomad.inbox5.com>
parents:
11149
diff
changeset
|
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)) | |
10549 | 83 rnd = ones (sz); |
7032 | 84 else |
10549 | 85 rnd = 2 ./ m .* randg(m / 2, sz); |
7032 | 86 endif |
87 if (! isinf (n)) | |
11587
c792872f8942
all script files: untabify and strip trailing whitespace
John W. Eaton <jwe@octave.org>
parents:
11523
diff
changeset
|
88 rnd = 0.5 .* n .* rnd ./ randg (n / 2, sz); |
7032 | 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 |
10525
3306cfcb856e
Replace constructs like "NaN * one()" with "NaN()" and "Inf * ones ()" with "Inf()"
David Bateman <dbateman@free.fr>
parents:
7032
diff
changeset
|
93 rnd = NaN (sz); |
5410 | 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 |