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} {} chi2rnd (@var{n}, @var{r}, @var{c}) |
|
21 ## @deftypefnx {Function File} {} chi2rnd (@var{n}, @var{sz}) |
5410
|
22 ## Return an @var{r} by @var{c} or a @code{size (@var{sz})} matrix of |
|
23 ## random samples from the chisquare distribution with @var{n} degrees |
|
24 ## of freedom. @var{n} must be a scalar or of size @var{r} by @var{c}. |
|
25 ## |
|
26 ## If @var{r} and @var{c} are omitted, the size of the result matrix is |
|
27 ## the size of @var{n}. |
|
28 ## @end deftypefn |
|
29 |
5428
|
30 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
5410
|
31 ## Description: Random deviates from the chi-square distribution |
|
32 |
5411
|
33 function rnd = chi2rnd (n, r, c) |
5410
|
34 |
|
35 if (nargin == 3) |
|
36 if (! (isscalar (r) && (r > 0) && (r == round (r)))) |
5411
|
37 error ("chi2rnd: r must be a positive integer"); |
5410
|
38 endif |
|
39 if (! (isscalar (c) && (c > 0) && (c == round (c)))) |
5411
|
40 error ("chi2rnd: c must be a positive integer"); |
5410
|
41 endif |
|
42 sz = [r, c]; |
|
43 |
|
44 if (any (size (n) != 1) |
|
45 && (length (size (n)) != length (sz) || any (size (n) != sz))) |
5411
|
46 error ("chi2rnd: n must be scalar or of size [r, c]"); |
5410
|
47 endif |
|
48 elseif (nargin == 2) |
|
49 if (isscalar (r) && (r > 0)) |
|
50 sz = [r, r]; |
|
51 elseif (isvector(r) && all (r > 0)) |
|
52 sz = r(:)'; |
|
53 else |
7007
|
54 error ("chi2rnd: r must be a positive integer or vector"); |
5410
|
55 endif |
|
56 |
|
57 if (any (size (n) != 1) |
|
58 && (length (size (n)) != length (sz) || any (size (n) != sz))) |
5411
|
59 error ("chi2rnd: n must be scalar or of size sz"); |
5410
|
60 endif |
|
61 elseif (nargin == 1) |
|
62 sz = size(n); |
|
63 else |
6046
|
64 print_usage (); |
5410
|
65 endif |
|
66 |
|
67 if (isscalar (n)) |
|
68 if (find (!(n > 0) | !(n < Inf))) |
|
69 rnd = NaN * ones (sz); |
|
70 else |
6339
|
71 rnd = 2 * randg(n/2, sz) |
5410
|
72 endif |
|
73 else |
|
74 [retval, n, dummy] = common_size (n, ones (sz)); |
|
75 if (retval > 0) |
5411
|
76 error ("chi2rnd: a and b must be of common size or scalar"); |
5410
|
77 endif |
|
78 |
|
79 rnd = zeros (sz); |
|
80 k = find (!(n > 0) | !(n < Inf)); |
|
81 if (any (k)) |
|
82 rnd(k) = NaN; |
|
83 endif |
|
84 |
|
85 k = find ((n > 0) & (n < Inf)); |
|
86 if (any (k)) |
6339
|
87 rnd(k) = 2 * randg(n(k)/2, size(k)) |
5410
|
88 endif |
|
89 endif |
|
90 |
|
91 endfunction |