RejSampling {BaPreStoPro} | R Documentation |
Algorithm to sample from an arbitrary density function.
RejSampling(Fun, dens, len, cand, grid = 0.001, method = c("vector", "free"))
Fun |
cumulative distribution function |
dens |
density |
len |
number of samples |
cand |
candidate area |
grid |
fineness degree |
method |
vectorial ("vector") or not ("free") |
Devroye, L. (1986). Non-Uniform Random Variate Generation. New York: Springer.
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)