Suppose that the density function $f$ is given by $$f(x) = \begin{cases}x^2, \, 0\leq x <1\\\frac{4}{27}(4 - x),\,1 \leq x < 4\\0, \text{ elsewhere.}\end{cases}$$ Exercise: Show how one may simulate from this distribution using at most two RNG calls per generated value.
What I've tried thus far: I thought about using the inverse transform method in which you set $$X = F^{-1}(U),$$ where $X$ is the simulated value you're we're looking for, $U$ is a generated uniform random variable between $0$ and $1$, and where $F$ is the inverse of the distribution function from which you want to sample. I think the distribution function corresponding to this density function is $$F(x) = \begin{cases}\frac{1}{3}x^2, 0\leq x < 1\\\frac{16}{27}x - \frac{2}{27}x^2, 1\leq x < 4\\ 0, \text{ elsewhere.}\end{cases}$$ This means that for $0\leq x < 1$ we have $X = ±\sqrt{3U}$, and for $1\leq x < 4$ I don't think $F(x)$ has an explicit inverse.
I don't know how to continue from here. Question: Is it possible to simulate from the given distribution with the inverse transform method, and if so how can it be done?
Thanks!
First of all, you got your probability distribution a bit wrong. It is
$$ F(x) = \begin{cases} 0, x \in (-\infty, 0)\\ \frac{\displaystyle{x^3}}{\displaystyle{3}}, x \in [0, 1]\\ \frac{\displaystyle{27 - 2(x - 4)^2}}{\displaystyle{27}}, x \in (1, 4]\\ 1, x \in (4, \infty) \end{cases} $$
Your approach with $x = F^{-1}(U)$ still works, only you will have piecewise $F^{-1}$ as well:
$$ x = \begin{cases} \sqrt[3]{3U}, U \in \Bigl[0, \frac{\displaystyle{1}}{\displaystyle{3}}\Bigr]\\ 4 - 3\sqrt{\frac{\displaystyle{3(1 - U)}}{\displaystyle{2}}}, U \in \Bigl(\frac{\displaystyle{1}}{\displaystyle{3}}, 1\Bigr] \end{cases} $$
Below you can see that sampling from this piecewise inverse distribution agrees with probability density.