Learn R Programming

bhm (version 1.19)

ars: Function to perform Adaptive Rejection Sampling

Description

Generates a sequence of random variables using Adaptive Rejection Sampling (ARS).

Usage

ars(logpdf, n = 1, lower=-14, upper=15, x0 = 0, ...)
arns(logpdf, n = 1, lower = -5, upper = 5, sigma.offset = 0.05, 
              fx.offset = 0.03, K = 100, verbose = FALSE, ...)

Value

An n vector, whose elements are the sampled points.

Arguments

n

Desired sample size.

x0

Initial point.

logpdf

Univariate log target density.

lower

lower limit of the random variable.

upper

upper limit of the random variable.

sigma.offset

offset of sigma for the normal envelope function to ensure it covers the logpdf.

fx.offset

offset of maximum for the normal envelope function to ensure it covers the logpdf.

K

number of points between lower and upper to evaluate logpdf to find the peak and bottom values.

verbose

print out the verbose, default is FALSE.

...

Parameters passed to logpdf

Author

Bingshu E. Chen bingshu.chen@queensu.ca

Details

The support of the target density must be a bounded convex set. When this is not the case, the following tricks usually work. If the support is not bounded, restrict it to a bounded set having probability practically one. A workaround, if the support is not convex, is to consider the convex set generated by the support.

Make sure the value returned by logpdf is never smaller than log(.Machine$double.xmin), to avoid divisions by zero.

References

Gilks, W.R., Best, N.G. and Tan, K.K.C. (1995) Adaptive rejection Metropolis sampling within Gibbs sampling (Corr: 97V46 p541-542 with Neal, R.M.), Applied Statistics 44:455--472.

Examples

Run this code
#### ==> Warning: running the examples may take a few minutes! <== ####    
### Univariate densities
## Normal(mean,1)
normlogdens <- function(x, mean) -(x-mean)^2/2 
y <- ars(normlogdens, n = 10, mean = 3.0)
summary(y); 

Run the code above in your browser using DataLab