R draw a random number from some distribution

337 Views Asked by At

Suppose I know the pdf of a distribution, say f(x). In R, how to draw a random number from this distribution. Thanks.

2

There are 2 best solutions below

0
On

There are several methods. One of them is Inverse transform sampling . If you know the PDF(x) of any distribution you can calculate the CDF(x) (integration). The CDF is always [0,1]. You work like this:

  1. Generate a random number u [0,1].
  2. Solve for x the CDF(x)=u
  3. x is a random number of the desired distribution
  4. repeat.

See here for more information and R code http://blog.quantitations.com/tutorial/2012/11/20/sampling-from-an-arbitrary-density/

0
On

For most of the distribution you have functions like (runif or rbeta) let's call it vanilla. When you have distribution somehow linked with vanilla distribution and in fact the graph of your f(x) can lay entirely under graph of vanilla distribution you can use acceptance-rejection method.

http://www.inference.phy.cam.ac.uk/tcs27/talks/sampling.html#rejs

enter image description here