In R, what's the best way to simulate an arbitrary univariate random variate if only its probability density function is available?
相关问题
- R - Quantstart: Testing Strategy on Multiple Equit
- Using predict with svyglm
- Reshape matrix by rows
- Extract P-Values from Dunnett Test into a Table by
- split data frame into two by column value [duplica
相关文章
- How to convert summary output to a data frame?
- How to plot smoother curves in R
- Paste all possible diagonals of an n*n matrix or d
- ess-rdired: I get this error “no ESS process is as
- How to use doMC under Windows or alternative paral
- dyLimit for limited time in Dygraphs
- Saving state of Shiny app to be restored later
- why 48 bit seed in util Random class?
Use cumulative distribution function http://en.wikipedia.org/wiki/Cumulative_distribution_function
Then just use its inverse. Check here for better picture http://en.wikipedia.org/wiki/Normal_distribution
That mean: pick random number from [0,1] and set as CDF, then check Value
It is also called quantile function.
Here is a (slow) implementation of the inverse cdf method when you are only given a density.
To clarify the "use Metropolis-Hastings" answer above:
suppose
ddist()
is your probability density functionsomething like:
Notes:
cand.sd
). For maximum efficiency, tunecand.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)