|
IDL Analyst Reference Guide: Random Number Generation |
|
The IMSL_RAND_GEN_DISCR function generates pseudorandom numbers from a general discrete distribution using an alias method or optionally a table lookup method.
| Note This routine requires an IDL Analyst license. For more information, contact your ITT Visual Information Solutions sales or technical support representative. |
Result = IMSL_RAND_GEN_DISCR(n, imin, nmass, probs [, /DOUBLE]
[, /TABLE] )
Integer array of length n containing the random discrete deviates.
Smallest value the random deviate can assume. This is the value corresponding to the probability in probs(0).
Number of mass points in the discrete distribution.
Number of random numbers to generate.
Array of length nmass containing probabilities associated with the individual mass points. The elements of probs must be nonnegative and must sum to 1.0.
If the keyword Table is used, then probs is a vector of length at least nmass + 1 containing in the first nmass positions the cumulative probabilities and, possibly, indexes to speed access to the probabilities. IMSL_DISCR_TABLE can be used to initialize probs properly. If no elements of probs are used as indexes, probs (nmass) is 0.0 on input. The value in probs(0) is the probability of imin. The value in probs (nmass – 1) must be exactly 1.0 (since this is the CDF at the upper range of the distribution.)
If present and nonzero, double precision is used.
If present and nonzero, generate pseudorandom numbers from a general discrete distribution using a table lookup method. If this keyword is used, then probs is a vector of length at least nmass + 1 containing in the first nmass positions the cumulative probabilities and, possibly, indexes to speed access to the probabilities. IMSL_DISCR_TABLE can be used to initialize probs properly.
IMSL_RAND_GEN_DISCR generates pseudorandom numbers from a discrete distribution with probability function given in the vector probs; that is:
Pr(X = i) = pj
for i = imin, imin + 1, ..., imin + nm – 1
where:
j = i – imin + 1, pj = probs(j), imin = imin, and nm = nmass
The algorithm is the alias method, due to Walker (1974), with modifications suggested by Kronmal and Peterson (1979).
If the keyword Table is used, IMSL_RAND_GEN_DISCR generates pseudorandom deviates from a discrete distribution, using the table probs, which contains the cumulative probabilities of the distribution and, possibly, indexes to speed the search of the table. IMSL_DISCR_TABLE can be used to set up the table probs. IMSL_RAND_GEN_DISCR uses the inverse CDF method to generate the variates.
In this example, IMSL_RAND_GEN_DISCR is used to generate five pseudorandom variates from the discrete distribution:
Pr(X = 1) = 0.05
Pr(X = 2) = 0.45
Pr(X = 3) = 0.31
Pr(X = 4) = 0.04
Pr(X = 5) = 0.15
probs = [0.05, 0.45, 0.31, 0.04, 0.15] n = 5 imin = 1 nmass = 5 IMSL_RANDOMOPT, Set_seed = 123457 r = IMSL_RAND_GEN_DISCR(n, imin, nmass, probs) PM, r 3 2 2 3 5
In this example, the IMSL_DISCR_TABLE is used to set up a table and then IMSL_RAND_GEN_DISCR is used to generate five pseudorandom variates from the binomial distribution with parameters 20 and 0.5.