CALL RANDGEN (result, distname <, parm1> <, parm2> <, parm3> );
The RANDGEN subroutine generates random numbers from a specified distribution.
The input arguments to the RANDGEN subroutine are as follows:
is a matrix that is to be filled with random samples from the specified distribution.
is the name of the distribution.
is a distribution parameter.
is a distribution parameter.
is a distribution parameter.
The RANDGEN subroutine generates random numbers by using the same numerical method as the RAND function in Base SAS software, with the efficiency optimized for matrices. You can initialize the random number stream that is used by RANDGEN by calling the RANDSEED subroutine . The result parameter should be preallocated to a size equal to the number of values that you want to generate. If result is not initialized, then it receives a single random value.
The following statements fill a vector with 1,000 random values from a standard normal distribution:
call randseed(12345); x = j(1000,1); /* allocate (1000 x 1) vector */ call randgen(x, "Normal"); /* fill it */
Except for the "Table" and "NormalMix" distributions, the distribution parameters are usually scalar values. However, the RANDGEN subroutine also accepts vectors of parameters. If result is an matrix, then parm1, parm2, and parm3 can contain 1, n, m, or elements. The different sizes are interpreted as follows:
If the parameters are scalar quantities, each element of result is a sample value from the same distribution.
Otherwise, if the parameters contain m elements, the jth column of the result matrix consists of random values drawn from the distribution with parameters param1[j], param2[j], and param3[j].
Otherwise, if the parameters contain n elements, the ith row of the result matrix consists of random values drawn from the distribution with parameters param1[i], param2[i], and param3[i].
Otherwise, if the parameters contain elements, the (i, j) element of the result matrix contains a random value drawn from the distribution with parameters param1[s], param2[s], and param3[s], where .
All parameters must be the same length. You cannot specify a scalar for one parameter and a vector for another. If you pass in parameter vectors that do not satisfy one of the above conditions, then the first element of each parameter is used.
As an example, the jth column of the following matrix is a sample drawn from a normal population with mean j and standard deviation :
n = 5; m = 4; x = j(n,m); Mu = 1:m; Sigma = (1:m)/m; call randgen(x, "Normal", Mu, Sigma); print x;
Figure 25.303: Columns Drawn from Different Distributions
The following sections describe the distributions that are supported.
The values of x are drawn from the probability density function:
The possible values of x are . The parameter p, , is the probability of a "success." A success means that x has the value 1.
The values of x are drawn from the probability density function:
The range of x is , and a and b are required shape parameters with values and .
The values of x are drawn from the probability density function:
The range of x is . The parameter p is the success probability, with range . The parameter n specifies the number of independent trials, .
Intuitively, x is the number of successes in n Bernoulli trials with probability p.
The values of x are drawn from the probability density function:
The range of x is .
The values of x are drawn from the probability density function:
The range of x is . The parameter d represents degrees of freedom, with .
The values of x are drawn from the probability density function:
The Erlang distribution is a gamma distribution with an integer value for the shape parameter, a.
The range of x is . The parameter a is an integer shape parameter, . The optional shape parameter has the default value .
The values of x are drawn from the probability density function:
The range of x is . The optional shape parameter has the default value .
The values of x are drawn from the probability density function:
The range of x is . The two parameters n and d are degrees of freedom, with values and .
The values of x are drawn from the probability density function:
The range of x is . The parameter a is a shape parameter, . The optional shape parameter has the default value .
The values of x are drawn from the probability density function:
The range of x is . The parameter p is the success probability, with range .
Intuitively, x is the number of Bernoulli trials (with probability p) until the first success occurs.
The values of x are drawn from the probability density function:
The range of x is , where and . The parameter N is the population size, with range . The parameter R is the size of the category of interest, with range . The parameter k is the sample size, with range .
Intuitively, x is obtained by the following experiment. Put R red balls and black balls into an urn. The value x is the number of red balls in a sample of size k that is drawn from the urn without replacement.
The values of x are drawn from the probability density function:
The range of x is . The optional location parameter has the default value . The optional scale parameter has the default value .
The values of x are drawn from the probability density function:
The range of x is . The optional location parameter has the default value . The optional scale parameter has the default value .
The values of x are drawn from the probability density function:
The range of x is . The optional log-scale parameter has the default value . The optional shape parameter has the default value .
The values of x are drawn from the probability density function:
The range of x is . The parameter p is the success probability with range . The parameter k is an integer that counts the number of successes, with range .
Intuitively, x is the number of failures before the kth success during a series of Bernoulli trials with probability of success p.
The values of x are drawn from the probability density function:
The range of x is . The optional parameter () is the mean (location) parameter, which has the default value . The optional parameter is the standard deviation, with the default value .
The values of x are drawn from the probability density function:
where is the normal PDF with mean and standard deviation , and where p is a vector of probabilities such that
The parameters p, , and are vectors with n elements.
The values of x are drawn from the probability density function:
The range of x is . The shape parameter a is valid for . The optional scale parameter has the default value .
The values of x are drawn from the probability density function:
The range of x is . The parameter m is a rate parameter with range .
The values of x are drawn from the probability density function:
The range of x is . The parameter d is the degrees of freedom, with the range .
The values of x are drawn from the probability density function:
where p is a vector of probabilities, such that , and n is the largest integer such that and
Notice that if , then the values of x are in the range .
The values of x are drawn from the probability density function:
The range of x is . The parameter h is the horizontal location of the peak of the triangle, with range .
Tweedie distributions have three parameters: is the power parameter, is the mean of the distribution, and is a scale parameter. The default values for the optional parameters are and . The Tweedie distribution has the property that the variance of the distribution is equal to .
The range of x is . The density function is given by
where for and for . The function does not have an analytical expression, but is typically represented by an infinite series.
For most modeling tasks, . For p in this range, the Tweedie distribution is a sum of N gamma random variables, where N is Poisson distributed. For details, see the documentation for the SEVERITY procedure in the SAS/ETS User's Guide. The documentation for the PDF function in SAS Language Reference: Dictionary is also relevant.
The values of x are drawn from the probability density function:
The range of x is . The parameters a and b default to the values and . You must specify values for both a and b if you do not want to use the default values.
The values of x are drawn from the probability density function:
The range of x is . The parameter is a shape parameter. The optional parameter has the default value .
Notice that many references, including the MCMC procedure, list as the first parameter for the inverse Gaussian distribution. However, the parameter is listed last for the RAND, PDF, CDF, and QUANTILE functions because it an optional parameter.
The values of x are drawn from the probability density function:
The range of x is . The shape parameters a and b are have values and .
Table 25.5 describes how parameters of the RANDGEN call correspond to the distribution parameters.
Table 25.5: Parameter Assignments for Distributions
Distribution |
distname |
parm1 |
parm2 |
parm3 |
---|---|---|---|---|
Bernoulli |
'BERNOULLI' |
p |
||
Beta |
'BETA' |
a |
b |
|
Binomial |
'BINOMIAL' |
p |
n |
|
Cauchy |
'CAUCHY' |
|||
Chi-Square |
'CHISQUARE' |
d |
||
Erlang |
'ERLANG' |
a |
<> |
|
Exponential |
'EXPONENTIAL' |
<> |
||
|
'F' |
n |
d |
|
Gamma |
'GAMMA' |
a |
<> |
|
Geometric |
'GEOMETRIC' |
p |
||
Hypergeometric |
'HYPERGEOMETRIC' |
N |
R |
n |
Laplace |
'LAPLACE' |
<> |
<> |
|
Logistic |
'LOGISTIC' |
<> |
<> |
|
Lognormal |
'LOGNORMAL' |
<> |
<> |
|
Negative Binomial |
'NEGBINOMIAL' |
p |
k |
|
Normal |
'NORMAL' |
<> |
<> |
|
Normal Mixture |
'NORMALMIX' |
p |
|
|
Pareto |
'PARETO' |
a |
<> |
|
Poisson |
'POISSON' |
m |
||
t |
'T' |
d |
||
Table |
'TABLE' |
p |
||
Triangle |
'TRIANGLE' |
h |
||
Tweedie |
'TWEEDIE' |
p |
<> |
<> |
Uniform |
'UNIFORM' |
<> |
<> |
|
Wald |
'WALD' or 'IGAUSS' |
|
<> |
|
Weibull |
'WEIBULL' |
a |
b |
The distname argument can be in lowercase or uppercase, and you need to specify only enough letters to distinguish one distribution from the others, as shown by the following statements:
/* generate 10 samples from a Bernoulli distribution */ r = j(10, 1, .); /* allocate room for samples */ call randgen(r, "ber", 0.5);
Optional arguments are enclosed in angle brackets, along with the default value when the argument is not specified. For example, if you do not supply values for the parameters of the normal distribution, the default values of and are used.
The following example illustrates the RANDGEN call for various distributions:
call randseed(12345); /* get four random observations from each distribution */ x = j(1, 4, .); /* each row comes from a different distribution */ DiscreteDist = {'BERN','BINOM','GEOM','HYPER', 'NEGB','POISSON','TABLE'}; D = j(nrow(DiscreteDist), 4, .); i = 1; call randgen(x, 'BERN', 0.75); D[i, ] = x; i = i+1; call randgen(x, 'BINOM', 0.75, 10); D[i, ] = x; i = i+1; call randgen(x, 'GEOM', 0.02); D[i, ] = x; i = i+1; call randgen(x, 'HYPER', 10, 3, 5); D[i, ] = x; i = i+1; call randgen(x, 'NEGB', 0.8, 5); D[i, ] = x; i = i+1; call randgen(x, 'POISSON', 6.1); D[i, ] = x; i = i+1; p = {0.2 0.5 0.3}; call randgen(x, 'TABLE', p); D[i, ] = x; i = i+1; print D[rowname=DiscreteDist label="Discrete"]; ContinDist = {'BETA','CAUCHY','CHISQ','ERLANG','EXPO', 'F','GAMMA','LAPLACE','LOGISTIC','LOGN', 'NORMAL','NORMALMIX','PARETO','T', 'TRIANGLE','TWEEDIE','UNIFORM','WALD','WEIB'}; C = j(nrow(ContinDist), 4, .); i = 1; call randgen(x, 'BETA', 3, 0.1); C[i, ] = x; i = i+1; call randgen(x, 'CAUCHY'); C[i, ] = x; i = i+1; call randgen(x, 'CHISQ', 22); C[i, ] = x; i = i+1; call randgen(x, 'ERLANG', 7); C[i, ] = x; i = i+1; call randgen(x, 'EXPO'); C[i, ] = x; i = i+1; call randgen(x, 'F', 12, 322); C[i, ] = x; i = i+1; call randgen(x, 'GAMMA', 7.25); C[i, ] = x; i = i+1; call randgen(x, 'LAPLACE'); C[i, ] = x; i = i+1; call randgen(x, 'LOGISTIC'); C[i, ] = x; i = i+1; call randgen(x, 'LOGN'); C[i, ] = x; i = i+1; call randgen(x, 'NORMAL'); C[i, ] = x; i = i+1; p = {0.2 0.5 0.3}; mu = {0 5 10}; sig = {1 1 2}; call randgen(x, 'NORMALMIX',p,mu,sig); C[i,] = x; i = i+1; call randgen(x, 'PARETO', 3, 1); C[i, ] = x; i = i+1; call randgen(x, 'T', 4); C[i, ] = x; i = i+1; call randgen(x, 'TRIANGLE', 0.7); C[i, ] = x; i = i+1; call randgen(x, 'TWEEDIE', 1.7); C[i, ] = x; i = i+1; call randgen(x, 'UNIFORM'); C[i, ] = x; i = i+1; call randgen(x, 'WALD', 1, 2); C[i, ] = x; i = i+1; call randgen(x, 'WEIB', 0.25, 2.1); C[i, ] = x; i = i+1; print C[rowname=ContinDist label="Continuous"];
Figure 25.304: Random Numbers from Various Distributions
Continuous | ||||
---|---|---|---|---|
BETA | 0.9698912 | 0.9986741 | 0.9530356 | 0.9999999 |
CAUCHY | -0.351223 | -79.19193 | -0.875086 | 0.2633447 |
CHISQ | 16.501429 | 10.905074 | 21.223624 | 15.693628 |
ERLANG | 3.9509215 | 3.9110053 | 12.242025 | 4.2987446 |
EXPO | 0.1435695 | 0.6908117 | 0.2160011 | 1.41259 |
F | 0.5212328 | 0.7306928 | 1.0089965 | 0.9442868 |
GAMMA | 6.6019823 | 11.56066 | 10.237334 | 2.6774555 |
LAPLACE | -0.084906 | 2.9727044 | 2.7944056 | -1.302167 |
LOGISTIC | 0.1334806 | -1.613977 | -0.528595 | -0.418451 |
LOGN | 1.2039346 | 1.5589409 | 0.2231522 | 0.1560639 |
NORMAL | 1.2507254 | -0.779791 | -1.716859 | 0.091384 |
NORMALMIX | 1.5133453 | 3.1300929 | 4.4290679 | 5.3063411 |
PARETO | 1.2940105 | 1.0310942 | 1.4971162 | 1.2676456 |
T | 0.2666685 | 0.2312119 | -0.047974 | -0.069328 |
TRIANGLE | 0.3098931 | 0.3216791 | 0.7828233 | 0.6975677 |
TWEEDIE | 0.0256424 | 1.7446859 | 2.8313134 | 0.6429287 |
UNIFORM | 0.9101531 | 0.4957422 | 0.6919957 | 0.7501369 |
WALD | 0.3298129 | 2.4390822 | 0.3872 | 1.6025807 |
WEIB | 0.000166 | 62.455757 | 17.343105 | 0.0000656 |