Learn R Programming

Runuran (version 0.40)

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, ...)
arsd.new(distr)

Value

An object of class "unuran".

Arguments

logpdf

log-density function. (R function)

dlogpdf

derivative of logpdf. (R function)

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.

distr

distribution object. (S4 object of class "unuran.cont")

Author

Josef Leydold and Wolfgang H\"ormann unuran@statmath.wu.ac.at.

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.

Function logpdf is the logarithm the density function of the target distribution. It must be a concave function (i.e., the distribution must be log-concave). However, it need not be normalized (i.e., it can be a log-density plus some arbitrary constant).

The derivative dlogpdf of the log-density is optional. If omitted, numerical differentiation is used. Notice, however, that this might cause some round-off errors such that the algorithm fails.

Alternatively, one can use function arsd.new where the object distr of class "unuran.cont" must contain all required information about the distribution.

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

## Alternative approach
distr <- udnorm()
gen <- arsd.new(distr)
x <- ur(gen,100)

Run the code above in your browser using DataLab