Learn R Programming

simEd (version 1.0.3)

vbinom: Variate Generator for the Binomial Distribution

Description

Generates random variates from the binomial distribution, with options for independent streams and antithetic variates.

Usage

vbinom(n, size, prob, stream = NULL, antithetic = FALSE)

Arguments

n

number of observations

size

integer number of trials (zero or more)

prob

probability of success on each trial

stream

if NULL (default), uses stats::runif to generate uniform variates to invert via stats::qbinom; otherwise, an integer in 1:25 indicates the rstream stream from which to generate uniform variates to invert via stats::qbinom

antithetic

if FALSE (default), inverts \(u\) = uniform(0,1) variate(s) generated via either stats::runif or rstream::rstream.sample; otherwise, uses \(1 - u\)

Value

A vector of binomial random variates.

Details

Generates random variates from the binomial distribution.

Binomial variates are generated by inverting uniform(0,1) variates produced either by stats::runif (if stream is NULL) or by rstream::rstream.sample (if stream is not NULL). In either case, stats::qbinom is used to invert the uniform(0,1) variate(s). In this way, using vbinom provides a monotone and synchronized binomial variate generator, although not particularly fast.

The stream indicated must be an integer between 1 and 25 inclusive.

The binomial distribution with parameters size = \(n\) and prob = \(p\) has pmf

$$p(x) = {n \choose x} p^x (1-p)^{(n-x)}$$

for \(x = 0, \ldots, n\).

See Also

stats::rbinom, stats::runif, rstream, set.seed

Examples

Run this code
# NOT RUN {
set.seed(8675309)
vbinom(3, size = 10, prob = 0.3)  # inverts stats::runif using stats::qbinom

set.seed(8675309)
vbinom(3, 10, 0.3, stream = 1) # inverts rstream::rstream.sample using stats::qbinom
vbinom(3, 10, 0.3, stream = 2)

set.seed(8675309)
vbinom(1, 10, 0.3, stream = 1) # inverts rstream::rstream.sample using stats::qbinom
vbinom(1, 10, 0.3, stream = 2)
vbinom(1, 10, 0.3, stream = 1)
vbinom(1, 10, 0.3, stream = 2)
vbinom(1, 10, 0.3, stream = 1)
vbinom(1, 10, 0.3, stream = 2)

set.seed(8675309)
variates <- vbinom(1000, size = 10, prob = 0.3, stream = 1)
set.seed(8675309)
variates <- vbinom(1000, size = 10, prob = 0.3, stream = 1, antithetic = TRUE)
# }

Run the code above in your browser using DataLab