In R, what\'s the best way to simulate an arbitrary univariate random variate if only its probability density function is available?
To clarify the "use Metropolis-Hastings" answer above:
suppose ddist() is your probability density function
something like:
n <- 10000
cand.sd <- 0.1
init <- 0
vals <- numeric(n)
vals[1] <- init
oldprob <- 0
for (i in 2:n) {
newval <- rnorm(1,mean=vals[i-1],sd=cand.sd)
newprob <- ddist(newval)
if (runif(1)
Notes:
cand.sd).
For maximum efficiency, tune cand.sd to an acceptance rate of 25-40%sample() the results to scramble them, or thin)The classical approach to this problem is rejection sampling (see e.g. Press et al Numerical Recipes)