Learn R Programming

simEd (version 2.0.0)

vgamma: Variate Generation for Gamma Distribution

Description

Variate Generation for Gamma Distribution

Usage

vgamma(
  n,
  shape,
  rate = 1,
  scale = 1/rate,
  stream = NULL,
  antithetic = FALSE,
  asList = FALSE
)

Arguments

n

number of observations

shape

Shape parameter

rate

Alternate parameterization for scale

scale

Scale parameter

stream

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

antithetic

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

asList

if FALSE (default), output only the generated random variates; otherwise, return a list with components suitable for visualizing inversion. See return for details

Value

If asList is FALSE (default), return a vector of random variates.

Otherwise, return a list with components suitable for visualizing inversion, specifically:

u

A vector of generated U(0,1) variates

x

A vector of gamma random variates

quantile

Parameterized quantile function

text

Parameterized title of distribution

Details

Generates random variates from the gamma distribution.

Gamma 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::qgamma is used to invert the uniform(0,1) variate(s). In this way, using vgamma 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 gamma distribution with parameters shape = \(a\) and scale = \(s\) has density

$$f(x) = \frac{1}{s^a\, \Gamma(a)} x^{a-1} e^{-x/s}$$

for \(x \ge 0\), \(a > 0\), and \(s > 0\). (Here \(\Gamma(a)\) is the function implemented by R's gamma() and defined in its help.)

The population mean and variance are \(E(X) = as\) and \(Var(X) = as^2\).

See Also

rstream, set.seed, stats::runif

stats::rgamma

Examples

Run this code
# NOT RUN {
 set.seed(8675309)
 # NOTE: following inverts rstream::rstream.sample using stats::qgamma
 vgamma(3, shape = 2, rate = 1)

 set.seed(8675309)
 # NOTE: following inverts rstream::rstream.sample using stats::qgamma
 vgamma(3, 2, scale = 1, stream = 1)
 vgamma(3, 2, scale = 1, stream = 2)

 set.seed(8675309)
 # NOTE: following inverts rstream::rstream.sample using stats::qgamma
 vgamma(1, 2, scale = 1, stream = 1)
 vgamma(1, 2, scale = 1, stream = 2)
 vgamma(1, 2, scale = 1, stream = 1)
 vgamma(1, 2, scale = 1, stream = 2)
 vgamma(1, 2, scale = 1, stream = 1)
 vgamma(1, 2, scale = 1, stream = 2)

 set.seed(8675309)
 variates <- vgamma(1000, 2, scale = 1, stream = 1)
 set.seed(8675309)
 variates <- vgamma(1000, 2, scale = 1, stream = 1, antithetic = TRUE)

# }

Run the code above in your browser using DataLab