B.12 Random-number generators
Random-numbers are generated using a common general seed number. The seed is set to 42 every time BayES starts and is advanced appropriately every time a random-number generator is called. The seed can be set at any point in a script file using the statement:
Note that when a model function is called, another seed is used, which is internal to the particular model and does not advance the general seed. This allows reproducibility of results irrespective of how many random numbers, if any, have been generated in statements before the call to the model function. The model’s internal seed is also set to 42 and this value can be altered by passing an optional argument of the form:
B.12.1 Univariate distributions
The generic syntax for calling a random-number generator for a univariate distribution is:
where distributionName is the name of a distribution for which BayES provides random numbers (see table below), and <parameters> is a list of the distribution’s parameters, as described in section B.11. These parameters must be separated by commas if more than one parameter needs to be supplied. The size of the return matrix, W, depends on the size of the parameters passed as arguments to the function and on whether the optional arguments, M and N, are supplied:
- if the distribution’s parameters are scalars and no optional arguments are supplied then W is an matrix which contains a random draw from the respective distribution.
- if the distribution’s parameters are scalars then M and N can be used to request multiple random numbers. W in this case will be an matrix which contains random draws from the distribution. If M is supplied then N must be supplied as well.
- if the distribution’s parameters are matrices and their dimensions match (if more than one parameter needs to be supplied) then W will have the same size as the parameters and it will contain random draws from the distribution, generated by combining parameters in an element-wise fashion. Since the dimensions of W are determined by the dimensions of the distribution’s parameters, M and N must not be supplied.
- for distributions which require specifying more than one parameter, if one of the parameters supplied is a scalar and another a matrix, then the scalar parameter is expanded to match the dimensions of the matrix parameter. In this case W has the same size as the matrix parameter. Again, M and N must not be supplied because the dimensions of W are determined by the dimensions of the distribution’s parameters.
The following two tables document the random-number generating functions for continuous and discrete random variables, respectively.
Syntax | Arguments and performed function |
W = betarnd(alpha, beta , M, N ); | W is a matrix of random numbers from a beta distribution with shape parameters alpha and beta.
see also betapdf, betacdf |
W = chi2rnd(p , M, N ); | W is a matrix of random numbers from a chi-squared distribution with p degrees of freedom.
see also chi2pdf, chi2cdf |
W = exprnd(lambda , M, N ); | W is a matrix of random numbers from an exponential distribution with rate parameter lambda.
see also exppdf, expcdf |
W = evrnd(mu, sigma , M, N ); | W is a matrix of random numbers from a type-I extreme-value distribution with location parameter mu and scale parameter sigma
see also evpdf, evcdf |
W = frnd(p1, p2 , M, N ); | W is a matrix of random numbers from Fisher’s F distribution with numerator degrees of freedom p1 and denominator degrees of freedom p2.
see also fpdf, fcdf |
W = gamrnd(alpha, beta , M, N ); | W is a matrix of random numbers from a gamma distribution with shape parameter alpha and rate parameter beta.
see also gampdf, gamcdf |
W = logisticrnd(mu, s , M, N ); | W is a matrix of random numbers from a logistic distribution with mean mu and scale parameter s (variance equal to ).
see also logisticpdf, logisticcdf |
W = logitnrnd(mu, sigma , M, N ); | W is a matrix of random numbers from a logit-normal distribution with location parameter mu and scale parameter sigma.
see also logitnpdf, logitncdf |
W = lognrnd(mu, sigma , M, N ); | W is a matrix of random numbers from a log-normal distribution with location parameter mu and scale parameter sigma.
see also lognpdf, logncdf |
W = normrnd(mu, sigma , M, N ); | W is a matrix of random numbers from a normal distribution with mean mu and standard deviation sigma.
see also normpdf, normcdf |
W = trnd(p , M, N ); | W is a matrix of random numbers from a t distribution with p degrees of freedom.
see also tpdf, tcdf |
W = truncnrnd(mu, sigma , M, N ); | W is a matrix of random numbers from a normal distribution with location parameter mu and scale parameter sigma, truncated from below at zero.
see also truncnpdf, truncncdf |
W = unifrnd( M, N ); | W is a matrix of random numbers from a uniform distribution on the interval .
|
W = wblrnd(alpha, beta , M, N ); | W is a matrix of random numbers from a Weibull distribution with shape parameter alpha and scale parameter beta.
see also wblpdf, wblcdf |
Syntax | Arguments and performed function |
W = catrnd(p , M, N ); | W is a matrix of random numbers from a categorical distribution with probability vector specified in each row of p. The number of outcomes is deduced from the number of columns of p and numbering starts at 0. That is, if p is a vector, the possible values of the random variable are and the probability of obtaining outcome is equal to the element of p.
|
W = binomrnd(n, p , M, N ); | W is a matrix of random numbers from a binomial distribution with n trials and probability of success in each trial p.
Note that the function allows for non-integer values of n by replacing the factorials
in the expressions for the probability mass and cumulative density functions by the
Gamma function. |
W = nbinomrnd(n, p , M, N ); | W is a matrix of random numbers from a negative binomial distribution with n failures before stopping and probability of success in each trial p.
Note that the function allows for non-integer values of n by replacing the factorials
in the expressions for the probability mass and cumulative density functions by the
Gamma function. |
W = poissonrnd(lambda , M, N ); | W is a matrix of random numbers from a Poisson distribution with rate parameter lambda.
see also poissonpdf, poissoncdf |
B.12.2 Multivariate distributions
The following table describes statements that are used to generate random numbers from multivariate distributions. The general format of these statements is similar to random-number generators for univariate distributions, but, because multivariate random-number generators return vectors or matrices, the optional dimension arguments do not have the usual meaning: these generators can generate or one random draw per call.
Syntax | Mathematical expression | Arguments and return values |
W = mvnrnd(mu, V , M ); | | W is an matrix of random numbers from a -variate normal distribution with mean mu (either or ) and variance matrix V ().
|
W = wishrnd(V, p); | | W is a matrix of random numbers from a -dimensional Wishart distribution with scale matrix V () and p degrees of freedom.
|
W = iwishrnd(V, p); | | W is a matrix of random numbers from a -dimensional inverse-Wishart distribution with scale matrix V () and p degrees of freedom.
|
W = drchrnd(alpha , M ); | | W is an matrix of random numbers from a -dimensional Dirichlet distribution with concentration parameters specified in each row of alpha. The dimension of the distribution, , is inferred by the number of columns of alpha.
|