Stata 15 help for mf_runiform

[M-5] runiform() -- Uniform and nonuniform pseudorandom variates

Syntax

real matrix runiform(real scalar r, real scalar c)

real matrix runiform(real scalar r, real scalar c, real matrix a, real matrix b)

real matrix runiformint(real scalar r, real scalar c, real matrix a, real matrix b)

string scalar rseed()

void rseed(real scalar newseed)

string scalar rngstate()

void rngstate(string scalar newstate)

real matrix rbeta(real scalar r, real scalar c, real matrix a, real matrix b)

real matrix rbinomial(real scalar r, real scalar c, real matrix n, real matrix p)

real matrix rcauchy(real scalar r, real scalar c, real matrix a, real matrix b)

real matrix rchi2(real scalar r, real scalar c, real matrix df)

real matrix rdiscrete(real scalar r, real scalar c, real colvector p)

real matrix rexponential(real scalar r, real scalar c, real matrix b)

real matrix rgamma(real scalar r, real scalar c, real matrix a, real matrix b)

real matrix rhypergeometric(real scalar r, real scalar c, real matrix N, real matrix K, real matrix n)

real matrix rigaussian(real scalar r, real scalar c, real matrix m, real matrix a)

real matrix rlaplace(real scalar r, real scalar c, real matrix m, real matrix b)

real matrix rlogistic(real scalar r, real scalar c)

real matrix rlogistic(real scalar r, real scalar c, real matrix s)

real matrix rlogistic(real scalar r, real scalar c, real matrix m, real matrix s)

real matrix rnbinomial(real scalar r, real scalar c, real matrix n, real matrix p)

real matrix rnormal(real scalar r, real scalar c, real matrix m, real matrix s)

real matrix rpoisson(real scalar r, real scalar c, real matrix m)

real matrix rt(real scalar r, real scalar c, real matrix df)

real matrix rweibull(real scalar r, real scalar c, real matrix a, real matrix b)

real matrix rweibull(real scalar r, real scalar c, real matrix a, real matrix b, real matrix g)

real matrix rweibullph(real scalar r, real scalar c, real matrix a, real matrix b)

real matrix rweibullph(real scalar r, real scalar c, real matrix a, real matrix b, real matrix g)

Description

runiform(r, c) returns an r x c real matrix containing uniformly distributed random variates over (0,1). runiform() is the same function as Stata's runiform() function.

runiform(r, c, a, b) returns an ir x jc real matrix containing uniformly distributed random variates over (a,b). The matrices a and b must be r-conformable, where i = max(rows(a),rows(b)) and j = max(cols(a),cols(b)).

runiformint(r, c, a, b) returns an ir x jc real matrix containing uniformly distributed random integer variates over [a,b]. The matrices a and b must be r-conformable, where i = max(rows(a),rows(b)) and j = max(cols(a),cols(b)).

rseed() returns the current random-variate seed in an encrypted string form.

rseed(newseed) sets the seed: an integer can be specified. rseed(newseed) has the same effect as Stata's set seed command; see [R] set seed.

rngstate() returns the current state of the random-number generator. rngstate() returns the same thing as Stata's c(rngstate); see [R] set seed and [P] creturn.

rngstate(newstate) sets the state of the random-number generator using a string previously obtained from rngstate(). rngstate(newstate) has the same effect as Stata's set rngstate newstate; see [R] set seed.

rbeta(r, c, a, b) returns an ir x jc real matrix containing beta random variates. The real-valued matrices a and b contain the beta shape parameters. The matrices a and b must be r-conformable, where i = max(rows(a),rows(b)) and j = max(cols(a),cols(b)).

rbinomial(r, c, n, p) returns an ir x jc real matrix containing binomial random variates. The real-valued matrices n and p contain the number of trials and the probability parameters, respectively. The matrices n and p must be r-conformable, where i = max(rows(n),rows(p)) and j = max(cols(n),cols(p)).

rcauchy(r, c, a, b) returns an ir x jc real matrix containing Cauchy random variates. The real-valued matrices a and b contain the Cauchy location and scale parameters, respectively. The matrices a and b must be r-conformable, where i = max(rows(a),rows(b)) and j = max(cols(a),cols(b)).

rchi2(r, c, df) returns an ir x jc real matrix containing chi-squared random variates. The real-valued matrix df contains the degrees of freedom parameters, where i = rows(df) and j = cols(df).

rdiscrete(r, c, p) returns an r x c real matrix containing random variates from the discrete distribution specified by the probabilities in the vector p of length k. The range of the discrete variates is 1, 2, ..., k, where 2 <= k <= 10000. The alias method of Walker (1977) is used to sample from the discrete distribution.

rexponential(r, c, b) returns an ir x jc real matrix containing exponential random variates. The real-valued matrix b contains the scale parameters, where i = rows(b) and j = cols(b).

rgamma(r, c, a, b) returns an ir x jc real matrix containing gamma random variates. The real-valued matrices a and b contain the gamma shape and scale parameters, respectively. The matrices a and b must be r-conformable, where i = max(rows(a),rows(b)) and j = max(cols(a),cols(b)).

rhypergeometric(r, c, N, K, n) returns an ir x jc real matrix containing hypergeometric random variates. The integer-valued matrix N contains the population sizes, the integer-valued matrix K contains the number of elements in each population that have the attribute of interest, and the integer-valued matrix n contains the sample size. The matrices N, K, and n must be r-conformable, where i = max(rows(N),rows(K),rows(n)) and j = max(cols(N),cols(K),cols(n)).

rigaussian(r, c, m, a) returns an ir x jc real matrix containing inverse Gaussian random variates. The real-valued matrices m and a contain the mean and shape parameters, respectively. The matrices m and a must be r-conformable, where i = max(rows(m),rows(a)) and j = max(cols(m),cols(a)).

rlaplace(r, c, m, b) returns an ir x jc real matrix containing Laplace random variates. The real-valued matrices m and b contain the mean and scale parameters, respectively. The matrices m and b must be r-conformable, where i = max(rows(m),rows(b)) and j = max(cols(m),cols(b)).

rlogistic(r, c) returns an r x c real matrix containing logistic random variates with mean zero and standard deviation pi/sqrt(3).

rlogistic(r, c, s) returns an ir x c real matrix containing mean-zero logistic random variates. The real-valued matrix s contains scale parameters, where i = rows(s) and j = cols(s).

rlogistic(r, c, m, s) returns an ir x jc real matrix containing logistic random variates. The real-valued matrices m and s contain the mean and scale parameters, respectively. The matrices m and s must be r-conformable, where i = max(rows(m),rows(s)) and j = max(cols(m),cols(s)).

rnbinomial(r, c, n, p) returns an ir x jc real matrix containing negative binomial random variates. When the elements of the matrix n are integer-valued, rnbinomial() returns the number of failures before the nth success, where the probability of success on a single draw is contained in the real-valued matrix p. The elements of n can also be nonintegral but must be positive. The matrices n and p must be r-conformable, where i = max(rows(n),rows(p)) and j = max(cols(n),cols(p)).

rnormal(r, c, m, s) returns an ir x jc real matrix containing normal (Gaussian) random variates. The real-valued matrices m and s contain the mean and standard deviation parameters, respectively. The matrices m and s must be r-conformable, where i = max(rows(m),rows(s)) and j = max(cols(m),cols(s)).

rpoisson(r, c, m) returns an ir x jc real matrix containing Poisson random variates. The real-valued matrix m contains the Poisson mean parameters, where i = rows(m) and j = cols(m).

rt(r, c, df) returns an ir x jc real matrix containing Student's t random variates. The real-valued matrix df contains the degrees-of-freedom parameters, where i = rows(df) and j = cols(df).

rweibull(r, c, a, b) returns an ir x jc real matrix containing Weibull random variates. The real-valued matrices a and b contain the shape and scale parameters, respectively. The matrices a and b must be r-conformable, where i = max(rows(a),rows(b)) and j = max(cols(a),cols(b)).

rweibull(r, c, a, b, g) returns an ir x jc real matrix containing Weibull random variates. The real-valued matrices a, b, and g contain the shape, scale, and location parameters, respectively. The matrices a, b, and g must be r-conformable, where i = max(rows(a),rows(b),rows(g)) and j = max(cols(a),cols(b),cols(g)).

rweibullph(r, c, a, b) returns an ir x jc real matrix containing Weibull (proportional hazards) random variates. The real-valued matrices a and b contain the shape and scale parameters, respectively. The matrices a and b must be r-conformable, where i = max(rows(a),rows(b)) and j = max(cols(a),cols(b)).

rweibullph(r, c, a, b, g) returns an ir x jc real matrix containing Weibull (proportional hazards) random variates. The real-valued matrices a, b, and g contain the shape, scale, and location parameters, respectively. The matrices a, b, and g must be r-conformable, where i = max(rows(a),rows(b),rows(g)) and j = max(cols(a),cols(b),cols(g)).

Remarks

The functions described here generate random variates. The parameter limits for each generator are the same as those documented for Stata's random-number functions, except for rdiscrete(), which has no Stata equivalent.

In the example below, we generate and summarize 1,000 random normal deviates with a mean of 3 and standard deviation of 1.

: rseed(13579) : x = rnormal(1000, 1, 3, 1) : meanvariance(x) 1 +---------------+ 1 | 2.99162691 | 2 | 1.056033182 | +---------------+

The next example uses a 1 x 3 vector of gamma shape parameters to generate a 1000 x 3 matrix of gamma random variates, X.

: a = (0.5, 1.5, 2.5) : rseed(13579) : X = rgamma(1000, 1, a, 1) : mean(X) 1 2 3 +-------------------------------------------+ 1 | .5022154504 1.502187839 2.417570905 | +-------------------------------------------+

: diagonal(variance(X))' 1 2 3 +-------------------------------------------+ 1 | .5082196561 1.434504411 2.512575559 | +-------------------------------------------+

The first column of X contains gamma variates with shape parameter 0.5, the second column contains gamma variates with shape parameter 1.5, and the third column contains gamma variates with shape parameter 2.5.

Below we generate a 4 x 3 matrix of beta variates where we demonstrate the use of two r-conformable parameter matrices, a and b.

: a = (0.5, 1.5, 2.5) : b = (0.5, 0.75, 1.0 \ 1.25, 1.5, 1.75) : rseed(13579) : rbeta(2, 1, a, b) 1 2 3 +-------------------------------------------+ 1 | .8389820448 .9707672865 .2122592494 | 2 | .5997013245 .6617211509 .8775212495 | 3 | .9552933701 .1133821372 .8006242906 | 4 | .2279075363 .4298247049 .6683477165 | +-------------------------------------------+

The 4 x 3 shape-parameter matrices used to generate these beta variates are given below:

: J(2, 1, J(rows(b), 1, a)) 1 2 3 +-------------------+ 1 | .5 1.5 2.5 | 2 | .5 1.5 2.5 | 3 | .5 1.5 2.5 | 4 | .5 1.5 2.5 | +-------------------+

: J(2, 1, b) 1 2 3 +----------------------+ 1 | .5 .75 1 | 2 | 1.25 1.5 1.75 | 3 | .5 .75 1 | 4 | 1.25 1.5 1.75 | +----------------------+

This example illustrates how to restart a random-number generator from a particular point in its sequence. We begin by setting the seed and drawing some uniform variates.

: rseed(12345) : x = runiform(1,3) : x 1 2 3 +-------------------------------------------+ 1 | .3576297229 .400442617 .689383317 | +-------------------------------------------+

We save off the current state of the random-number generator, so that we can subsequently return to this point in the sequence.

: rngstate = rngstate()

Having saved off the state, we draw some more numbers from the sequence.

: x = runiform(1,3) : x 1 2 3 +-------------------------------------------+ 1 | .5597355706 .574451294 .2076905269 | +-------------------------------------------+

Now we restore the state of the random-number generator to where it was and obtain the same numbers from the sequence.

: rngstate(rngstate) : x = runiform(1,3) : x 1 2 3 +-------------------------------------------+ 1 | .5597355706 .574451294 .2076905269 | +-------------------------------------------+

Conformability

runiform(r, c): r: 1 x 1 c: 1 x 1 result: r x c

runiform(r, c, a, b): r: 1 x 1 c: 1 x 1 a: 1 x 1 or i x 1 or 1 x j or i x j b: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

runiformint(r, c, a, b): r: 1 x 1 c: 1 x 1 a: 1 x 1 or i x 1 or 1 x j or i x j b: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rseed(): result: 1 x 1

rseed(newseed): newseed: 1 x 1 result: void

rngstate(): result: 1 x 1

rngstate(newstate): newseed: 1 x 1 result: void

rbeta(r, c, a, b): r: 1 x 1 c: 1 x 1 a: 1 x 1 or i x 1 or 1 x j or i x j b: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rbinomial(r, c, n, p): r: 1 x 1 c: 1 x 1 n: 1 x 1 or i x 1 or 1 x j or i x j p: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rcauchy(r, c, a, b): r: 1 x 1 c: 1 x 1 a: 1 x 1 or i x 1 or 1 x j or i x j b: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rchi2(r, c, df): r: 1 x 1 c: 1 x 1 df: i x j result: ir x jc

rdiscrete(r, c, p): r: 1 x 1 c: 1 x 1 p: k x 1 result: r x c

rexponential(r, c, b): r: 1 x 1 c: 1 x 1 b: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rgamma(r, c, a, b): r: 1 x 1 c: 1 x 1 a: 1 x 1 or i x 1 or 1 x j or i x j b: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rhypergeometric(r, c, N, K, n): r: 1 x 1 c: 1 x 1 N: 1 x 1 K: 1 x 1 or i x 1 or 1 x j or i x j n: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rigaussian(r, c, m, a): r: 1 x 1 c: 1 x 1 m: 1 x 1 or i x 1 or 1 x j or i x j a: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rlaplace(r, c, m, b): r: 1 x 1 c: 1 x 1 m: 1 x 1 or i x 1 or 1 x j or i x j b: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rlogistic(r, c): r: 1 x 1 c: 1 x 1 result: r x c

rlogistic(r, c, s): r: 1 x 1 c: 1 x 1 s: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rlogistic(r, c, m, s): r: 1 x 1 c: 1 x 1 m: 1 x 1 or i x 1 or 1 x j or i x j s: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rnbinomial(r, c, n, p): r: 1 x 1 c: 1 x 1 n: 1 x 1 or i x 1 or 1 x j or i x j p: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rnormal(r, c, m, s): r: 1 x 1 c: 1 x 1 m: 1 x 1 or i x 1 or 1 x j or i x j s: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rpoisson(r, c, m): r: 1 x 1 c: 1 x 1 m: i x j result: ir x jc

rt(r, c, df): r: 1 x 1 c: 1 x 1 df: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rweibull(r, c, a, b): r: 1 x 1 c: 1 x 1 a: 1 x 1 or i x 1 or 1 x j or i x j b: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rweibull(r, c, a, b, g): r: 1 x 1 c: 1 x 1 a: 1 x 1 or i x 1 or 1 x j or i x j b: 1 x 1 or i x 1 or 1 x j or i x j g: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rweibullph(r, c, a, b): r: 1 x 1 c: 1 x 1 a: 1 x 1 or i x 1 or 1 x j or i x j b: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

rweibullph(r, c, a, b, g): r: 1 x 1 c: 1 x 1 a: 1 x 1 or i x 1 or 1 x j or i x j b: 1 x 1 or i x 1 or 1 x j or i x j g: 1 x 1 or i x 1 or 1 x j or i x j result: r x c or ir x c or r x jc or ir x jc

Diagnostics

All random-variate generators abort with error if r<0 or c<0.

rseed(seed) aborts with error if a string seed is specified and it is malformed.

rngstate(newstate) aborts with error if the specified newstate is malformed, which almost certainly is the case if newstate was not previously obtained from rngstate().

runiform(r, c, a, b), runiformint(r, c, a, b), rnormal(r, c, m, s), rbeta(r, c, a, b), rbinomial(r, c, n, p), rcauchy(r, c, a, b), rgamma(r, c, a, b), rhypergeometric(r, c, N, K, n), rigaussian(r, c, m, a), rlaplace(r, c, m, b), rlogistic(r, c, m, s), rnbinomial(r, c, n, p), rweibull(r, c, a, b), rweibull(r, c, a, b, g), rweibullph(r, c, a, b), and rweibullph(r, c, a, b, g) abort with an error if the parameter matrices do not conform. See r-conformability in [M-6] Glossary for rules on matrix conformability.

rdiscrete() aborts with error if the probabilities in p are not in [0,1] or do not sum to 1.

Source code

Functions are built in.

Reference

Walker, A. J. 1977. An efficient method for generating discrete random variables with general distributions. ACM Transactions on Mathematical Software 3: 253-256.


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index