Suppose I know the pdf of a distribution, say f(x). In R, how to draw a random number from this distribution. Thanks.
R draw a random number from some distribution
337 Views Asked by huadaixia At
2
There are 2 best solutions below
0

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
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:u
[0,1].CDF(x)=u
x
is a random number of the desired distributionSee here for more information and R code http://blog.quantitations.com/tutorial/2012/11/20/sampling-from-an-arbitrary-density/