How do I best simulate an arbitrary univariate random variate using its probability function?

后端 未结 4 478
温柔的废话
温柔的废话 2020-12-02 17:54

In R, what\'s the best way to simulate an arbitrary univariate random variate if only its probability density function is available?

4条回答
  •  Happy的楠姐
    2020-12-02 18:17

    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:

    1. completely untested
    2. efficiency depends on candidate distribution (i.e. value of cand.sd). For maximum efficiency, tune cand.sd to an acceptance rate of 25-40%
    3. results will be autocorrelated ... (although I guess you could always sample() the results to scramble them, or thin)
    4. may need to discard a "burn-in", if your starting value is weird

    The classical approach to this problem is rejection sampling (see e.g. Press et al Numerical Recipes)

提交回复
热议问题