# Given is density - Create a random variable with it

Given is following function which is also a density

$$f(x)=\left\{\begin{matrix} \frac{3}{4}(2x-x^2) \text{ if } x \in (0,2)\\ 0 \,\,\,\,\,\,\,\text{ else } \end{matrix}\right.$$

Create a random variable with this density.

I ask this question because they can ask similar in exam and I like to know how to do it good and correct?

I think what would be good to use here is rejection sampling. I read about it on Wikipedia but not sure how use it here but nothing else comes to my mind to solve it.

Formula I find is $$M := \sup_{x}\frac{f(x)}{g(x)} < \infty$$

I sum up algorithm because on Wikipedia it's very long:

1. Simulate $U \sim R(0,1)$, in case you don't know this notation: It means that random variable $U$ is equally / uniformly distributed from interval $0$ to $1$
2. Simulate $Y \sim g$
3. Accept $X=Y$ if $U \leq \frac{1}{M} \frac{f(Y)}{g(Y)}$, else go to step 1. (repeat it)

1. We have $P(X<x) = \int_{0}^{x} dt = x$ so $X$ has density function

$$f(x)=\left\{\begin{matrix} 1 \text{ if }0 \leq x < 1\\ 0 \text { else } \end{matrix}\right.$$

I hope this is correct for first step? But I don't know how to do second step and that's why third I can't do too : /

Just wanted to say, the CDF ($CDF(x)=\int_0^xf(x')dx'$) of your probability function can actually be inverted. And if you fill in your $x\sim~Uniform[0,1]$ into the inverted CDF, you get a random variable distributed like your $f(x)$. The method is explained here: https://en.wikipedia.org/wiki/Inverse_transform_sampling
• Yes, I agree ; in such a case, it's inverse transform sampling $F^{-1}(U)$ where $U$ is uniform on $[0,1]$ and $F$ a primitive function = cdf $F$ of pdf $f$ that will be the most simple/efficient. – Jean Marie Jan 23 '18 at 22:34