# Inverse CDF (Quantile) of Piecewise Function [duplicate]

This question may be insanely simple, but I'm unsure.

Let's say we have the following function: $$f(x) = \begin{cases} x & 0 \leq x < 1 \\ x-1 & 1 \leq x < 2 \\ 0 & \text{otherwise} \end{cases}$$

The CDF is obviously $$F(x) = \begin{cases} \frac{1}{2}x^2 & 0 \leq x < 1 \\ \frac{1}{2}x^2-x+1 & 1 \leq x < 2 \\ 0 & \text{otherwise} \end{cases}$$

How would I calculate the Inverse CDF from this?

(Note: The +1 is required to make it add to 1 and not suddenly become negative. I don't know if that matters for the Inverse CDF.)

In practice, the first step would be to generate a random deviate for the first branch of the specified CDF with half of the probability. So, let ½ U (where U is a Uniform random deviate on (0,1] ) = F(x) = ½ $${x^2}$$, where x lies between 0 and 1, producing the obvious answer (per the Monte Carlo inversion approach for deriving random deviates) that $${X = SQRT (U)}$$.

Step 2, for the 2nd branch similarly generate a random deviate from ½ U’ (where U is a Uniform random deviate on (1,2] ) = F(x’) = ½$${(x’-1)^2}$$ + ½ where x’ lies between 1 and 2 (so, $${x = x’ - 1}$$). The inversion random deviate is correspondingly derived as follows:

$${½ U’ = ½(x’-1)^2 + ½ }$$

Or: $${U’ = (x’-1)^2 + 1}$$

As: $${U = U’ -1}$$ and $${x = x’ - 1}$$

Or: $${U = x^2}$$

And again:

$${X = SQRT (U)}$$

So, per this approach, there is no inconsistency in the generation process for random deviates of the inverse of the complete two-branch CDF as specified in the question.