The IMSL_RAND_GEN_DISCR function generates pseudorandom numbers from a general discrete distribution using an alias method or optionally a table lookup method.

This routine requires an IDL Advanced Math and Stats license. For more information, contact your sales or technical support representative.

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.

Examples


Example 1

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

Example 2

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.

.RUN
FUNCTION prf, ix
  RETURN, IMSL_BINOMIALPDF(ix, 20, .5)
END
 
imin = 0
nmass = 21
IMSL_RANDOMOPT, Set_seed = 123457
cumpr = IMSL_DISCR_TABLE('prf', 0.00001, 12, imin, nmass)
r = IMSL_RAND_GEN_DISCR(n, imin, nmass, cumpr, /TABLE)
PM, r
 
14
9
12
10
12

Syntax


Result = IMSL_RAND_GEN_DISCR(N, Imin, Nmass, Probs [, /DOUBLE] [, /TABLE])

Return Value


Integer array of length n containing the random discrete deviates.

Arguments


imin

Smallest value the random deviate can assume. This is the value corresponding to the probability in Probs(0).

N

Number of random numbers to generate.

Nmass

Number of mass points in the discrete distribution.

Probs

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.)

Keywords


DOUBLE (optional)

If present and nonzero, double precision is used.

TABLE (optional)

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.

Version History


6.4

Introduced

See Also


IMSL_DISCR_TABLE, IMSL_RANDOM, Overview of Random Number Generation