[Math] Random number generator from a piecewise PDF

probabilityprobability distributionsrandomrandom variablesstatistics

I'm trying to create a random number generator on the interval $(a,c)$ given a probability density function defined as:
$$f(x) = \left\{
\begin{array}{lr}
\dfrac{C}{x} &, x \in (a,b)\\
\dfrac{\gamma C}{x} &, x \in (b,c)
\end{array}
\right.
$$
Where $C$ is a normalization constant given by
$$
\int^b_a\frac{C}{x}dx+\int^c_b\frac{\gamma C}{x}dx=1
$$
I know that I'm supposed to use the inverse transform method to find the random number $y$ given a uniformly generated random number $u(0,1)$.

I obtain the following two expressions for $y$:
$$
y=\exp\left(\frac{u(0,1)}{C}+\ln{a}\right)
$$
and
$$
y=\exp\left(\frac{u(0,1)}{\gamma C}+\ln{b}\right)
$$
However, I'm not sure how to select between the two depending on the value of $u(0,1)$ and I'm getting numbers outside the interval $(a,c)$.

Any help would be appreciated.

Best Answer

You have

$$F(x) = \left\{ \begin{array}{lr} C\log_e(x/a) ,& \qquad x \in (a,b)\\ C\log_e(b/a)+\gamma C\log_e(x/b) ,& \qquad x \in (b,c) \end{array} \right.$$

So

$$F^{-1}(y) = \left\{ \begin{array}{lr} a\exp\left(\frac{y}{C}\right) ,& \qquad y \in (0,C\log_e(b/a))\\ b\exp\left(\frac{y-C\log_e(b/a)}{\gamma C}\right) ,& \qquad y \in (C\log_e(b/a),1) \end{array} \right.$$

and if $u$ is uniformly distributed, you want $F^{-1}(u)$, with the choice depending on whether it is more or less than $C\log_e(b/a)$.