# How should I simulate a value from the distribution corresponding to this density function?

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!

• Yes it is possible. First you need to double check again the definition of CDF and how to get it right by integration. Next you will probably need to find the inverse of a quadratic function on $[1, 4)$ but it is monotonic increasing in that interval so it will be fine - you may just need to completing square. Combining the cases, you should also obtain a piecewise inverse function. – BGM Jul 4 '18 at 13:23
• Your F(x) is wrong. $F(x)=\frac{x^3}{3},\ 0\le x \lt 1$. $F(x)=\frac{1}{3}+\frac{4}{27}(4x-\frac{x^2}{2}-\frac{7}{2}),\ 1\le x\lt 4$. $F(x)=1,\ x\ge 4$. – herb steinberg Jul 4 '18 at 22:02

$$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}$$
To use the inverse transform method, generate a random number R. If it is less than $\frac{1}{3}$, then $x=(3R)^\frac{1}{3}$. If R is greater than$\frac{1}{3}$, then solve the quadratic $R-\frac{1}{3}=\frac{4}{27}(4x -\frac{x^2}{2}-\frac{7}{2})$ for x, making sure you pick the root in the range (1,4).