Skip to content

Simulating a variable following an arbitrary distribution

3 messages · Fernando Henrique Ferraz P. da Rosa, Edgar Acuna, Spencer Graves

#
Hi, I'd like to know if there's anything in R that could help me do
that. Let's suppose I have a density function of a random variable, for example
f(x) = (x^3)/4 0 < x < 2 and I would like to simulate it. For the common
distributions (exponencial, gamma, cauchy) there are the r-functions (rgamma,
rexp, runif, rcauchy, and so on).. But when the variable I want to simulate is
not one of those, how should I procede? I read some references on the subject
and saw that there are some algorithms that can do that, but I just wonder if
there is any implemented in R?

Thank you,

--
#
Hi,
Use the Inverse transformation method. See any basic Cbook in simulation
for instance Sheldon Ross's book.
Regards,
Edgar
On Mon, 2 Jun 2003, Fernando Henrique Ferraz Pereira da Rosa wrote:

            
#
Your specific example is a scaled beta.  Therefore, "2*rbeta(1000, 4, 
1)" will generate 1000 random numbers according to that distribution. 
You can get the same distribution from "2*qbeta(runif(1000), 4, 1)".
	  We can generalize this last example to any case of interest.  Example:

df4 <- function(x)(x^3)/4
pf4 <- function(q)(q^4)/16
qf4 <- function(p)(16*p)^0.25
rf4 <- function(n)qf4(runif(n))

Then "rf4(1000)" will produce 1000 pseudo-random deviates following this 
distribution.

hth.  spencer graves
Edgar Acuna wrote: