Learn R Programming

BaPreStoPro (version 0.1)

RejSampling: Rejection Sampling Algorithm

Description

Algorithm to sample from an arbitrary density function.

Usage

RejSampling(Fun, dens, len, cand, grid = 0.001, method = c("vector", "free"))

Arguments

Fun
cumulative distribution function
dens
density
len
number of samples
cand
candidate area
grid
fineness degree
method
vectorial ("vector") or not ("free")

References

Devroye, L. (1986). Non-Uniform Random Variate Generation. New York: Springer.

Examples

Run this code
plot(density(RejSampling(dens = function(x) dnorm(x, 5, 1),
   len = 500, cand = seq(2, 9, by = 0.001), method = "free")))
lines(density(RejSampling(dens = function(x) dnorm(x, 5, 1), len = 500,
      cand = seq(2, 9, by = 0.001), method = "vector")), col=2)
curve(dnorm(x, 5, 1), from = 2, to = 8, add = TRUE, col = 3)

Run the code above in your browser using DataLab