6
$\begingroup$

This might be a really elementary question, but I'm not sure what it means. I have a density function f(x). How do I sample a value from f? For known distributions there are functions in R which do it for you (e.g. runif, rnorm, etc.) but how do I generate a random number using my own density?

$\endgroup$
3
  • 1
    $\begingroup$ Why the down votes? This is highly nontrivial (see my answer) $\endgroup$
    – Igor Rivin
    Mar 29, 2012 at 17:43
  • $\begingroup$ I agree with that, it had also intrigued me, and I was a bit confused to ask ;) $\endgroup$
    – Amin
    Mar 29, 2012 at 21:49
  • $\begingroup$ Read chapter 3.4.1 from Knuth's The Art of Computer Programming. $\endgroup$ Mar 30, 2012 at 6:28

3 Answers 3

3
$\begingroup$

In the simple case that $X$ is a real valued random variable, the first thing I would reach for is the inverse-cdf method, especially since you have mentioned "runif" which gives draws from a uniform distribution.

There is a pretty extensive literature on ways to sample from a variety of distributions, with names like Gibbs sampling, Metropolis-Hastings, slice samplers, perfect samplers, etc. A Google search of any of these should bring up a wealth of info. Did you want something more specific?

$\endgroup$
3
$\begingroup$

I am not going to answer the philosophical question of "what does it mean", but for the practical question, there is the Ziggurat method of Marsaglia to generate a sample from your favorite distribution. Read all about it.

$\endgroup$
1
$\begingroup$

I suppose a definition of a random sample would be a sequence of numbers {$a_{n}$} such that, for any measurable set S we have $\sum_{1}^{n}\chi_{S}(a_{i})/n\rightarrow\mu(S)$ as $n\rightarrow\infty$, where $\mu(S):=\int_{S}f(y)dy$, with $f(y)$ your density.

How to generate such a sequence is another, much more involved question; there's the inverse CDF technique, various algorithms like Metropolis Hastings or Gibbs sampling (for higher dimensional densities), etc. etc.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.