Learn R Programming

Runuran (version 0.10.1)

ars.new: UNU.RAN generator based on Adaptive Rejection Sampling (ARS)

Description

UNU.RAN random variate generator for continuous distributions with given probability density function (PDF). It is based on Adaptive Rejection Sampling (ARS).

[Universal] -- Rejection Method.

Usage

ars.new(logpdf, dlogpdf=NULL, lb, ub, ...)

Arguments

logpdf
log-density function. (Rfunction)
dlogpdf
derivative of logpdf. (Rfunction)
lb
lower bound of domain; use -Inf if unbounded from left. (numeric)
ub
upper bound of domain; use Inf if unbounded from right. (numeric)
...
(optional) arguments for logpdf

Details

This function creates a unuran object based on ARS (Adaptive Rejection Sampling). It can be used to draw samples from continuous distributions with given probability density function using ur. The log-density must be provided by a function logpdf which need not be normalized (i.e., it can be a log-density plus some arbitrary constant). Moreover, the given function must be concave (i.e., the distribution must be log-concave). The derivative dlogpdf is optional. If omitted, numerical differentiation is used. Notice, however, that this might cause some round-off errors such that the algorithm fails.

The setup time of this method depends on the given PDF, whereas its marginal generation times are almost independent of the target distribution. ARS is a special case of method TDR (see tdr.new). It is a bit slower and less flexible but numerically more stable. In particular, it is useful if one wants to sample from truncated distributions with extreme truncation points; or when the integral of the given density function is only known to be extremely large or small. However, this assumes that the log-density is computed analytically and not by just using log(pdf(x)).

References

W. H"ormann, J. Leydold, and G. Derflinger (2004): Automatic Nonuniform Random Variate Generation. Springer-Verlag, Berlin Heidelberg. See Chapter 4 (Tranformed Density Rejection). W. R. Gilks and P. Wild (1992): Adaptive rejection sampling for Gibbs sampling. Applied Statistics 41(2), pp. 337--348.

See Also

ur, tdr.new, unuran.new, unuran.

Examples

Run this code
## Create a sample of size 100 for a 
## Gaussian distribution (use logPDF)
lpdf <- function (x) { -0.5*x^2 }
gen <- ars.new(logpdf=lpdf, lb=-Inf, ub=Inf)
x <- ur(gen,100)

## Same example but additionally provide derivative of log-density
## to prevent possible round-off errors
lpdf <- function (x) { -0.5*x^2 }
dlpdf <- function (x) { -x }
gen <- ars.new(logpdf=lpdf, dlogpdf=dlpdf, lb=-Inf, ub=Inf)
x <- ur(gen,100)

## Draw a sample from a truncated Gaussian distribution
## on domain [100,Inf)
lpdf <- function (x) { -0.5*x^2 }
gen <- ars.new(logpdf=lpdf, lb=50, ub=Inf)
x <- ur(gen,100)

Run the code above in your browser using DataLab