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