1 ## Copyright (C) 2012 Arno Onken <asnelt@asnelt.org>
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
17 ## @deftypefn {Function File} {@var{x} =} mvtrnd (@var{sigma}, @var{nu})
18 ## @deftypefnx {Function File} {@var{x} =} mvtrnd (@var{sigma}, @var{nu}, @var{n})
19 ## Generate random samples from the multivariate t-distribution.
21 ## @subheading Arguments
25 ## @var{sigma} is the matrix of correlation coefficients. If there are any
26 ## non-unit diagonal elements then @var{sigma} will be normalized.
29 ## @var{nu} is the degrees of freedom for the multivariate t-distribution.
30 ## @var{nu} must be a vector with the same number of elements as samples to be
31 ## generated or be scalar.
34 ## @var{n} is the number of rows of the matrix to be generated. @var{n} must be
35 ## a non-negative integer and corresponds to the number of samples to be
39 ## @subheading Return values
43 ## @var{x} is a matrix of random samples from the multivariate t-distribution
44 ## with @var{n} row samples.
47 ## @subheading Examples
51 ## sigma = [1, 0.5; 0.5, 1];
54 ## x = mvtrnd (sigma, nu, n);
58 ## sigma = [1, 0.5; 0.5, 1];
61 ## x = mvtrnd (sigma, nu, 2);
65 ## @subheading References
69 ## Wendy L. Martinez and Angel R. Martinez. @cite{Computational Statistics
70 ## Handbook with MATLAB}. Appendix E, pages 547-557, Chapman & Hall/CRC, 2001.
73 ## Samuel Kotz and Saralees Nadarajah. @cite{Multivariate t Distributions and
74 ## Their Applications}. Cambridge University Press, Cambridge, 2004.
78 ## Author: Arno Onken <asnelt@asnelt.org>
79 ## Description: Random samples from the multivariate t-distribution
81 function x = mvtrnd (sigma, nu, n)
88 if (! ismatrix (sigma) || any (any (sigma != sigma')) || min (eig (sigma)) <= 0)
89 error ("mvtrnd: sigma must be a positive definite matrix");
92 if (!isvector (nu) || any (nu <= 0))
93 error ("mvtrnd: nu must be a positive scalar or vector");
98 if (! isscalar (n) || n < 0 | round (n) != n)
99 error ("mvtrnd: n must be a non-negative integer")
102 nu = nu * ones (n, 1);
104 if (length (nu) != n)
105 error ("mvtrnd: n must match the length of nu")
113 if (any (diag (sigma) != 1))
114 sigma = sigma ./ sqrt (diag (sigma) * diag (sigma)');
120 y = mvnrnd (zeros (1, d), sigma, n);
121 u = repmat (chi2rnd (nu), 1, d);
122 x = y .* sqrt (repmat (nu, 1, d) ./ u);
126 %! sigma = [1, 0.5; 0.5, 1];
129 %! x = mvtrnd (sigma, nu, n);
130 %! assert (size (x), [10, 2]);
133 %! sigma = [1, 0.5; 0.5, 1];
136 %! x = mvtrnd (sigma, nu, 2);
137 %! assert (size (x), [2, 2]);