Estoy tratando de generar muestras aleatorias a partir de un pdf personalizados mediante R. Mi pdf es: $$f_{X}(x) = \frac{3}{2} (1-x^2), 0 \le x \le 1$$
I generated uniform samples and then tried to transform it to my custom distribution. I did this by finding the cdf of my distribution ($F_{X}(x)$) and setting it to the uniform sample ($u$) and solving for $x$.
$$ F_{X}(x) = \Pr[X \le x] = \int_{0}^{x} \frac{3}{2} (1-y^2) dy = \frac{3}{2} (x - \frac{x^3}{3}) $$
To generate a random sample with the above distribution, get a uniform sample $u \en[0,1]$ and solve for $x$ in $$\frac{3}{2} (x - \frac{x^3}{3}) = u $$
He implementado en R
y no me da la distribución esperada. Puede alguien señalar el error en mi entendimiento?
nsamples <- 1000;
x <- runif(nsamples);
f <- function(x, u) {
return(3/2*(x-x^3/3) - u);
}
z <- c();
for (i in 1:nsamples) {
# find the root within (0,1)
r <- uniroot(f, c(0,1), tol = 0.0001, u = x[i])$root;
z <- c(z, r);
}