Constructing inversion method from a given pdf by finding inverse of cdf

The p.d.f. of the random variable $$X$$ is given by $$f(x) = \begin{cases} e^{x-2} & \mbox{for 0 \leq x \leq 2}, \\ e^{-x} & \mbox{for x > 2}, \\ 0 & \mbox{otherwise,} \end{cases}$$

I need to find $$F^{-1}(y)$$ to construct a generator for $$X$$. So I start with calculating the cdf by taking the integral of $$f(x)$$: $$F(X)= \begin{cases} \frac{e^x-1}{e^2} & 0 \leq x \leq 2 \\ -e^{-x}+e^{-2} & x \geq 2 \\ 0 & o.w \end{cases}$$ Next, I need to find the inverse of this function: $$y = \frac{e^x-1}{e^2} \implies x= \ln(e^2y+1) \text{ for } y \in [0, 1-e^{-2}]$$

$$y=-e^{-x}+e^{-2} \implies x = -\ln(e^{-2}-y) \text{ for }y \in (0, e^{-2})$$

Now at this point I am confused since the intervals for $$F^{-1}(y)$$ overlap. How do I break this up into cases? Did I screw up a calculation along the way( I retraced my steps and it seems fine...)?

• One way to simplify your work is to express $X$ as a mixture of two variables (one is supported on $[0,2]$ and the other on $(2,\infty).$) This splits the problem into one part concerned within finding the CDF of a mixture (which is easy), finding its inverse (also easy), and inverting the individual CDFs of the mixture components (elementary and easy in this example).
– whuber
Sep 27, 2020 at 15:03
• I am not quite sure what you mean when you say "express as a mixture of two variables". If I may request, could you put those terms into an example with math symbols? Sep 27, 2020 at 15:07
• A mixture distribution $F$ is of the form $F(x)=\sum_{i=1}^n \omega_i F_i(x)$ where each of the $F_i$ is a distribution function, implying the $\omega_i$ sum to unity (and usually people take all $\omega_i$ to be positive). One way random variables with mixture distributions arise is through a two-stage process: Let $U$ be a discrete distribution with $\Pr(U=i)=\omega_i$ and let the distribution of $X$ conditional on $U=i$ be given by $F_i.$ The only calculations you need to do are to figure out the $\omega_i.$
– whuber
Sep 27, 2020 at 15:12
• BTW, your expression for $F$ is incorrect: notice it resets to $0$ as $x$ crosses from less than $2$ to greater than $2,$ yet it's impossible for any valid distribution function to decrease at all. It can be helpful to graph $f$ and use that graph to sketch what $F$ must look like: that tends to prevent such mistakes.
 curve((exp(x)-1)/exp(x), 0,2, xlim=c(0,10), ylim=0:1,ylab="CDF")